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 
13 #include "mythconfig.h"
14 #include "mythbaseexp.h" // MBASE_PUBLIC , etc.
15 #include "verbosedefs.h"
16 #include "mthread.h"
17 #include "referencecounter.h"
18 #include "compat.h"
19 #include "mythchrono.h"
20 
21 #ifdef _MSC_VER
22 # include <unistd.h> // pid_t
23 #endif
24 
25 #if CONFIG_SYSTEMD_JOURNAL
26 static constexpr int SYSTEMD_JOURNAL_FACILITY { -99 };
27 #endif
28 
29 class QString;
30 class MSqlQuery;
31 class LoggingItem;
32 
33 void loggingRegisterThread(const QString &name);
34 void loggingDeregisterThread(void);
35 
36 class QWaitCondition;
37 
39  kMessage = 0x01,
40  kRegistering = 0x02,
42  kFlush = 0x08,
43  kStandardIO = 0x10,
44  kInitializing = 0x20,
45 };
46 
47 class LoggerThread;
48 
49 using tmType = struct tm;
50 
53 class LoggingItem: public QObject, public ReferenceCounter
54 {
55  Q_OBJECT
56 
57  Q_PROPERTY(int pid READ pid WRITE setPid)
58  Q_PROPERTY(qlonglong tid READ tid WRITE setTid)
59  Q_PROPERTY(qulonglong threadId READ threadId WRITE setThreadId)
60  Q_PROPERTY(int line READ line WRITE setLine)
61  Q_PROPERTY(int type READ type WRITE setType)
62  Q_PROPERTY(int level READ level WRITE setLevel)
63  Q_PROPERTY(int facility READ facility WRITE setFacility)
64  Q_PROPERTY(qlonglong epoch READ epochInt WRITE setEpochInt)
65  Q_PROPERTY(QString file READ file WRITE setFile)
66  Q_PROPERTY(QString function READ function WRITE setFunction)
67  Q_PROPERTY(QString threadName READ threadName WRITE setThreadName)
68  Q_PROPERTY(QString appName READ appName WRITE setAppName)
69  Q_PROPERTY(QString logFile READ logFile WRITE setLogFile)
70  Q_PROPERTY(QString message READ message WRITE setMessage)
71 
72  friend class LoggerThread;
73  friend MBASE_PUBLIC void LogPrintLine(uint64_t mask, LogLevel_t level, const char *file, int line,
74  const char *function, QString message);
75 
76  public:
77  QString getThreadName(void);
78  int64_t getThreadTid(void);
79  void setThreadTid(void);
80  static LoggingItem *create(const char *_file, const char *_function, int _line, LogLevel_t _level,
81  LoggingType _type);
82  QString getTimestamp(const char *format = "yyyy-MM-dd HH:mm:ss") const;
83  QString getTimestampUs(const char *format = "yyyy-MM-dd HH:mm:ss") const;
84  char getLevelChar(void);
85 
86  int pid() const { return m_pid; };
87  qlonglong tid() const { return m_tid; };
88  qulonglong threadId() const { return m_threadId; };
89  int line() const { return m_line; };
90  int type() const { return (int)m_type; };
91  int level() const { return (int)m_level; };
92  int facility() const { return m_facility; };
93  std::chrono::microseconds epoch() const { return m_epoch; };
94  qlonglong epochInt() const { return m_epoch.count(); };
95  QString file() const { return m_file; };
96  QString function() const { return m_function; };
97  QString threadName() const { return m_threadName; };
98  QString appName() const { return m_appName; };
99  QString logFile() const { return m_logFile; };
100  QString message() const { return m_message; };
101 
102  void setPid(const int val) { m_pid = val; };
103  void setTid(const qlonglong val) { m_tid = val; };
104  void setThreadId(const qulonglong val) { m_threadId = val; };
105  void setLine(const int val) { m_line = val; };
106  void setType(const int val) { m_type = (LoggingType)val; };
107  void setLevel(const int val) { m_level = (LogLevel_t)val; };
108  void setFacility(const int val) { m_facility = val; };
109  void setEpoch(std::chrono::microseconds val) { m_epoch = val; };
110  void setEpochInt(const qlonglong val) { setEpoch(std::chrono::microseconds(val)); };
111  void setFile(const QString &val) { m_file = val; };
112  void setFunction(const QString &val) { m_function = val; };
113  void setThreadName(const QString &val) { m_threadName = val; };
114  void setAppName(const QString &val) { m_appName = val; };
115  void setLogFile(const QString &val) { m_logFile = val; };
116  void setMessage(const QString &val) { m_message = val; };
117 
118  protected:
119  int m_pid {-1};
120  qlonglong m_tid {-1};
121  qulonglong m_threadId {UINT64_MAX};
122  int m_line {0};
124  LogLevel_t m_level {LOG_INFO};
125  int m_facility {0};
126  std::chrono::microseconds m_epoch {0us};
127  QString m_file;
128  QString m_function;
129  QString m_threadName;
130  QString m_appName;
131  QString m_logFile;
132  QString m_message;
133 
134  private:
136  : ReferenceCounter("LoggingItem", false) {};
137  LoggingItem(const char *_file, const char *_function,
138  int _line, LogLevel_t _level, LoggingType _type);
140 };
141 
144 class LoggerThread : public QObject, public MThread
145 {
146  Q_OBJECT
147 
148  friend MBASE_PUBLIC void LogPrintLine(uint64_t mask, LogLevel_t level, const char *file, int line,
149  const char *function, QString message);
150  public:
151  LoggerThread(QString filename, bool progress, bool quiet, int facility);
152  ~LoggerThread() override;
153  void run(void) override; // MThread
154  void stop(void);
155  bool flush(int timeoutMS = 200000);
156  static void handleItem(LoggingItem *item);
157  void fillItem(LoggingItem *item);
158  private:
160  QWaitCondition *m_waitNotEmpty {nullptr};
164  QWaitCondition *m_waitEmpty {nullptr};
168  bool m_aborted {false};
169  QString m_filename;
171  bool m_progress;
172  bool m_quiet;
173  QString m_appname {QCoreApplication::applicationName()};
176  pid_t m_pid;
177 
178  protected:
179  bool logConsole(LoggingItem *item) const;
180 };
181 
182 #endif
183 
184 /*
185  * vim:ts=4:sw=4:ai:et:si:sts=4
186  */
LoggingItem::m_facility
int m_facility
Definition: logging.h:125
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:122
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:191
kInitializing
@ kInitializing
Definition: logging.h:44
LoggingItem::epoch
std::chrono::microseconds epoch() const
Definition: logging.h:93
LoggingItem::threadName
QString threadName
Definition: logging.h:67
false
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:89
LoggingItem::threadName
QString threadName() const
Definition: logging.h:97
LoggerThread::m_waitNotEmpty
QWaitCondition * m_waitNotEmpty
Condition variable for waiting for the queue to not be empty Protected by logQueueMutex.
Definition: logging.h:160
LoggingItem::logFile
QString logFile
Definition: logging.h:69
progress
bool progress
Definition: mythcommflag.cpp:69
LoggerThread::m_appname
QString m_appname
Cached application name.
Definition: logging.h:173
mythbaseexp.h
LoggingItem::getThreadName
QString getThreadName(void)
Get the name of the thread that produced the LoggingItem.
Definition: logging.cpp:137
LoggingItem::m_logFile
QString m_logFile
Definition: logging.h:131
LoggingItem::m_type
LoggingType m_type
Definition: logging.h:123
LoggingItem::m_pid
int m_pid
Definition: logging.h:119
LoggerThread
The logging thread that consumes the logging queue and dispatches each LoggingItem.
Definition: logging.h:144
LoggerThread::m_waitEmpty
QWaitCondition * m_waitEmpty
Condition variable for waiting for the queue to be empty Protected by logQueueMutex.
Definition: logging.h:164
kDeregistering
@ kDeregistering
Definition: logging.h:41
LoggingItem::epoch
qlonglong epoch
Definition: logging.h:64
quiet
int quiet
Definition: mythcommflag.cpp:68
verbosedefs.h
build_compdb.file
file
Definition: build_compdb.py:55
LoggingItem::tid
qlonglong tid
Definition: logging.h:58
LoggerThread::Q_DISABLE_COPY
Q_DISABLE_COPY(LoggerThread)
LoggingItem::facility
int facility() const
Definition: logging.h:92
LoggingType
LoggingType
Definition: logging.h:38
LoggingItem::line
int line() const
Definition: logging.h:89
LoggingItem::m_function
QString m_function
Definition: logging.h:128
loggingRegisterThread
void loggingRegisterThread(const QString &name)
Register the current thread with the given name.
Definition: logging.cpp:684
LoggingItem::level
int level
Definition: logging.h:62
LoggingItem::message
QString message() const
Definition: logging.h:100
LoggingItem::logFile
QString logFile() const
Definition: logging.h:99
LoggingItem::setFile
void setFile(const QString &val)
Definition: logging.h:111
LoggingItem::setThreadName
void setThreadName(const QString &val)
Definition: logging.h:113
LoggingItem
The logging items that are generated by LOG() and are sent to the console.
Definition: logging.h:53
LoggerThread::m_progress
bool m_progress
show only LOG_ERR and more important (console only)
Definition: logging.h:171
LoggingItem::setMessage
void setMessage(const QString &val)
Definition: logging.h:116
LoggerThread::fillItem
void fillItem(LoggingItem *item)
Definition: logging.cpp:490
LoggingItem::file
QString file() const
Definition: logging.h:95
LoggingItem::getLevelChar
char getLevelChar(void)
Get the message log level as a single character.
Definition: logging.cpp:206
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
kFlush
@ kFlush
Definition: logging.h:42
LoggingItem::m_level
LogLevel_t m_level
Definition: logging.h:124
LoggingItem::setEpoch
void setEpoch(std::chrono::microseconds val)
Definition: logging.h:109
LoggingItem::pid
int pid
Definition: logging.h:57
compat.h
LoggerThread::m_aborted
bool m_aborted
Flag to abort the thread.
Definition: logging.h:168
LoggingItem::setLine
void setLine(const int val)
Definition: logging.h:105
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:166
LoggerThread::m_filename
QString m_filename
Filename of debug logfile.
Definition: logging.h:170
LoggerThread::run
void run(void) override
Run the logging thread.
Definition: logging.cpp:256
LoggingItem::m_message
QString m_message
Definition: logging.h:132
kMessage
@ kMessage
Definition: logging.h:39
LoggerThread::logConsole
bool logConsole(LoggingItem *item) const
Process a log message, writing to the console.
Definition: logging.cpp:364
LoggingItem::message
QString message
Definition: logging.h:70
LoggingItem::m_appName
QString m_appName
Definition: logging.h:130
LoggerThread::flush
bool flush(int timeoutMS=200000)
Wait for the queue to be flushed (up to a timeout)
Definition: logging.cpp:476
LoggingItem::type
int type() const
Definition: logging.h:90
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:528
LoggingItem::setFacility
void setFacility(const int val)
Definition: logging.h:108
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:464
LoggerThread::m_quiet
bool m_quiet
silence the console (console only)
Definition: logging.h:172
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:528
LoggingItem::appName
QString appName
Definition: logging.h:68
LoggerThread::m_facility
int m_facility
Cached syslog facility (or -1 to disable)
Definition: logging.h:175
LoggingItem::tid
qlonglong tid() const
Definition: logging.h:87
referencecounter.h
LoggerThread::LoggerThread
LoggerThread(QString filename, bool progress, bool quiet, int facility)
LoggerThread constructor.
Definition: logging.cpp:218
LoggingItem::threadId
qulonglong threadId() const
Definition: logging.h:88
LoggingItem::file
QString file
Definition: logging.h:65
LoggingItem::m_epoch
std::chrono::microseconds m_epoch
Definition: logging.h:126
LoggingItem::appName
QString appName() const
Definition: logging.h:98
LoggingItem::line
int line
Definition: logging.h:60
LoggingItem::m_threadName
QString m_threadName
Definition: logging.h:129
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:510
LoggingItem::m_file
QString m_file
Definition: logging.h:127
LoggingItem::type
int type
Definition: logging.h:61
LoggingItem::level
int level() const
Definition: logging.h:91
LoggingItem::setLevel
void setLevel(const int val)
Definition: logging.h:107
LoggingItem::getTimestampUs
QString getTimestampUs(const char *format="yyyy-MM-dd HH:mm:ss") const
Definition: logging.cpp:198
LoggingItem::m_threadId
qulonglong m_threadId
Definition: logging.h:121
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:120
LoggingItem::setEpochInt
void setEpochInt(const qlonglong val)
Definition: logging.h:110
LoggingItem::setTid
void setTid(const qlonglong val)
Definition: logging.h:103
kRegistering
@ kRegistering
Definition: logging.h:40
tmType
struct tm tmType
Definition: logging.h:49
LoggingItem::setAppName
void setAppName(const QString &val)
Definition: logging.h:114
loggingDeregisterThread
void loggingDeregisterThread(void)
Deregister the current thread's name.
Definition: logging.cpp:703
LoggingItem::setType
void setType(const int val)
Definition: logging.h:106
LoggingItem::function
QString function
Definition: logging.h:66
LoggerThread::m_pid
pid_t m_pid
Cached pid value.
Definition: logging.h:176
LoggingItem::epochInt
qlonglong epochInt() const
Definition: logging.h:94
LoggingItem::getThreadTid
int64_t getThreadTid(void)
Get the thread ID of the thread that produced the LoggingItem.
Definition: logging.cpp:153
LoggingItem::setFunction
void setFunction(const QString &val)
Definition: logging.h:112
LoggerThread::handleItem
static void handleItem(LoggingItem *item)
Handles each LoggingItem.
Definition: logging.cpp:312
LoggingItem::threadId
qulonglong threadId
Definition: logging.h:59
LoggingItem::facility
int facility
Definition: logging.h:63
LoggingItem::LoggingItem
LoggingItem()
Definition: logging.h:135
kStandardIO
@ kStandardIO
Definition: logging.h:43
build_compdb.filename
filename
Definition: build_compdb.py:21
LoggerThread::~LoggerThread
~LoggerThread() override
LoggerThread destructor. Triggers the deletion of all loggers.
Definition: logging.cpp:242
LoggingItem::setPid
void setPid(const int val)
Definition: logging.h:102
LoggingItem::setLogFile
void setLogFile(const QString &val)
Definition: logging.h:115
ReferenceCounter
General purpose reference counter.
Definition: referencecounter.h:26
LoggingItem::setThreadId
void setThreadId(const qulonglong val)
Definition: logging.h:104