Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Help finding appropriate design pattern for event loggin

Reply
Thread Tools

Help finding appropriate design pattern for event loggin

 
 
Greg Dharma
Guest
Posts: n/a
 
      06-01-2009
Hello,

I am using C++ for about a year now and am comfortable with the basics
of the language but need some help here with finding a design pattern
for a problem I am trying to solve.

Basically I am trying to log events happening in different classes
(spread across different .cxx files). The events are in most cases
certain checks being done before accessing data from a database. I
need to log which check was done, whether it passed/failed, why it
passed/failed etc.

I am thinking of having a "event collector" class. I will then create
a single object of this class in my program. Then from the various
parts of my code I will call a function like eventCollector-
>pushEventInfo(.....)


Now from different parts of the code I would be passing in different
amounts of information (e.g. a couple of strings from one function, a
bunch of int values from another etc). So I would overload the
pushEventInfo function to accept structs of the different types of
info I will be passing to the "event collector" class.

I am wondering if there is any design patten which closely matches
what I am trying to do here. If so, I could read it and clear up any
issues that might occur before I get into implementing my solution to
this problem.

Any other ideas about the general theme of "sending information from
different classes/files to a central location for data collection"
would also be appreciated.

Thanks a lot guys!
Greg.
 
Reply With Quote
 
 
 
 
Maxim Yegorushkin
Guest
Posts: n/a
 
      06-01-2009
On Jun 1, 4:30*pm, Greg Dharma <(E-Mail Removed)> wrote:

> I am using C++ for about a year now and am comfortable with the basics
> of the language but need some help here with finding a design pattern
> for a problem I am trying to solve.
>
> Basically I am trying to log events happening in different classes
> (spread across different .cxx files). The events are in most cases
> certain checks being done before accessing data from a database. I
> need to log which check was done, whether it passed/failed, why it
> passed/failed etc.
>
> I am thinking of having a "event collector" class. I will then create
> a single object of this class in my program. Then from the various
> parts of my code I will call a function like eventCollector-
>
> >pushEventInfo(.....)

>
> Now from different parts of the code I would be passing in different
> amounts of information (e.g. a couple of strings from one function, a
> bunch of int values from another etc). So I would overload the
> pushEventInfo function to accept structs of the different types of
> info I will be passing to the "event collector" class.


Why can you not use a log file for logging events?

--
Max
 
Reply With Quote
 
 
 
 
Greg Dharma
Guest
Posts: n/a
 
      06-01-2009
On Jun 1, 5:42*pm, Maxim Yegorushkin <(E-Mail Removed)>
wrote:
> On Jun 1, 4:30*pm, Greg Dharma <(E-Mail Removed)> wrote:
>
>
>
> > I am using C++ for about a year now and am comfortable with the basics
> > of the language but need some help here with finding a design pattern
> > for a problem I am trying to solve.

>
> > Basically I am trying to log events happening in different classes
> > (spread across different .cxx files). The events are in most cases
> > certain checks being done before accessing data from a database. I
> > need to log which check was done, whether it passed/failed, why it
> > passed/failed etc.

>
> > I am thinking of having a "event collector" class. I will then create
> > a single object of this class in my program. Then from the various
> > parts of my code I will call a function like eventCollector-

>
> > >pushEventInfo(.....)

>
> > Now from different parts of the code I would be passing in different
> > amounts of information (e.g. a couple of strings from one function, a
> > bunch of int values from another etc). So I would overload the
> > pushEventInfo function to accept structs of the different types of
> > info I will be passing to the "event collector" class.

>
> Why can you not use a log file for logging events?
>
> --
> Max


I do not need to log to a file...the utility needs to print out all
information right away to a screen and I do not want many couts/prints
cluttering up the code in various places. I would like to be able to
send event information to a central location and this could then
format the data appropriately and then output it (to the screen for
now) but possibly to a DB or file later (in which case having all data
being collected in a central place would be definitely useful).

Any thoughts/pointers please?

Thanks again!
Greg.
 
Reply With Quote
 
AnonMail2005@gmail.com
Guest
Posts: n/a
 
      06-01-2009
On Jun 1, 11:30*am, Greg Dharma <(E-Mail Removed)>
wrote:
> Hello,
>
> I am using C++ for about a year now and am comfortable with the basics
> of the language but need some help here with finding a design pattern
> for a problem I am trying to solve.
>
> Basically I am trying to log events happening in different classes
> (spread across different .cxx files). The events are in most cases
> certain checks being done before accessing data from a database. I
> need to log which check was done, whether it passed/failed, why it
> passed/failed etc.
>
> I am thinking of having a "event collector" class. I will then create
> a single object of this class in my program. Then from the various
> parts of my code I will call a function like eventCollector-
>
> >pushEventInfo(.....)

>
> Now from different parts of the code I would be passing in different
> amounts of information (e.g. a couple of strings from one function, a
> bunch of int values from another etc). So I would overload the
> pushEventInfo function to accept structs of the different types of
> info I will be passing to the "event collector" class.
>
> I am wondering if there is any design patten which closely matches
> what I am trying to do here. If so, I could read it and clear up any
> issues that might occur before I get into implementing my solution to
> this problem.
>
> Any other ideas about the general theme of "sending information from
> different classes/files to a central location for data collection"
> would also be appreciated.
>
> Thanks a lot guys!
> Greg.


Take a look at various logging classes (e.g. log4cpp) or write your
own. The logger doesn't have to log to a file, it can output it's
"stuff" to where ever - a file, network, terminal, etc.

The key point is that the outputting is left to that class and the
users of your class do not need to know about those details. Other
key points are the logger is typically a singleton.

Another typical feature is that users of this type of class use
std:stream operators to format their output. This way, your logging
class doesn't need to know about how to output the various things your
client code has to deal with. The logger typically deals with already
formatted output like a std::string.

HTH
 
Reply With Quote
 
Marcel Müller
Guest
Posts: n/a
 
      06-01-2009
Hi,

Greg Dharma wrote:
> I do not need to log to a file...the utility needs to print out all
> information right away to a screen and I do not want many couts/prints
> cluttering up the code in various places. I would like to be able to
> send event information to a central location and this could then
> format the data appropriately and then output it (to the screen for
> now) but possibly to a DB or file later (in which case having all data
> being collected in a central place would be definitely useful).


since you have to serialize the information anyway all event classes
should provide a common interface for this job. Furthermore it is
helpful to have some common properties like a severity level. That might
control e.g. coloring in case of screen visualization. And the
collection might calculate some aggregate information like total success
from that. If you want to support filters, more common properties like a
time stamp and context information about the event source, e.g. the
logical component of the application. Individual structures for each
type of event are only useful if there is someone who needs this kind
information in machine readable format. And this is rather unlikely to
happen outside your event classes because it would require RTTI dispatching.

If you do not want to pass a reference to your event collector anywhere
around, you could use thread local storage. For the time of a service
request the TLS could point to your individual request collector. And
once the request is completed the collector is detached from the current
thread. This is an easy and usually high-performance solution.
Unfortunately it requires platform dependencies until C++0x.


Marcel
 
Reply With Quote
 
KjellKod
Guest
Posts: n/a
 
      06-02-2009
I second Marcel's advice.
In addition I would've used some namespace logic &
signals (Qt, Boost, KjellKod signals, sigslot or similar) for making
this loosely coupled
(to avoid having to include or pass around reference to 'event
collector')

Further,. if you are to thread the event collection I strongly
recommend encapsulating it
within an (Asynchronous) Agent,.. communicating with the thread using
a message queue

Kjell
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      06-14-2009
On Mon, 1 Jun 2009 10:31:49 -0700 (PDT), Greg Dharma <(E-Mail Removed)> wrote:
> On Jun 1, 5:42*pm, Maxim Yegorushkin <(E-Mail Removed)>
> wrote:
>> On Jun 1, 4:30*pm, Greg Dharma <(E-Mail Removed)> wrote:
>>
>>
>>
>> > I am using C++ for about a year now and am comfortable with the basics
>> > of the language but need some help here with finding a design pattern
>> > for a problem I am trying to solve.

>>
>> > Basically I am trying to log events happening in different classes
>> > (spread across different .cxx files). The events are in most cases
>> > certain checks being done before accessing data from a database. I
>> > need to log which check was done, whether it passed/failed, why it
>> > passed/failed etc.

>>
>> > I am thinking of having a "event collector" class. I will then create
>> > a single object of this class in my program. Then from the various
>> > parts of my code I will call a function like eventCollector-

>>
>> > >pushEventInfo(.....)

>>
>> > Now from different parts of the code I would be passing in different
>> > amounts of information (e.g. a couple of strings from one function, a
>> > bunch of int values from another etc). So I would overload the
>> > pushEventInfo function to accept structs of the different types of
>> > info I will be passing to the "event collector" class.

>>
>> Why can you not use a log file for logging events?
>>
>> --
>> Max

>
> I do not need to log to a file...the utility needs to print out all
> information right away to a screen


Based on his headers, Maxim seems to be a Unix programmer, so he
probably thinks of a screen (whatever that is) as just another kind of
file.

> and I do not want many couts/prints
> cluttering up the code in various places.


You mean "the_event_log << stuff << ..." calls I guess -- noone
suggested logging to standard output.

But why would that be more clutter in various places than the event
logging you're already planning? It can be as simple as

mylog << LOGHEAD << result_of_the_foo_check;

(MYLOG being a macro which expands to something which formats file,
line, and/or function name information, if you want those things.)

> I would like to be able to
> send event information to a central location and this could then
> format the data appropriately and then output it (to the screen for
> now) but possibly to a DB or file later (in which case having all data
> being collected in a central place would be definitely useful).


That still sounds like a std:stream to me.

For one design pattern, have a look at the Unix syslog facility. It's
two or three different functions, you only use the printf-like
function syslog(), and it has been good enough for everybody since at
least the early 1980s. (Except for C++ usage, because it is --
unsurprisingly -- not a std:stream.)

/Jorgen

--
// Jorgen Grahn <grahn@ Ph'nglui mglw'nafh Cthulhu
\X/ snipabacken.se> R'lyeh wgah'nagl fhtagn!
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 1 01-22-2012 10:48 PM
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 0 01-22-2012 10:26 PM
C++ and design Pattern (Composite design Pattern ) Pallav singh C++ 0 01-22-2012 10:25 PM
May I have a example of design pattern of "composite", I still feel fuzzy after reading book of Addison-Wesley's"design pattern " jones9413@yahoo.com C++ 1 08-31-2007 04:09 AM
documents related to factory design pattern and Abstract foctory pattern. sunny C++ 1 12-07-2006 04:26 AM



Advertisments