RTAB-Map 0.23.10
Real-Time Appearance-Based Mapping
Loading...
Searching...
No Matches
ULogger Class Reference

#include <ULogger.h>

Public Types

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

Static Public Member Functions

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

Static Public Attributes

static const std::string kDefaultLogFileName
 

Protected Member Functions

void _flush ()
 

Static Protected Member Functions

static ULoggergetInstance ()
 

Static Protected Attributes

static std::string logFileName_
 
static bool append_
 

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).
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;
}
ULogger class and convenient macros.
#define UERROR(...)
Definition ULogger.h:62
#define UINFO(...)
Definition ULogger.h:60
#define UDEBUG(...)
Definition ULogger.h:59
#define UWARN(...)
Definition ULogger.h:61
static void setType(Type type, const std::string &fileName=kDefaultLogFileName, bool append=true)
static void setLevel(ULogger::Level level)
Definition ULogger.h:343

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

◆ Type

Loggers available:

kTypeNoLog, kTypeConsole, kTypeFile

Definition at line 244 of file ULogger.h.

◆ Level

Logger levels, from lowest severity to highest:

kDebug, kInfo, kWarning, kError, kFatal

Definition at line 252 of file ULogger.h.

Constructor & Destructor Documentation

◆ ULogger()

ULogger::ULogger ( )
inlineprotected

Definition at line 434 of file ULogger.h.

Member Function Documentation

◆ setType()

static 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.

◆ type()

static Type ULogger::type ( )
inlinestatic

Definition at line 266 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.

◆ isPrintTime()

static bool ULogger::isPrintTime ( )
inlinestatic

Definition at line 274 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.

◆ isPrintLevel()

static bool ULogger::isPrintLevel ( )
inlinestatic

Definition at line 281 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.

◆ isPrintEndLine()

static bool ULogger::isPrintEndLine ( )
inlinestatic

Definition at line 288 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.

◆ isPrintColored()

static bool ULogger::isPrintColored ( )
inlinestatic

Definition at line 296 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.

◆ isPrintWhere()

static bool ULogger::isPrintWhere ( )
inlinestatic

Definition at line 303 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.

◆ isPrintThreadId()

static bool ULogger::isPrintThreadId ( )
inlinestatic

Definition at line 310 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.

◆ isPrintWhereFullPath()

static bool ULogger::isPrintWhereFullPath ( )
inlinestatic

Definition at line 317 of file ULogger.h.

◆ setBuffered()

static 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.

Note that if logging to a file, flush doesn't mean that messages are written to file right away. To write everything on disk, set logger type to kTypeNoLog.

See also
ULogger::flush()
Parameters
bufferedtrue to buffer messages, otherwise set to false.

◆ isBuffered()

static bool ULogger::isBuffered ( )
inlinestatic

Definition at line 330 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 343 of file ULogger.h.

◆ level()

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

Definition at line 344 of file ULogger.h.

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

◆ eventLevel()

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

Definition at line 353 of file ULogger.h.

◆ setTreadIdFilter()

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

◆ getTreadIdFilter()

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

Definition at line 360 of file ULogger.h.

◆ registerCurrentThread()

static 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.

◆ reset()

static void ULogger::reset ( )
static

Reset to default parameters.

◆ flush()

static void ULogger::flush ( )
static

Flush buffered messages.

See also
setBuffered()

◆ getTime()

static 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.

Friends And Related Symbol Documentation

◆ UDestroyer< ULogger >

friend class UDestroyer< ULogger >
friend

Definition at line 445 of file ULogger.h.

Member Data Documentation

◆ kDefaultLogFileName

const std::string ULogger::kDefaultLogFileName
static

The default log file name.

Definition at line 236 of file ULogger.h.

◆ logFileName_

std::string ULogger::logFileName_
staticprotected

Definition at line 459 of file ULogger.h.

◆ append_

bool ULogger::append_
staticprotected

Definition at line 464 of file ULogger.h.


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