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 | List of all members
ULogger Class Reference

#include <ULogger.h>

Inheritance diagram for ULogger:
Inheritance graph
[legend]

Public Types

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

Static Public Member Functions

static ULogger::Level eventLevel ()
 
static void flush ()
 
static std::map< std::string, unsigned long > getRegisteredThreads ()
 
static int getTime (std::string &timeStr)
 
static const std::set< unsigned long > & getTreadIdFilter ()
 
static bool isBuffered ()
 
static bool isPrintColored ()
 
static bool isPrintEndLine ()
 
static bool isPrintLevel ()
 
static bool isPrintThreadId ()
 
static bool isPrintTime ()
 
static bool isPrintWhere ()
 
static bool isPrintWhereFullPath ()
 
static ULogger::Level level ()
 
static void registerCurrentThread (const std::string &name)
 
static void reset ()
 
static void setBuffered (bool buffered)
 
static void setEventLevel (ULogger::Level eventSentLevel)
 
static void setLevel (ULogger::Level level)
 
static void setPrintColored (bool printColored)
 
static void setPrintEndline (bool printEndline)
 
static void setPrintLevel (bool printLevel)
 
static void setPrintThreadId (bool printThreadId)
 
static void setPrintTime (bool printTime)
 
static void setPrintWhere (bool printWhere)
 
static void setPrintWhereFullPath (bool printWhereFullPath)
 
static void setTreadIdFilter (const std::set< unsigned long > &ids)
 
static void setTreadIdFilter (const std::vector< std::string > &ids)
 
static void setType (Type type, const std::string &fileName=kDefaultLogFileName, bool append=true)
 
static Type type ()
 
static void unregisterCurrentThread ()
 
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 *, va_list)
 
virtual void _writeStr (const char *)
 

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 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 printThreadID_ = false
 
static bool printTime_ = true
 
static bool printWhere_ = true
 
static bool printWhereFullPath_ = false
 
static std::map< std::string, unsigned long > registeredThreads_
 
static std::set< unsigned long > threadIdFilter_
 
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:

int main(int argc, char * argv[])
{
// Set the logger type. The choices are kTypeConsole,
// kTypeFile or kTypeNoLog (nothing is logged).
// 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.
// 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/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 229 of file ULogger.h.

Member Enumeration Documentation

◆ Level

Logger levels, from lowest severity to highest:

Enumerator
kDebug 
kInfo 
kWarning 
kError 
kFatal 

Definition at line 252 of file ULogger.h.

◆ Type

Loggers available:

Enumerator
kTypeNoLog 
kTypeConsole 
kTypeFile 

Definition at line 244 of file ULogger.h.

Constructor & Destructor Documentation

◆ ULogger()

ULogger::ULogger ( )
inlineprotected

Definition at line 426 of file ULogger.h.

◆ ~ULogger()

ULogger::~ULogger ( )
protectedvirtual

Definition at line 665 of file ULogger.cpp.

Member Function Documentation

◆ _flush()

void ULogger::_flush ( )
protected

Definition at line 294 of file ULogger.cpp.

◆ _write()

virtual void ULogger::_write ( const char *  ,
va_list   
)
inlineprivatevirtual

Reimplemented in UFileLogger, and UConsoleLogger.

Definition at line 475 of file ULogger.h.

◆ _writeStr()

virtual void ULogger::_writeStr ( const char *  )
inlineprivatevirtual

Reimplemented in UFileLogger, and UConsoleLogger.

Definition at line 476 of file ULogger.h.

◆ createInstance()

ULogger * ULogger::createInstance ( )
staticprivate

Definition at line 650 of file ULogger.cpp.

◆ eventLevel()

static ULogger::Level ULogger::eventLevel ( )
inlinestatic

Definition at line 349 of file ULogger.h.

◆ flush()

void ULogger::flush ( )
static

Flush buffered messages.

See also
setBuffered()

Definition at line 281 of file ULogger.cpp.

◆ getInstance()

ULogger * ULogger::getInstance ( )
staticprotected

Definition at line 641 of file ULogger.cpp.

◆ getRegisteredThreads()

std::map< std::string, unsigned long > ULogger::getRegisteredThreads ( )
static

Definition at line 247 of file ULogger.cpp.

◆ getTime()

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 593 of file ULogger.cpp.

◆ getTreadIdFilter()

static const std::set<unsigned long>& ULogger::getTreadIdFilter ( )
inlinestatic

Definition at line 356 of file ULogger.h.

◆ isBuffered()

static bool ULogger::isBuffered ( )
inlinestatic

Definition at line 326 of file ULogger.h.

◆ isPrintColored()

static bool ULogger::isPrintColored ( )
inlinestatic

Definition at line 296 of file ULogger.h.

◆ isPrintEndLine()

static bool ULogger::isPrintEndLine ( )
inlinestatic

Definition at line 288 of file ULogger.h.

◆ isPrintLevel()

static bool ULogger::isPrintLevel ( )
inlinestatic

Definition at line 281 of file ULogger.h.

◆ isPrintThreadId()

static bool ULogger::isPrintThreadId ( )
inlinestatic

Definition at line 310 of file ULogger.h.

◆ isPrintTime()

static bool ULogger::isPrintTime ( )
inlinestatic

Definition at line 274 of file ULogger.h.

◆ isPrintWhere()

static bool ULogger::isPrintWhere ( )
inlinestatic

Definition at line 303 of file ULogger.h.

◆ isPrintWhereFullPath()

static bool ULogger::isPrintWhereFullPath ( )
inlinestatic

Definition at line 317 of file ULogger.h.

◆ level()

static ULogger::Level ULogger::level ( )
inlinestatic

Definition at line 340 of file ULogger.h.

◆ registerCurrentThread()

void ULogger::registerCurrentThread ( const std::string name)
static

Threads can register to this list. If name is empty, it will clear the thread in the list. Should be called from the thread itself.

Definition at line 218 of file ULogger.cpp.

◆ reset()

void ULogger::reset ( )
static

Reset to default parameters.

Definition at line 255 of file ULogger.cpp.

◆ setBuffered()

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 271 of file ULogger.cpp.

◆ setEventLevel()

static void ULogger::setEventLevel ( ULogger::Level  eventSentLevel)
inlinestatic

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 348 of file ULogger.h.

◆ setLevel()

static void ULogger::setLevel ( ULogger::Level  level)
inlinestatic

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 339 of file ULogger.h.

◆ setPrintColored()

static void ULogger::setPrintColored ( bool  printColored)
inlinestatic

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 295 of file ULogger.h.

◆ setPrintEndline()

static void ULogger::setPrintEndline ( bool  printEndline)
inlinestatic

Print end of line: default true.

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

Definition at line 287 of file ULogger.h.

◆ setPrintLevel()

static void ULogger::setPrintLevel ( bool  printLevel)
inlinestatic

Print level: default true.

Parameters
printLeveltrue to print level, otherwise set to false.

Definition at line 280 of file ULogger.h.

◆ setPrintThreadId()

static void ULogger::setPrintThreadId ( bool  printThreadId)
inlinestatic

Print thread ID: default false.

Parameters
printThreadIdtrue to print where, otherwise set to false.

Definition at line 309 of file ULogger.h.

◆ setPrintTime()

static void ULogger::setPrintTime ( bool  printTime)
inlinestatic

Print time: default true.

Parameters
printTimetrue to print time, otherwise set to false.

Definition at line 273 of file ULogger.h.

◆ setPrintWhere()

static void ULogger::setPrintWhere ( bool  printWhere)
inlinestatic

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

Parameters
printWheretrue to print where, otherwise set to false.

Definition at line 302 of file ULogger.h.

◆ setPrintWhereFullPath()

static void ULogger::setPrintWhereFullPath ( bool  printWhereFullPath)
inlinestatic

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 316 of file ULogger.h.

◆ setTreadIdFilter() [1/2]

static void ULogger::setTreadIdFilter ( const std::set< unsigned long > &  ids)
inlinestatic

If not empty, only show log messages from threads included in this list.

Definition at line 354 of file ULogger.h.

◆ setTreadIdFilter() [2/2]

void ULogger::setTreadIdFilter ( const std::vector< std::string > &  ids)
static

Definition at line 204 of file ULogger.cpp.

◆ setType()

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 176 of file ULogger.cpp.

◆ type()

static Type ULogger::type ( )
inlinestatic

Definition at line 266 of file ULogger.h.

◆ unregisterCurrentThread()

void ULogger::unregisterCurrentThread ( )
static

Definition at line 226 of file ULogger.cpp.

◆ write() [1/2]

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 300 of file ULogger.cpp.

◆ write() [2/2]

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

Definition at line 360 of file ULogger.cpp.

Friends And Related Function Documentation

◆ UDestroyer< ULogger >

friend class UDestroyer< ULogger >
friend

Definition at line 446 of file ULogger.h.

Member Data Documentation

◆ append_

bool ULogger::append_ = true
staticprotected

Definition at line 456 of file ULogger.h.

◆ buffered_

bool ULogger::buffered_ = false
staticprivate

Definition at line 567 of file ULogger.h.

◆ bufferedMsgs_

std::string ULogger::bufferedMsgs_
staticprivate

Definition at line 569 of file ULogger.h.

◆ destroyer_

UDestroyer< ULogger > ULogger::destroyer_
staticprivate

Definition at line 487 of file ULogger.h.

◆ eventLevel_

ULogger::Level ULogger::eventLevel_ = kFatal
staticprivate

Definition at line 554 of file ULogger.h.

◆ instance_

ULogger * ULogger::instance_ = 0
staticprivate

Definition at line 482 of file ULogger.h.

◆ kDefaultLogFileName

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

The default log file name.

Definition at line 236 of file ULogger.h.

◆ level_

ULogger::Level ULogger::level_ = kInfo
staticprivate

Definition at line 549 of file ULogger.h.

◆ levelName_

const char * ULogger::levelName_ = {"DEBUG", " INFO", " WARN", "ERROR", "FATAL"}
staticprivate

Definition at line 556 of file ULogger.h.

◆ limitWhereLength_

bool ULogger::limitWhereLength_ = false
staticprivate

Definition at line 539 of file ULogger.h.

◆ logFileName_

std::string ULogger::logFileName_
staticprotected

Definition at line 451 of file ULogger.h.

◆ loggerMutex_

UMutex ULogger::loggerMutex_
staticprivate

Definition at line 561 of file ULogger.h.

◆ printColored_

bool ULogger::printColored_ = true
staticprivate

Definition at line 511 of file ULogger.h.

◆ printEndline_

bool ULogger::printEndline_ = true
staticprivate

Definition at line 505 of file ULogger.h.

◆ printLevel_

bool ULogger::printLevel_ = true
staticprivate

Definition at line 499 of file ULogger.h.

◆ printThreadID_

bool ULogger::printThreadID_ = false
staticprivate

Definition at line 531 of file ULogger.h.

◆ printTime_

bool ULogger::printTime_ = true
staticprivate

Definition at line 493 of file ULogger.h.

◆ printWhere_

bool ULogger::printWhere_ = true
staticprivate

Definition at line 517 of file ULogger.h.

◆ printWhereFullPath_

bool ULogger::printWhereFullPath_ = false
staticprivate

Definition at line 525 of file ULogger.h.

◆ registeredThreads_

std::map< std::string, unsigned long > ULogger::registeredThreads_
staticprivate

Definition at line 572 of file ULogger.h.

◆ threadIdFilter_

std::set< unsigned long > ULogger::threadIdFilter_
staticprivate

Definition at line 571 of file ULogger.h.

◆ type_

ULogger::Type ULogger::type_ = ULogger::kTypeNoLog
staticprivate

Definition at line 544 of file ULogger.h.


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


rtabmap
Author(s): Mathieu Labbe
autogenerated on Mon Jan 23 2023 03:38:59