hydrogen 1.2.5
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.
bool getLogColors () const
void log (unsigned level, const QString &sClassName, const char *func_name, const QString &sMsg, const QString &sColor="")
 the log function
bool should_log (unsigned lvl) const
 return true if the level is set in the bitmask

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, bool bLogTimestamps=false, bool bLogColors=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, bool bLogTimestamps=false, bool bLogColors=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 bool isAvailable ()
 Checks whether the Logger instances was already created and can be used by other parts of Hydrogen.
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, bool bLogTimestamps=false, bool bLogColors=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 m_bLogColors
bool m_bLogTimestamps
bool m_bUseStdout
QStringList m_colorList
QStringList m_prefixList
QString m_sColorOff
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 41 of file Logger.h.

Member Typedef Documentation

◆ queue_t

typedef std::list<QString> queue_t

message queue type

Definition at line 55 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 44 of file Logger.h.

Constructor & Destructor Documentation

◆ ~Logger()

~Logger ( )

destructor

Definition at line 217 of file Logger.cpp.

◆ Logger()

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

constructor

Definition at line 170 of file Logger.cpp.

Member Function Documentation

◆ bit_mask()

unsigned bit_mask ( )
inlinestatic

return the current log level bit mask

Definition at line 103 of file Logger.h.

◆ bootstrap()

Logger * bootstrap ( unsigned msk,
const QString & sLogFilePath = QString(),
bool bUseStdout = true,
bool bLogTimestamps = false,
bool bLogColors = 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 136 of file Logger.cpp.

◆ create_instance()

Logger * create_instance ( const QString & sLogFilePath = QString(),
bool bUseStdout = true,
bool bLogTimestamps = false,
bool bLogColors = 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 161 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 276 of file Logger.cpp.

◆ get_instance()

Logger * get_instance ( )
inlinestatic

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

Definition at line 81 of file Logger.h.

◆ getCrashContext()

QString * getCrashContext ( )
inlinestatic

Definition at line 145 of file Logger.h.

◆ getLogColors()

bool getLogColors ( ) const
inline

Definition at line 201 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 322 of file Logger.cpp.

◆ isAvailable()

bool isAvailable ( )
inlinestatic

Checks whether the Logger instances was already created and can be used by other parts of Hydrogen.

Definition at line 88 of file Logger.h.

◆ log()

void log ( unsigned level,
const QString & sClassName,
const char * func_name,
const QString & sMsg,
const QString & sColor = "" )

the log function

Parameters
levelused to output the corresponding level string
sClassNamethe name of the calling class
func_namethe name of the calling function/method
sMsgthe message to log
sColoralternate color

Definition at line 223 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 289 of file Logger.cpp.

◆ set_bit_mask()

void set_bit_mask ( unsigned msk)
inlinestatic

set the bitmask

Parameters
mskthe new bitmask to set

Definition at line 101 of file Logger.h.

◆ setCrashContext()

void setCrashContext ( QString * pContext)
inlinestatic

Definition at line 144 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 96 of file Logger.h.

◆ 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 54 of file Logger.cpp.

Field Documentation

◆ __bit_msk

unsigned __bit_msk = 0
staticprivate

the bitmask of log_level_t

Definition at line 171 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 167 of file Logger.h.

◆ __levels

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

levels strings

Definition at line 172 of file Logger.h.

◆ __messages_available

pthread_cond_t __messages_available
private

Definition at line 173 of file Logger.h.

◆ __msg_queue

queue_t __msg_queue
private

the message queue

Definition at line 170 of file Logger.h.

◆ __mutex

pthread_mutex_t __mutex
private

lock for adding or removing elements only

Definition at line 169 of file Logger.h.

◆ __running

bool __running
private

set to true when the logger thread is running

Definition at line 168 of file Logger.h.

◆ m_bLogColors

bool m_bLogColors
private

Definition at line 182 of file Logger.h.

◆ m_bLogTimestamps

bool m_bLogTimestamps
private

Definition at line 181 of file Logger.h.

◆ m_bUseStdout

bool m_bUseStdout
private

Definition at line 180 of file Logger.h.

◆ m_colorList

QStringList m_colorList
private

Definition at line 177 of file Logger.h.

◆ m_prefixList

QStringList m_prefixList
private

Definition at line 176 of file Logger.h.

◆ m_sColorOff

QString m_sColorOff
private

Definition at line 178 of file Logger.h.

◆ m_sLogFilePath

QString m_sLogFilePath
private

Definition at line 174 of file Logger.h.

◆ pCrashContext

thread_local QString * pCrashContext = nullptr
staticprivate

Definition at line 184 of file Logger.h.