Public Types | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Static Protected Member Functions | Static Protected Attributes | Private Member Functions | Static Private Member Functions | Static Private Attributes | Friends
ULogger Class Reference

#include <ULogger.h>

Inheritance diagram for ULogger:
Inheritance graph
[legend]

List of all members.

Public Types

enum  Level {
  kDebug, kInfo, kWarning, kError,
  kFatal
}
enum  Type { kTypeNoLog, kTypeConsole, kTypeFile }

Static Public Member Functions

static ULogger::Level eventLevel ()
static ULogger::Level exitLevel ()
static void flush ()
static int getTime (std::string &timeStr)
static bool isBuffered ()
static bool isPrintColored ()
static bool isPrintEndLine ()
static bool isPrintLevel ()
static bool isPrintTime ()
static bool isPrintWhere ()
static bool isPrintWhereFullPath ()
static ULogger::Level level ()
static void reset ()
static void setBuffered (bool buffered)
static void setEventLevel (ULogger::Level eventSentLevel)
static void setExitLevel (ULogger::Level exitLevel)
static void setLevel (ULogger::Level level)
static void setPrintColored (bool printColored)
static void setPrintEndline (bool printEndline)
static void setPrintLevel (bool printLevel)
static void setPrintTime (bool printTime)
static void setPrintWhere (bool printWhere)
static void setPrintWhereFullPath (bool printWhereFullPath)
static void setType (Type type, const std::string &fileName=kDefaultLogFileName, bool append=true)
static Type type ()
static void write (const char *msg,...)
static void write (ULogger::Level level, const char *file, int line, const char *function, const char *msg,...)

Static Public Attributes

static const std::string kDefaultLogFileName = "./ULog.txt"

Protected Member Functions

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

Static Protected Member Functions

static ULoggergetInstance ()

Static Protected Attributes

static bool append_ = true
static std::string logFileName_

Private Member Functions

virtual void _write (const char *msg, va_list arg)
virtual void _writeStr (const char *msg)

Static Private Member Functions

static ULoggercreateInstance ()

Static Private Attributes

static bool buffered_ = false
static std::string bufferedMsgs_
static UDestroyer< ULoggerdestroyer_
static Level eventLevel_ = kFatal
static bool exitingState_ = false
static Level exitLevel_ = kFatal
static ULoggerinstance_ = 0
static Level level_ = kInfo
static const char * levelName_ [5] = {"DEBUG", " INFO", " WARN", "ERROR", "FATAL"}
static bool limitWhereLength_ = false
static UMutex loggerMutex_
static bool printColored_ = true
static bool printEndline_ = true
static bool printLevel_ = true
static bool printTime_ = true
static bool printWhere_ = true
static bool printWhereFullPath_ = false
static Type type_ = ULogger::kTypeNoLog

Friends

class UDestroyer< ULogger >

Detailed Description

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()
setLevel()
UDEBUG(), UINFO(), UWARN(), UERROR(), UFATAL()

Definition at line 225 of file ULogger.h.


Member Enumeration Documentation

Logger levels, from lowest severity to highest:

Enumerator:
kDebug 
kInfo 
kWarning 
kError 
kFatal 

Definition at line 248 of file ULogger.h.

Loggers available:

Enumerator:
kTypeNoLog 
kTypeConsole 
kTypeFile 

Definition at line 240 of file ULogger.h.


Constructor & Destructor Documentation

ULogger::ULogger ( ) [inline, protected]

Definition at line 409 of file ULogger.h.

ULogger::~ULogger ( ) [protected, virtual]

Definition at line 603 of file ULogger.cpp.


Member Function Documentation

void ULogger::_flush ( ) [protected]

Definition at line 241 of file ULogger.cpp.

virtual void ULogger::_write ( const char *  msg,
va_list  arg 
) [inline, private, virtual]

Reimplemented in UFileLogger, and UConsoleLogger.

Definition at line 458 of file ULogger.h.

virtual void ULogger::_writeStr ( const char *  msg) [inline, private, virtual]

Reimplemented in UFileLogger, and UConsoleLogger.

Definition at line 459 of file ULogger.h.

ULogger * ULogger::createInstance ( ) [static, private]

Definition at line 588 of file ULogger.cpp.

static ULogger::Level ULogger::eventLevel ( ) [inline, static]

Definition at line 347 of file ULogger.h.

static ULogger::Level ULogger::exitLevel ( ) [inline, static]

Definition at line 338 of file ULogger.h.

void ULogger::flush ( ) [static]

Flush buffered messages.

See also:
setBuffered()

Definition at line 228 of file ULogger.cpp.

ULogger * ULogger::getInstance ( ) [static, protected]

Definition at line 579 of file ULogger.cpp.

int ULogger::getTime ( std::string &  timeStr) [static]

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

Parameters:
timeStrstring were the time will be copied.
Returns:
the number of characters written, or 0 if an error occurred.

Definition at line 531 of file ULogger.cpp.

static bool ULogger::isBuffered ( ) [inline, static]

Definition at line 315 of file ULogger.h.

static bool ULogger::isPrintColored ( ) [inline, static]

Definition at line 292 of file ULogger.h.

static bool ULogger::isPrintEndLine ( ) [inline, static]

Definition at line 284 of file ULogger.h.

static bool ULogger::isPrintLevel ( ) [inline, static]

Definition at line 277 of file ULogger.h.

static bool ULogger::isPrintTime ( ) [inline, static]

Definition at line 270 of file ULogger.h.

static bool ULogger::isPrintWhere ( ) [inline, static]

Definition at line 299 of file ULogger.h.

static bool ULogger::isPrintWhereFullPath ( ) [inline, static]

Definition at line 306 of file ULogger.h.

static ULogger::Level ULogger::level ( ) [inline, static]

Definition at line 329 of file ULogger.h.

void ULogger::reset ( ) [static]

Reset to default parameters.

Definition at line 203 of file ULogger.cpp.

void ULogger::setBuffered ( bool  buffered) [static]

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:
bufferedtrue to buffer messages, otherwise set to false.

Definition at line 218 of file ULogger.cpp.

static void ULogger::setEventLevel ( ULogger::Level  eventSentLevel) [inline, static]

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
setExitLevel()

Definition at line 346 of file ULogger.h.

static void ULogger::setExitLevel ( ULogger::Level  exitLevel) [inline, static]

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.

Definition at line 337 of file ULogger.h.

static void ULogger::setLevel ( ULogger::Level  level) [inline, static]

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:
    levelthe minimum level of the messages printed.

Definition at line 328 of file ULogger.h.

static void ULogger::setPrintColored ( bool  printColored) [inline, static]

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

Parameters:
printColoredtrue to print text with color, otherwise set to false.

Definition at line 291 of file ULogger.h.

static void ULogger::setPrintEndline ( bool  printEndline) [inline, static]

Print end of line: default true.

Parameters:
printLeveltrue to print end of line, otherwise set to false.

Definition at line 283 of file ULogger.h.

static void ULogger::setPrintLevel ( bool  printLevel) [inline, static]

Print level: default true.

Parameters:
printLeveltrue to print level, otherwise set to false.

Definition at line 276 of file ULogger.h.

static void ULogger::setPrintTime ( bool  printTime) [inline, static]

Print time: default true.

Parameters:
printTimetrue to print time, otherwise set to false.

Definition at line 269 of file ULogger.h.

static void ULogger::setPrintWhere ( bool  printWhere) [inline, static]

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

Parameters:
printWheretrue to print where, otherwise set to false.

Definition at line 298 of file ULogger.h.

static void ULogger::setPrintWhereFullPath ( bool  printWhereFullPath) [inline, static]

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

Parameters:
printWhereFullPathtrue to print the full path, otherwise set to false.

Definition at line 305 of file ULogger.h.

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

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.

Parameters:
typethe ULogger::Type of the logger.
fileNamefile name used with a file logger type.
appendif true, the file isn't overwritten, otherwise it is.

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

Definition at line 175 of file ULogger.cpp.

static Type ULogger::type ( ) [inline, static]

Definition at line 262 of file ULogger.h.

void ULogger::write ( const char *  msg,
  ... 
) [static]

Write a message directly to logger without level handling.

Parameters:
msgthe message to write.
...the variable arguments
Deprecated:
use UDEBUG(), UINFO(), UWARN(), UERROR() or UFATAL()

Definition at line 247 of file ULogger.cpp.

void ULogger::write ( ULogger::Level  level,
const char *  file,
int  line,
const char *  function,
const char *  msg,
  ... 
) [static]

Definition at line 307 of file ULogger.cpp.


Friends And Related Function Documentation

friend class UDestroyer< ULogger > [friend]

Definition at line 429 of file ULogger.h.


Member Data Documentation

bool ULogger::append_ = true [static, protected]

Definition at line 439 of file ULogger.h.

bool ULogger::buffered_ = false [static, private]

Definition at line 550 of file ULogger.h.

std::string ULogger::bufferedMsgs_ [static, private]

Reimplemented in UFileLogger.

Definition at line 552 of file ULogger.h.

UDestroyer< ULogger > ULogger::destroyer_ [static, private]

Definition at line 470 of file ULogger.h.

Definition at line 537 of file ULogger.h.

bool ULogger::exitingState_ = false [static, private]

Definition at line 560 of file ULogger.h.

Definition at line 532 of file ULogger.h.

ULogger * ULogger::instance_ = 0 [static, private]

Definition at line 465 of file ULogger.h.

const std::string ULogger::kDefaultLogFileName = "./ULog.txt" [static]

The default log file name.

Definition at line 232 of file ULogger.h.

ULogger::Level ULogger::level_ = kInfo [static, private]

Definition at line 526 of file ULogger.h.

const char * ULogger::levelName_ = {"DEBUG", " INFO", " WARN", "ERROR", "FATAL"} [static, private]

Definition at line 539 of file ULogger.h.

bool ULogger::limitWhereLength_ = false [static, private]

Definition at line 516 of file ULogger.h.

std::string ULogger::logFileName_ [static, protected]

Definition at line 434 of file ULogger.h.

UMutex ULogger::loggerMutex_ [static, private]

Definition at line 544 of file ULogger.h.

bool ULogger::printColored_ = true [static, private]

Definition at line 494 of file ULogger.h.

bool ULogger::printEndline_ = true [static, private]

Definition at line 488 of file ULogger.h.

bool ULogger::printLevel_ = true [static, private]

Definition at line 482 of file ULogger.h.

bool ULogger::printTime_ = true [static, private]

Definition at line 476 of file ULogger.h.

bool ULogger::printWhere_ = true [static, private]

Definition at line 500 of file ULogger.h.

bool ULogger::printWhereFullPath_ = false [static, private]

Definition at line 508 of file ULogger.h.

Definition at line 521 of file ULogger.h.


The documentation for this class was generated from the following files:


rtabmap
Author(s): Mathieu Labbe
autogenerated on Fri Aug 28 2015 12:51:43