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 <QTime>
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  virtual ~LoggerBase();
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();
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();
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();
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();
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  bool tableExists(const QString &table);
116 
118  QString m_query;
119  bool m_opened {true};
120  bool m_loggingTableExists {false};
121  bool m_disabled {false};
124  static const int kMinDisabledTime;
125 };
127 
128 typedef QList<QByteArray> LogMessage;
129 typedef QList<LogMessage *> LogMessageList;
130 
133 class LogForwardThread : public QObject, public MThread
134 {
135  Q_OBJECT
136 
137  friend void logSigHup(void);
138  public:
141  void run(void) override; // MThread
142  void stop(void);
143  private:
144  bool m_aborted {false};
145 
146  void forwardMessage(LogMessage *msg);
147  signals:
148  void incomingSigHup(void);
149  protected slots:
150  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();
171  void run(void) override; // MThread
172  void stop(void);
175  bool enqueue(LoggingItem *item);
176 
179  bool queueFull(void)
180  {
181  QMutexLocker qLock(&m_queueMutex);
182  return (m_queue->size() >= MAX_QUEUE_LEN);
183  }
184  private:
185  DBLoggerThread(const DBLoggerThread &) = delete; // not copyable
186  DBLoggerThread &operator=(const DBLoggerThread &) = delete; // not copyable
187 
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  */
bool m_opened
The database is opened.
bool m_loggingTableExists
The desired logging table exists.
Thread that manages the queueing of logging inserts for the database.
This is a wrapper around QThread that does several additional things.
Definition: mthread.h:46
bool m_aborted
Flag to abort the thread.
unsigned int slots[4]
Definition: element.c:38
Syslog-based logger (not available in Windows)
Definition: loggingserver.h:64
QWaitCondition * m_wait
Wait condition used for waiting for the queue to not be full.
~DatabaseLogger()
DatabaseLogger deconstructor.
void run(void) override
Start the thread.
Base class for the various logging mechanisms.
Definition: loggingserver.h:27
bool queueFull(void)
Indicates when the queue is full.
DatabaseLogger(const char *table)
DatabaseLogger constructor.
QString m_query
The database query to insert log messages.
int m_fd
contains the file descriptor for the logfile
Definition: loggingserver.h:60
void forwardMessage(LogMessage *msg)
~LogForwardThread()
LogForwardThread destructor.
QList< QByteArray > LogMessage
QSqlQuery wrapper that fetches a DB connection from the connection pool.
Definition: mythdbcon.h:125
virtual bool logmsg(LoggingItem *item)=0
Process a log message for the logger instance.
#define MAX_QUEUE_LEN
DBLoggerThread(DatabaseLogger *logger)
DBLoggerThread constructor.
void reopen(void) override
Reopen the logfile after a SIGHUP.
Database logger - logs to the MythTV database.
Definition: loggingserver.h:98
QTime m_errorLoggingTime
Time when DB error logging was last done.
The logging items that are generated by LOG() and are sent to the console.
Definition: logging.h:62
LoggerBase(const char *string)
LoggerBase Constructor.
QList< LogMessage * > LogMessageList
void reopen(void) override
Reopen the log file to facilitate log rolling.
void incomingSigHup(void)
friend void logSigHup(void)
The logging thread that forwards received messages to the consuming loggers via ZeroMQ.
MBASE_PUBLIC bool logForwardStart(void)
void stop(void)
Stop the thread by setting the abort flag.
#define MBASE_PUBLIC
Definition: mythbaseexp.h:15
FileLogger(const char *filename)
FileLogger constructor.
void reopen(void) override
Unused for this logger.
Definition: loggingserver.h:74
static FileLogger * create(const QString &filename, QMutex *mutex)
bool isDatabaseReady(void)
Check if the database is ready for use.
bool tableExists(const QString &table)
Checks whether table exists and is ready for writing.
bool logmsg(LoggingItem *item) override
Process a log message for the logger instance.
bool logmsg(LoggingItem *item) override
Process a log message, queuing it for logging to the database.
void prepare(MSqlQuery &query)
Prepare the database query for use, and bind constant values to it.
static DatabaseLogger * create(const QString &table, QMutex *mutex)
bool m_opened
true when the logfile is opened
Definition: loggingserver.h:59
void stop(void)
Tell the thread to stop by setting the m_aborted flag.
virtual ~LoggerBase()
LoggerBase Deconstructor.
File-based logger - used for logfiles and console.
Definition: loggingserver.h:48
static const int kMinDisabledTime
Minimum time to disable DB logging (in ms)
bool logmsg(LoggingItem *item) override
Process a log message, writing to the logfile.
~DBLoggerThread()
DBLoggerThread deconstructor.
void handleSigHup(void)
SIGHUP handler - reopen all open logfiles for logrollers.
bool m_disabled
DB logging is temporarily disabled.
void stopDatabaseAccess(void) override
Stop logging to the database and wait for the thread to stop.
bool enqueue(LoggingItem *item)
Enqueues a LoggingItem onto the queue for the thread to consume.
bool logqmsg(MSqlQuery &query, LoggingItem *item)
Actually insert a log message from the queue into the database.
void run(void) override
Run the log forwarding thread.
QTime m_disabledTime
Time when the DB logging was disabled.
char * m_handle
semi-opaque handle for identifying instance
Definition: loggingserver.h:44
DBLoggerThread * m_thread
The database queue handling thread.
DatabaseLogger * m_logger
The associated logger instance.
volatile bool m_aborted
Used during shutdown to indicate that the thread should stop ASAP.
QMutex m_queueMutex
Mutex for protecting the queue.
QQueue< LoggingItem * > * m_queue
Queue of LoggingItems to insert.
DBLoggerThread & operator=(const DBLoggerThread &)=delete
static void logger(cdio_log_level_t level, const char message[])
Definition: cddecoder.cpp:37
static SyslogLogger * create(QMutex *mutex, bool open=true)
virtual void stopDatabaseAccess(void)
Stop logging to the database.
Definition: loggingserver.h:42
bool m_opened
true when syslog channel open.
Definition: loggingserver.h:77
~FileLogger()
FileLogger deconstructor - close the logfile.
virtual void reopen(void)=0
Reopen the log file to facilitate log rolling.
MBASE_PUBLIC void logForwardStop(void)
MBASE_PUBLIC void logForwardMessage(const QList< QByteArray > &msg)
LogForwardThread()
LogForwardThread constructor.