Making a logger accessable to all classes?
You could have your objects know who owns them and have their owner look after logging.
This way means you can have multiple logs, you can add extra information to the log if you want, it is more modular as you aren't relying upon global variables and singletons, this makes testing easier etc.
Obviously there is the memory overhead of a pointer to the owner for any class that wants to be able to Log. You may already have this in your design so it won't be any new burden. Also, if there is a deep hierarchy of ownership (not necessarily inheritance), then there will be multiple function calls until you actually log something.
Or you can pass context as a parameter into a function which will allow you to log. E.g.
void MakeSoup(const Kitchen& kitchen);
where the kitchen supports logging.
struct CanLog { void Log(const std::string&);};struct Owner : CanLog {};struct SomeClass { SomeClass(Owner* owner) : m_owner(owner) { } void Log(const std::string& message) { m_owner->Log(message); }private: Owner* m_owner;};
This way means you can have multiple logs, you can add extra information to the log if you want, it is more modular as you aren't relying upon global variables and singletons, this makes testing easier etc.
Obviously there is the memory overhead of a pointer to the owner for any class that wants to be able to Log. You may already have this in your design so it won't be any new burden. Also, if there is a deep hierarchy of ownership (not necessarily inheritance), then there will be multiple function calls until you actually log something.
Or you can pass context as a parameter into a function which will allow you to log. E.g.
void MakeSoup(const Kitchen& kitchen);
where the kitchen supports logging.
the logger i wrote is a static member of my engine and i provided static methods to write log entries. i'm sure there are downsides to this, but i haven't encountered any as of yet.
Quote:Original post by AyCee
the logger i wrote is a static member of my engine and i provided static methods to write log entries. i'm sure there are downsides to this, but i haven't encountered any as of yet.
One benefit of a singleton over static members is ability to use virtual functions. With static members you can't suddenly decide you want to log to a database instead of a file. With a singleton (if it's properly designed), you can. Not much of a problem for a game engine logger though, as those practically never change.
Quote:Original post by CoffeeMugQuote:Original post by AyCee
the logger i wrote is a static member of my engine and i provided static methods to write log entries. i'm sure there are downsides to this, but i haven't encountered any as of yet.
One benefit of a singleton over static members is ability to use virtual functions. With static members you can't suddenly decide you want to log to a database instead of a file. With a singleton (if it's properly designed), you can. Not much of a problem for a game engine logger though, as those practically never change.
There is a hidden message in CoffeeMug's post:
(static data + static functions) != singleton
Singleton is defined to be:
Quote:
Ensures a class only has one instance, and provide a global point of access to it.
Quote:Original post by elementary
static data + static functions = Monostate Pattern.
C/C++ gotcha, thats an assignement operation, just joking [smile]
uncutno:
Regarding opening and closing a file to ensure that the contents are wrote to disk; It's a lot easier to use the flush command to write the data to disk. If you're using the c++ filestreams then it's just myfilestream.flush(); iirc.
There's no excuse not to leave the file open while the program runs.
Regarding opening and closing a file to ensure that the contents are wrote to disk; It's a lot easier to use the flush command to write the data to disk. If you're using the c++ filestreams then it's just myfilestream.flush(); iirc.
There's no excuse not to leave the file open while the program runs.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement