MythTV  master
logging.h
Go to the documentation of this file.
1 #ifndef LOGGING_H_
2 #define LOGGING_H_
3 
4 #include <QMutexLocker>
5 #include <QMutex>
6 #include <QQueue>
7 #include <QPointer>
8 #include <QCoreApplication>
9 
10 #include <cstdint>
11 #include <cstdlib>
12 #include <string>
13 
14 #include "mythconfig.h"
15 #include "mythbaseexp.h" // MBASE_PUBLIC , etc.
16 #include "verbosedefs.h"
17 #include "mthread.h"
18 #include "referencecounter.h"
19 #include "compat.h"
20 #include "mythchrono.h"
21 
22 #ifdef _MSC_VER
23 # include <unistd.h> // pid_t
24 #endif
25 
26 #if CONFIG_SYSTEMD_JOURNAL
27 static constexpr int SYSTEMD_JOURNAL_FACILITY { -99 };
28 #endif
29 
30 class QString;
31 class MSqlQuery;
32 class LoggingItem;
33 
34 void loggingRegisterThread(const QString &name);
35 void loggingDeregisterThread(void);
36 
37 class QWaitCondition;
38 
39 enum LoggingType : std::uint8_t {
40  kMessage = 0x01,
41  kRegistering = 0x02,
43  kFlush = 0x08,
44  kStandardIO = 0x10,
45  kInitializing = 0x20,
46 };
47 
48 class LoggerThread;
49 
50 using tmType = struct tm;
51 
54 class LoggingItem: public QObject, public ReferenceCounter
55 {
56  Q_OBJECT
57 
58  Q_PROPERTY(int pid READ pid WRITE setPid)
59  Q_PROPERTY(qlonglong tid READ tid WRITE setTid)
60  Q_PROPERTY(qulonglong threadId READ threadId WRITE setThreadId)
61  Q_PROPERTY(int line READ line WRITE setLine)
62  Q_PROPERTY(int type READ type WRITE setType)
63  Q_PROPERTY(int level READ level WRITE setLevel)
64  Q_PROPERTY(int facility READ facility WRITE setFacility)
65  Q_PROPERTY(qlonglong epoch READ epochInt WRITE setEpochInt)
66  Q_PROPERTY(QString file READ file WRITE setFile)
67  Q_PROPERTY(QString function READ function WRITE setFunction)
68  Q_PROPERTY(QString threadName READ threadName WRITE setThreadName)
69  Q_PROPERTY(QString appName READ appName WRITE setAppName)
70  Q_PROPERTY(QString logFile READ logFile WRITE setLogFile)
71  Q_PROPERTY(QString message READ message WRITE setMessage)
72 
73  friend class LoggerThread;
74  friend MBASE_PUBLIC void LogPrintLine(uint64_t mask, LogLevel_t level, const char *file, int line,
75  const char *function, QString message);
76 
77  public:
78  QString getThreadName(void);
79  int64_t getThreadTid(void);
80  void setThreadTid(void);
81  static LoggingItem *create(const char *_file, const char *_function, int _line, LogLevel_t _level,
82  LoggingType _type);
83  QString getTimestamp(const char *format = "yyyy-MM-dd HH:mm:ss") const;
84  QString getTimestampUs(const char *format = "yyyy-MM-dd HH:mm:ss") const;
85  char getLevelChar(void);
86 
87  int pid() const { return m_pid; };
88  qlonglong tid() const { return m_tid; };
89  qulonglong threadId() const { return m_threadId; };
90  int line() const { return m_line; };
91  int type() const { return (int)m_type; };
92  int level() const { return (int)m_level; };
93  int facility() const { return m_facility; };
94  std::chrono::microseconds epoch() const { return m_epoch; };
95  qlonglong epochInt() const { return m_epoch.count(); };
96  QString file() const { return m_file; };
97  QString function() const { return m_function; };
98  QString threadName() const { return m_threadName; };
99  QString appName() const { return m_appName; };
100  QString logFile() const { return m_logFile; };
101  QString message() const { return m_message; };
102 
103  void setPid(const int val) { m_pid = val; };
104  void setTid(const qlonglong val) { m_tid = val; };
105  void setThreadId(const qulonglong val) { m_threadId = val; };
106  void setLine(const int val) { m_line = val; };
107  void setType(const int val) { m_type = (LoggingType)val; };
108  void setLevel(const int val) { m_level = (LogLevel_t)val; };
109  void setFacility(const int val) { m_facility = val; };
110  void setEpoch(std::chrono::microseconds val) { m_epoch = val; };
111  void setEpochInt(const qlonglong val) { setEpoch(std::chrono::microseconds(val)); };
112  void setFile(const QString &val) { m_file = val; };
113  void setFunction(const QString &val) { m_function = val; };
114  void setThreadName(const QString &val) { m_threadName = val; };
115  void setAppName(const QString &val) { m_appName = val; };
116  void setLogFile(const QString &val) { m_logFile = val; };
117  void setMessage(const QString &val) { m_message = val; };
118 
119  std::string toString();
120  std::string toStringShort();
121 
122  protected:
123  int m_pid {-1};
124  qlonglong m_tid {-1};
125  qulonglong m_threadId {UINT64_MAX};
126  int m_line {0};
128  LogLevel_t m_level {LOG_INFO};
129  int m_facility {0};
130  std::chrono::microseconds m_epoch {0us};
131  QString m_file;
132  QString m_function;
133  QString m_threadName;
134  QString m_appName;
135  QString m_logFile;
136  QString m_message;
137 
138  private:
140  : ReferenceCounter("LoggingItem", false) {};
141  LoggingItem(const char *_file, const char *_function,
142  int _line, LogLevel_t _level, LoggingType _type);
144 };
145 
148 class LoggerThread : public QObject, public MThread
149 {
150  Q_OBJECT
151 
152  friend MBASE_PUBLIC void LogPrintLine(uint64_t mask, LogLevel_t level, const char *file, int line,
153  const char *function, QString message);
154  public:
155  LoggerThread(QString filename, bool progress, bool quiet, int facility, bool loglong);
156  ~LoggerThread() override;
157  void run(void) override; // MThread
158  void stop(void);
159  bool flush(int timeoutMS = 200000);
160  static void handleItem(LoggingItem *item);
161  void fillItem(LoggingItem *item);
162  private:
164  QWaitCondition *m_waitNotEmpty {nullptr};
168  QWaitCondition *m_waitEmpty {nullptr};
172  bool m_aborted {false};
173  QString m_filename;
175  bool m_progress;
176  bool m_quiet;
177  bool m_loglong;
178  QString m_appname {QCoreApplication::applicationName()};
181  pid_t m_pid;
182 
183  protected:
184  bool logConsole(LoggingItem *item) const;
185 };
186 
187 #endif
188 
189 /*
190  * vim:ts=4:sw=4:ai:et:si:sts=4
191  */
LoggingItem::m_facility
int m_facility
Definition: logging.h:129
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:127
LoggingItem::m_line
int m_line
Definition: logging.h:126
LoggingItem::getTimestamp
QString getTimestamp(const char *format="yyyy-MM-dd HH:mm:ss") const
Convert numerical timestamp to a readable date and time.
Definition: logging.cpp:192
LoggingItem::epoch
std::chrono::microseconds epoch() const
Definition: logging.h:94
LoggingItem::threadName
QString threadName
Definition: logging.h:68
false
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:89
LoggingItem::threadName
QString threadName() const
Definition: logging.h:98
LoggerThread::m_waitNotEmpty
QWaitCondition * m_waitNotEmpty
Condition variable for waiting for the queue to not be empty Protected by logQueueMutex.
Definition: logging.h:164
LoggingItem::logFile
QString logFile
Definition: logging.h:70
progress
bool progress
Definition: mythcommflag.cpp:69
LoggerThread::m_appname
QString m_appname
Cached application name.
Definition: logging.h:178
mythbaseexp.h
LoggingItem::getThreadName
QString getThreadName(void)
Get the name of the thread that produced the LoggingItem.
Definition: logging.cpp:138
LoggingItem::m_logFile
QString m_logFile
Definition: logging.h:135
LoggingItem::m_type
LoggingType m_type
Definition: logging.h:127
LoggingItem::m_pid
int m_pid
Definition: logging.h:123
LoggerThread
The logging thread that consumes the logging queue and dispatches each LoggingItem.
Definition: logging.h:148
LoggerThread::m_waitEmpty
QWaitCondition * m_waitEmpty
Condition variable for waiting for the queue to be empty Protected by logQueueMutex.
Definition: logging.h:168
LoggingItem::epoch
qlonglong epoch
Definition: logging.h:65
quiet
int quiet
Definition: mythcommflag.cpp:68
verbosedefs.h
LoggingItem::toString
std::string toString()
Long format to string.
Definition: logging.cpp:216
build_compdb.file
file
Definition: build_compdb.py:55
LoggingItem::tid
qlonglong tid
Definition: logging.h:59
LoggerThread::Q_DISABLE_COPY
Q_DISABLE_COPY(LoggerThread)
kStandardIO
@ kStandardIO
Definition: logging.h:44
LoggingItem::facility
int facility() const
Definition: logging.h:93
kDeregistering
@ kDeregistering
Definition: logging.h:42
LoggingItem::line
int line() const
Definition: logging.h:90
LoggingItem::m_function
QString m_function
Definition: logging.h:132
loggingRegisterThread
void loggingRegisterThread(const QString &name)
Register the current thread with the given name.
Definition: logging.cpp:701
LoggingItem::level
int level
Definition: logging.h:63
LoggingItem::message
QString message() const
Definition: logging.h:101
LoggingItem::logFile
QString logFile() const
Definition: logging.h:100
LoggingItem::setFile
void setFile(const QString &val)
Definition: logging.h:112
LoggingItem::setThreadName
void setThreadName(const QString &val)
Definition: logging.h:114
LoggingItem
The logging items that are generated by LOG() and are sent to the console.
Definition: logging.h:54
LoggerThread::m_progress
bool m_progress
show only LOG_ERR and more important (console only)
Definition: logging.h:175
LoggingItem::setMessage
void setMessage(const QString &val)
Definition: logging.h:117
kFlush
@ kFlush
Definition: logging.h:43
LoggerThread::fillItem
void fillItem(LoggingItem *item)
Definition: logging.cpp:500
LoggingItem::file
QString file() const
Definition: logging.h:96
LoggingItem::getLevelChar
char getLevelChar(void)
Get the message log level as a single character.
Definition: logging.cpp:207
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
kRegistering
@ kRegistering
Definition: logging.h:41
LoggingItem::m_level
LogLevel_t m_level
Definition: logging.h:128
LoggingItem::setEpoch
void setEpoch(std::chrono::microseconds val)
Definition: logging.h:110
LoggingItem::pid
int pid
Definition: logging.h:58
kMessage
@ kMessage
Definition: logging.h:40
compat.h
LoggerThread::m_aborted
bool m_aborted
Flag to abort the thread.
Definition: logging.h:172
LoggingItem::setLine
void setLine(const int val)
Definition: logging.h:106
LoggingItem::Q_DISABLE_COPY
Q_DISABLE_COPY(LoggingItem)
LoggingItem::setThreadTid
void setThreadTid(void)
Set the thread ID of the thread that produced the LoggingItem.
Definition: logging.cpp:167
LoggerThread::m_filename
QString m_filename
Filename of debug logfile.
Definition: logging.h:174
LoggerThread::run
void run(void) override
Run the logging thread.
Definition: logging.cpp:286
LoggingItem::m_message
QString m_message
Definition: logging.h:136
LoggerThread::logConsole
bool logConsole(LoggingItem *item) const
Process a log message, writing to the console.
Definition: logging.cpp:394
LoggingItem::message
QString message
Definition: logging.h:71
LoggingItem::m_appName
QString m_appName
Definition: logging.h:134
LoggerThread::flush
bool flush(int timeoutMS=200000)
Wait for the queue to be flushed (up to a timeout)
Definition: logging.cpp:486
LoggingItem::type
int type() const
Definition: logging.h:91
LoggingItem::LogPrintLine
friend MBASE_PUBLIC void LogPrintLine(uint64_t mask, LogLevel_t level, const char *file, int line, const char *function, QString message)
Format and send a log message into the queue.
Definition: logging.cpp:538
LoggingItem::setFacility
void setFacility(const int val)
Definition: logging.h:109
LoggerThread::stop
void stop(void)
Stop the thread by setting the abort flag after waiting a second for the queue to be flushed.
Definition: logging.cpp:474
LoggerThread::m_quiet
bool m_quiet
silence the console (console only)
Definition: logging.h:176
LoggerThread::LogPrintLine
friend MBASE_PUBLIC void LogPrintLine(uint64_t mask, LogLevel_t level, const char *file, int line, const char *function, QString message)
Format and send a log message into the queue.
Definition: logging.cpp:538
LoggingItem::appName
QString appName
Definition: logging.h:69
LoggerThread::m_facility
int m_facility
Cached syslog facility (or -1 to disable)
Definition: logging.h:180
LoggingItem::tid
qlonglong tid() const
Definition: logging.h:88
referencecounter.h
LoggingItem::threadId
qulonglong threadId() const
Definition: logging.h:89
LoggingItem::file
QString file
Definition: logging.h:66
LoggingItem::m_epoch
std::chrono::microseconds m_epoch
Definition: logging.h:130
LoggingType
LoggingType
Definition: logging.h:39
LoggingItem::toStringShort
std::string toStringShort()
short console format
Definition: logging.cpp:235
LoggingItem::appName
QString appName() const
Definition: logging.h:99
LoggingItem::line
int line
Definition: logging.h:61
LoggerThread::LoggerThread
LoggerThread(QString filename, bool progress, bool quiet, int facility, bool loglong)
LoggerThread constructor.
Definition: logging.cpp:247
LoggingItem::m_threadName
QString m_threadName
Definition: logging.h:133
LoggingItem::create
static LoggingItem * create(const char *_file, const char *_function, int _line, LogLevel_t _level, LoggingType _type)
Create a new LoggingItem.
Definition: logging.cpp:520
LoggingItem::m_file
QString m_file
Definition: logging.h:131
LoggingItem::type
int type
Definition: logging.h:62
LoggingItem::level
int level() const
Definition: logging.h:92
LoggingItem::setLevel
void setLevel(const int val)
Definition: logging.h:108
LoggingItem::getTimestampUs
QString getTimestampUs(const char *format="yyyy-MM-dd HH:mm:ss") const
Definition: logging.cpp:199
LoggingItem::m_threadId
qulonglong m_threadId
Definition: logging.h:125
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
mythchrono.h
mthread.h
LoggingItem::m_tid
qlonglong m_tid
Definition: logging.h:124
LoggingItem::setEpochInt
void setEpochInt(const qlonglong val)
Definition: logging.h:111
LoggingItem::setTid
void setTid(const qlonglong val)
Definition: logging.h:104
tmType
struct tm tmType
Definition: logging.h:50
LoggingItem::setAppName
void setAppName(const QString &val)
Definition: logging.h:115
loggingDeregisterThread
void loggingDeregisterThread(void)
Deregister the current thread's name.
Definition: logging.cpp:720
LoggingItem::setType
void setType(const int val)
Definition: logging.h:107
LoggingItem::function
QString function
Definition: logging.h:67
LoggerThread::m_pid
pid_t m_pid
Cached pid value.
Definition: logging.h:181
LoggingItem::epochInt
qlonglong epochInt() const
Definition: logging.h:95
LoggingItem::getThreadTid
int64_t getThreadTid(void)
Get the thread ID of the thread that produced the LoggingItem.
Definition: logging.cpp:154
LoggingItem::setFunction
void setFunction(const QString &val)
Definition: logging.h:113
LoggerThread::handleItem
static void handleItem(LoggingItem *item)
Handles each LoggingItem.
Definition: logging.cpp:342
LoggingItem::threadId
qulonglong threadId
Definition: logging.h:60
LoggerThread::m_loglong
bool m_loglong
use long log format (console only)
Definition: logging.h:177
LoggingItem::facility
int facility
Definition: logging.h:64
LoggingItem::LoggingItem
LoggingItem()
Definition: logging.h:139
build_compdb.filename
filename
Definition: build_compdb.py:21
LoggerThread::~LoggerThread
~LoggerThread() override
LoggerThread destructor. Triggers the deletion of all loggers.
Definition: logging.cpp:272
LoggingItem::setPid
void setPid(const int val)
Definition: logging.h:103
LoggingItem::setLogFile
void setLogFile(const QString &val)
Definition: logging.h:116
ReferenceCounter
General purpose reference counter.
Definition: referencecounter.h:26
kInitializing
@ kInitializing
Definition: logging.h:45
LoggingItem::setThreadId
void setThreadId(const qulonglong val)
Definition: logging.h:105