Click here to Skip to main content
Licence GPL3
First Posted 10 Oct 2006
Views 17,969
Downloads 418
Bookmarked 17 times

Simple Multi-Logger (No MFC)

By | 10 Oct 2006 | Article
A simple HTML and plain-text logger.

Introduction

This is a simple logger that can output to *.html or *.log. I needed a reader-friendly log file for my game and apps. So, I woke up, one morning, and I thought that HTML log would be the most readable, but quite hard to code for me, as I am not a pro in C++. On the other hand, plain-text log files are the easiest way to code a logger, but quite hard to read for a game player. I passed two compete days to find the best way to code it.

Using the code

My main goal in everything I code, is to help the developer to make the source code more readable and more easy to code (less lines). So, to use the class, you simply create a CLogFile with one the followings constructors:

class CLogFile
{
public:
    CLogFile(void);
    CLogFile(const CLogFile& other);
    CLogFile(const string& Filename, ELogType Type);

    //...

};

If you use CLogFile::CLogFile(const string& Filename, ELogType Type), "Filename" is the name of the log file without the extension. If you use the default constructor (CLogFile::CLogFile(void);), you will need to call void CLogFile::open(const std::string& Filename, ELogType Type). The parameter is the same as the constructor explained before. After that, you simply call void CLogFile::log(const std::string& Message, ELogLevel Level) or void CLogFile::log(const boost::format& Message, ELogLevel Level) to log a message to your file. Note that I overloaded the CLogFile::log to use boost::format.

Example

#include <iostream>
#include <stdlib.h>
#include "CLogFile.h"

CLogFile* log1;
CLogFile* log2;

#define my_log(a, b) log1->log(a, b); log2->log(a, b);
//Just to make a macro that does all for me!! ;)

int main(int argc, char* argv[])
{
    try
    {
        log1 = new CLogFile("log", CLogFile::ELT_HTML);
        log2 = new CLogFile("log", CLogFile::ELT_TEXT);

        my_log("PROGRAM STARTED", CLogFile::ELL_DEBUG);

        my_log("", CLogFile::ELL_WARNING);
        my_log("This is a Warning", CLogFile::ELL_WARNING);
        my_log("", CLogFile::ELL_WARNING);

        my_log("", CLogFile::ELL_ERROR);
        my_log("This is an Error", CLogFile::ELL_ERROR);
        my_log("", CLogFile::ELL_ERROR);

        system("pause");

        my_log("PROGRAM ENDED", CLogFile::ELL_DEBUG);
        delete log1;
        delete log2;
    }
    catch(CLogFile::Exception& e)
    {
        std::cout << e.getMessageAsStr() << std::endl;
        system("pause");
    }
    catch(...)
    {
        std::cout << "Unknown Exception Thrown" << std::endl;
        system("pause");
    }

    return 0;
}

Important note

To build everything correctly, you need the Boost library 1.33.0 buils and add the directory to your IDE.

Conclusion

That's it for my first article! I hope that it will be useful for someone. :D If anyone has some upgrades, e-mail me!

History

  • 10/11/2006: Initial release.
  • 11/11/2006: Update -> Now, the class throws a CLogFile::Exception on error. Also some bug fixes.

License

This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)

About the Author

Daniel Grondin

Software Developer (Junior)

Canada Canada

Member

I'm a french canadian and I'm 21 years old. I started coding at 14 with Visual Basic. At 15, I began C++ and, one year later, I started a game with some friends.
 
I am currently coding my own game engine in C++ and C# using OpenGL and it uses up most of my spare time.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Questionwhere the source code? PinmemberYDLU13:17 11 Oct '06  
AnswerRe: where the source code? PinmemberKuroiKitsune14:44 11 Oct '06  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120517.1 | Last Updated 10 Oct 2006
Article Copyright 2006 by Daniel Grondin
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid