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 "mythsignalingtimer.h"
18 #include "mthread.h"
19 
20 #define LOGLINE_MAX (2048-120)
21 
22 class QString;
23 class MSqlQuery;
24 class LoggingItem;
25 
27 class LoggerBase : public QObject
28 {
29  Q_OBJECT
30 
31  public:
33  explicit LoggerBase(const char *string);
35  ~LoggerBase() override;
38  virtual bool logmsg(LoggingItem *item) = 0;
40  virtual void reopen(void) = 0;
42  virtual void stopDatabaseAccess(void) { }
43  protected:
44  char *m_handle {nullptr};
45 };
46 
48 class FileLogger : public LoggerBase
49 {
50  Q_OBJECT
51 
52  public:
53  explicit FileLogger(const char *filename);
54  ~FileLogger() override;
55  bool logmsg(LoggingItem *item) override; // LoggerBase
56  void reopen(void) override; // LoggerBase
57  static FileLogger *create(const QString& filename, QMutex *mutex);
58  private:
59  bool m_opened {false};
60  int m_fd {-1};
61 };
62 
64 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 
80 #if CONFIG_SYSTEMD_JOURNAL
81 class JournalLogger : public LoggerBase
82 {
83  Q_OBJECT
84 
85  public:
86  JournalLogger();
87  ~JournalLogger() override;
88  bool logmsg(LoggingItem *item) override; // LoggerBase
90  void reopen(void) override { }; // LoggerBase
91  static JournalLogger *create(QMutex *mutex);
92 };
93 #endif
94 
95 class DBLoggerThread;
96 
98 class DatabaseLogger : public LoggerBase
99 {
100  Q_OBJECT
101 
102  friend class DBLoggerThread;
103  public:
104  explicit DatabaseLogger(const char *table);
105  ~DatabaseLogger() override;
106  bool logmsg(LoggingItem *item) override; // LoggerBase
107  void reopen(void) override { }; // LoggerBase
108  void stopDatabaseAccess(void) override; // LoggerBase
109  static DatabaseLogger *create(const QString& table, QMutex *mutex);
110  protected:
111  bool logqmsg(MSqlQuery &query, LoggingItem *item);
112  void prepare(MSqlQuery &query);
113  private:
114  bool isDatabaseReady(void);
115  static bool tableExists(const QString &table);
116 
118  QString m_query;
119  bool m_opened {true};
120  bool m_loggingTableExists {false};
121  QElapsedTimer m_disabledTime;
122  QElapsedTimer m_errorLoggingTime;
123  static const int kMinDisabledTime;
124 };
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:437
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:122
DatabaseLogger::reopen
void reopen(void) override
Reopen the log file to facilitate log rolling.
Definition: loggingserver.h:107
FileLogger::m_opened
bool m_opened
true when the logfile is opened
Definition: loggingserver.h:59
MAX_QUEUE_LEN
#define MAX_QUEUE_LEN
Definition: loggingserver.h:158
DBLoggerThread::~DBLoggerThread
~DBLoggerThread() override
DBLoggerThread deconstructor.
Definition: loggingserver.cpp:537
LogMessageList
QList< LogMessage * > LogMessageList
Definition: loggingserver.h:128
LoggerBase
Base class for the various logging mechanisms.
Definition: loggingserver.h:27
SyslogLogger::logmsg
bool logmsg(LoggingItem *item) override
Process a log message for the logger instance.
DatabaseLogger::create
static DatabaseLogger * create(const QString &table, QMutex *mutex)
Definition: loggingserver.cpp:364
DatabaseLogger::m_thread
DBLoggerThread * m_thread
The database queue handling thread.
Definition: loggingserver.h:117
mythbaseexp.h
FileLogger::logmsg
bool logmsg(LoggingItem *item) override
Process a log message, writing to the logfile.
Definition: loggingserver.cpp:177
DatabaseLogger::tableExists
static bool tableExists(const QString &table)
Checks whether table exists and is ready for writing.
Definition: loggingserver.cpp:503
DatabaseLogger::logmsg
bool logmsg(LoggingItem *item) override
Process a log message, queuing it for logging to the database.
Definition: loggingserver.cpp:399
slots
unsigned int slots[4]
Definition: element.cpp:38
verbosedefs.h
LoggerBase::~LoggerBase
~LoggerBase() override
LoggerBase Deconstructor.
Definition: loggingserver.cpp:108
DBLoggerThread::DBLoggerThread
DBLoggerThread(DatabaseLogger *logger)
DBLoggerThread constructor.
Definition: loggingserver.cpp:528
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:386
SyslogLogger::reopen
void reopen(void) override
Unused for this logger.
Definition: loggingserver.h:74
SyslogLogger::m_opened
bool m_opened
true when syslog channel open.
Definition: loggingserver.h:77
LoggingItem
The logging items that are generated by LOG() and are sent to the console.
Definition: logging.h:61
DatabaseLogger::isDatabaseReady
bool isDatabaseReady(void)
Check if the database is ready for use.
Definition: loggingserver.cpp:483
MBASE_PUBLIC
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
logger
static void logger(cdio_log_level_t level, const char message[])
Definition: cddecoder.cpp:37
logForwardStop
MBASE_PUBLIC void logForwardStop(void)
Definition: loggingserver.cpp:894
DatabaseLogger::m_loggingTableExists
bool m_loggingTableExists
The desired logging table exists.
Definition: loggingserver.h:120
DatabaseLogger::m_query
QString m_query
The database query to insert log messages.
Definition: loggingserver.h:118
LogForwardThread
The logging thread that forwards received messages to the consuming loggers via ZeroMQ.
Definition: loggingserver.h:132
DatabaseLogger::kMinDisabledTime
static const int kMinDisabledTime
Minimum time to disable DB logging (in ms)
Definition: loggingserver.h:123
LogForwardThread::~LogForwardThread
~LogForwardThread() override
LogForwardThread destructor.
Definition: loggingserver.cpp:663
DBLoggerThread::stop
void stop(void)
Tell the thread to stop by setting the m_aborted flag.
Definition: loggingserver.cpp:616
LogForwardThread::handleSigHup
static void handleSigHup(void)
SIGHUP handler - reopen all open logfiles for logrollers.
Definition: loggingserver.cpp:734
DatabaseLogger::m_opened
bool m_opened
The database is opened.
Definition: loggingserver.h:119
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:672
DatabaseLogger::DatabaseLogger
DatabaseLogger(const char *table)
DatabaseLogger constructor.
Definition: loggingserver.cpp:338
filename
QString filename
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:634
LoggerBase::LoggerBase
LoggerBase(const char *string)
LoggerBase Constructor.
Definition: loggingserver.cpp:90
DatabaseLogger::~DatabaseLogger
~DatabaseLogger() override
DatabaseLogger deconstructor.
Definition: loggingserver.cpp:357
DBLoggerThread::operator=
DBLoggerThread & operator=(const DBLoggerThread &)=delete
FileLogger::m_fd
int m_fd
contains the file descriptor for the logfile
Definition: loggingserver.h:60
LogForwardThread::LogForwardThread
LogForwardThread()
LogForwardThread constructor.
Definition: loggingserver.cpp:656
SyslogLogger::SyslogLogger
SyslogLogger()
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:910
DatabaseLogger
Database logger - logs to the MythTV database.
Definition: loggingserver.h:98
DatabaseLogger::m_disabledTime
QElapsedTimer m_disabledTime
Elapsed time since the DB logging was disabled.
Definition: loggingserver.h:121
FileLogger
File-based logger - used for logfiles and console.
Definition: loggingserver.h:48
LogForwardThread::stop
void stop(void)
Stop the thread by setting the abort flag.
Definition: loggingserver.cpp:880
DatabaseLogger::prepare
void prepare(MSqlQuery &query)
Prepare the database query for use, and bind constant values to it.
Definition: loggingserver.cpp:475
FileLogger::~FileLogger
~FileLogger() override
FileLogger deconstructor - close the logfile.
Definition: loggingserver.cpp:131
SyslogLogger::create
static SyslogLogger * create(QMutex *mutex, bool open=true)
DBLoggerThread::enqueue
bool enqueue(LoggingItem *item)
Enqueues a LoggingItem onto the queue for the thread to consume.
Definition: loggingserver.cpp:623
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
mythsignalingtimer.h
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.
SyslogLogger::~SyslogLogger
~SyslogLogger() override
FileLogger::FileLogger
FileLogger(const char *filename)
FileLogger constructor.
Definition: loggingserver.cpp:120
LoggerBase::m_handle
char * m_handle
semi-opaque handle for identifying instance
Definition: loggingserver.h:44
LogForwardThread::incomingSigHup
void incomingSigHup(void)
FileLogger::reopen
void reopen(void) override
Reopen the logfile after a SIGHUP.
Definition: loggingserver.cpp:166
LogForwardThread::logSigHup
friend void logSigHup(void)
FileLogger::create
static FileLogger * create(const QString &filename, QMutex *mutex)
Definition: loggingserver.cpp:143
DBLoggerThread::queueFull
bool queueFull(void)
Indicates when the queue is full.
Definition: loggingserver.h:181
LogMessage
QList< QByteArray > LogMessage
Definition: loggingserver.h:127
SyslogLogger
Syslog-based logger (not available in Windows)
Definition: loggingserver.h:64
logForwardStart
MBASE_PUBLIC bool logForwardStart(void)
Definition: loggingserver.cpp:885
query
MSqlQuery query(MSqlQuery::InitCon())
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:749
DBLoggerThread::run
void run(void) override
Start the thread.
Definition: loggingserver.cpp:552
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:42