hydrogen 1.2.3
Logger Class Reference

Class for writing logs to the console. More...

#include <Logger.h>

Data Structures

class  CrashContext
 Helper class to preserve and restore recursive crash context strings using an RAAI pattern. More...
 

Public Types

enum  log_levels {
  None = 0x00 , Error = 0x01 , Warning = 0x02 , Info = 0x04 ,
  Debug = 0x08 , Constructors = 0x10 , Locks = 0x20
}
 possible logging bits More...
 
typedef std::list< QString > queue_t
 message queue type
 

Public Member Functions

 ~Logger ()
 destructor
 
void flush () const
 Waits till the logger thread poped all remaining messages from __msg_queue.
 
void log (unsigned level, const QString &class_name, const char *func_name, const QString &msg)
 the log function
 
void set_use_file (bool use)
 set use file flag
 
bool should_log (unsigned lvl) const
 return true if the level is set in the bitmask
 
bool use_file () const
 return __use_file
 

Static Public Member Functions

static unsigned bit_mask ()
 return the current log level bit mask
 
static Loggerbootstrap (unsigned msk, const QString &sLogFilePath=QString(), bool bUseStdout=true)
 create the logger instance if not exists, set the log level and return the instance
 
static Loggercreate_instance (const QString &sLogFilePath=QString(), bool bUseStdout=true)
 If __instance equals 0, a new H2Core::Logger singleton will be created and stored in it.
 
static Loggerget_instance ()
 Returns a pointer to the current H2Core::Logger singleton stored in __instance.
 
static unsigned parse_log_level (const char *lvl)
 parse a log level string and return the corresponding bit mask
 
static void set_bit_mask (unsigned msk)
 set the bitmask
 
Crash context management

Access the crash context string that can be used to report what caused a crash.

The crash-context string is a thread-local property, and may be read by a fatal exception handler (which will execute in the same thread that caused the crash). This avoids potential contention for locking and unlocking of a shared crash context structure.

static void setCrashContext (QString *pContext)
 
static QString * getCrashContext ()
 

Private Member Functions

 Logger (const QString &sLogFilePath=QString(), bool bUseStdout=true)
 constructor
 

Static Private Member Functions

static int hextoi (const char *str, long len)
 convert an hex string to an integer.
 

Private Attributes

pthread_cond_t __messages_available
 
queue_t __msg_queue
 the message queue
 
pthread_mutex_t __mutex
 lock for adding or removing elements only
 
bool __running
 set to true when the logger thread is running
 
bool __use_file
 write log to file if set to true
 
bool m_bUseStdout
 
QString m_sLogFilePath
 

Static Private Attributes

static unsigned __bit_msk = 0
 the bitmask of log_level_t
 
static Logger__instance =nullptr
 Object holding the current H2Core::Logger singleton.
 
static const char * __levels [] = { "None", "Error", "Warning", "Info", "Debug", "Constructors", "Locks" }
 levels strings
 
static thread_local QString * pCrashContext = nullptr
 

Friends

void * loggerThread_func (void *param)
 needed for being able to access logger internal
 

Detailed Description

Class for writing logs to the console.

Definition at line 42 of file Logger.h.

Member Typedef Documentation

◆ queue_t

typedef std::list<QString> queue_t

message queue type

Definition at line 56 of file Logger.h.

Member Enumeration Documentation

◆ log_levels

enum log_levels

possible logging bits

Enumerator
None 
Error 
Warning 
Info 
Debug 
Constructors 
Locks 

Definition at line 45 of file Logger.h.

Constructor & Destructor Documentation

◆ ~Logger()

~Logger ( )

destructor

Definition at line 138 of file Logger.cpp.

◆ Logger()

Logger ( const QString & sLogFilePath = QString(),
bool bUseStdout = true )
private

constructor

Definition at line 112 of file Logger.cpp.

Member Function Documentation

◆ bit_mask()

static unsigned bit_mask ( )
inlinestatic

return the current log level bit mask

Definition at line 90 of file Logger.h.

◆ bootstrap()

Logger * bootstrap ( unsigned msk,
const QString & sLogFilePath = QString(),
bool bUseStdout = true )
static

create the logger instance if not exists, set the log level and return the instance

Parameters
mskthe logging level bitmask

Definition at line 102 of file Logger.cpp.

◆ create_instance()

Logger * create_instance ( const QString & sLogFilePath = QString(),
bool bUseStdout = true )
static

If __instance equals 0, a new H2Core::Logger singleton will be created and stored in it.

It is called in Hydrogen::create_instance().

Definition at line 107 of file Logger.cpp.

◆ flush()

void flush ( ) const

Waits till the logger thread poped all remaining messages from __msg_queue.

Note that this function will neither lock __msg_queue nor prevent routines from adding new messages to the queue.

Definition at line 195 of file Logger.cpp.

◆ get_instance()

static Logger * get_instance ( )
inlinestatic

Returns a pointer to the current H2Core::Logger singleton stored in __instance.

Definition at line 74 of file Logger.h.

◆ getCrashContext()

static QString * getCrashContext ( )
inlinestatic

Definition at line 136 of file Logger.h.

◆ hextoi()

int hextoi ( const char * str,
long len )
staticprivate

convert an hex string to an integer.

returns -1 on failure.

Parameters
strthe hex string to convert
lenthe length of the string

Definition at line 241 of file Logger.cpp.

◆ log()

void log ( unsigned level,
const QString & class_name,
const char * func_name,
const QString & msg )

the log function

Parameters
levelused to output the corresponding level string
class_namethe name of the calling class
func_namethe name of the calling function/method
msgthe message to log

Definition at line 144 of file Logger.cpp.

◆ parse_log_level()

unsigned parse_log_level ( const char * lvl)
static

parse a log level string and return the corresponding bit mask

Parameters
lvlthe log level string

Definition at line 208 of file Logger.cpp.

◆ set_bit_mask()

static void set_bit_mask ( unsigned msk)
inlinestatic

set the bitmask

Parameters
mskthe new bitmask to set

Definition at line 88 of file Logger.h.

◆ set_use_file()

void set_use_file ( bool use)
inline

set use file flag

Parameters
usethe flag status

Definition at line 95 of file Logger.h.

◆ setCrashContext()

static void setCrashContext ( QString * pContext)
inlinestatic

Definition at line 135 of file Logger.h.

◆ should_log()

bool should_log ( unsigned lvl) const
inline

return true if the level is set in the bitmask

Parameters
lvlthe level to check

Definition at line 83 of file Logger.h.

◆ use_file()

bool use_file ( ) const
inline

return __use_file

Definition at line 97 of file Logger.h.

Friends And Related Symbol Documentation

◆ loggerThread_func

void * loggerThread_func ( void * param)
friend

needed for being able to access logger internal

Parameters
paramis a pointer to the logger instance

Definition at line 44 of file Logger.cpp.

Field Documentation

◆ __bit_msk

unsigned __bit_msk = 0
staticprivate

the bitmask of log_level_t

Definition at line 161 of file Logger.h.

◆ __instance

Logger * __instance =nullptr
staticprivate

Object holding the current H2Core::Logger singleton.

It is initialized with NULL, set with create_instance(), and accessed with get_instance().

Definition at line 156 of file Logger.h.

◆ __levels

const char * __levels = { "None", "Error", "Warning", "Info", "Debug", "Constructors", "Locks" }
staticprivate

levels strings

Definition at line 162 of file Logger.h.

◆ __messages_available

pthread_cond_t __messages_available
private

Definition at line 163 of file Logger.h.

◆ __msg_queue

queue_t __msg_queue
private

the message queue

Definition at line 160 of file Logger.h.

◆ __mutex

pthread_mutex_t __mutex
private

lock for adding or removing elements only

Definition at line 159 of file Logger.h.

◆ __running

bool __running
private

set to true when the logger thread is running

Definition at line 158 of file Logger.h.

◆ __use_file

bool __use_file
private

write log to file if set to true

Definition at line 157 of file Logger.h.

◆ m_bUseStdout

bool m_bUseStdout
private

Definition at line 165 of file Logger.h.

◆ m_sLogFilePath

QString m_sLogFilePath
private

Definition at line 164 of file Logger.h.

◆ pCrashContext

thread_local QString * pCrashContext = nullptr
staticprivate

Definition at line 167 of file Logger.h.