MythTV  master
Go to the documentation of this file.
1 #ifndef LOGGING_H_
2 #define LOGGING_H_
4 #include <QMutexLocker>
5 #include <QMutex>
6 #include <QQueue>
7 #include <QPointer>
8 #include <QCoreApplication>
10 #include <cstdint>
11 #include <cstdlib>
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"
21 #ifdef _MSC_VER
22 # include <unistd.h> // pid_t
23 #endif
27 #endif
29 #define LOGLINE_MAX (2048-120)
31 class QString;
32 class MSqlQuery;
33 class LoggingItem;
35 void loggingRegisterThread(const QString &name);
36 void loggingDeregisterThread(void);
37 void loggingGetTimeStamp(qlonglong *epoch, uint *usec);
39 class QWaitCondition;
42  kMessage = 0x01,
43  kRegistering = 0x02,
45  kFlush = 0x08,
46  kStandardIO = 0x10,
47  kInitializing = 0x20,
48 };
50 class LoggerThread;
52 using tmType = struct tm;
54 #define SET_LOGGING_ARG(arg){ \
55  free(arg); \
56  (arg) = strdup(val.toLocal8Bit().constData()); \
57  }
61 class LoggingItem: public QObject, public ReferenceCounter
62 {
65  Q_PROPERTY(int pid READ pid WRITE setPid)
66  Q_PROPERTY(qlonglong tid READ tid WRITE setTid)
67  Q_PROPERTY(qulonglong threadId READ threadId WRITE setThreadId)
68  Q_PROPERTY(uint usec READ usec WRITE setUsec)
69  Q_PROPERTY(int line READ line WRITE setLine)
70  Q_PROPERTY(int type READ type WRITE setType)
71  Q_PROPERTY(int level READ level WRITE setLevel)
72  Q_PROPERTY(int facility READ facility WRITE setFacility)
73  Q_PROPERTY(qlonglong epoch READ epoch WRITE setEpoch)
74  Q_PROPERTY(QString file READ file WRITE setFile)
75  Q_PROPERTY(QString function READ function WRITE setFunction)
76  Q_PROPERTY(QString threadName READ threadName WRITE setThreadName)
77  Q_PROPERTY(QString appName READ appName WRITE setAppName)
78  Q_PROPERTY(QString table READ table WRITE setTable)
79  Q_PROPERTY(QString logFile READ logFile WRITE setLogFile)
80  Q_PROPERTY(QString message READ message WRITE setMessage)
82  friend class LoggerThread;
83  friend void LogPrintLine(uint64_t mask, LogLevel_t level, const char *file, int line,
84  const char *function, QString message);
86  public:
87  char *getThreadName(void);
88  int64_t getThreadTid(void);
89  void setThreadTid(void);
90  static LoggingItem *create(const char *_file, const char *_function, int _line, LogLevel_t _level,
91  LoggingType _type);
92  static LoggingItem *create(QByteArray &buf);
93  QByteArray toByteArray(void);
94  QString getTimestamp(void) const;
95  QString getTimestampUs(void) const;
96  char getLevelChar(void);
98  int pid() const { return m_pid; };
99  qlonglong tid() const { return m_tid; };
100  qulonglong threadId() const { return m_threadId; };
101  uint usec() const { return m_usec; };
102  int line() const { return m_line; };
103  int type() const { return (int)m_type; };
104  int level() const { return (int)m_level; };
105  int facility() const { return m_facility; };
106  qlonglong epoch() const { return m_epoch; };
107  QString file() const { return QString(m_file); };
108  QString function() const { return QString(m_function); };
109  QString threadName() const { return QString(m_threadName); };
110  QString appName() const { return QString(m_appName); };
111  QString table() const { return QString(m_table); };
112  QString logFile() const { return QString(m_logFile); };
113  QString message() const { return m_message; };
115  void setPid(const int val) { m_pid = val; };
116  void setTid(const qlonglong val) { m_tid = val; };
117  void setThreadId(const qulonglong val) { m_threadId = val; };
118  void setUsec(const uint val) { m_usec = val; };
119  void setLine(const int val) { m_line = val; };
120  void setType(const int val) { m_type = (LoggingType)val; };
121  void setLevel(const int val) { m_level = (LogLevel_t)val; };
122  void setFacility(const int val) { m_facility = val; };
123  void setEpoch(const qlonglong val) { m_epoch = val; };
124  void setFile(const QString &val) SET_LOGGING_ARG(m_file)
125  void setFunction(const QString &val) SET_LOGGING_ARG(m_function)
126  void setThreadName(const QString &val) SET_LOGGING_ARG(m_threadName)
127  void setAppName(const QString &val) SET_LOGGING_ARG(m_appName)
128  void setTable(const QString &val) SET_LOGGING_ARG(m_table)
129  void setLogFile(const QString &val) SET_LOGGING_ARG(m_logFile)
130  void setMessage(const QString &val) { m_message = val; };
132  const char *rawFile() const { return m_file; };
133  const char *rawFunction() const { return m_function; };
134  const char *rawThreadName() const { return m_threadName; };
135  const char *rawAppName() const { return m_appName; };
136  const char *rawTable() const { return m_table; };
137  const char *rawLogFile() const { return m_logFile; };
139  protected:
140  int m_pid {-1};
141  qlonglong m_tid {-1};
142  qulonglong m_threadId {UINT64_MAX};
144  int m_line {0};
146  LogLevel_t m_level {LOG_INFO};
147  int m_facility {0};
148  qlonglong m_epoch {0};
149  char *m_file {nullptr};
150  char *m_function {nullptr};
151  char *m_threadName {nullptr};
152  char *m_appName {nullptr};
153  char *m_table {nullptr};
154  char *m_logFile {nullptr};
155  QString m_message {};
157  private:
159  : ReferenceCounter("LoggingItem", false) {};
160  LoggingItem(const char *_file, const char *_function,
161  int _line, LogLevel_t _level, LoggingType _type);
162  ~LoggingItem() override;
164 };
168 class LoggerThread : public QObject, public MThread
169 {
172  friend void LogPrintLine(uint64_t mask, LogLevel_t level, const char *file, int line,
173  const char *function, QString message);
174  public:
175  LoggerThread(QString filename, bool progress, bool quiet, QString table,
176  int facility);
177  ~LoggerThread() override;
178  void run(void) override; // MThread
179  void stop(void);
180  bool flush(int timeoutMS = 200000);
181  static void handleItem(LoggingItem *item);
182  void fillItem(LoggingItem *item);
183  private:
185  QWaitCondition *m_waitNotEmpty {nullptr};
189  QWaitCondition *m_waitEmpty {nullptr};
193  bool m_aborted {false};
194  QString m_filename;
196  bool m_progress;
197  bool m_quiet;
198  QString m_appname {QCoreApplication::applicationName()};
200  QString m_tablename;
202  pid_t m_pid;
204  protected:
205  bool logConsole(LoggingItem *item) const;
206 };
208 #endif
210 /*
211  * vim:ts=4:sw=4:ai:et:si:sts=4
212  */
int m_pid
Definition: logging.h:140
char * m_function
Definition: logging.h:150
void loggingDeregisterThread(void)
Deregister the current thread's name.
Definition: logging.cpp:779
The logging thread that consumes the logging queue and dispatches each LoggingItem.
Definition: logging.h:168
int line() const
Definition: logging.h:102
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
Definition: logging.h:158
void setTid(const qlonglong val)
Definition: logging.h:116
QString message() const
Definition: logging.h:113
uint usec() const
Definition: logging.h:101
QString m_tablename
Cached table name for db logging.
Definition: logging.h:200
char * m_file
Definition: logging.h:149
char * getThreadName(void)
Get the name of the thread that produced the LoggingItem.
Definition: logging.cpp:170
bool m_quiet
silence the console (console only)
Definition: logging.h:197
int m_facility
Cached syslog facility (or -1 to disable)
Definition: logging.h:201
void loggingRegisterThread(const QString &name)
Register the current thread with the given name.
Definition: logging.cpp:760
const char * rawThreadName() const
Definition: logging.h:134
QString m_filename
Filename of debug logfile.
Definition: logging.h:195
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:596
General purpose reference counter.
bool flush(int timeoutMS=200000)
Wait for the queue to be flushed (up to a timeout)
Definition: logging.cpp:531
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
int pid() const
Definition: logging.h:98
const char * rawFunction() const
Definition: logging.h:133
bool m_progress
show only LOG_ERR and more important (console only)
Definition: logging.h:196
void setUsec(const uint val)
Definition: logging.h:118
void setFile(const QString &val) SET_LOGGING_ARG(m_file) void setFunction(const QString &val) SET_LOGGING_ARG(m_function) void setThreadName(const QString &val) SET_LOGGING_ARG(m_threadName) void setAppName(const QString &val) SET_LOGGING_ARG(m_appName) void setTable(const QString &val) SET_LOGGING_ARG(m_table) void setLogFile(const QString &val) SET_LOGGING_ARG(m_logFile) void setMessage(const QString &val)
Definition: logging.h:124
#define SET_LOGGING_ARG(arg)
Definition: logging.h:54
The logging items that are generated by LOG() and are sent to the console.
Definition: logging.h:61
void fillItem(LoggingItem *item)
Definition: logging.cpp:545
int type() const
Definition: logging.h:103
LoggingType m_type
Definition: logging.h:145
QWaitCondition * m_waitEmpty
Condition variable for waiting for the queue to be empty Protected by logQueueMutex.
Definition: logging.h:189
struct tm tmType
Definition: logging.h:52
qlonglong tid() const
Definition: logging.h:99
void setEpoch(const qlonglong val)
Definition: logging.h:123
pid_t m_pid
Cached pid value.
Definition: logging.h:202
static void handleItem(LoggingItem *item)
Handles each LoggingItem.
Definition: logging.cpp:352
QString getTimestamp(void) const
Convert numerical timestamp to a readable date and time.
Definition: logging.cpp:225
Definition: verbosedefs.h:85
QString getTimestampUs(void) const
Definition: logging.cpp:236
void stop(void)
Stop the thread by setting the abort flag after waiting a second for the queue to be flushed.
Definition: logging.cpp:519
QString table() const
Definition: logging.h:111
~LoggingItem() override
Definition: logging.cpp:148
QString logFile() const
Definition: logging.h:112
QString appName() const
Definition: logging.h:110
Definition: logging.h:41
QString file() const
Definition: logging.h:107
int facility() const
Definition: logging.h:105
char getLevelChar(void)
Get the message log level as a single character.
Definition: logging.cpp:244
unsigned int uint
Definition: compat.h:140
void setType(const int val)
Definition: logging.h:120
QByteArray toByteArray(void)
Definition: logging.cpp:158
const char * rawLogFile() const
Definition: logging.h:137
void setLevel(const int val)
Definition: logging.h:121
QString m_appname
Cached application name.
Definition: logging.h:198
qulonglong m_threadId
Definition: logging.h:142
const char * rawFile() const
Definition: logging.h:132
const char * rawAppName() const
Definition: logging.h:135
bool logConsole(LoggingItem *item) const
Process a log message, writing to the console.
Definition: logging.cpp:420
QWaitCondition * m_waitNotEmpty
Condition variable for waiting for the queue to not be empty Protected by logQueueMutex.
Definition: logging.h:185
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:596
LogLevel_t m_level
Definition: logging.h:146
QString m_message
Definition: logging.h:155
char * m_threadName
Definition: logging.h:151
char * m_table
Definition: logging.h:153
int64_t getThreadTid(void)
Get the thread ID of the thread that produced the LoggingItem.
Definition: logging.cpp:186
QString function() const
Definition: logging.h:108
char * m_appName
Definition: logging.h:152
qlonglong epoch() const
Definition: logging.h:106
void setLine(const int val)
Definition: logging.h:119
uint m_usec
Definition: logging.h:143
int m_facility
Definition: logging.h:147
void setThreadTid(void)
Set the thread ID of the thread that produced the LoggingItem.
Definition: logging.cpp:199
void setPid(const int val)
Definition: logging.h:115
char * m_logFile
Definition: logging.h:154
void setFacility(const int val)
Definition: logging.h:122
void setThreadId(const qulonglong val)
Definition: logging.h:117
LoggerThread(QString filename, bool progress, bool quiet, QString table, int facility)
LoggerThread constructor.
Definition: logging.cpp:256
void loggingGetTimeStamp(qlonglong *epoch, uint *usec)
Definition: logging.cpp:117
int m_line
Definition: logging.h:144
qulonglong threadId() const
Definition: logging.h:100
qlonglong m_epoch
Definition: logging.h:148
int level() const
Definition: logging.h:104
qlonglong m_tid
Definition: logging.h:141
void run(void) override
Run the logging thread.
Definition: logging.cpp:295
const char * rawTable() const
Definition: logging.h:136
QString threadName() const
Definition: logging.h:109
Definition: logging.h:45
static LoggingItem * create(const char *_file, const char *_function, int _line, LogLevel_t _level, LoggingType _type)
Create a new LoggingItem.
Definition: logging.cpp:566
~LoggerThread() override
LoggerThread destructor. Triggers the deletion of all loggers.
Definition: logging.cpp:281
bool m_aborted
Flag to abort the thread.
Definition: logging.h:193