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 <QTime>
8 #include <QPointer>
9 #include <QCoreApplication>
10 
11 #include <cstdint>
12 #include <cstdlib>
13 
14 #include "mythconfig.h"
15 #include "mythbaseexp.h" // MBASE_PUBLIC , etc.
16 #include "verbosedefs.h"
17 #include "mythsignalingtimer.h"
18 #include "mthread.h"
19 #include "referencecounter.h"
20 #include "compat.h"
21 
22 #ifdef _MSC_VER
23 # include <unistd.h> // pid_t
24 #endif
25 
26 #if CONFIG_SYSTEMD_JOURNAL
27 #define SYSTEMD_JOURNAL_FACILITY -99
28 #endif
29 
30 #define LOGLINE_MAX (2048-120)
31 
32 class QString;
33 class MSqlQuery;
34 class LoggingItem;
35 
36 void loggingRegisterThread(const QString &name);
37 void loggingDeregisterThread(void);
38 void loggingGetTimeStamp(qlonglong *epoch, uint *usec);
39 
40 class QWaitCondition;
41 
42 typedef enum {
43  kMessage = 0x01,
44  kRegistering = 0x02,
46  kFlush = 0x08,
47  kStandardIO = 0x10,
48  kInitializing = 0x20,
49 } LoggingType;
50 
51 class LoggerThread;
52 
53 typedef struct tm tmType;
54 
55 #define SET_LOGGING_ARG(arg){ \
56  free(arg); \
57  arg = strdup(val.toLocal8Bit().constData()); \
58  }
59 
62 class LoggingItem: public QObject, public ReferenceCounter
63 {
64  Q_OBJECT
65 
66  Q_PROPERTY(int pid READ pid WRITE setPid)
67  Q_PROPERTY(qlonglong tid READ tid WRITE setTid)
68  Q_PROPERTY(qulonglong threadId READ threadId WRITE setThreadId)
69  Q_PROPERTY(uint usec READ usec WRITE setUsec)
70  Q_PROPERTY(int line READ line WRITE setLine)
71  Q_PROPERTY(int type READ type WRITE setType)
72  Q_PROPERTY(int level READ level WRITE setLevel)
73  Q_PROPERTY(int facility READ facility WRITE setFacility)
74  Q_PROPERTY(qlonglong epoch READ epoch WRITE setEpoch)
75  Q_PROPERTY(QString file READ file WRITE setFile)
76  Q_PROPERTY(QString function READ function WRITE setFunction)
77  Q_PROPERTY(QString threadName READ threadName WRITE setThreadName)
78  Q_PROPERTY(QString appName READ appName WRITE setAppName)
79  Q_PROPERTY(QString table READ table WRITE setTable)
80  Q_PROPERTY(QString logFile READ logFile WRITE setLogFile)
81  Q_PROPERTY(QString message READ message WRITE setMessage)
82 
83  friend class LoggerThread;
84  friend void LogPrintLine(uint64_t, LogLevel_t, const char *, int,
85  const char *, int, const char *, ... );
86 
87  public:
88  char *getThreadName(void);
89  int64_t getThreadTid(void);
90  void setThreadTid(void);
91  static LoggingItem *create(const char *, const char *, int, LogLevel_t,
92  LoggingType);
93  static LoggingItem *create(QByteArray &buf);
94  QByteArray toByteArray(void);
95 
96  int pid() const { return m_pid; };
97  qlonglong tid() const { return m_tid; };
98  qulonglong threadId() const { return m_threadId; };
99  uint usec() const { return m_usec; };
100  int line() const { return m_line; };
101  int type() const { return (int)m_type; };
102  int level() const { return (int)m_level; };
103  int facility() const { return m_facility; };
104  qlonglong epoch() const { return m_epoch; };
105  QString file() const { return QString(m_file); };
106  QString function() const { return QString(m_function); };
107  QString threadName() const { return QString(m_threadName); };
108  QString appName() const { return QString(m_appName); };
109  QString table() const { return QString(m_table); };
110  QString logFile() const { return QString(m_logFile); };
111  QString message() const { return QString(m_message); };
112 
113  void setPid(const int val) { m_pid = val; };
114  void setTid(const qlonglong val) { m_tid = val; };
115  void setThreadId(const qulonglong val) { m_threadId = val; };
116  void setUsec(const uint val) { m_usec = val; };
117  void setLine(const int val) { m_line = val; };
118  void setType(const int val) { m_type = (LoggingType)val; };
119  void setLevel(const int val) { m_level = (LogLevel_t)val; };
120  void setFacility(const int val) { m_facility = val; };
121  void setEpoch(const qlonglong val) { m_epoch = val; };
122  void setFile(const QString &val) SET_LOGGING_ARG(m_file)
123  void setFunction(const QString &val) SET_LOGGING_ARG(m_function)
124  void setThreadName(const QString &val) SET_LOGGING_ARG(m_threadName)
125  void setAppName(const QString &val) SET_LOGGING_ARG(m_appName)
126  void setTable(const QString &val) SET_LOGGING_ARG(m_table)
127  void setLogFile(const QString &val) SET_LOGGING_ARG(m_logFile)
128  void setMessage(const QString &val)
129  {
130  strncpy(m_message, val.toLocal8Bit().constData(), LOGLINE_MAX);
131  m_message[LOGLINE_MAX] = '\0';
132  };
133 
134  const char *rawFile() const { return m_file; };
135  const char *rawFunction() const { return m_function; };
136  const char *rawThreadName() const { return m_threadName; };
137  const char *rawAppName() const { return m_appName; };
138  const char *rawTable() const { return m_table; };
139  const char *rawLogFile() const { return m_logFile; };
140  const char *rawMessage() const { return m_message; };
141 
142  protected:
143  int m_pid {-1};
144  qlonglong m_tid {-1};
145  qulonglong m_threadId {(qulonglong)-1};
147  int m_line {0};
149  LogLevel_t m_level {LOG_INFO};
150  int m_facility {0};
151  qlonglong m_epoch {0};
152  char *m_file {nullptr};
153  char *m_function {nullptr};
154  char *m_threadName {nullptr};
155  char *m_appName {nullptr};
156  char *m_table {nullptr};
157  char *m_logFile {nullptr};
158  char m_message[LOGLINE_MAX+1] {0};
159 
160  private:
162  : ReferenceCounter("LoggingItem", false) {};
163  LoggingItem(const char *_file, const char *_function,
164  int _line, LogLevel_t _level, LoggingType _type);
165  ~LoggingItem();
167 };
168 
171 class LoggerThread : public QObject, public MThread
172 {
173  Q_OBJECT
174 
175  friend void LogPrintLine(uint64_t, LogLevel_t, const char *, int,
176  const char *, int, const char *, ... );
177  public:
178  LoggerThread(QString filename, bool progress, bool quiet, QString table,
179  int facility);
180  ~LoggerThread();
181  void run(void) override; // MThread
182  void stop(void);
183  bool flush(int timeoutMS = 200000);
184  static void handleItem(LoggingItem *item);
185  void fillItem(LoggingItem *item);
186  private:
188  QWaitCondition *m_waitNotEmpty {nullptr};
192  QWaitCondition *m_waitEmpty {nullptr};
196  bool m_aborted {false};
197  QString m_filename;
199  bool m_progress;
200  int m_quiet;
201  QString m_appname {QCoreApplication::applicationName()};
203  QString m_tablename;
205  pid_t m_pid;
206 
207  protected:
208  bool logConsole(LoggingItem *item);
209 };
210 
211 #endif
212 
213 /*
214  * vim:ts=4:sw=4:ai:et:si:sts=4
215  */
int m_pid
Definition: logging.h:143
char * m_function
Definition: logging.h:153
char m_message[LOGLINE_MAX+1]
Definition: logging.h:158
void loggingDeregisterThread(void)
Deregister the current thread's name.
Definition: logging.cpp:774
The logging thread that consumes the logging queue and dispatches each LoggingItem.
Definition: logging.h:171
int line() const
Definition: logging.h:100
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
LoggingItem()
Definition: logging.h:161
void setTid(const qlonglong val)
Definition: logging.h:114
QString message() const
Definition: logging.h:111
uint usec() const
Definition: logging.h:99
QString m_tablename
Cached table name for db logging.
Definition: logging.h:203
char * m_file
Definition: logging.h:152
char * getThreadName(void)
Get the name of the thread that produced the LoggingItem.
Definition: logging.cpp:178
int m_facility
Cached syslog facility (or -1 to disable)
Definition: logging.h:204
void loggingRegisterThread(const QString &name)
Register the current thread with the given name.
Definition: logging.cpp:755
const char * rawThreadName() const
Definition: logging.h:136
QString m_filename
Filename of debug logfile.
Definition: logging.h:198
General purpose reference counter.
bool flush(int timeoutMS=200000)
Wait for the queue to be flushed (up to a timeout)
Definition: logging.cpp:506
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
int pid() const
Definition: logging.h:96
const char * rawFunction() const
Definition: logging.h:135
bool m_progress
show only LOG_ERR and more important (console only)
Definition: logging.h:199
void setUsec(const uint val)
Definition: logging.h:116
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:122
#define SET_LOGGING_ARG(arg)
Definition: logging.h:55
The logging items that are generated by LOG() and are sent to the console.
Definition: logging.h:62
void fillItem(LoggingItem *item)
Definition: logging.cpp:520
unsigned int uint
Definition: compat.h:140
bool logConsole(LoggingItem *item)
Process a log message, writing to the console.
Definition: logging.cpp:403
int type() const
Definition: logging.h:101
LoggingType m_type
Definition: logging.h:148
QWaitCondition * m_waitEmpty
Condition variable for waiting for the queue to be empty Protected by logQueueMutex.
Definition: logging.h:192
qlonglong tid() const
Definition: logging.h:97
Q_DISABLE_COPY(LoggerThread)
void setEpoch(const qlonglong val)
Definition: logging.h:121
pid_t m_pid
Cached pid value.
Definition: logging.h:205
static void handleItem(LoggingItem *item)
Handles each LoggingItem.
Definition: logging.cpp:331
VERBOSE_PREAMBLE false
Definition: verbosedefs.h:85
void stop(void)
Stop the thread by setting the abort flag after waiting a second for the queue to be flushed.
Definition: logging.cpp:494
QString table() const
Definition: logging.h:109
friend void LogPrintLine(uint64_t, LogLevel_t, const char *, int, const char *, int, const char *,...)
Format and send a log message into the queue.
Definition: logging.cpp:574
QString logFile() const
Definition: logging.h:110
QString appName() const
Definition: logging.h:108
LoggingType
Definition: logging.h:42
QString file() const
Definition: logging.h:105
int facility() const
Definition: logging.h:103
void setType(const int val)
Definition: logging.h:118
const char * name
Definition: ParseText.cpp:328
QByteArray toByteArray(void)
Definition: logging.cpp:166
const char * rawLogFile() const
Definition: logging.h:139
#define LOGLINE_MAX
Definition: logging.h:30
void setLevel(const int val)
Definition: logging.h:119
QString m_appname
Cached application name.
Definition: logging.h:201
qulonglong m_threadId
Definition: logging.h:145
const char * rawFile() const
Definition: logging.h:134
const char * rawAppName() const
Definition: logging.h:137
QWaitCondition * m_waitNotEmpty
Condition variable for waiting for the queue to not be empty Protected by logQueueMutex.
Definition: logging.h:188
LogLevel_t m_level
Definition: logging.h:149
char * m_threadName
Definition: logging.h:154
const char * rawMessage() const
Definition: logging.h:140
char * m_table
Definition: logging.h:156
int64_t getThreadTid(void)
Get the thread ID of the thread that produced the LoggingItem.
Definition: logging.cpp:194
QString function() const
Definition: logging.h:106
char * m_appName
Definition: logging.h:155
qlonglong epoch() const
Definition: logging.h:104
Q_DISABLE_COPY(LoggingItem)
void setLine(const int val)
Definition: logging.h:117
uint m_usec
Definition: logging.h:146
struct tm tmType
Definition: logging.h:53
int m_facility
Definition: logging.h:150
void setThreadTid(void)
Set the thread ID of the thread that produced the LoggingItem.
Definition: logging.cpp:207
void setPid(const int val)
Definition: logging.h:113
char * m_logFile
Definition: logging.h:157
void setFacility(const int val)
Definition: logging.h:120
void setThreadId(const qulonglong val)
Definition: logging.h:115
LoggerThread(QString filename, bool progress, bool quiet, QString table, int facility)
LoggerThread constructor.
Definition: logging.cpp:235
void loggingGetTimeStamp(qlonglong *epoch, uint *usec)
Definition: logging.cpp:120
int m_line
Definition: logging.h:147
qulonglong threadId() const
Definition: logging.h:98
qlonglong m_epoch
Definition: logging.h:151
int level() const
Definition: logging.h:102
friend void LogPrintLine(uint64_t, LogLevel_t, const char *, int, const char *, int, const char *,...)
Format and send a log message into the queue.
Definition: logging.cpp:574
int m_quiet
silence the console (console only)
Definition: logging.h:200
qlonglong m_tid
Definition: logging.h:144
void run(void) override
Run the logging thread.
Definition: logging.cpp:274
const char * rawTable() const
Definition: logging.h:138
QString threadName() const
Definition: logging.h:107
Definition: logging.h:46
~LoggerThread()
LoggerThread destructor. Triggers the deletion of all loggers.
Definition: logging.cpp:260
static LoggingItem * create(const char *, const char *, int, LogLevel_t, LoggingType)
Create a new LoggingItem.
Definition: logging.cpp:541
bool m_aborted
Flag to abort the thread.
Definition: logging.h:196