Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / desktop / MFC

Simple Multi-Logger (No MFC)

1.86/5 (5 votes)
10 Oct 2006GPL32 min read 1   802  
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:

C++
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

C++
#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)