A Logger Makes Your Life Easier

Click here for a larger image.

Every programmer needs a way to know what is happening with his application. For example: You write some source code, compile it, and than you are very happy because your app is working. But wait… The strange part begins now: You have some errors in the release version. What would you give to know what is wrong? Enough with this annoying situation! USE A LOGGER!

Java inspires me. So, I’ve created a logger based on some ideas from java.util.logging. This logger is able to log on multiple “media” files, consoles, TCP connections, and table windows. Soon, I will add support for database logging. This is accomplished by “mounting” and/or “unmounting” those types of handlers into the logger.

The logger also supports seven levels of logging: SEVERE, WARNING, INFO, CONFIG, FINE, FINER, and FINEST. These levels are very well explained in the source code (file “logger.h”). Every media handler has a logging level. If a message arrives to a media, it will be saved only if the media logging level<=message logging level. A good use of this is that you can log severe and warning messages into a file, info, and debug levels to a window with a table and the rest of the messages to the console. This logger’s settings are very easy to make. After that, by using the macros defined in the logger, you can log anything you want, anyware you want.

Here are the instructions to make a logger work:

  1. Add logger.h and logger.cpp to your project and make sure that logger.h is accesible by adding the right include directory.
  2. Set logger.cpp not to use a precompiled header. BE CAREFUL. Only logger.cpp needs this!
  3. Include logger.h in the most visible header file from your project. For MFC applications, that file is stdafx.h.
  4. Instantiate a CLogger object as global so that it can be visible from all your project. This isn’t a “must.” You can use several loggers if you want.
  5. CLogger logger(“put your logger name here”);

  6. Instantiate some handlers with various levels of logging:

  7. CFileLogHandler fileLogHandler(LEVEL_WARNING,
    CFileLogHandler consoleLogHandler(LEVEL_CONFIG, “ignored”,
    -1, TRUE);
    CWndLogHandler wndLogHandler(LEVEL_ALL,
    “The title of the logger window”);

  8. Add the handlers to the logger:
  9. logger.AddHandler(&fileLogHandler);

    At this point, you are ready to use the logger. Here are some examples:

    SEVERE_LOG(logger, "severe message.");
    WARNING_LOG(logger, "warning message");
    INFO_LOG(logger, "info message");
    CONFIG_LOG(logger, "config message");
    FINE_LOG(logger, "fine message");
    FINER_LOG(logger, "finer message");
    FINEST_LOG(logger, "finest message");

CFileLogHandler Logs Data into a File

The constructor of this handle needs the following items:

  • LEVEL level—this is the handler level of logging
  • CString fileName—the name of the logging file. If the console==TRUE, this parameter is ignored (logging to console)
  • DWORD maxLength—maximum allowable size of the file. If the size of the file is greater than this value, a new file is created and numerated by adding a number to the end of the file name along with the current system date
  • BOOL console—if TRUE, logs to the console; if FALSE, logs to the specified file

Remarks: The file name should not have an extension. The logger adds the current date and a sequence number to the file name, along with the “*.log” extension.

CWndLogHandler Logs Data to a Window with a Table

The constructor of this handle needs the following items:

  • LEVEL level—this is the handler level of logging
  • CString title—the title associated with the window

Remarks: This handler was tested with 65536 records, 4096 bytes/message.

CSocketLogHandler Logs Data to a TCP Connection

The constructor of this handle needs the following items:

  • LEVEL level—this is the handler level of logging
  • CString host—the destination host. Ex: your.domain.com or IP:
  • unsigned short port—the destination port
  • BOOL keepAlive—if TRUE, the connection is not closed. If TRUE, the connection is closed after each message and reopened before sending a message


Download source – 12 Kb

More by Author

Must Read