Logging

From Spartacats
Jump to: navigation, search

Levels

There are 6 levels in logging:

  1. Fatal
  2. Error
  3. Warning
  4. Info
  5. Debug
  6. Trace
    • Function

Messages should be set to the correct level as logging levels can be turned on and off from a header file.
NOTE: Function logging is at the Trace level

Functions

int LogTrace (const char *fmt,...)
int LogDebug (const char *fmt,...)
int LogInfo (const char *fmt,...)
int LogWarn (const char *fmt,...)
int LogError (const char *fmt,...)
int LogFatal (const char *fmt,...)
int FuncLogEntry (const char *funcName)
int FuncLogExit (const char *funcName, const int lineNumber)

For each logging function, you simply put what you would normally for printf().
For FuncLog, put the name of the function and for FuncLogExit, lineNumber is the line number the function exited on.
In the log, the name of the function will show up along with '{' for the reader to know all messages after are from within the function until '}' is seen.

Include

The beginning of each file should have:

    #include "../LibLogger/liblogger.h"
    #include "../LibLogger/file_logger.h"

As well, any file where the log is initialized needs to include:

    #include <memory.h>


Initialize

The following code is used to initialize the logger:

    tFileLoggerInitParams fileInitParams;
    ZeroMemory(&fileInitParams, sizeof(tFileLoggerInitParams));
    fileInitParams.fileName = L"Logs/LOGNAME.log"; 
    fileInitParams.fileOpenMode = AppendMode; 
    InitLogger(LogToFile, &fileInitParams);


The logs will be stored in the folder "Resources/Logs"
If you would like to see a new set of logs each time (overwrite all previous logs) omit the line with 'AppendMode'.

Deinitializing

    DeInitLogger()


Control Logging

You can attach a Module Name to logs by associating files under a module name in a header file. This allows you to also control the level of logs that show up if any at all.
More information can be found here.

Example

file.cpp:

       int main(int argc, char** argv)
       {
               FuncLogEntry(L"main");
               LogDebug(L" Debug message... ");
               LogInfo (L" int val = %d ", 9);
               // ......
               LogFatal(L" Fatal error has occured, ptr %#x ", 0XABCD );
               // .... other log functions. ........
               FuncLogEntry(L"main", 10);
               DeInitLogger();
               return 0;
       }


Log output:

    ----- Logging Started on  04/20/13 17:27:40  -----
    { main
    [D]:c:\users\me\documents\ucsd 12-13\liblogger-0.2\liblogger\src\testapp\file.cpp:main:4: Debug message...
    [I]:c:\users\me\documents\ucsd 12-13\liblogger-0.2\liblogger\src\testapp\file.cpp:main:5: int val = 9
    [F]:c:\users\me\documents\ucsd 12-13\liblogger-0.2\liblogger\src\testapp\file.cpp:main:7: Fatal error has occured, ptr OXABCD
    main : 10 }


More Information

LibLogger Main Page: http://liblogger.sourceforge.net/main.html
LibLogger Tutorial: http://liblogger.sourceforge.net/page_tutorial.html