00001 #ifndef LOGGING_H_ 00002 #define LOGGING_H_ 00003 00004 #include <QMutexLocker> 00005 #include <QMutex> 00006 #include <QQueue> 00007 #include <QTime> 00008 00009 #include <stdint.h> 00010 #include <time.h> 00011 #include <unistd.h> 00012 00013 #include "mythbaseexp.h" // MBASE_PUBLIC , etc. 00014 #include "verbosedefs.h" 00015 #include "mthread.h" 00016 00017 #define LOGLINE_MAX (2048-120) 00018 00019 class QString; 00020 class MSqlQuery; 00021 class LoggingItem; 00022 00023 void loggingRegisterThread(const QString &name); 00024 void loggingDeregisterThread(void); 00025 00028 typedef union { 00029 char *string; 00030 int number; 00031 } LoggerHandle_t; 00032 00034 class LoggerBase : public QObject { 00035 Q_OBJECT 00036 00037 public: 00039 LoggerBase(char *string, int number); 00041 virtual ~LoggerBase(); 00044 virtual bool logmsg(LoggingItem *item) = 0; 00046 virtual void reopen(void) = 0; 00048 virtual void stopDatabaseAccess(void) { } 00049 protected: 00050 LoggerHandle_t m_handle; 00051 bool m_string; 00052 }; 00053 00055 class FileLogger : public LoggerBase { 00056 public: 00057 FileLogger(char *filename, bool progress, int quiet); 00058 ~FileLogger(); 00059 bool logmsg(LoggingItem *item); 00060 void reopen(void); 00061 private: 00062 bool m_opened; 00063 int m_fd; 00064 bool m_progress; 00065 int m_quiet; 00066 }; 00067 00068 #ifndef _WIN32 00069 00070 class SyslogLogger : public LoggerBase { 00071 public: 00072 SyslogLogger(int facility); 00073 ~SyslogLogger(); 00074 bool logmsg(LoggingItem *item); 00076 void reopen(void) { }; 00077 private: 00078 char *m_application; 00079 bool m_opened; 00080 }; 00081 #endif 00082 00083 class DBLoggerThread; 00084 00086 class DatabaseLogger : public LoggerBase { 00087 friend class DBLoggerThread; 00088 public: 00089 DatabaseLogger(char *table); 00090 ~DatabaseLogger(); 00091 bool logmsg(LoggingItem *item); 00092 void reopen(void) { }; 00093 virtual void stopDatabaseAccess(void); 00094 protected: 00095 bool logqmsg(MSqlQuery &query, LoggingItem *item); 00096 void prepare(MSqlQuery &query); 00097 private: 00098 bool isDatabaseReady(void); 00099 bool tableExists(const QString &table); 00100 00101 DBLoggerThread *m_thread; 00102 QString m_query; 00103 bool m_opened; 00104 bool m_loggingTableExists; 00105 bool m_disabled; 00106 QTime m_disabledTime; 00107 QTime m_errorLoggingTime; 00108 static const int kMinDisabledTime; 00109 00110 }; 00111 00112 class QWaitCondition; 00113 00116 class LoggerThread : public MThread 00117 { 00118 public: 00119 LoggerThread(); 00120 ~LoggerThread(); 00121 void run(void); 00122 void stop(void); 00123 bool flush(int timeoutMS = 200000); 00124 void handleItem(LoggingItem *item); 00125 private: 00126 QWaitCondition *m_waitNotEmpty; 00127 00128 00129 QWaitCondition *m_waitEmpty; 00130 00131 00132 bool aborted; 00133 00134 }; 00135 00136 #define MAX_QUEUE_LEN 1000 00137 00143 class DBLoggerThread : public MThread 00144 { 00145 public: 00146 DBLoggerThread(DatabaseLogger *logger); 00147 ~DBLoggerThread(); 00148 void run(void); 00149 void stop(void); 00152 bool enqueue(LoggingItem *item) 00153 { 00154 QMutexLocker qLock(&m_queueMutex); 00155 if (!aborted) 00156 m_queue->enqueue(item); 00157 return true; 00158 } 00159 00162 bool queueFull(void) 00163 { 00164 QMutexLocker qLock(&m_queueMutex); 00165 return (m_queue->size() >= MAX_QUEUE_LEN); 00166 } 00167 private: 00168 DatabaseLogger *m_logger; 00169 QMutex m_queueMutex; 00170 QQueue<LoggingItem *> *m_queue; 00171 QWaitCondition *m_wait; 00172 00173 00174 volatile bool aborted; 00175 00176 00177 }; 00178 00179 #endif 00180 00181 /* 00182 * vim:ts=4:sw=4:ai:et:si:sts=4 00183 */
1.6.3