MythTV  master
loggingserver.h
Go to the documentation of this file.
1 #ifndef LOGGINGSERVER_H_
2 #define LOGGINGSERVER_H_
3 
4 #include <QMutexLocker>
5 #include <QSocketNotifier>
6 #include <QMutex>
7 #include <QQueue>
8 #include <QElapsedTimer>
9 
10 #include <cstdint>
11 #include <ctime>
12 #include <unistd.h>
13 
14 #include "mythconfig.h"
15 #include "mythbaseexp.h" // MBASE_PUBLIC , etc.
16 #include "verbosedefs.h"
17 #include "mthread.h"
18 
19 #define LOGLINE_MAX (2048-120)
20 
21 class QString;
22 class MSqlQuery;
23 class LoggingItem;
24 
26 class LoggerBase : public QObject
27 {
28  Q_OBJECT
29 
30  public:
32  explicit LoggerBase(const char *string);
34  ~LoggerBase() override;
37  virtual bool logmsg(LoggingItem *item) = 0;
39  virtual void reopen(void) = 0;
41  virtual void stopDatabaseAccess(void) { }
42  protected:
43  QString m_handle {};
44 };
45 
47 class FileLogger : public LoggerBase
48 {
49  Q_OBJECT
50 
51  public:
52  explicit FileLogger(const char *filename);
53  ~FileLogger() override;
54  bool logmsg(LoggingItem *item) override; // LoggerBase
55  void reopen(void) override; // LoggerBase
56  static FileLogger *create(const QString& filename, QMutex *mutex);
57  private:
58  bool m_opened {false};
59  int m_fd {-1};
60 };
61 
62 #ifndef _WIN32
63 class SyslogLogger : public LoggerBase
65 {
66  Q_OBJECT
67 
68  public:
69  SyslogLogger();
70  explicit SyslogLogger(bool open);
71  ~SyslogLogger() override;
72  bool logmsg(LoggingItem *item) override; // LoggerBase
74  void reopen(void) override { } // LoggerBase
75  static SyslogLogger *create(QMutex *mutex, bool open = true);
76  private:
77  bool m_opened {false};
78 };
79 #endif
80 
81 #if CONFIG_SYSTEMD_JOURNAL
82 class JournalLogger : public LoggerBase
83 {
84  Q_OBJECT
85 
86  public:
87  JournalLogger();
88  ~JournalLogger() override;
89  bool logmsg(LoggingItem *item) override; // LoggerBase
91  void reopen(void) override { }; // LoggerBase
92  static JournalLogger *create(QMutex *mutex);
93 };
94 #endif
95 
96 class DBLoggerThread;
97 
99 class DatabaseLogger : public LoggerBase
100 {
101  Q_OBJECT
102 
103  friend class DBLoggerThread;
104  public:
105  explicit DatabaseLogger(const char *table);
106  ~DatabaseLogger() override;
107  bool logmsg(LoggingItem *item) override; // LoggerBase
108  void reopen(void) override { }; // LoggerBase
109  void stopDatabaseAccess(void) override; // LoggerBase
110  static DatabaseLogger *create(const QString& table, QMutex *mutex);
111  protected:
112  bool logqmsg(MSqlQuery &query, LoggingItem *item);
113  void prepare(MSqlQuery &query);
114  private:
115  bool isDatabaseReady(void);
116  static bool tableExists(const QString &table);
117 
119  QString m_query;
120  bool m_opened {true};
121  bool m_loggingTableExists {false};
122  QElapsedTimer m_disabledTime;
123  QElapsedTimer m_errorLoggingTime;
124  static const int kMinDisabledTime;
125 };
127 
128 using LogMessage = QList<QByteArray>;
129 using LogMessageList = QList<LogMessage *>;
130 
133 class LogForwardThread : public QObject, public MThread
134 {
135  Q_OBJECT
136 
137  friend void logSigHup(void);
138  public:
140  ~LogForwardThread() override;
141  void run(void) override; // MThread
142  void stop(void);
143  private:
144  bool m_aborted {false};
145 
146  static void forwardMessage(LogMessage *msg);
147  signals:
148  void incomingSigHup(void);
149  protected slots:
150  static void handleSigHup(void);
151 };
152 
153 MBASE_PUBLIC bool logForwardStart(void);
154 MBASE_PUBLIC void logForwardStop(void);
155 MBASE_PUBLIC void logForwardMessage(const QList<QByteArray> &msg);
156 
157 
158 class QWaitCondition;
159 #define MAX_QUEUE_LEN 1000
160 
166 class DBLoggerThread : public MThread
167 {
168  public:
170  ~DBLoggerThread() override;
171  DBLoggerThread(const DBLoggerThread &) = delete; // not copyable
172  DBLoggerThread &operator=(const DBLoggerThread &) = delete; // not copyable
173 
174  void run(void) override; // MThread
175  void stop(void);
178  bool enqueue(LoggingItem *item);
179 
182  bool queueFull(void)
183  {
184  QMutexLocker qLock(&m_queueMutex);
185  return (m_queue->size() >= MAX_QUEUE_LEN);
186  }
187  private:
189  QMutex m_queueMutex;
190  QQueue<LoggingItem *> *m_queue {nullptr};
191  QWaitCondition *m_wait {nullptr};
192  volatile bool m_aborted {false};
195 };
198 
199 #ifndef _WIN32
200 MBASE_PUBLIC void logSigHup(void);
201 #endif
202 
203 #endif
204 
205 /*
206  * vim:ts=4:sw=4:ai:et:si:sts=4
207  */
DatabaseLogger::logqmsg
bool logqmsg(MSqlQuery &query, LoggingItem *item)
Actually insert a log message from the queue into the database.
Definition: loggingserver.cpp:419
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
DatabaseLogger::m_errorLoggingTime
QElapsedTimer m_errorLoggingTime
Elapsed time since DB error logging was last done.
Definition: loggingserver.h:123
DatabaseLogger::reopen
void reopen(void) override
Reopen the log file to facilitate log rolling.
Definition: loggingserver.h:108
FileLogger::m_opened
bool m_opened
true when the logfile is opened
Definition: loggingserver.h:58
MAX_QUEUE_LEN
#define MAX_QUEUE_LEN
Definition: loggingserver.h:159
DBLoggerThread::~DBLoggerThread
~DBLoggerThread() override
DBLoggerThread deconstructor.
Definition: loggingserver.cpp:519
LogMessageList
QList< LogMessage * > LogMessageList
Definition: loggingserver.h:129
LoggerBase
Base class for the various logging mechanisms.
Definition: loggingserver.h:26
DatabaseLogger::create
static DatabaseLogger * create(const QString &table, QMutex *mutex)
Definition: loggingserver.cpp:346
DatabaseLogger::m_thread
DBLoggerThread * m_thread
The database queue handling thread.
Definition: loggingserver.h:118
mythbaseexp.h
FileLogger::logmsg
bool logmsg(LoggingItem *item) override
Process a log message, writing to the logfile.
Definition: loggingserver.cpp:163
DatabaseLogger::tableExists
static bool tableExists(const QString &table)
Checks whether table exists and is ready for writing.
Definition: loggingserver.cpp:485
DatabaseLogger::logmsg
bool logmsg(LoggingItem *item) override
Process a log message, queuing it for logging to the database.
Definition: loggingserver.cpp:381
verbosedefs.h
LoggerBase::~LoggerBase
~LoggerBase() override
LoggerBase Deconstructor.
Definition: loggingserver.cpp:97
DBLoggerThread::DBLoggerThread
DBLoggerThread(DatabaseLogger *logger)
DBLoggerThread constructor.
Definition: loggingserver.cpp:510
LogForwardThread::m_aborted
bool m_aborted
Flag to abort the thread.
Definition: loggingserver.h:144
DatabaseLogger::stopDatabaseAccess
void stopDatabaseAccess(void) override
Stop logging to the database and wait for the thread to stop.
Definition: loggingserver.cpp:368
LoggingItem
The logging items that are generated by LOG() and are sent to the console.
Definition: logging.h:55
DatabaseLogger::isDatabaseReady
bool isDatabaseReady(void)
Check if the database is ready for use.
Definition: loggingserver.cpp:465
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
logForwardStop
MBASE_PUBLIC void logForwardStop(void)
Definition: loggingserver.cpp:877
DatabaseLogger::m_loggingTableExists
bool m_loggingTableExists
The desired logging table exists.
Definition: loggingserver.h:121
DatabaseLogger::m_query
QString m_query
The database query to insert log messages.
Definition: loggingserver.h:119
LogForwardThread
The logging thread that forwards received messages to the consuming loggers via ZeroMQ.
Definition: loggingserver.h:133
DatabaseLogger::kMinDisabledTime
static const int kMinDisabledTime
Minimum time to disable DB logging (in ms)
Definition: loggingserver.h:124
LogForwardThread::~LogForwardThread
~LogForwardThread() override
LogForwardThread destructor.
Definition: loggingserver.cpp:645
DBLoggerThread::stop
void stop(void)
Tell the thread to stop by setting the m_aborted flag.
Definition: loggingserver.cpp:598
LogForwardThread::handleSigHup
static void handleSigHup(void)
SIGHUP handler - reopen all open logfiles for logrollers.
Definition: loggingserver.cpp:717
DatabaseLogger::m_opened
bool m_opened
The database is opened.
Definition: loggingserver.h:120
DBLoggerThread
Thread that manages the queueing of logging inserts for the database.
Definition: loggingserver.h:166
LogForwardThread::run
void run(void) override
Run the log forwarding thread.
Definition: loggingserver.cpp:654
LoggerBase::m_handle
QString m_handle
semi-opaque handle for identifying instance
Definition: loggingserver.h:43
DatabaseLogger::DatabaseLogger
DatabaseLogger(const char *table)
DatabaseLogger constructor.
Definition: loggingserver.cpp:320
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:637
LoggerBase::LoggerBase
LoggerBase(const char *string)
LoggerBase Constructor.
Definition: loggingserver.cpp:87
DatabaseLogger::~DatabaseLogger
~DatabaseLogger() override
DatabaseLogger deconstructor.
Definition: loggingserver.cpp:339
DBLoggerThread::operator=
DBLoggerThread & operator=(const DBLoggerThread &)=delete
FileLogger::m_fd
int m_fd
contains the file descriptor for the logfile
Definition: loggingserver.h:59
LogForwardThread::LogForwardThread
LogForwardThread()
LogForwardThread constructor.
Definition: loggingserver.cpp:638
DBLoggerThread::m_aborted
volatile bool m_aborted
Used during shutdown to indicate that the thread should stop ASAP.
Definition: loggingserver.h:194
logForwardMessage
MBASE_PUBLIC void logForwardMessage(const QList< QByteArray > &msg)
Definition: loggingserver.cpp:893
DatabaseLogger
Database logger - logs to the MythTV database.
Definition: loggingserver.h:99
DatabaseLogger::m_disabledTime
QElapsedTimer m_disabledTime
Elapsed time since the DB logging was disabled.
Definition: loggingserver.h:122
FileLogger
File-based logger - used for logfiles and console.
Definition: loggingserver.h:47
LogForwardThread::stop
void stop(void)
Stop the thread by setting the abort flag.
Definition: loggingserver.cpp:863
DatabaseLogger::prepare
void prepare(MSqlQuery &query)
Prepare the database query for use, and bind constant values to it.
Definition: loggingserver.cpp:457
FileLogger::~FileLogger
~FileLogger() override
FileLogger deconstructor - close the logfile.
Definition: loggingserver.cpp:117
DBLoggerThread::enqueue
bool enqueue(LoggingItem *item)
Enqueues a LoggingItem onto the queue for the thread to consume.
Definition: loggingserver.cpp:605
logger
static void logger(cdio_log_level_t level, const char *message)
Definition: cddecoder.cpp:36
MThread
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:48
mthread.h
DBLoggerThread::m_logger
DatabaseLogger * m_logger
The associated logger instance.
Definition: loggingserver.h:188
DBLoggerThread::m_queueMutex
QMutex m_queueMutex
Mutex for protecting the queue.
Definition: loggingserver.h:189
LoggerBase::logmsg
virtual bool logmsg(LoggingItem *item)=0
Process a log message for the logger instance.
LoggerBase::reopen
virtual void reopen(void)=0
Reopen the log file to facilitate log rolling.
FileLogger::FileLogger
FileLogger(const char *filename)
FileLogger constructor.
Definition: loggingserver.cpp:106
LogForwardThread::incomingSigHup
void incomingSigHup(void)
FileLogger::reopen
void reopen(void) override
Reopen the logfile after a SIGHUP.
Definition: loggingserver.cpp:152
LogForwardThread::logSigHup
friend void logSigHup(void)
FileLogger::create
static FileLogger * create(const QString &filename, QMutex *mutex)
Definition: loggingserver.cpp:129
DBLoggerThread::queueFull
bool queueFull(void)
Indicates when the queue is full.
Definition: loggingserver.h:182
LogMessage
QList< QByteArray > LogMessage
Definition: loggingserver.h:128
logForwardStart
MBASE_PUBLIC bool logForwardStart(void)
Definition: loggingserver.cpp:868
query
MSqlQuery query(MSqlQuery::InitCon())
DBLoggerThread::m_queue
QQueue< LoggingItem * > * m_queue
Queue of LoggingItems to insert.
Definition: loggingserver.h:190
LogForwardThread::forwardMessage
static void forwardMessage(LogMessage *msg)
Definition: loggingserver.cpp:732
DBLoggerThread::run
void run(void) override
Start the thread.
Definition: loggingserver.cpp:534
DBLoggerThread::m_wait
QWaitCondition * m_wait
Wait condition used for waiting for the queue to not be full.
Definition: loggingserver.h:191
LoggerBase::stopDatabaseAccess
virtual void stopDatabaseAccess(void)
Stop logging to the database.
Definition: loggingserver.h:41