hydrogen 1.2.6
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-2025 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#include <QStringList>
32
33#include <core/config.h>
34
35namespace H2Core {
36
41class Logger {
42 public:
45 None = 0x00,
46 Error = 0x01,
47 Warning = 0x02,
48 Info = 0x04,
49 Debug = 0x08,
51 Locks = 0x20
52 };
53
55 typedef std::list<QString> queue_t;
56
61 static Logger* bootstrap( unsigned msk,
62 const QString& sLogFilePath = QString(),
63 bool bUseStdout = true,
64 bool bLogTimestamps = false,
65 bool bLogColors = true );
72 static Logger* create_instance( const QString& sLogFilePath = QString(),
73 bool bUseStdout = true,
74 bool bLogTimestamps = false,
75 bool bLogColors = true );
76
81 static Logger* get_instance(){ assert(__instance); return __instance; }
82
84 ~Logger();
85
88 static bool isAvailable() {
89 return __instance == nullptr ? false : true;
90 }
91
96 bool should_log( unsigned lvl ) const { return (lvl&__bit_msk); }
101 static void set_bit_mask( unsigned msk ) { __bit_msk = msk; }
103 static unsigned bit_mask() { return __bit_msk; }
104
112 void flush() const;
113
118 static unsigned parse_log_level( const char* lvl );
119
128 void log( unsigned level, const QString& sClassName,
129 const char* func_name, const QString& sMsg,
130 const QString& sColor = "" );
135 friend void* loggerThread_func( void* param );
136
144 static void setCrashContext( QString *pContext ) { Logger::pCrashContext = pContext; }
145 static QString *getCrashContext() { return Logger::pCrashContext; }
147
148 bool getLogColors() const;
149
153 QString *pThisContext;
154 public:
155 CrashContext( QString *pContext );
156 CrashContext( QString sContext );
158 };
159
160 private:
169 pthread_mutex_t __mutex;
171 static unsigned __bit_msk;
172 static const char* __levels[];
173 pthread_cond_t __messages_available;
175
176 QStringList m_prefixList;
177 QStringList m_colorList;
178 QString m_sColorOff;
179
183
184 thread_local static QString *pCrashContext;
185
187 Logger( const QString& sLogFilePath = QString(), bool bUseStdout = true,
188 bool bLogTimestamps = false, bool bLogColors = true );
189
190#ifndef HAVE_SSCANF
197 static int hextoi( const char* str, long len );
198#endif // HAVE_SSCANF
199};
200
201inline bool Logger::getLogColors() const {
202 return m_bLogColors;
203}
204
205};
206
207#endif // H2C_LOGGER_H
208
209/* vim: set softtabstop=4 noexpandtab: */
CrashContext(QString *pContext)
Definition Logger.cpp:369
static Logger * create_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.
Definition Logger.cpp:161
bool getLogColors() const
Definition Logger.h:201
~Logger()
destructor
Definition Logger.cpp:217
bool m_bLogColors
Definition Logger.h:182
pthread_cond_t __messages_available
Definition Logger.h:173
QString m_sColorOff
Definition Logger.h:178
static QString * getCrashContext()
Definition Logger.h:145
std::list< QString > queue_t
message queue type
Definition Logger.h:55
static Logger * bootstrap(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
Definition Logger.cpp:136
pthread_mutex_t __mutex
lock for adding or removing elements only
Definition Logger.h:169
void flush() const
Waits till the logger thread poped all remaining messages from __msg_queue.
Definition Logger.cpp:276
Logger(const QString &sLogFilePath=QString(), bool bUseStdout=true, bool bLogTimestamps=false, bool bLogColors=true)
constructor
Definition Logger.cpp:170
bool __running
set to true when the logger thread is running
Definition Logger.h:168
static Logger * __instance
Object holding the current H2Core::Logger singleton.
Definition Logger.h:167
static bool isAvailable()
Checks whether the Logger instances was already created and can be used by other parts of Hydrogen.
Definition Logger.h:88
static int hextoi(const char *str, long len)
convert an hex string to an integer.
Definition Logger.cpp:322
bool m_bUseStdout
Definition Logger.h:180
static unsigned bit_mask()
return the current log level bit mask
Definition Logger.h:103
queue_t __msg_queue
the message queue
Definition Logger.h:170
static thread_local QString * pCrashContext
Definition Logger.h:184
QStringList m_colorList
Definition Logger.h:177
QStringList m_prefixList
Definition Logger.h:176
static const char * __levels[]
levels strings
Definition Logger.h:172
QString m_sLogFilePath
Definition Logger.h:174
void log(unsigned level, const QString &sClassName, const char *func_name, const QString &sMsg, const QString &sColor="")
the log function
Definition Logger.cpp:223
bool should_log(unsigned lvl) const
return true if the level is set in the bitmask
Definition Logger.h:96
static Logger * get_instance()
Returns a pointer to the current H2Core::Logger singleton stored in __instance.
Definition Logger.h:81
static void set_bit_mask(unsigned msk)
set the bitmask
Definition Logger.h:101
static unsigned parse_log_level(const char *lvl)
parse a log level string and return the corresponding bit mask
Definition Logger.cpp:289
static void setCrashContext(QString *pContext)
Definition Logger.h:144
static unsigned __bit_msk
the bitmask of log_level_t
Definition Logger.h:171
bool m_bLogTimestamps
Definition Logger.h:181
friend void * loggerThread_func(void *param)
needed for being able to access logger internal
Definition Logger.cpp:54
log_levels
possible logging bits
Definition Logger.h:44