Torc  0.1
torcloggingimp.h
Go to the documentation of this file.
1 #ifndef TORCLOGGINGIMP_H
2 #define TORCLOGGINGIMP_H
3 
4 #define LOGLINE_MAX (2048-120)
5 
6 // Qt
7 #include <QFile>
8 #include <QWaitCondition>
9 
10 // Torc
11 #include "torcqthread.h"
12 #include "torchttpservice.h"
13 
14 class LogItem;
15 
16 void RegisterLoggingThread(void);
17 void DeregisterLoggingThread(void);
18 
19 class LoggingThread : public TorcQThread
20 {
21  Q_OBJECT
22  public:
23  LoggingThread();
25 
26  // TorcQThread
27  void run(void);
28  void Start(void);
29  void Finish(void);
30  void Stop(void);
31  bool Flush(int TimeoutMS = 200000);
32  void HandleItem(LogItem *Item);
33 
34  private:
35  QWaitCondition m_waitNotEmpty;
36  QWaitCondition m_waitEmpty;
37  bool m_aborted;
38 };
39 
41 {
42  public:
43  explicit LoggerBase(const QString &FileName);
44  virtual ~LoggerBase() = default;
45 
46  virtual bool Logmsg(LogItem *Item) = 0;
47 
48  protected:
49  virtual bool PrintLine (QByteArray &Line) = 0;
50  QByteArray GetLine(LogItem *Item);
51 
52  protected:
53  QString m_fileName;
54 
55  private:
56  Q_DISABLE_COPY(LoggerBase)
57 };
58 
59 class FileLogger : public LoggerBase
60 {
61  public:
62  FileLogger(const QString &Filename, bool ErrorsOnly, int Quiet);
63  ~FileLogger();
64 
65  bool Logmsg(LogItem *Item) override;
66 
67  protected:
68  bool PrintLine (QByteArray &Line) override;
69 
70  protected:
71  bool m_opened;
72  QFile m_file;
74  int m_quiet;
75 };
76 
77 class WebLogger : public QObject, public TorcHTTPService, public FileLogger
78 {
79  Q_OBJECT
80  Q_CLASSINFO("Version", "1.0.0")
81  Q_CLASSINFO("GetLog", "type=Log,methods=GET+AUTH")
82  Q_CLASSINFO("GetTail", "type=Logline,methods=GET+AUTH")
83  Q_PROPERTY(QByteArray log READ GetLog NOTIFY logChanged)
84  Q_PROPERTY(QByteArray tail READ GetTail NOTIFY tailChanged)
85 
86  public:
87  explicit WebLogger(const QString &Filename);
88  ~WebLogger() = default;
89 
90  bool Logmsg (LogItem *Item) override;
91 
92  public slots:
93  bool event (QEvent *event) override;
94  void SubscriberDeleted (QObject *Subscriber);
95  QByteArray GetLog (void);
96  QByteArray GetTail (void);
97 
98  signals:
99  void logChanged (void);
100  void tailChanged (void);
101 
102  private:
103  QByteArray log;
104  QByteArray tail;
105  bool changed;
106 };
107 
108 #endif // TORCLOGGINGIMP_H
bool Flush(int TimeoutMS=200000)
void Finish(void)
void RegisterLoggingThread(void)
QString m_fileName
Serves log content to registered subscribers and HTTP clients.
bool m_errorsOnly
void HandleItem(LogItem *Item)
void Start(void)
A Torc specific wrapper around QThread.
Definition: torcqthread.h:7
void DeregisterLoggingThread(void)