Writing to a log

Started by
7 comments, last by XTAL256 14 years, 1 month ago
How is this generally handled? For example, when I load resources, I might want to write "texture.png successfully loaded" or "texture.png failed to load". It seems like a global handle to a file would not be a very good idea, and passing a stream as a parameter to every function that might report errors/other data doesn't seem so great either. I guess one possibility is to have some functions defined in a .h file like writeToLog( const std::string & text ); and then the .cpp file could implement the specifics of how the functions work (possibly even leaving this function empty if no log is desired, and the compiler would optimize it out), and any functions that need to write to the log include the .h file. What approaches are usually taken? I'd like to implement something like this, but I don't want to screw up the design by making some global thing that would be hard to rip out if it didn't work well.
Advertisement
Depends on how large is your application.Both methods you mentioned are used to manage logs.If the application is quite small(like games) then global stream variable and function for writing the message will be enough.
For larger application there will be a class or module(for non-object-oriented code) for managing the log.
One thing you could do is open a file, redirect stdout and simply use printf. No seriously, this works just fine.
Unless you want to redirect different message types to different logs or have some other special needs, there's actually no real reason to do something much more complicated either, in my opinion.
Quote:Original post by samoth
One thing you could do is open a file, redirect stdout and simply use printf. No seriously, this works just fine.
Unless you want to redirect different message types to different logs or have some other special needs, there's actually no real reason to do something much more complicated either, in my opinion.


Or even geekier, dump to std::clog.
you can create a Singleton LogManager and use it like that:

LogManager::Instance().PrintLog("Texture not found", LogManager::LOG_ERROR);

Note: Log file is opened or created during initialization
Quote:Original post by Kasya
you can create a Singleton LogManager and use it like that:

LogManager::Instance().PrintLog("Texture not found", LogManager::LOG_ERROR);

Note: Log file is opened or created during initialization


I do the same, but with some much shorter macros: scLog(), scWarning(), scError()
I also have a set of macros where I pipe the attributes to sprintf_s so I can add contextual information with a format string:
scErrorF(("Texture %s not found", Filename));
visualnovelty.com - Novelty - Visual novel maker
A logger is the perfect example of a thing that should be a global variable: You generally want exactly one per application invocation and want to not have to pass it around everywhere, except for special cases where you want to create a different, independent one.

And, yeah, use std::clog unless you know a good reason not to.
Quote:Original post by Scarabus2

I do the same, but with some much shorter macros: scLog(), scWarning(), scError()
I also have a set of macros where I pipe the attributes to sprintf_s so I can add contextual information with a format string:
scErrorF(("Texture %s not found", Filename));


I was thinking of it but i saw i don't need that because im using std::string's which means i do it like that:

LogManager::Instance().Print("Texture " + filename + " not found");

but i'll add format string in the future because they are very good looking :)



I have a ready to use logging system. But it uses irrlicht containers and strings. you can easyly replace them by the std:: ones.
The system flushes on every nlog, nlogl, nlerror automaticly and appends a linebreak at the end of the lastly written log/error text.
if u want to flush directly pass nlflush as last parameter.

Code
For large projects, log4j or it's C++ equivalent log4cpp are excellent logging libraries. Your project is probably not very big so these libraries would be overkill, but definitely worth looking into if you want some ideas on writing your own simple logging mechanism.
[Window Detective] - Windows UI spy utility for programmers

This topic is closed to new replies.

Advertisement