Sign in to follow this  

Singleton vs member variable

This topic is 4162 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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,

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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) ...

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

This topic is 4162 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this