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 #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 
38 class QWaitCondition;
39 
41  kMessage = 0x01,
42  kRegistering = 0x02,
44  kFlush = 0x08,
45  kStandardIO = 0x10,
46  kInitializing = 0x20,
47 };
48 
49 class LoggerThread;
50 
51 using tmType = struct tm;
52 
55 class LoggingItem: public QObject, public ReferenceCounter
56 {
57  Q_OBJECT
58 
59  Q_PROPERTY(int pid READ pid WRITE setPid)
60  Q_PROPERTY(qlonglong tid READ tid WRITE setTid)
61  Q_PROPERTY(qulonglong threadId READ threadId WRITE setThreadId)
62  Q_PROPERTY(int line READ line WRITE setLine)
63  Q_PROPERTY(int type READ type WRITE setType)
64  Q_PROPERTY(int level READ level WRITE setLevel)
65  Q_PROPERTY(int facility READ facility WRITE setFacility)
66  Q_PROPERTY(qlonglong epoch READ epochInt WRITE setEpochInt)
67  Q_PROPERTY(QString file READ file WRITE setFile)
68  Q_PROPERTY(QString function READ function WRITE setFunction)
69  Q_PROPERTY(QString threadName READ threadName WRITE setThreadName)
70  Q_PROPERTY(QString appName READ appName WRITE setAppName)
71  Q_PROPERTY(QString table READ table WRITE setTable)
72  Q_PROPERTY(QString logFile READ logFile WRITE setLogFile)
73  Q_PROPERTY(QString message READ message WRITE setMessage)
74 
75  friend class LoggerThread;
76  friend void LogPrintLine(uint64_t mask, LogLevel_t level, const char *file, int line,
77  const char *function, QString message);
78 
79  public:
80  QString getThreadName(void);
81  int64_t getThreadTid(void);
82  void setThreadTid(void);
83  static LoggingItem *create(const char *_file, const char *_function, int _line, LogLevel_t _level,
84  LoggingType _type);
85  static LoggingItem *create(QByteArray &buf);
86  QByteArray toByteArray(void);
87  QString getTimestamp(const char *format = "yyyy-MM-dd HH:mm:ss") const;
88  QString getTimestampUs(const char *format = "yyyy-MM-dd HH:mm:ss") const;
89  char getLevelChar(void);
90 
91  int pid() const { return m_pid; };
92  qlonglong tid() const { return m_tid; };
93  qulonglong threadId() const { return m_threadId; };
94  int line() const { return m_line; };
95  int type() const { return (int)m_type; };
96  int level() const { return (int)m_level; };
97  int facility() const { return m_facility; };
98  std::chrono::microseconds epoch() const { return m_epoch; };
99  qlonglong epochInt() const { return m_epoch.count(); };
100  QString file() const { return m_file; };
101  QString function() const { return m_function; };
102  QString threadName() const { return m_threadName; };
103  QString appName() const { return m_appName; };
104  QString table() const { return m_table; };
105  QString logFile() const { return m_logFile; };
106  QString message() const { return m_message; };
107 
108  void setPid(const int val) { m_pid = val; };
109  void setTid(const qlonglong val) { m_tid = val; };
110  void setThreadId(const qulonglong val) { m_threadId = val; };
111  void setLine(const int val) { m_line = val; };
112  void setType(const int val) { m_type = (LoggingType)val; };
113  void setLevel(const int val) { m_level = (LogLevel_t)val; };
114  void setFacility(const int val) { m_facility = val; };
115  void setEpoch(std::chrono::microseconds val) { m_epoch = val; };
116  void setEpochInt(const qlonglong val) { setEpoch(std::chrono::microseconds(val)); };
117  void setFile(const QString &val) { m_file = val; };
118  void setFunction(const QString &val) { m_function = val; };
119  void setThreadName(const QString &val) { m_threadName = val; };
120  void setAppName(const QString &val) { m_appName = val; };
121  void setTable(const QString &val) { m_table = val; };
122  void setLogFile(const QString &val) { m_logFile = val; };
123  void setMessage(const QString &val) { m_message = val; };
124 
125  protected:
126  int m_pid {-1};
127  qlonglong m_tid {-1};
128  qulonglong m_threadId {UINT64_MAX};
129  int m_line {0};
131  LogLevel_t m_level {LOG_INFO};
132  int m_facility {0};
133  std::chrono::microseconds m_epoch {0us};
134  QString m_file {};
135  QString m_function {};
136  QString m_threadName {};
137  QString m_appName {};
138  QString m_table {};
139  QString m_logFile {};
140  QString m_message {};
141 
142  private:
144  : ReferenceCounter("LoggingItem", false) {};
145  LoggingItem(const char *_file, const char *_function,
146  int _line, LogLevel_t _level, LoggingType _type);
148 };
149 
152 class LoggerThread : public QObject, public MThread
153 {
154  Q_OBJECT
155 
156  friend void LogPrintLine(uint64_t mask, LogLevel_t level, const char *file, int line,
157  const char *function, QString message);
158  public:
159  LoggerThread(QString filename, bool progress, bool quiet, QString table,
160  int facility);
161  ~LoggerThread() override;
162  void run(void) override; // MThread
163  void stop(void);
164  bool flush(int timeoutMS = 200000);
165  static void handleItem(LoggingItem *item);
166  void fillItem(LoggingItem *item);
167  private:
169  QWaitCondition *m_waitNotEmpty {nullptr};
173  QWaitCondition *m_waitEmpty {nullptr};
177  bool m_aborted {false};
178  QString m_filename;
180  bool m_progress;
181  bool m_quiet;
182  QString m_appname {QCoreApplication::applicationName()};
184  QString m_tablename;
186  pid_t m_pid;
187 
188  protected:
189  bool logConsole(LoggingItem *item) const;
190 };
191 
192 #endif
193 
194 /*
195  * vim:ts=4:sw=4:ai:et:si:sts=4
196  */
LoggingItem::m_facility
int m_facility
Definition: logging.h:132
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:124
LoggingItem::m_line
int m_line
Definition: logging.h:129
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:203
kInitializing
@ kInitializing
Definition: logging.h:46
LoggingItem::epoch
std::chrono::microseconds epoch() const
Definition: logging.h:98
LoggingItem::threadName
QString threadName
Definition: logging.h:69
LoggerThread::m_tablename
QString m_tablename
Cached table name for db logging.
Definition: logging.h:184
false
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:85
LoggingItem::threadName
QString threadName() const
Definition: logging.h:102
LoggerThread::m_waitNotEmpty
QWaitCondition * m_waitNotEmpty
Condition variable for waiting for the queue to not be empty Protected by logQueueMutex.
Definition: logging.h:169
LoggingItem::logFile
QString logFile
Definition: logging.h:72
LoggerThread::m_appname
QString m_appname
Cached application name.
Definition: logging.h:182
mythbaseexp.h
LoggingItem::getThreadName
QString getThreadName(void)
Get the name of the thread that produced the LoggingItem.
Definition: logging.cpp:148
LoggingItem::m_logFile
QString m_logFile
Definition: logging.h:139
LoggingItem::m_type
LoggingType m_type
Definition: logging.h:130
LoggingItem::m_pid
int m_pid
Definition: logging.h:126
progress
bool progress
Definition: mythtv/programs/mythcommflag/main.cpp:71
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:563
LoggerThread
The logging thread that consumes the logging queue and dispatches each LoggingItem.
Definition: logging.h:152
LoggerThread::m_waitEmpty
QWaitCondition * m_waitEmpty
Condition variable for waiting for the queue to be empty Protected by logQueueMutex.
Definition: logging.h:173
kDeregistering
@ kDeregistering
Definition: logging.h:43
LoggingItem::epoch
qlonglong epoch
Definition: logging.h:66
LoggerThread::LoggerThread
LoggerThread(QString filename, bool progress, bool quiet, QString table, int facility)
LoggerThread constructor.
Definition: logging.cpp:230
verbosedefs.h
LoggingItem::table
QString table
Definition: logging.h:71
build_compdb.file
file
Definition: build_compdb.py:55
LoggingItem::tid
qlonglong tid
Definition: logging.h:60
LoggerThread::Q_DISABLE_COPY
Q_DISABLE_COPY(LoggerThread)
LoggingItem::facility
int facility() const
Definition: logging.h:97
LoggingType
LoggingType
Definition: logging.h:40
LoggingItem::line
int line() const
Definition: logging.h:94
LoggingItem::m_function
QString m_function
Definition: logging.h:135
loggingRegisterThread
void loggingRegisterThread(const QString &name)
Register the current thread with the given name.
Definition: logging.cpp:729
LoggingItem::level
int level
Definition: logging.h:64
LoggingItem::message
QString message() const
Definition: logging.h:106
LoggingItem::logFile
QString logFile() const
Definition: logging.h:105
LoggingItem::setFile
void setFile(const QString &val)
Definition: logging.h:117
LoggingItem::setThreadName
void setThreadName(const QString &val)
Definition: logging.h:119
LoggingItem
The logging items that are generated by LOG() and are sent to the console.
Definition: logging.h:55
LoggerThread::m_progress
bool m_progress
show only LOG_ERR and more important (console only)
Definition: logging.h:180
LoggingItem::setMessage
void setMessage(const QString &val)
Definition: logging.h:123
LoggerThread::fillItem
void fillItem(LoggingItem *item)
Definition: logging.cpp:512
LoggingItem::file
QString file() const
Definition: logging.h:100
LoggingItem::getLevelChar
char getLevelChar(void)
Get the message log level as a single character.
Definition: logging.cpp:218
kFlush
@ kFlush
Definition: logging.h:44
LoggingItem::m_level
LogLevel_t m_level
Definition: logging.h:131
LoggingItem::setEpoch
void setEpoch(std::chrono::microseconds val)
Definition: logging.h:115
LoggingItem::pid
int pid
Definition: logging.h:59
compat.h
LoggerThread::m_aborted
bool m_aborted
Flag to abort the thread.
Definition: logging.h:177
LoggingItem::setLine
void setLine(const int val)
Definition: logging.h:111
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:177
LoggerThread::m_filename
QString m_filename
Filename of debug logfile.
Definition: logging.h:179
LoggerThread::run
void run(void) override
Run the logging thread.
Definition: logging.cpp:268
LoggingItem::m_message
QString m_message
Definition: logging.h:140
kMessage
@ kMessage
Definition: logging.h:41
LoggerThread::logConsole
bool logConsole(LoggingItem *item) const
Process a log message, writing to the console.
Definition: logging.cpp:386
LoggingItem::message
QString message
Definition: logging.h:73
LoggingItem::m_appName
QString m_appName
Definition: logging.h:137
LoggerThread::flush
bool flush(int timeoutMS=200000)
Wait for the queue to be flushed (up to a timeout)
Definition: logging.cpp:498
LoggingItem::type
int type() const
Definition: logging.h:95
LoggingItem::setFacility
void setFacility(const int val)
Definition: logging.h:114
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:486
quiet
int quiet
Definition: mythtv/programs/mythcommflag/main.cpp:70
LoggingItem::setTable
void setTable(const QString &val)
Definition: logging.h:121
LoggerThread::m_quiet
bool m_quiet
silence the console (console only)
Definition: logging.h:181
LoggingItem::appName
QString appName
Definition: logging.h:70
LoggerThread::m_facility
int m_facility
Cached syslog facility (or -1 to disable)
Definition: logging.h:185
LoggingItem::tid
qlonglong tid() const
Definition: logging.h:92
LoggingItem::table
QString table() const
Definition: logging.h:104
referencecounter.h
LoggingItem::threadId
qulonglong threadId() const
Definition: logging.h:93
LoggingItem::file
QString file
Definition: logging.h:67
LoggingItem::m_epoch
std::chrono::microseconds m_epoch
Definition: logging.h:133
LoggingItem::m_table
QString m_table
Definition: logging.h:138
LoggingItem::appName
QString appName() const
Definition: logging.h:103
LoggingItem::line
int line
Definition: logging.h:62
LoggingItem::m_threadName
QString m_threadName
Definition: logging.h:136
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:533
LoggingItem::m_file
QString m_file
Definition: logging.h:134
LoggingItem::type
int type
Definition: logging.h:63
LoggingItem::level
int level() const
Definition: logging.h:96
LoggingItem::setLevel
void setLevel(const int val)
Definition: logging.h:113
LoggingItem::getTimestampUs
QString getTimestampUs(const char *format="yyyy-MM-dd HH:mm:ss") const
Definition: logging.cpp:210
LoggingItem::m_threadId
qulonglong m_threadId
Definition: logging.h:128
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:127
LoggingItem::setEpochInt
void setEpochInt(const qlonglong val)
Definition: logging.h:116
LoggingItem::setTid
void setTid(const qlonglong val)
Definition: logging.h:109
kRegistering
@ kRegistering
Definition: logging.h:42
tmType
struct tm tmType
Definition: logging.h:51
LoggingItem::setAppName
void setAppName(const QString &val)
Definition: logging.h:120
loggingDeregisterThread
void loggingDeregisterThread(void)
Deregister the current thread's name.
Definition: logging.cpp:748
LoggingItem::setType
void setType(const int val)
Definition: logging.h:112
LoggingItem::function
QString function
Definition: logging.h:68
LoggerThread::m_pid
pid_t m_pid
Cached pid value.
Definition: logging.h:186
LoggingItem::epochInt
qlonglong epochInt() const
Definition: logging.h:99
LoggingItem::getThreadTid
int64_t getThreadTid(void)
Get the thread ID of the thread that produced the LoggingItem.
Definition: logging.cpp:164
LoggingItem::toByteArray
QByteArray toByteArray(void)
Definition: logging.cpp:136
LoggingItem::setFunction
void setFunction(const QString &val)
Definition: logging.h:118
LoggerThread::handleItem
static void handleItem(LoggingItem *item)
Handles each LoggingItem.
Definition: logging.cpp:324
LoggingItem::threadId
qulonglong threadId
Definition: logging.h:61
LoggingItem::facility
int facility
Definition: logging.h:65
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:563
LoggingItem::LoggingItem
LoggingItem()
Definition: logging.h:143
kStandardIO
@ kStandardIO
Definition: logging.h:45
build_compdb.filename
filename
Definition: build_compdb.py:21
LoggerThread::~LoggerThread
~LoggerThread() override
LoggerThread destructor. Triggers the deletion of all loggers.
Definition: logging.cpp:254
LoggingItem::setPid
void setPid(const int val)
Definition: logging.h:108
LoggingItem::setLogFile
void setLogFile(const QString &val)
Definition: logging.h:122
ReferenceCounter
General purpose reference counter.
Definition: referencecounter.h:26
LoggingItem::setThreadId
void setThreadId(const qulonglong val)
Definition: logging.h:110