Class ULogger

Class Documentation

class ULogger

This class is used to log messages with time on a console, in a file and/or with an event. At the start of the application, call ULogger::setType() with the type of the logger you want (see ULogger::Type, the type of the output can be changed at the run-time.). To use it, simply call the convenient macros UDEBUG(), UINFO(), UWARN(), UERROR(), UFATAL() depending of the severity of the message. You can disable some messages by setting the logger level ULogger::setLevel() to severity you want, defined by ULogger::Level. A fatal message will make the application to exit, printing the message on console (whatever the logger type) and posting a ULogEvent (synchronously… see UEventsManager::post()) before exiting.

The display of the logged messages can be modified:

When using a file logger (kTypeLogger), it can be useful in some application to buffer messages before writing them to hard drive (avoiding hard drive latencies). You can set ULogger::setBuffered() to true to do that. When the buffered messages will be written to file on appllciation exit (ULogger destructor) or when ULogger::flush() is called.

If you want the application to exit on a lower severity level than kFatal, you can set ULogger::setExitLevel() to any ULogger::Type you want.

Example:

#include <utilite/ULogger.h>
int main(int argc, char * argv[])
{
   // Set the logger type. The choices are kTypeConsole,
   // kTypeFile or kTypeNoLog (nothing is logged).
   ULogger::setType(ULogger::kTypeConsole);

   // Set the logger severity level (kDebug, kInfo, kWarning, kError, kFatal).
   // All log entries under the severity level are not logged. Here,
   // only debug messages are not logged.
   ULogger::setLevel(ULogger::kInfo);

   // Use a predefined Macro to easy logging. It can be
   // called anywhere in the application as the logger is
   // a Singleton.
   UDEBUG("This message won't be logged because the "
          "severity level of the logger is set to kInfo.");

   UINFO("This message is logged.");

   UWARN("A warning message...");

   UERROR("An error message with code %d.", 42);

   return 0;
}
Output:
[ INFO] (2010-09-25 18:08:20) main.cpp:18::main() This message is logged.
[ WARN] (2010-09-25 18:08:20) main.cpp:20::main() A warning message...
[ERROR] (2010-09-25 18:08:20) main.cpp:22::main() An error message with code 42.

Another useful form of the ULogger is to use it with the UTimer class. Here an example:

#include <utilite/ULogger.h>
#include <utilite/UTimer.h>
...
UTimer timer; // automatically starts
// do some works for part A
UINFO("Time for part A = %f s", timer.ticks());
// do some works for part B
UINFO("Time for part B = %f s", timer.ticks());
// do some works for part C
UINFO("Time for part C = %f s", timer.ticks());
...

See also

setType()

See also

setLevel()

Public Types

enum Type

Loggers available:

kTypeNoLog, kTypeConsole, kTypeFile

Values:

enumerator kTypeNoLog
enumerator kTypeConsole
enumerator kTypeFile
enum Level

Logger levels, from lowest severity to highest:

kDebug, kInfo, kWarning, kError, kFatal

Values:

enumerator kDebug
enumerator kInfo
enumerator kWarning
enumerator kError
enumerator kFatal

Public Static Functions

static void setType(Type type, const std::string &fileName = kDefaultLogFileName, bool append = true)

Set the type of the logger. When using kTypeFile, the parameter “fileName” would be changed (default is “./ULog.txt”), and optionally “append” if we want the logger to append messages to file or to overwrite the file.

TODO : Can it be useful to have 2 or more types at the same time ? Print in console and file at the same time.

Parameters
  • type – the ULogger::Type of the logger.

  • fileName – file name used with a file logger type.

  • append – if true, the file isn’t overwritten, otherwise it is.

static inline Type type()
static inline void setPrintTime(bool printTime)

Print time: default true.

Parameters

printTime – true to print time, otherwise set to false.

static inline bool isPrintTime()
static inline void setPrintLevel(bool printLevel)

Print level: default true.

Parameters

printLevel – true to print level, otherwise set to false.

static inline bool isPrintLevel()
static inline void setPrintEndline(bool printEndline)

Print end of line: default true.

Parameters

printLevel – true to print end of line, otherwise set to false.

static inline bool isPrintEndLine()
static inline void setPrintColored(bool printColored)

Print text with color: default true. Dark green for Debug, white for Info, yellow for Warning, red for Error and Fatal.

Parameters

printColored – true to print text with color, otherwise set to false.

static inline bool isPrintColored()
static inline void setPrintWhere(bool printWhere)

Print where is this message in source code: default true.

Parameters

printWhere – true to print where, otherwise set to false.

static inline bool isPrintWhere()
static inline void setPrintWhereFullPath(bool printWhereFullPath)

Print the full path: default true. ULogger::setPrintWhere() must be true to have path printed.

Parameters

printWhereFullPath – true to print the full path, otherwise set to false.

static inline bool isPrintWhereFullPath()
static void setBuffered(bool buffered)

Set is the logger buffers messages, default false. When true, the messages are buffered until the application is closed or ULogger::flush() is called.

See also

ULogger::flush()

Parameters

buffered – true to buffer messages, otherwise set to false.

static inline bool isBuffered()
static inline void setLevel(ULogger::Level level)

Set logger level: default kInfo. All messages over the severity set are printed, other are ignored. The severity is from the lowest to highest:

  • kDebug

  • kInfo

  • kWarning

  • kError

  • kFatal

Parameters

level – the minimum level of the messages printed.

static inline ULogger::Level level()
static inline void setExitLevel(ULogger::Level exitLevel)

Make application to exit when a log with level is written (useful for debugging). The message is printed to console (whatever the logger type) and an ULogEvent is sent (synchronously… see UEventsManager::post()) before exiting.

Note : A kFatal level will always exit whatever the level specified here.

static inline ULogger::Level exitLevel()
static inline void setEventLevel(ULogger::Level eventSentLevel)

An ULogEvent is sent on each message logged at the specified level. Note : On message with level >= exitLevel, the event is sent synchronously (see UEventsManager::post()).

See also

ULogEvent

See also

setExitLevel()

static inline ULogger::Level eventLevel()
static void reset()

Reset to default parameters.

static void flush()

Flush buffered messages.

See also

setBuffered()

static void write(const char *msg, ...)

Write a message directly to logger without level handling.

Deprecated:

use UDEBUG(), UINFO(), UWARN(), UERROR() or UFATAL()

Parameters
  • msg – the message to write.

  • ... – the variable arguments

static void write(ULogger::Level level, const char *file, int line, const char *function, const char *msg, ...)
static int getTime(std::string &timeStr)

Get the time in the format “2008-7-13 12:23:44”.

Parameters

timeStr – string were the time will be copied.

Returns

the number of characters written, or 0 if an error occurred.

Public Static Attributes

static const std::string kDefaultLogFileName

The default log file name.

Protected Functions

inline ULogger()
virtual ~ULogger()
void _flush()

Protected Static Functions

static ULogger *getInstance()

Protected Static Attributes

static std::string logFileName_
static bool append_

Friends

friend class UDestroyer< ULogger >