Skip to content

How and When to Use CuteLogger

sdcityrobotics edited this page Sep 8, 2013 · 1 revision

CuteLogger is the library we are utilizing in Commander to record and/or display information while the programs are running. The main functionality of the library comes from its Logger class. The Logger class has one or more Appender objects registered to it. An Appender object represents where to log information, what information to log and how to format that information.

Here is a modified example from the Doxygen comments that come with the library:

#include <QCoreApplication>
#include <Logger.h> 
#include <ConsoleAppender.h> 

int main(int argc, char* argv[]) 
{   
   QCoreApplication app(argc, argv);

   ConsoleAppender* consoleAppender = new ConsoleAppender();   
   consoleAppender->setFormat("%m\n");   

   Logger::registerAppender(consoleAppender);   

   LOG_INFO("Starting the application");   
  
   return app.exec(); 
}

In that example you can see an instance of ConsoleAppender is being created. The member function setFormat() is being called for the consoleAppender object. The string passed as a parameter tells the Logger what information to log. In this case %m is the message and \n is a newline character.

The above example would have the follow output in a console window

Starting the application

Here is a more complex example that is similar to the way we are currently using CuteLogger in Mission_Control

#include <QCoreApplication>
#include <Logger.h>
#include <ConsoleAppender.h>
#include <FileAppender.h>

void aFunction()
{
   LOG_TRACE("");
}

int main(int argc, char* argv[])
{
   QCoreApplication app(argc, argv);

   ConsoleAppender *consoleAppender = new ConsoleAppender();
   consoleAppender->setForm("%m\n");
   consoleAppender->setDetailsLevel(Logger::Info);

   FileAppender *fileAppender = new FileAppender("logfile.txt");
   fileAppender->setFormat("<%C> %m\n");
   fileAppender->setDetailsLevel(Logger::Trace);

   Logger::registerAppender(consoleAppender);
   Logger::registerAppender(fileAppender);

   LOG_INFO("Starting the application");

   LOG_TRACE("");
   aFunction();

   return app.exec();
}

The first new thing you may notice is the FileAppender class. This is very similar to the ConsoleAppender class, but instead of displaying information in a console window it saves the information in a file (which is specified in the constructor).

Additionally both the fileAppender and consoleAppener objects invoke the member function setDetailsLevel(). Each appender can log a different depth of information. CuteLogger supports the following log levels:

  • Trace
  • Debug
  • Info
  • Warning
  • Error
  • Fatal

Because the fileAppender is set to the Trace level it will output all calls on the Trace, Debug, Info, Warning and Error levels. With consoleAppender set to Info it will display all calls on the Info, Warning and Error levels.

The console output from the above example would look like the following:

Starting the application

The logfile.txt would look like the following:

Starting the application
<int __cdecl main(int,char *[])> 
<void __thiscall aFunction(void)> 

As you can see placing a call to LOG_TRACE("") at the start of every function and creating a fileAppender set to the Trace level allows us to follow the exact execution path our program took. This will help isolate problems.

The CuteLogger source code is documented using Doxygen style comments. Our repository contains a Doxyfile to build the documentation in the cutelogger directory. For more information on CuteLogger, refer to that documentation.