Static Variables and Multiple Inheritance

J

Jaco Naude

Hi

I am trying to think of an efficient way of implementing a logging
system for a big program. My thoughts are to create a message logging
base class and letting all objects that need logging ability inherit
from this base class along with the other base classes they inherit
from. This should work (not sure how efficient it is though) but I
want to share the data structure that keeps the log messages between
all the classes that's inherited from this base class. (This data
structure will either be written to the disk, displayed using message
boxes or added to a tree view type of class in the main GUI). To do
this, I am thinking to declare the data structure as static, but I'm
not sure if this will do the trick...

Any idea if this will work? If not, a global data structure can
probably do the trick I don't want to do that. Is there maybe a
simpler way of doing this type of thing?

Thanks in advance
Jaco
 
M

mlimber

Hi

I am trying to think of an efficient way of implementing a logging
system for a big program. My thoughts are to create a message logging
base class and letting all objects that need logging ability inherit
from this base class along with the other base classes they inherit
from. This should work (not sure how efficient it is though) but I
want to share the data structure that keeps the log messages between
all the classes that's inherited from this base class. (This data
structure will either be written to the disk, displayed using message
boxes or added to a tree view type of class in the main GUI). To do
this, I am thinking to declare the data structure as static, but I'm
not sure if this will do the trick...

Any idea if this will work? If not, a global data structure can
probably do the trick I don't want to do that. Is there maybe a
simpler way of doing this type of thing?

Using inheritance may work, but it doesn't follow the "is-a" vs. "has-
a" OO design principle. That is, your classes are not kinds of
loggers; they use a logger. Inheritance represents the former, not the
latter (except for non-public inheritance, but still it's somewhat
misleading to implement "has-a" this way IMHO). Hence, you should
probably either pass a logger instance in via the constructor, which
could be passed all the way to the base class, or use a global,
possibly a singleton. See the chapter on singletons in Alexandrescu's
_Modern C++ Design_ where he dwells on how to implement loggers with
various feature sets, and see the most updated code from that book
here:

http://loki-lib.sourceforge.net/

Cheers! --M
 
T

tomisarobot

Global access around a structure is usually the most convenient, but
nothing is really great for logging in OO systems. Allow your design
multiple logs accessed, you generally end up wanting it at some
point. If you want one default, just overload with a literal.

namespace log {
template<typename T>
Log(const std::string &whichLog, const T &t);
}

std::stringstream is your friend for impl.
 
J

James Kanze

I am trying to think of an efficient way of implementing a
logging system for a big program. My thoughts are to create a
message logging base class and letting all objects that need
logging ability inherit from this base class along with the
other base classes they inherit from. This should work (not
sure how efficient it is though) but I want to share the data
structure that keeps the log messages between all the classes
that's inherited from this base class. (This data structure
will either be written to the disk, displayed using message
boxes or added to a tree view type of class in the main GUI).
To do this, I am thinking to declare the data structure as
static, but I'm not sure if this will do the trick...

It doesn't really make sense, any more than having every class
inherit from a class Object makes sense. Everything is, or
should be "loggable" (including things like int, which can't
derive from a LoggableObject class).
Any idea if this will work? If not, a global data structure
can probably do the trick I don't want to do that. Is there
maybe a simpler way of doing this type of thing?

Logging is a global service, which means at some level, you
always end up with some sort of "global" object. Usually
several, in fact.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,142
Latest member
arinsharma
Top