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 "mythsignalingtimer.h"
17 #include "mthread.h"
18 #include "referencecounter.h"
19 #include "compat.h"
20 
21 #ifdef _MSC_VER
22 # include <unistd.h> // pid_t
23 #endif
24 
25 #if CONFIG_SYSTEMD_JOURNAL
26 #define SYSTEMD_JOURNAL_FACILITY (-99)
27 #endif
28 
29 #define LOGLINE_MAX (2048-120)
30 
31 class QString;
32 class MSqlQuery;
33 class LoggingItem;
34 
35 void loggingRegisterThread(const QString &name);
36 void loggingDeregisterThread(void);
37 void loggingGetTimeStamp(qlonglong *epoch, uint *usec);
38 
39 class QWaitCondition;
40 
42  kMessage = 0x01,
43  kRegistering = 0x02,
45  kFlush = 0x08,
46  kStandardIO = 0x10,
47  kInitializing = 0x20,
48 };
49 
50 class LoggerThread;
51 
52 using tmType = struct tm;
53 
56 class LoggingItem: public QObject, public ReferenceCounter
57 {
58  Q_OBJECT
59 
60  Q_PROPERTY(int pid READ pid WRITE setPid)
61  Q_PROPERTY(qlonglong tid READ tid WRITE setTid)
62  Q_PROPERTY(qulonglong threadId READ threadId WRITE setThreadId)
63  Q_PROPERTY(uint usec READ usec WRITE setUsec)
64  Q_PROPERTY(int line READ line WRITE setLine)
65  Q_PROPERTY(int type READ type WRITE setType)
66  Q_PROPERTY(int level READ level WRITE setLevel)
67  Q_PROPERTY(int facility READ facility WRITE setFacility)
68  Q_PROPERTY(qlonglong epoch READ epoch WRITE setEpoch)
69  Q_PROPERTY(QString file READ file WRITE setFile)
70  Q_PROPERTY(QString function READ function WRITE setFunction)
71  Q_PROPERTY(QString threadName READ threadName WRITE setThreadName)
72  Q_PROPERTY(QString appName READ appName WRITE setAppName)
73  Q_PROPERTY(QString table READ table WRITE setTable)
74  Q_PROPERTY(QString logFile READ logFile WRITE setLogFile)
75  Q_PROPERTY(QString message READ message WRITE setMessage)
76 
77  friend class LoggerThread;
78  friend void LogPrintLine(uint64_t mask, LogLevel_t level, const char *file, int line,
79  const char *function, QString message);
80 
81  public:
82  QString getThreadName(void);
83  int64_t getThreadTid(void);
84  void setThreadTid(void);
85  static LoggingItem *create(const char *_file, const char *_function, int _line, LogLevel_t _level,
86  LoggingType _type);
87  static LoggingItem *create(QByteArray &buf);
88  QByteArray toByteArray(void);
89  QString getTimestamp(void) const;
90  QString getTimestampUs(void) const;
91  char getLevelChar(void);
92 
93  int pid() const { return m_pid; };
94  qlonglong tid() const { return m_tid; };
95  qulonglong threadId() const { return m_threadId; };
96  uint usec() const { return m_usec; };
97  int line() const { return m_line; };
98  int type() const { return (int)m_type; };
99  int level() const { return (int)m_level; };
100  int facility() const { return m_facility; };
101  qlonglong epoch() const { return m_epoch; };
102  QString file() const { return m_file; };
103  QString function() const { return m_function; };
104  QString threadName() const { return m_threadName; };
105  QString appName() const { return m_appName; };
106  QString table() const { return m_table; };
107  QString logFile() const { return m_logFile; };
108  QString message() const { return m_message; };
109 
110  void setPid(const int val) { m_pid = val; };
111  void setTid(const qlonglong val) { m_tid = val; };
112  void setThreadId(const qulonglong val) { m_threadId = val; };
113  void setUsec(const uint val) { m_usec = val; };
114  void setLine(const int val) { m_line = val; };
115  void setType(const int val) { m_type = (LoggingType)val; };
116  void setLevel(const int val) { m_level = (LogLevel_t)val; };
117  void setFacility(const int val) { m_facility = val; };
118  void setEpoch(const qlonglong val) { m_epoch = val; };
119  void setFile(const QString &val) { m_file = val; };
120  void setFunction(const QString &val) { m_function = val; };
121  void setThreadName(const QString &val) { m_threadName = val; };
122  void setAppName(const QString &val) { m_appName = val; };
123  void setTable(const QString &val) { m_table = val; };
124  void setLogFile(const QString &val) { m_logFile = val; };
125  void setMessage(const QString &val) { m_message = val; };
126 
127  protected:
128  int m_pid {-1};
129  qlonglong m_tid {-1};
130  qulonglong m_threadId {UINT64_MAX};
132  int m_line {0};
134  LogLevel_t m_level {LOG_INFO};
135  int m_facility {0};
136  qlonglong m_epoch {0};
137  QString m_file {};
138  QString m_function {};
139  QString m_threadName {};
140  QString m_appName {};
141  QString m_table {};
142  QString m_logFile {};
143  QString m_message {};
144 
145  private:
147  : ReferenceCounter("LoggingItem", false) {};
148  LoggingItem(const char *_file, const char *_function,
149  int _line, LogLevel_t _level, LoggingType _type);
151 };
152 
155 class LoggerThread : public QObject, public MThread
156 {
157  Q_OBJECT
158 
159  friend void LogPrintLine(uint64_t mask, LogLevel_t level, const char *file, int line,
160  const char *function, QString message);
161  public:
162  LoggerThread(QString filename, bool progress, bool quiet, QString table,
163  int facility);
164  ~LoggerThread() override;
165  void run(void) override; // MThread
166  void stop(void);
167  bool flush(int timeoutMS = 200000);
168  static void handleItem(LoggingItem *item);
169  void fillItem(LoggingItem *item);
170  private:
172  QWaitCondition *m_waitNotEmpty {nullptr};
176  QWaitCondition *m_waitEmpty {nullptr};
180  bool m_aborted {false};
181  QString m_filename;
183  bool m_progress;
184  bool m_quiet;
185  QString m_appname {QCoreApplication::applicationName()};
187  QString m_tablename;
189  pid_t m_pid;
190 
191  protected:
192  bool logConsole(LoggingItem *item) const;
193 };
194 
195 #endif
196 
197 /*
198  * vim:ts=4:sw=4:ai:et:si:sts=4
199  */
LoggingItem::m_facility
int m_facility
Definition: logging.h:135
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:126
LoggingItem::m_line
int m_line
Definition: logging.h:132
kInitializing
@ kInitializing
Definition: logging.h:47
LoggingItem::threadName
QString threadName
Definition: logging.h:71
LoggerThread::m_tablename
QString m_tablename
Cached table name for db logging.
Definition: logging.h:187
LoggingItem::setEpoch
void setEpoch(const qlonglong val)
Definition: logging.h:118
false
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:85
LoggingItem::threadName
QString threadName() const
Definition: logging.h:104
LoggerThread::m_waitNotEmpty
QWaitCondition * m_waitNotEmpty
Condition variable for waiting for the queue to not be empty Protected by logQueueMutex.
Definition: logging.h:172
LoggingItem::logFile
QString logFile
Definition: logging.h:74
LoggerThread::m_appname
QString m_appname
Cached application name.
Definition: logging.h:185
mythbaseexp.h
LoggingItem::getThreadName
QString getThreadName(void)
Get the name of the thread that produced the LoggingItem.
Definition: logging.cpp:170
LoggingItem::m_epoch
qlonglong m_epoch
Definition: logging.h:136
LoggingItem::m_logFile
QString m_logFile
Definition: logging.h:142
LoggingItem::m_type
LoggingType m_type
Definition: logging.h:133
LoggingItem::m_pid
int m_pid
Definition: logging.h:128
progress
bool progress
Definition: mythtv/programs/mythcommflag/main.cpp:73
LoggerThread::LogPrintLine
friend 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:586
LoggerThread
The logging thread that consumes the logging queue and dispatches each LoggingItem.
Definition: logging.h:156
LoggerThread::m_waitEmpty
QWaitCondition * m_waitEmpty
Condition variable for waiting for the queue to be empty Protected by logQueueMutex.
Definition: logging.h:176
kDeregistering
@ kDeregistering
Definition: logging.h:44
LoggingItem::epoch
qlonglong epoch
Definition: logging.h:68
LoggerThread::LoggerThread
LoggerThread(QString filename, bool progress, bool quiet, QString table, int facility)
LoggerThread constructor.
Definition: logging.cpp:252
verbosedefs.h
LoggingItem::table
QString table
Definition: logging.h:73
build_compdb.file
file
Definition: build_compdb.py:55
LoggingItem::tid
qlonglong tid
Definition: logging.h:61
LoggerThread::Q_DISABLE_COPY
Q_DISABLE_COPY(LoggerThread)
LoggingItem::facility
int facility() const
Definition: logging.h:100
LoggingType
LoggingType
Definition: logging.h:41
LoggingItem::line
int line() const
Definition: logging.h:97
LoggingItem::m_function
QString m_function
Definition: logging.h:138
loggingRegisterThread
void loggingRegisterThread(const QString &name)
Register the current thread with the given name.
Definition: logging.cpp:752
LoggingItem::level
int level
Definition: logging.h:66
LoggingItem::message
QString message() const
Definition: logging.h:108
LoggingItem::usec
uint usec() const
Definition: logging.h:96
LoggingItem::logFile
QString logFile() const
Definition: logging.h:107
LoggingItem::setFile
void setFile(const QString &val)
Definition: logging.h:119
LoggingItem::setThreadName
void setThreadName(const QString &val)
Definition: logging.h:121
LoggingItem
The logging items that are generated by LOG() and are sent to the console.
Definition: logging.h:57
LoggerThread::m_progress
bool m_progress
show only LOG_ERR and more important (console only)
Definition: logging.h:183
LoggingItem::setMessage
void setMessage(const QString &val)
Definition: logging.h:125
LoggerThread::fillItem
void fillItem(LoggingItem *item)
Definition: logging.cpp:535
LoggingItem::file
QString file() const
Definition: logging.h:102
LoggingItem::getLevelChar
char getLevelChar(void)
Get the message log level as a single character.
Definition: logging.cpp:240
kFlush
@ kFlush
Definition: logging.h:45
LoggingItem::m_level
LogLevel_t m_level
Definition: logging.h:134
LoggingItem::pid
int pid
Definition: logging.h:60
compat.h
LoggerThread::m_aborted
bool m_aborted
Flag to abort the thread.
Definition: logging.h:180
LoggingItem::setUsec
void setUsec(const uint val)
Definition: logging.h:113
LoggingItem::setLine
void setLine(const int val)
Definition: logging.h:114
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:199
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:637
LoggerThread::m_filename
QString m_filename
Filename of debug logfile.
Definition: logging.h:182
LoggerThread::run
void run(void) override
Run the logging thread.
Definition: logging.cpp:290
LoggingItem::m_message
QString m_message
Definition: logging.h:143
kMessage
@ kMessage
Definition: logging.h:42
LoggerThread::logConsole
bool logConsole(LoggingItem *item) const
Process a log message, writing to the console.
Definition: logging.cpp:409
LoggingItem::message
QString message
Definition: logging.h:75
LoggingItem::getTimestamp
QString getTimestamp(void) const
Convert numerical timestamp to a readable date and time.
Definition: logging.cpp:225
LoggingItem::m_appName
QString m_appName
Definition: logging.h:140
LoggerThread::flush
bool flush(int timeoutMS=200000)
Wait for the queue to be flushed (up to a timeout)
Definition: logging.cpp:521
LoggingItem::type
int type() const
Definition: logging.h:98
LoggingItem::setFacility
void setFacility(const int val)
Definition: logging.h:117
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:509
quiet
int quiet
Definition: mythtv/programs/mythcommflag/main.cpp:72
LoggingItem::setTable
void setTable(const QString &val)
Definition: logging.h:123
LoggerThread::m_quiet
bool m_quiet
silence the console (console only)
Definition: logging.h:184
LoggingItem::usec
uint usec
Definition: logging.h:63
uint
unsigned int uint
Definition: compat.h:141
LoggingItem::m_usec
uint m_usec
Definition: logging.h:131
LoggingItem::appName
QString appName
Definition: logging.h:72
LoggerThread::m_facility
int m_facility
Cached syslog facility (or -1 to disable)
Definition: logging.h:188
LoggingItem::tid
qlonglong tid() const
Definition: logging.h:94
LoggingItem::table
QString table() const
Definition: logging.h:106
referencecounter.h
LoggingItem::threadId
qulonglong threadId() const
Definition: logging.h:95
LoggingItem::file
QString file
Definition: logging.h:69
LoggingItem::m_table
QString m_table
Definition: logging.h:141
LoggingItem::appName
QString appName() const
Definition: logging.h:105
LoggingItem::line
int line
Definition: logging.h:64
LoggingItem::getTimestampUs
QString getTimestampUs(void) const
Definition: logging.cpp:232
LoggingItem::m_threadName
QString m_threadName
Definition: logging.h:139
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:556
LoggingItem::m_file
QString m_file
Definition: logging.h:137
LoggingItem::type
int type
Definition: logging.h:65
LoggingItem::level
int level() const
Definition: logging.h:99
LoggingItem::setLevel
void setLevel(const int val)
Definition: logging.h:116
LoggingItem::m_threadId
qulonglong m_threadId
Definition: logging.h:130
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:49
mthread.h
LoggingItem::m_tid
qlonglong m_tid
Definition: logging.h:129
LoggingItem::setTid
void setTid(const qlonglong val)
Definition: logging.h:111
kRegistering
@ kRegistering
Definition: logging.h:43
tmType
struct tm tmType
Definition: logging.h:52
LoggingItem::setAppName
void setAppName(const QString &val)
Definition: logging.h:122
mythsignalingtimer.h
loggingDeregisterThread
void loggingDeregisterThread(void)
Deregister the current thread's name.
Definition: logging.cpp:771
LoggingItem::setType
void setType(const int val)
Definition: logging.h:115
LoggingItem::function
QString function
Definition: logging.h:70
LoggerThread::m_pid
pid_t m_pid
Cached pid value.
Definition: logging.h:189
LoggingItem::getThreadTid
int64_t getThreadTid(void)
Get the thread ID of the thread that produced the LoggingItem.
Definition: logging.cpp:186
LoggingItem::toByteArray
QByteArray toByteArray(void)
Definition: logging.cpp:158
LoggingItem::setFunction
void setFunction(const QString &val)
Definition: logging.h:120
LoggingItem::epoch
qlonglong epoch() const
Definition: logging.h:101
LoggerThread::handleItem
static void handleItem(LoggingItem *item)
Handles each LoggingItem.
Definition: logging.cpp:347
LoggingItem::threadId
qulonglong threadId
Definition: logging.h:62
LoggingItem::facility
int facility
Definition: logging.h:67
LoggingItem::LogPrintLine
friend 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:586
LoggingItem::LoggingItem
LoggingItem()
Definition: logging.h:146
kStandardIO
@ kStandardIO
Definition: logging.h:46
LoggerThread::~LoggerThread
~LoggerThread() override
LoggerThread destructor. Triggers the deletion of all loggers.
Definition: logging.cpp:276
LoggingItem::setPid
void setPid(const int val)
Definition: logging.h:110
LoggingItem::setLogFile
void setLogFile(const QString &val)
Definition: logging.h:124
ReferenceCounter
General purpose reference counter.
Definition: referencecounter.h:27
loggingGetTimeStamp
void loggingGetTimeStamp(qlonglong *epoch, uint *usec)
Definition: logging.cpp:127
LoggingItem::setThreadId
void setThreadId(const qulonglong val)
Definition: logging.h:112