hydrogen 1.2.3
Logger.h
Go to the documentation of this file.
1/*
2 * Hydrogen
3 * Copyright(c) 2002-2008 by Alex >Comix< Cominu [comix@users.sourceforge.net]
4 * Copyright(c) 2008-2024 The hydrogen development team [hydrogen-devel@lists.sourceforge.net]
5 *
6 * http://www.hydrogen-music.org
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY, without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see https://www.gnu.org/licenses
20 *
21 */
22
23#ifndef H2C_LOGGER_H
24#define H2C_LOGGER_H
25
26#include <cassert>
27#include <list>
28#include <pthread.h>
29#include <memory>
30#include <QtCore/QString>
31
32#include <core/config.h>
33
34class QStringList;
35
36namespace H2Core {
37
42class Logger {
43 public:
46 None = 0x00,
47 Error = 0x01,
48 Warning = 0x02,
49 Info = 0x04,
50 Debug = 0x08,
52 Locks = 0x20
53 };
54
56 typedef std::list<QString> queue_t;
57
62 static Logger* bootstrap( unsigned msk, const QString& sLogFilePath = QString(), bool bUseStdout = true );
69 static Logger* create_instance( const QString& sLogFilePath = QString(), bool bUseStdout = true );
74 static Logger* get_instance(){ assert(__instance); return __instance; }
75
77 ~Logger();
78
83 bool should_log( unsigned lvl ) const { return (lvl&__bit_msk); }
88 static void set_bit_mask( unsigned msk ) { __bit_msk = msk; }
90 static unsigned bit_mask() { return __bit_msk; }
95 void set_use_file( bool use ) { __use_file = use; }
97 bool use_file() const { return __use_file; }
98
106 void flush() const;
107
112 static unsigned parse_log_level( const char* lvl );
113
121 void log( unsigned level, const QString& class_name, const char* func_name, const QString& msg );
126 friend void* loggerThread_func( void* param );
127
135 static void setCrashContext( QString *pContext ) { Logger::pCrashContext = pContext; }
136 static QString *getCrashContext() { return Logger::pCrashContext; }
142 QString *pThisContext;
143 public:
144 CrashContext( QString *pContext );
145 CrashContext( QString sContext );
147 };
148
149 private:
159 pthread_mutex_t __mutex;
161 static unsigned __bit_msk;
162 static const char* __levels[];
163 pthread_cond_t __messages_available;
166
167 thread_local static QString *pCrashContext;
168
170 Logger( const QString& sLogFilePath = QString(), bool bUseStdout = true );
171
172#ifndef HAVE_SSCANF
179 static int hextoi( const char* str, long len );
180#endif // HAVE_SSCANF
181};
182
183};
184
185#endif // H2C_LOGGER_H
186
187/* vim: set softtabstop=4 noexpandtab: */
Helper class to preserve and restore recursive crash context strings using an RAAI pattern.
Definition Logger.h:140
CrashContext(QString *pContext)
Definition Logger.cpp:288
Class for writing logs to the console.
Definition Logger.h:42
~Logger()
destructor
Definition Logger.cpp:138
static Logger * create_instance(const QString &sLogFilePath=QString(), bool bUseStdout=true)
If __instance equals 0, a new H2Core::Logger singleton will be created and stored in it.
Definition Logger.cpp:107
pthread_cond_t __messages_available
Definition Logger.h:163
bool __use_file
write log to file if set to true
Definition Logger.h:157
void log(unsigned level, const QString &class_name, const char *func_name, const QString &msg)
the log function
Definition Logger.cpp:144
static QString * getCrashContext()
Definition Logger.h:136
std::list< QString > queue_t
message queue type
Definition Logger.h:56
pthread_mutex_t __mutex
lock for adding or removing elements only
Definition Logger.h:159
void flush() const
Waits till the logger thread poped all remaining messages from __msg_queue.
Definition Logger.cpp:195
bool __running
set to true when the logger thread is running
Definition Logger.h:158
void set_use_file(bool use)
set use file flag
Definition Logger.h:95
static Logger * __instance
Object holding the current H2Core::Logger singleton.
Definition Logger.h:156
Logger(const QString &sLogFilePath=QString(), bool bUseStdout=true)
constructor
Definition Logger.cpp:112
bool use_file() const
return __use_file
Definition Logger.h:97
static int hextoi(const char *str, long len)
convert an hex string to an integer.
Definition Logger.cpp:241
bool m_bUseStdout
Definition Logger.h:165
static unsigned bit_mask()
return the current log level bit mask
Definition Logger.h:90
static Logger * bootstrap(unsigned msk, const QString &sLogFilePath=QString(), bool bUseStdout=true)
create the logger instance if not exists, set the log level and return the instance
Definition Logger.cpp:102
queue_t __msg_queue
the message queue
Definition Logger.h:160
static thread_local QString * pCrashContext
Definition Logger.h:167
static const char * __levels[]
levels strings
Definition Logger.h:162
QString m_sLogFilePath
Definition Logger.h:164
bool should_log(unsigned lvl) const
return true if the level is set in the bitmask
Definition Logger.h:83
static Logger * get_instance()
Returns a pointer to the current H2Core::Logger singleton stored in __instance.
Definition Logger.h:74
static void set_bit_mask(unsigned msk)
set the bitmask
Definition Logger.h:88
static unsigned parse_log_level(const char *lvl)
parse a log level string and return the corresponding bit mask
Definition Logger.cpp:208
static void setCrashContext(QString *pContext)
Definition Logger.h:135
static unsigned __bit_msk
the bitmask of log_level_t
Definition Logger.h:161
friend void * loggerThread_func(void *param)
needed for being able to access logger internal
Definition Logger.cpp:44
log_levels
possible logging bits
Definition Logger.h:45