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, int fromQString, const char *format, ... );
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);
95  int pid() const { return m_pid; };
96  qlonglong tid() const { return m_tid; };
97  qulonglong threadId() const { return m_threadId; };
98  uint usec() const { return m_usec; };
99  int line() const { return m_line; };
100  int type() const { return (int)m_type; };
101  int level() const { return (int)m_level; };
102  int facility() const { return m_facility; };
103  qlonglong epoch() const { return m_epoch; };
104  QString file() const { return QString(m_file); };
105  QString function() const { return QString(m_function); };
106  QString threadName() const { return QString(m_threadName); };
107  QString appName() const { return QString(m_appName); };
108  QString table() const { return QString(m_table); };
109  QString logFile() const { return QString(m_logFile); };
110  QString message() const { return QString(m_message); };
112  void setPid(const int val) { m_pid = val; };
113  void setTid(const qlonglong val) { m_tid = val; };
114  void setThreadId(const qulonglong val) { m_threadId = val; };
115  void setUsec(const uint val) { m_usec = val; };
116  void setLine(const int val) { m_line = val; };
117  void setType(const int val) { m_type = (LoggingType)val; };
118  void setLevel(const int val) { m_level = (LogLevel_t)val; };
119  void setFacility(const int val) { m_facility = val; };
120  void setEpoch(const qlonglong val) { m_epoch = val; };
121  void setFile(const QString &val) SET_LOGGING_ARG(m_file)
122  void setFunction(const QString &val) SET_LOGGING_ARG(m_function)
123  void setThreadName(const QString &val) SET_LOGGING_ARG(m_threadName)
124  void setAppName(const QString &val) SET_LOGGING_ARG(m_appName)
125  void setTable(const QString &val) SET_LOGGING_ARG(m_table)
126  void setLogFile(const QString &val) SET_LOGGING_ARG(m_logFile)
127  void setMessage(const QString &val)
128  {
129  strncpy(m_message, val.toLocal8Bit().constData(), LOGLINE_MAX);
130  m_message[LOGLINE_MAX] = '\0';
131  };
133  const char *rawFile() const { return m_file; };
134  const char *rawFunction() const { return m_function; };
135  const char *rawThreadName() const { return m_threadName; };
136  const char *rawAppName() const { return m_appName; };
137  const char *rawTable() const { return m_table; };
138  const char *rawLogFile() const { return m_logFile; };
139  const char *rawMessage() const { return m_message; };
141  protected:
142  int m_pid {-1};
143  qlonglong m_tid {-1};
144  qulonglong m_threadId {(qulonglong)-1};
146  int m_line {0};
148  LogLevel_t m_level {LOG_INFO};
149  int m_facility {0};
150  qlonglong m_epoch {0};
151  char *m_file {nullptr};
152  char *m_function {nullptr};
153  char *m_threadName {nullptr};
154  char *m_appName {nullptr};
155  char *m_table {nullptr};
156  char *m_logFile {nullptr};
157  char m_message[LOGLINE_MAX+1] {0};
159  private:
161  : ReferenceCounter("LoggingItem", false) {};
162  LoggingItem(const char *_file, const char *_function,
163  int _line, LogLevel_t _level, LoggingType _type);
164  ~LoggingItem() override;
166 };
170 class LoggerThread : public QObject, public MThread
171 {
174  friend void LogPrintLine(uint64_t mask, LogLevel_t lavel, const char *file, int line,
175  const char *funcion, int fromQString, const char *format, ... );
176  public:
177  LoggerThread(QString filename, bool progress, bool quiet, QString table,
178  int facility);
179  ~LoggerThread() override;
180  void run(void) override; // MThread
181  void stop(void);
182  bool flush(int timeoutMS = 200000);
183  static void handleItem(LoggingItem *item);
184  void fillItem(LoggingItem *item);
185  private:
187  QWaitCondition *m_waitNotEmpty {nullptr};
191  QWaitCondition *m_waitEmpty {nullptr};
195  bool m_aborted {false};
196  QString m_filename;
198  bool m_progress;
199  int m_quiet;
200  QString m_appname {QCoreApplication::applicationName()};
202  QString m_tablename;
204  pid_t m_pid;
206  protected:
207  bool logConsole(LoggingItem *item);
208 };
210 #endif
212 /*
213  * vim:ts=4:sw=4:ai:et:si:sts=4
214  */
int m_pid
Definition: logging.h:142
char * m_function
Definition: logging.h:152
char m_message[LOGLINE_MAX+1]
Definition: logging.h:157
void loggingDeregisterThread(void)
Deregister the current thread's name.
Definition: logging.cpp:784
The logging thread that consumes the logging queue and dispatches each LoggingItem.
Definition: logging.h:170
int line() const
Definition: logging.h:99
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
Definition: logging.h:160
void setTid(const qlonglong val)
Definition: logging.h:113
QString message() const
Definition: logging.h:110
uint usec() const
Definition: logging.h:98
QString m_tablename
Cached table name for db logging.
Definition: logging.h:202
char * m_file
Definition: logging.h:151
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:203
void loggingRegisterThread(const QString &name)
Register the current thread with the given name.
Definition: logging.cpp:765
const char * rawThreadName() const
Definition: logging.h:135
QString m_filename
Filename of debug logfile.
Definition: logging.h:197
General purpose reference counter.
bool flush(int timeoutMS=200000)
Wait for the queue to be flushed (up to a timeout)
Definition: logging.cpp:516
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
int pid() const
Definition: logging.h:95
const char * rawFunction() const
Definition: logging.h:134
bool m_progress
show only LOG_ERR and more important (console only)
Definition: logging.h:198
void setUsec(const uint val)
Definition: logging.h:115
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:121
#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:530
bool logConsole(LoggingItem *item)
Process a log message, writing to the console.
Definition: logging.cpp:403
int type() const
Definition: logging.h:100
LoggingType m_type
Definition: logging.h:147
QWaitCondition * m_waitEmpty
Condition variable for waiting for the queue to be empty Protected by logQueueMutex.
Definition: logging.h:191
struct tm tmType
Definition: logging.h:52
qlonglong tid() const
Definition: logging.h:96
void setEpoch(const qlonglong val)
Definition: logging.h:120
pid_t m_pid
Cached pid value.
Definition: logging.h:204
static void handleItem(LoggingItem *item)
Handles each LoggingItem.
Definition: logging.cpp:331
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:504
QString table() const
Definition: logging.h:108
~LoggingItem() override
Definition: logging.cpp:151
QString logFile() const
Definition: logging.h:109
friend void LogPrintLine(uint64_t mask, LogLevel_t level, const char *file, int line, const char *function, int fromQString, const char *format,...)
Format and send a log message into the queue.
Definition: logging.cpp:584
QString appName() const
Definition: logging.h:107
Definition: logging.h:41
QString file() const
Definition: logging.h:104
int facility() const
Definition: logging.h:102
unsigned int uint
Definition: compat.h:140
void setType(const int val)
Definition: logging.h:117
QByteArray toByteArray(void)
Definition: logging.cpp:166
const char * rawLogFile() const
Definition: logging.h:138
Definition: logging.h:29
void setLevel(const int val)
Definition: logging.h:118
QString m_appname
Cached application name.
Definition: logging.h:200
qulonglong m_threadId
Definition: logging.h:144
const char * rawFile() const
Definition: logging.h:133
const char * rawAppName() const
Definition: logging.h:136
QWaitCondition * m_waitNotEmpty
Condition variable for waiting for the queue to not be empty Protected by logQueueMutex.
Definition: logging.h:187
LogLevel_t m_level
Definition: logging.h:148
char * m_threadName
Definition: logging.h:153
const char * rawMessage() const
Definition: logging.h:139
char * m_table
Definition: logging.h:155
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:105
char * m_appName
Definition: logging.h:154
qlonglong epoch() const
Definition: logging.h:103
void setLine(const int val)
Definition: logging.h:116
uint m_usec
Definition: logging.h:145
int m_facility
Definition: logging.h:149
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:112
char * m_logFile
Definition: logging.h:156
void setFacility(const int val)
Definition: logging.h:119
void setThreadId(const qulonglong val)
Definition: logging.h:114
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:146
qulonglong threadId() const
Definition: logging.h:97
qlonglong m_epoch
Definition: logging.h:150
int level() const
Definition: logging.h:101
int m_quiet
silence the console (console only)
Definition: logging.h:199
qlonglong m_tid
Definition: logging.h:143
void run(void) override
Run the logging thread.
Definition: logging.cpp:274
const char * rawTable() const
Definition: logging.h:137
QString threadName() const
Definition: logging.h:106
Definition: logging.h:45
friend void LogPrintLine(uint64_t mask, LogLevel_t lavel, const char *file, int line, const char *funcion, int fromQString, const char *format,...)
Format and send a log message into the queue.
Definition: logging.cpp:584
static LoggingItem * create(const char *_file, const char *_function, int _line, LogLevel_t _level, LoggingType _type)
Create a new LoggingItem.
Definition: logging.cpp:551
~LoggerThread() override
LoggerThread destructor. Triggers the deletion of all loggers.
Definition: logging.cpp:260
bool m_aborted
Flag to abort the thread.
Definition: logging.h:195