How to Log and stay Modular

Started by
55 comments, last by ApochPiQ 12 years, 7 months ago
Well in my team's game, we have a logging system setup. There is a Log class that allows you to write to a log by just specifying a file in the constructor then using the WriteToLog call.
Example:

Log EngineLog("Engine.log");
EngineLog.WriteToLog("Failed to load...");

Now, all over the code I have included a file that declares a global log that is used by the engine. This global log is used throughout the engine and really messes up my goal of keeping things modular. Now, what is the best way to log but still keep things modular? Please provide more than just, "Use cout or printf".

Thanks,
Brent
Advertisement

Now, all over the code I have included a file that declares a global log that is used by the engine. This global log is used throughout the engine and really messes up my goal of keeping things modular. Now, what is the best way to log but still keep things modular? Please provide more than just, "Use cout or printf".



A global log is by definition, well, global. So I'm not sure what you're really after. But perhaps rather than calling functions on the global log directly, you could implement a reference mechanism in your log class and do something like this:


Log EngineLog("Engine.log");
Log GlobalLog(App.Log);
...
EngineLog.WriteToLog("Failed to load...");
GlobalLog.WriteToLog("foo...");



or, if you want to include data in both global and local logs,


Log MyLog("Engine.log", App.Log); //localfile_name, global_log_ref
...
MyLog.WriteToLocal("Failed to load...");
MyLog.WriteToGlobal("foo...");
MyLog.WriteToBoth("...bar");



Not really sure I understand what you're really after, perhaps...
Why does a log need to be an object? Any reason you can't just use a free function?

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I am guessing you have an existing logging system implemented as per your snippet that forces you to specify the logfile name every time you want to log something. And you are not comfortable with having to specify this logfile name everywhere in your code.

Well the simplest solution is to just create a global variable for the filename that is initialized/set in only one place (if your code is like mine, you would probably have one or more header files somewhere that declares some global enums, variables, references, etc). And of course you could do stuff like having a startup object read from a config file to populate all these global variables when your game starts up.

Another way is for you to write a wrapper class around the existing Log class. This wrapper will manage (and hide) all the details of what logfile to open, when to open it, when to write, etc. So wherever you need to log a message, just call the constructor with the message and just let autoscoping take care of destroying the object.

LogWrapper EngineLog("this is a log...");

I have a logger class that collects log messages together in memory until a certain size before opening the logfile and dumping everything in. Works for logs that do not deal with debugging crashes!
At the risk of sounding like a broken record....

I don't understand the fascination with doing logging with objects. I think I've run into maybe three situations in my entire programming career where I needed more heavyweight state than I could handle with just a free function/static function someplace; for those cases it made sense to write a log wrapper object. Everywhere else... a lone function is more than sufficient.

Even better, write a lone function like Log() which can internally be hooked with various logging backends. Poof - all your code now logs to HTML or XML or the network or whatever else, just by changing the backend hook. No need to touch potentially hundreds of thousands of lines of code that rely on the logger.

In my experience, something like Log(EnumChannel, const char * text) is almost always enough. Specify the log destination/severity/etc. using an enum instead of hardcoded file names; this makes it a lot simpler to move things around/reorganize your logs if you so desire. It also lets you easily filter things based on their channel, so e.g. you can have Release builds only output Channel_Error or whatnot.

Even in languages that don't support true free functions, something like a static Logging.Write() function is a suitable alternative. This covers pretty much every base there is outside of functional languages.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

I don't understand this fascination either. It would drive me insane having to create a logging object everywhere I needed to add an entry, or passing a logger reference to all my functions, let alone having to manually enter the filename to write to. In my opinion the single most useful and quickest to use logging method is:

#define Log(msg) SomeFreeLoggingFunction(msg, __FILE__, __LINE__)

Now go and worry about writing the game instead.

"The right, man, in the wrong, place, can make all the dif-fer-rence in the world..." - GMan, Half-Life 2

A blog of my SEGA Megadrive development adventures: http://www.bigevilcorporation.co.uk

What I've seen in the past is that Log is a strategy associated with each module. Your 'glue' creates some common log and when the modules are created the glue specifies the common log as the strategy. Easier in not-C++, overkill for most things. As others have said (more or less), worry about functionality before maintainability/reusability; worry about your game not your code.

Brent,

It is exactly stuff like this that led to the development of aspect oriented programming. Unfortunately you won't find researching that helpful because solutions tend to involve inventing new language features.

But I tackled this exact question myself a while back, and the whole issue of "globals are bad".

Obviously, it would be easy enough to make globally visible log functions, but globals are bad right? But then I realized that the entire C runtime library is nothing but a bunch of globally visible functions. So if using globals destroyed programs, then you would never be able to do anything with a C++ program. So maybe it's not "globals" that are bad, but just global variables. But wait. Don't they amount to the same thing? Couldn't I call a function on a global object, and couldn't I access an object through a global function? What's the difference?

So after pondering the meaning of life for a while, I realized that the reason global variables are bad is because they store state which could change the behavior of any other part of the code that looks at them. If you think about the effects of, say, "printf", it is intended to effect the state of the world outside the program, but it is not intended to store any state which effects the future behavior of the program. In other words, nothing I do with printf now should change the behavior of the program when call printf later.

To a large degree, logging is similar. You want to provide a stateless function. Code makes calls through it to send information out to a log, but not to set or change any state which effects future behavior of the program. So it seems clear that if making printf global doesn't destroy programs, then making Log() global should be ok too.

But wait. What if I want the logging system to have state? What if it allows the code to log to arbitrary "channels" to keep logs about different stuff separate, e.g. Log(ChannelNameString, LogInfoString)? And what if I want to provide functions through which channels can be sent to different destinations, e.g. SetLogChannel("AI", ConsoleOuputFlag | FileStorageFlag)? Now what? Now I do want the logging system to store state. So can it use global functions or not?

The solution I came up with is to decompose certain systems into "Global-like" parts and "Object-like" parts.

So the logging system has a global Log() function which any code can access by including a header, and which has well defined default behavior when no state has been stored. Then there is a logging system class which provides control over the logging system's internal state, through an object which has a destructor to clean it up.

So in the end, any code which wants to fiddle with the system state needs to get access to the object, while any code at all can use the system in ways that don't change state. If no state is ever set, there's nothing to clean up, and if state is ever set, it's cleaned up correctly through normal object handling.

As another example, I used this same technique with a memory manager. It kept memory pools for different sized allocations and could be configured, say, with a configuration file. It had a fallback to call malloc() if there was no appropriate memory pool set up for the allocation request. The result was that if there was no configuration it would just forward all allocations to the system memory manager. And if it was configured well for the usage of the application it would improve memory characteristics considerably, but none of the code which used it ever had to handle an allocator object or worry about its internal state.

My engine is so modular that each module is an entirely different Microsoft® Visual Studio® project, combined into one solution.

The solution you are seeking is quite simple.
One of the modules should act as a foundation for all of the others. In my case, my foundation is the “L. Spiro Standard” library and its job is only to expose features that could be shared across not only the game engine but any project. For example, a CTime class, a StrLen() function, some routines for UTF handling, etc.

If I had a need for a logging function, it would go there.


There is nothing non-modular about this. My model library has no idea what a “terrain library” is, yet both of them are built on top of a few common foundation layers, which themselves also don’t know what models or terrain is.

If you do not have such an organization, you need to make such an organization.


L. Spiro

I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

" but globals are bad right?"

No, they're not. They're bad if you're an academic computer scientist. Academic computer scientists never actually have to ship code. Globals work, use what works, ship code.

"really messes up my goal of keeping things modular."

Don't have a goal of being modular. Have a goal of "shipping code". What value to you is "being modular"? None. It's an **ideal**. It's not a goal. Your goal is to ship code. That means you get to sacrifice ideals sometimes. This could be one of those times.

You're over-thinking this problem. Everyone always does. Everyone thinks there's a problem with "logging" because it's always either too verbose or not verbose enough and it's never quite right and everyone thinks the way to fix it is to somehow make the logging production more complication.

The problem isn't with the logging. It's with the tool on the other end. Make your program be either "verbose" or "succinct" based on a flag. That's all you need on that front.

Then just output strings but have smarter tools looking at the output.

The UNIX world manages to handle tons and tons of logging using "syslog" which is hardly sophisticated. "syslogd", the program on the other end, that has versions which'll do everything up to handling global networks with millions of servers...

Don't try and solve your logging analysis problems during your log generation.

This topic is closed to new replies.

Advertisement