filelogger.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /**
  2. @file
  3. @author Stefan Frings
  4. */
  5. #include "filelogger.h"
  6. #include <QTime>
  7. #include <QStringList>
  8. #include <QThread>
  9. #include <QtGlobal>
  10. #include <QFile>
  11. #include <QTimerEvent>
  12. #include <QDir>
  13. #include <QFileInfo>
  14. #include <stdio.h>
  15. using namespace stefanfrings;
  16. void FileLogger::refreshSettings()
  17. {
  18. mutex.lock();
  19. // Save old file name for later comparision with new settings
  20. QString oldFileName=fileName;
  21. // Load new config settings
  22. settings->sync();
  23. fileName=settings->value("fileName").toString();
  24. // Convert relative fileName to absolute, based on the directory of the config file.
  25. #ifdef Q_OS_WIN32
  26. if (QDir::isRelativePath(fileName) && settings->format()!=QSettings::NativeFormat)
  27. #else
  28. if (QDir::isRelativePath(fileName))
  29. #endif
  30. {
  31. QFileInfo configFile(settings->fileName());
  32. fileName=QFileInfo(configFile.absolutePath(),fileName).absoluteFilePath();
  33. }
  34. maxSize=settings->value("maxSize",0).toLongLong();
  35. maxBackups=settings->value("maxBackups",0).toInt();
  36. msgFormat=settings->value("msgFormat","{timestamp} {type} {msg}").toString();
  37. timestampFormat=settings->value("timestampFormat","yyyy-MM-dd hh:mm:ss.zzz").toString();
  38. bufferSize=settings->value("bufferSize",0).toInt();
  39. // Translate log level settings to enumeration value
  40. QByteArray minLevelStr = settings->value("minLevel","ALL").toByteArray();
  41. if (minLevelStr=="ALL" || minLevelStr=="DEBUG" || minLevelStr=="0")
  42. {
  43. minLevel=QtMsgType::QtDebugMsg;
  44. }
  45. else if (minLevelStr=="WARNING" || minLevelStr=="WARN" || minLevelStr=="1")
  46. {
  47. minLevel=QtMsgType::QtWarningMsg;
  48. }
  49. else if (minLevelStr=="ERROR" || minLevelStr=="CRITICAL" || minLevelStr=="2")
  50. {
  51. minLevel=QtMsgType::QtCriticalMsg;
  52. }
  53. else if (minLevelStr=="FATAL" || minLevelStr=="3")
  54. {
  55. minLevel=QtMsgType::QtFatalMsg;
  56. }
  57. else if (minLevelStr=="INFO" || minLevelStr=="4")
  58. {
  59. minLevel=QtMsgType::QtInfoMsg;
  60. }
  61. // Create new file if the filename has been changed
  62. if (oldFileName!=fileName)
  63. {
  64. fprintf(stderr,"Logging to %s\n",qPrintable(fileName));
  65. close();
  66. open();
  67. }
  68. mutex.unlock();
  69. }
  70. FileLogger::FileLogger(QSettings *settings, const int refreshInterval, QObject* parent)
  71. : Logger(parent)
  72. {
  73. Q_ASSERT(settings!=nullptr);
  74. Q_ASSERT(refreshInterval>=0);
  75. this->settings=settings;
  76. file=nullptr;
  77. if (refreshInterval>0)
  78. {
  79. refreshTimer.start(refreshInterval,this);
  80. }
  81. flushTimer.start(1000,this);
  82. refreshSettings();
  83. }
  84. FileLogger::~FileLogger()
  85. {
  86. close();
  87. }
  88. void FileLogger::write(const LogMessage* logMessage)
  89. {
  90. // Try to write to the file
  91. if (file)
  92. {
  93. // Write the message
  94. file->write(qPrintable(logMessage->toString(msgFormat,timestampFormat)));
  95. // Flush error messages immediately, to ensure that no important message
  96. // gets lost when the program terinates abnormally.
  97. if (logMessage->getType()>=QtCriticalMsg)
  98. {
  99. file->flush();
  100. }
  101. // Check for success
  102. if (file->error())
  103. {
  104. qWarning("Cannot write to log file %s: %s",qPrintable(fileName),qPrintable(file->errorString()));
  105. close();
  106. }
  107. }
  108. // Fall-back to the super class method, if writing failed
  109. if (!file)
  110. {
  111. Logger::write(logMessage);
  112. }
  113. }
  114. void FileLogger::open()
  115. {
  116. if (fileName.isEmpty())
  117. {
  118. qWarning("Name of logFile is empty");
  119. }
  120. else {
  121. file=new QFile(fileName);
  122. if (!file->open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text))
  123. {
  124. qWarning("Cannot open log file %s: %s",qPrintable(fileName),qPrintable(file->errorString()));
  125. file=nullptr;
  126. }
  127. }
  128. }
  129. void FileLogger::close()
  130. {
  131. if (file)
  132. {
  133. file->close();
  134. delete file;
  135. file=nullptr;
  136. }
  137. }
  138. void FileLogger::rotate() {
  139. // count current number of existing backup files
  140. int count=0;
  141. forever
  142. {
  143. QFile bakFile(QString("%1.%2").arg(fileName).arg(count+1));
  144. if (bakFile.exists())
  145. {
  146. ++count;
  147. }
  148. else
  149. {
  150. break;
  151. }
  152. }
  153. // Remove all old backup files that exceed the maximum number
  154. while (maxBackups>0 && count>=maxBackups)
  155. {
  156. QFile::remove(QString("%1.%2").arg(fileName).arg(count));
  157. --count;
  158. }
  159. // Rotate backup files
  160. for (int i=count; i>0; --i) {
  161. QFile::rename(QString("%1.%2").arg(fileName).arg(i),QString("%1.%2").arg(fileName).arg(i+1));
  162. }
  163. // Backup the current logfile
  164. QFile::rename(fileName,fileName+".1");
  165. }
  166. void FileLogger::timerEvent(QTimerEvent* event)
  167. {
  168. if (!event)
  169. {
  170. return;
  171. }
  172. else if (event->timerId()==refreshTimer.timerId())
  173. {
  174. refreshSettings();
  175. }
  176. else if (event->timerId()==flushTimer.timerId() && file)
  177. {
  178. mutex.lock();
  179. // Flush the I/O buffer
  180. file->flush();
  181. // Rotate the file if it is too large
  182. if (maxSize>0 && file->size()>=maxSize)
  183. {
  184. close();
  185. rotate();
  186. open();
  187. }
  188. mutex.unlock();
  189. }
  190. }