I'm looking for a library that can handle logging for me. It must support multi-threading and output to HTML or similar with severity level highlighting. Such as blue is a note, red is a severe error, etc. Does anyone know of such a library?
I'm looking for a library that can handle logging for me. It must support multi-threading and output to HTML or similar with severity level highlighting. Such as blue is a note, red is a severe error, etc. Does anyone know of such a library?
Founder and avid member of the Internationsl Typo Associateion
I'm not aware of any.
It's probably not that difficult to achieve as long as you don't need to handle multiple threads logging in any other ways than let them wait [this may, however, synchronize threads in a way that you don't like].
Something along these lines (incomplete):
Code:#include <iostream> enum { SevUnknown = 0 SevWarning = 1, SevError = 2, SevFatal = 3, } Severity; // Replace as needed. class Logger { public: Logger(const char *fname); ~Logger(); void StartMessage(Severity sev, const std::string &msg); void Message(const std::string &msg); void EndMessage(std::string &msg); void EndMessage(); private: ofstream logStream; Mutex mutex; Severity currentSev; void WriteMessage(const std::string &msg); void SetColor(); }; Logger::Logger(const char *fname) { logStream(fname); // May want to output timestamp here. Init(mutex); } Logger::~Logger() { logStream.close(); Close(mutex); } Logger::StartMessage(Severity sev, const std::string &msg) { Lock(mutex); currentSev = sev; SetColor(); // Timestamp? WriteMessage(msg); } Logger::Message(const std::string &msg) { WriteMessage(msg); } Logger::EndMessage(const std::string &msg) { EndCOlor(); WriteMessage(msg); Unlock(mutex); } Logger::EndMessage() { EndColor(); Unlock(mutex); }
--
Mats
Compilers can produce warnings - make the compiler programmers happy: Use them!
Please don't PM me for help - and no, I don't do help over instant messengers.
Possibly modify Pantheios to meet your requirements???
What are you trying to log, exactly? The answer could be simpler than any of us are thinking if you are desiring something less complicated than you make it sound like.
Yes I looked into Pantheios and it looks like a very nice candidate. The reason I want the output in HTML is to differentiate between different types of messages (ie. Warnings, Critical Errors, Notes, etc by using color coating). Also To create a thread-safe environment so that all threads could output properly.
What I'm logging is mainly error messages, but debug notes as well (such as entering a critical section, etc). I am also toying with the idea of forking a secondary process that is the logger, but I might be over architecting it at this point.
NOTE: where the logger is going to be used is in my game engine (And if its generic enough, in all my other projects). The engine is multi threaded and has several DLLs (which have object boundaries). Hence making global/static variables to hold the logger is out of the question. The implementation above requires setup and tear down each time I want to log something.
Last edited by Mastadex; 08-28-2008 at 06:56 AM.
Founder and avid member of the Internationsl Typo Associateion
Boost has currently two candidate libraries for logging. Look at the sandbox and the mailing list.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
You can also output to a really simple format and use php to convert it to html with the colors you like.
example log file
php scriptCode:player.cpp:decide:32:warning: Mouse is out of bounds factory:create:32:error: Not enough memory player.cpp:update:34:warning: vector seems to large (232112, 23212) player.cpp:upddate:45:error: player position is away from map
Batch file to generate your html fileCode:// tohtml.php // php tohtml.php > messages.html // opens example.log // expects something of the form :file:32:warning: message // only highlights warnings and messages <html> <body> <table> <tr> <td>Type</td> <td>Message</td> </tr> <? $fh = fopen("example.log", "r"); if($fh){ do{ $line = fgets($fh); if(!$line) break; echo ("<tr>"); echo ("<td> <font color='"); if(ereg('^[^ ]*warning:', $line)){ echo ("blue"); } if(ereg('^[^ ]*error:', $line)){ echo("red"); } echo("'>"); if(ereg('^([^ ]*): *(.*[^ ])', $line, $regs)){ $type = $regs[1]; $msg = $regs[2]; echo("$type </font></td>"); echo("<td>$msg</td>"); echo("</tr>"); } else { echo ("reg expression failed</font></td>"); echo ("<td>reg failed</td>"); } }while($line); fclose($fh); } ?> </table> </body> </html>
now simply open up messages.html after running the batch file. This is kind of complicated but allows you to not worry about colors when you coding in C/C++.Code:php tohtml.php > messages.html