Singleton vs member variable

Started by
9 comments, last by CrazyCdn 17 years, 9 months ago
I've written a log manager class, that basically has to be accessible from everywhere. The problem is, as I'm sure you've guessed from the title, is that I don't know whether to make the log manager class a singleton, have it as a member variable (ptr), or just have it as a static class. Obviously, the problems with the first and third are that I later, might want more than one log manager. For what, I don't know, but I'd prefer to not have to re-write every line of code in my engine that logs a message, more than once. The problem with the second is that it obviously won't be accessible from everywhere. Even though I could add a pointer to it into the video driver class, which all scene graph nodes have a pointer to, I'd still end up with classes which don't have any access to it. When presented with a similar situation, which did you pick, and more importantly, why?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Advertisement
Hi,
Normally i don't like singleton. But for my logger i also use a singleton.
My log class is able to log to different outputters. This way you can have one log class that logs to different files,console,...

greets,
Quote:
Obviously, the problems with the first and third are that I later, might want more than one log manager. For what, I don't know, but I'd prefer to not have to re-write every line of code in my engine that logs a message, more than once.


If this is the way you want to go; then why not create a class called Global_logger which inherts from Logger and is a singleton. Thus giving you the chance to create local Loggers and a global without any extra work.
Or have the singleton object return a logger based on a parameter, ie LogManager.getLog( DEBUG_LOG ) or whatever, then it'd be easy to accomodate more loggers in the future, with no changes to the interface for existing client code.
Quote:Original post by codehunter13
Hi,
Normally i don't like singleton. But for my logger i also use a singleton.
My log class is able to log to different outputters. This way you can have one log class that logs to different files,console,...

greets,


I would use this approach.
Quote:Original post by Endar
I've written a log manager class, that basically has to be accessible from everywhere. The problem is, as I'm sure you've guessed from the title, is that I don't know whether to make the log manager class a singleton, have it as a member variable (ptr), or just have it as a static class.

Obviously, the problems with the first and third are that I later, might want more than one log manager. For what, I don't know, but I'd prefer to not have to re-write every line of code in my engine that logs a message, more than once.

The problem with the second is that it obviously won't be accessible from everywhere. Even though I could add a pointer to it into the video driver class, which all scene graph nodes have a pointer to, I'd still end up with classes which don't have any access to it.

When presented with a similar situation, which did you pick, and more importantly, why?


you can design your singleton as kind of manager, where you registrate more than one logfile ... by using an identifier for example ...

manager.register( "filename", LOG_XY );

manager.log( LOG_XY, "error message" );


or sth like that (probably more complicated) ...
Currently, I have a log manager (the class I'm trying to decide to make static, or a singleton, etc), which handles many logs.

I'm logging messages with a message type (MSG for a normal message, ERROR for an error message, etc, etc), and when a log is created and added to the log manager, you specify what kinds of messages you want it to pick up. This could be some, or all. The flags can be combined with the bitwise OR operator.

I like the fact that the thing that most of you told me to do, I've already got figured and running.

Great minds ...

[grin]
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
Not everything needs to be in a class. This sort of functionality is a prime candidate for global functions (within a namespace). A nice and simple Log(destination, level, message) is much easier then LogManager.Get().Write(destination, level, message). I have the ability to log to multiple files (more added on the fly), different output such as the Visual Studios debugger, my custom remote debugger, a text console or if I wish to add others in the future it's easy enough to do.

"Those who would give up essential liberty to purchase a little temporary safety deserve neither liberty nor safety." --Benjamin Franklin

Singleton? Why?

I just use a global for my log. Just like the standard library does.
Quote:Original post by Mike2343
Not everything needs to be in a class. This sort of functionality is a prime candidate for global functions (within a namespace).


Yeah, I've decided that a singleton is out the window. I'm leaning toward static functions and member vars inside a class. The reason is encapsulation. You can't declare a global variable to be const to all but a few functions, can you?
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper

This topic is closed to new replies.

Advertisement