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 constexpr std::chrono::milliseconds kMinDisabledTime {1s};
125 };
126 
127 using LogMessage = QList<QByteArray>;
128 using LogMessageList = QList<LogMessage *>;
129 
132 class LogForwardThread : public QObject, public MThread
133 {
134  Q_OBJECT
135 
136  friend void logSigHup(void);
137  public:
139  ~LogForwardThread() override;
140  void run(void) override; // MThread
141  void stop(void);
142  private:
143  bool m_aborted {false};
144 
145  static void forwardMessage(LogMessage *msg);
146  signals:
147  void incomingSigHup(void);
148  protected slots:
149  static void handleSigHup(void);
150 };
151 
152 MBASE_PUBLIC bool logForwardStart(void);
153 MBASE_PUBLIC void logForwardStop(void);
154 MBASE_PUBLIC void logForwardMessage(const QList<QByteArray> &msg);
155 
156 
157 class QWaitCondition;
158 #define MAX_QUEUE_LEN 1000
159 
165 class DBLoggerThread : public MThread
166 {
167  public:
169  ~DBLoggerThread() override;
170  DBLoggerThread(const DBLoggerThread &) = delete; // not copyable
171  DBLoggerThread &operator=(const DBLoggerThread &) = delete; // not copyable
172 
173  void run(void) override; // MThread
174  void stop(void);
177  bool enqueue(LoggingItem *item);
178 
181  bool queueFull(void)
182  {
183  QMutexLocker qLock(&m_queueMutex);
184  return (m_queue->size() >= MAX_QUEUE_LEN);
185  }
186  private:
188  QMutex m_queueMutex;
189  QQueue<LoggingItem *> *m_queue {nullptr};
190  QWaitCondition *m_wait {nullptr};
191  volatile bool m_aborted {false};
194 };
197 
198 #ifndef _WIN32
199 MBASE_PUBLIC void logSigHup(void);
200 #endif
201 
202 #endif
203 
204 /*
205  * vim:ts=4:sw=4:ai:et:si:sts=4
206  */
DatabaseLogger::logqmsg
bool logqmsg(MSqlQuery &query, LoggingItem *item)
Actually insert a log message from the queue into the database.
Definition: loggingserver.cpp:416
MSqlQuery
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:124
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:158
DBLoggerThread::~DBLoggerThread
~DBLoggerThread() override
DBLoggerThread deconstructor.
Definition: loggingserver.cpp:516
LogMessageList
QList< LogMessage * > LogMessageList
Definition: loggingserver.h:128
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:343
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:162
DatabaseLogger::tableExists
static bool tableExists(const QString &table)
Checks whether table exists and is ready for writing.
Definition: loggingserver.cpp:482
DatabaseLogger::logmsg
bool logmsg(LoggingItem *item) override
Process a log message, queuing it for logging to the database.
Definition: loggingserver.cpp:378
verbosedefs.h
LoggerBase::~LoggerBase
~LoggerBase() override
LoggerBase Deconstructor.
Definition: loggingserver.cpp:96
DBLoggerThread::DBLoggerThread
DBLoggerThread(DatabaseLogger *logger)
DBLoggerThread constructor.
Definition: loggingserver.cpp:507
LogForwardThread::m_aborted
bool m_aborted
Flag to abort the thread.
Definition: loggingserver.h:143
DatabaseLogger::stopDatabaseAccess
void stopDatabaseAccess(void) override
Stop logging to the database and wait for the thread to stop.
Definition: loggingserver.cpp:365
DatabaseLogger::kMinDisabledTime
static constexpr std::chrono::milliseconds kMinDisabledTime
Minimum time to disable DB logging.
Definition: loggingserver.h:124
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:462
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
logForwardStop
MBASE_PUBLIC void logForwardStop(void)
Definition: loggingserver.cpp:874
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:132
LogForwardThread::~LogForwardThread
~LogForwardThread() override
LogForwardThread destructor.
Definition: loggingserver.cpp:642
DBLoggerThread::stop
void stop(void)
Tell the thread to stop by setting the m_aborted flag.
Definition: loggingserver.cpp:595
LogForwardThread::handleSigHup
static void handleSigHup(void)
SIGHUP handler - reopen all open logfiles for logrollers.
Definition: loggingserver.cpp:714
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:165
LogForwardThread::run
void run(void) override
Run the log forwarding thread.
Definition: loggingserver.cpp:651
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:317
LoggerBase::LoggerBase
LoggerBase(const char *string)
LoggerBase Constructor.
Definition: loggingserver.cpp:86
DatabaseLogger::~DatabaseLogger
~DatabaseLogger() override
DatabaseLogger deconstructor.
Definition: loggingserver.cpp:336
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:635
DBLoggerThread::m_aborted
volatile bool m_aborted
Used during shutdown to indicate that the thread should stop ASAP.
Definition: loggingserver.h:193
logForwardMessage
MBASE_PUBLIC void logForwardMessage(const QList< QByteArray > &msg)
Definition: loggingserver.cpp:890
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:860
DatabaseLogger::prepare
void prepare(MSqlQuery &query)
Prepare the database query for use, and bind constant values to it.
Definition: loggingserver.cpp:454
FileLogger::~FileLogger
~FileLogger() override
FileLogger deconstructor - close the logfile.
Definition: loggingserver.cpp:116
DBLoggerThread::enqueue
bool enqueue(LoggingItem *item)
Enqueues a LoggingItem onto the queue for the thread to consume.
Definition: loggingserver.cpp:602
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:187
DBLoggerThread::m_queueMutex
QMutex m_queueMutex
Mutex for protecting the queue.
Definition: loggingserver.h:188
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:105
LogForwardThread::incomingSigHup
void incomingSigHup(void)
FileLogger::reopen
void reopen(void) override
Reopen the logfile after a SIGHUP.
Definition: loggingserver.cpp:151
LogForwardThread::logSigHup
friend void logSigHup(void)
FileLogger::create
static FileLogger * create(const QString &filename, QMutex *mutex)
Definition: loggingserver.cpp:128
DBLoggerThread::queueFull
bool queueFull(void)
Indicates when the queue is full.
Definition: loggingserver.h:181
build_compdb.filename
filename
Definition: build_compdb.py:21
LogMessage
QList< QByteArray > LogMessage
Definition: loggingserver.h:127
logForwardStart
MBASE_PUBLIC bool logForwardStart(void)
Definition: loggingserver.cpp:865
DBLoggerThread::m_queue
QQueue< LoggingItem * > * m_queue
Queue of LoggingItems to insert.
Definition: loggingserver.h:189
LogForwardThread::forwardMessage
static void forwardMessage(LogMessage *msg)
Definition: loggingserver.cpp:729
DBLoggerThread::run
void run(void) override
Start the thread.
Definition: loggingserver.cpp:531
DBLoggerThread::m_wait
QWaitCondition * m_wait
Wait condition used for waiting for the queue to not be full.
Definition: loggingserver.h:190
LoggerBase::stopDatabaseAccess
virtual void stopDatabaseAccess(void)
Stop logging to the database.
Definition: loggingserver.h:41