Thread: Looking for a Logging Library/Code

  1. #1
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195

    Looking for a Logging Library/Code

    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

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    Possibly modify Pantheios to meet your requirements???

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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.

  5. #5
    Codebot
    Join Date
    Jun 2004
    Location
    Toronto
    Posts
    195
    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

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    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

  7. #7
    Registered User
    Join Date
    Nov 2003
    Posts
    161
    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
    Code:
    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
    php script
    Code:
    // 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>
    Batch file to generate your html file
    Code:
    php tohtml.php > messages.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++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IP logging
    By cruxis in forum Networking/Device Communication
    Replies: 5
    Last Post: 10-05-2004, 04:31 AM
  2. Resource ICONs
    By gbaker in forum Windows Programming
    Replies: 4
    Last Post: 12-15-2003, 07:18 AM
  3. Logging Data
    By Quantrizi in forum C++ Programming
    Replies: 17
    Last Post: 08-10-2003, 10:06 AM
  4. Logging a process's function calls
    By SMurf in forum Windows Programming
    Replies: 4
    Last Post: 12-14-2002, 05:41 PM
  5. logging key activity
    By threahdead in forum C Programming
    Replies: 1
    Last Post: 10-03-2002, 01:05 AM