filelogger.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**
  2. @file
  3. @author Stefan Frings
  4. */
  5. #ifndef FILELOGGER_H
  6. #define FILELOGGER_H
  7. #include <QtGlobal>
  8. #include <QSettings>
  9. #include <QFile>
  10. #include <QMutex>
  11. #include <QBasicTimer>
  12. #include "logglobal.h"
  13. #include "logger.h"
  14. namespace stefanfrings {
  15. /**
  16. Logger that uses a text file for output. Settings are read from a
  17. config file using a QSettings object. Config settings can be changed at runtime.
  18. <p>
  19. Example for the configuration settings:
  20. <code><pre>
  21. fileName=logs/QtWebApp.log
  22. maxSize=1000000
  23. maxBackups=2
  24. bufferSize=0
  25. minLevel=WARNING
  26. msgformat={timestamp} {typeNr} {type} thread={thread}: {msg}
  27. timestampFormat=dd.MM.yyyy hh:mm:ss.zzz
  28. </pre></code>
  29. - Possible log levels are: ALL/DEBUG=0, INFO=4, WARN/WARNING=1, ERROR/CRITICAL=2, FATAL=3
  30. - fileName is the name of the log file, relative to the directory of the settings file.
  31. In case of windows, if the settings are in the registry, the path is relative to the current
  32. working directory.
  33. - maxSize is the maximum size of that file in bytes. The file will be backed up and
  34. replaced by a new file if it becomes larger than this limit. Please note that
  35. the actual file size may become a little bit larger than this limit. Default is 0=unlimited.
  36. - maxBackups defines the number of backup files to keep. Default is 0=unlimited.
  37. - bufferSize defines the size of the ring buffer. Default is 0=disabled.
  38. - minLevel If bufferSize=0: Messages with lower level are discarded.<br>
  39. If buffersize>0: Messages with lower level are buffered, messages with equal or higher
  40. level (except INFO) trigger writing the buffered messages into the file.<br>
  41. Defaults is 0=debug.
  42. - msgFormat defines the decoration of log messages, see LogMessage class. Default is "{timestamp} {type} {msg}".
  43. - timestampFormat defines the format of timestamps, see QDateTime::toString(). Default is "yyyy-MM-dd hh:mm:ss.zzz".
  44. @see set() describes how to set logger variables
  45. @see LogMessage for a description of the message decoration.
  46. @see Logger for a descrition of the buffer.
  47. */
  48. class DECLSPEC FileLogger : public Logger {
  49. Q_OBJECT
  50. Q_DISABLE_COPY(FileLogger)
  51. public:
  52. /**
  53. Constructor.
  54. @param settings Configuration settings, usually stored in an INI file. Must not be 0.
  55. Settings are read from the current group, so the caller must have called settings->beginGroup().
  56. Because the group must not change during runtime, it is recommended to provide a
  57. separate QSettings instance that is not used by other parts of the program.
  58. The FileLogger does not take over ownership of the QSettings instance, so the caller
  59. should destroy it during shutdown.
  60. @param refreshInterval Interval of checking for changed config settings in msec, or 0=disabled
  61. @param parent Parent object
  62. */
  63. FileLogger(QSettings* settings, const int refreshInterval=10000, QObject* parent = nullptr);
  64. /**
  65. Destructor. Closes the file.
  66. */
  67. virtual ~FileLogger();
  68. /** Write a message to the log file */
  69. virtual void write(const LogMessage* logMessage);
  70. protected:
  71. /**
  72. Handler for timer events.
  73. Refreshes config settings or synchronizes I/O buffer, depending on the event.
  74. This method is thread-safe.
  75. @param event used to distinguish between the two timers.
  76. */
  77. void timerEvent(QTimerEvent* event);
  78. private:
  79. /** Configured name of the log file */
  80. QString fileName;
  81. /** Configured maximum size of the file in bytes, or 0=unlimited */
  82. long maxSize;
  83. /** Configured maximum number of backup files, or 0=unlimited */
  84. int maxBackups;
  85. /** Pointer to the configuration settings */
  86. QSettings* settings;
  87. /** Output file, or 0=disabled */
  88. QFile* file;
  89. /** Timer for refreshing configuration settings */
  90. QBasicTimer refreshTimer;
  91. /** Timer for flushing the file I/O buffer */
  92. QBasicTimer flushTimer;
  93. /** Open the output file */
  94. void open();
  95. /** Close the output file */
  96. void close();
  97. /** Rotate files and delete some backups if there are too many */
  98. void rotate();
  99. /**
  100. Refreshes the configuration settings.
  101. This method is thread-safe.
  102. */
  103. void refreshSettings();
  104. };
  105. } // end of namespace
  106. #endif // FILELOGGER_H