alog.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #include "alog.h"
  2. #include <QMessageBox>
  3. namespace ALog {
  4. static bool logDebug = false;
  5. /*!
  6. * \brief setLogFileName sets a log file name ("Log_<Date>_<Time>.txt")
  7. */
  8. void setLogFileName()
  9. {
  10. logFileName = QString(logFolder.absolutePath() + QLatin1String("/Log_%1_%2.txt")
  11. ).arg(QDate::currentDate().toString(QStringLiteral("yyyy_MM_dd")),
  12. QTime::currentTime().toString(QStringLiteral("hh_mm_ss")));
  13. }
  14. /*!
  15. * \brief Cleans up old logs and initializes logging by preparing and installing a QMessageHandler
  16. *
  17. */
  18. void deleteOldLogs()
  19. {
  20. logFolder.setSorting(QDir::Time | QDir::Reversed);
  21. QFileInfoList logs_list = logFolder.entryInfoList();
  22. if (logs_list.size() <= numberOfLogs) {
  23. return;
  24. } else {
  25. for (int i = 0; i < (logs_list.size() - numberOfLogs); i++) {
  26. const QString path = logs_list.at(i).absoluteFilePath();
  27. QFile file(path);
  28. file.remove();
  29. }
  30. }
  31. }
  32. /*!
  33. * \brief initialise logging, clean up logfiles and install a QMessageHandler. To enable
  34. * logging of debug messages, pass parameter as true.
  35. */
  36. bool init(bool log_debug)
  37. {
  38. logDebug = log_debug;
  39. logFolder = AStandardPaths::directory(AStandardPaths::Log);
  40. deleteOldLogs();
  41. setLogFileName();
  42. QFile log_file((logFileName));
  43. if(log_file.open(QIODevice::WriteOnly | QIODevice::Append)) {
  44. qInstallMessageHandler(ALog::aMessageHandler);
  45. return true;
  46. } else {
  47. return false;
  48. }
  49. }
  50. /*!
  51. * \brief aMessageHandler Intercepts Messages and prints to console and log file
  52. *
  53. * \abstract The message handler is responsible for intercepting the output from
  54. * qDebug(), qInfo(), qWarning() and qCritical(), formatting them and printing them
  55. * to the standard console out and to a logfile using QTextStream. Debug messages are
  56. * not written to the log file.
  57. *
  58. */
  59. void aMessageHandler(QtMsgType type, const QMessageLogContext &context,
  60. const QString& msg)
  61. {
  62. const char *function = context.function ? context.function : "";
  63. //check file size and if needed create new log!
  64. {
  65. QFile outFileCheck(logFileName);
  66. int size = outFileCheck.size();
  67. if (size > sizeOfLogs) {
  68. deleteOldLogs();
  69. setLogFileName();
  70. }
  71. }
  72. // open the log file and prepare a textstream to write to it
  73. QFile log_file(logFileName);
  74. log_file.open(QIODevice::WriteOnly | QIODevice::Append);
  75. QTextStream log_stream(&log_file);
  76. switch (type) {
  77. case QtDebugMsg:
  78. QTextStream(stdout) << DEB_HEADER_CONSOLE << msg << "\n\t" << function << "\033[m" << endl;
  79. if(logDebug)
  80. log_stream << timeNow() << DEB_HEADER << msg << "\t\t" << function << endl;
  81. break;
  82. case QtInfoMsg:
  83. log_stream << timeNow() << INFO_HEADER << msg.chopped(2) << "\t\t" << function << endl;
  84. QTextStream(stdout) << INFO_HEADER_CONSOLE << msg;
  85. break;
  86. case QtWarningMsg:
  87. log_stream << timeNow() << WARN_HEADER << msg.chopped(2) << "\t\t" << endl;
  88. QTextStream(stdout) << WARN_HEADER_CONSOLE << msg << endl;
  89. break;
  90. case QtCriticalMsg:
  91. log_stream << timeNow() << CRIT_HEADER << msg.chopped(2) << "\t\t" << endl;
  92. QTextStream(stdout) << CRIT_HEADER_CONSOLE << msg << endl;
  93. break;
  94. default:
  95. log_stream << QTime::currentTime().toString(Qt::ISODate) << INFO_HEADER << msg << function << endl;
  96. QTextStream(stdout) << INFO_HEADER_CONSOLE << msg << endl;
  97. break;
  98. }
  99. }
  100. } // namespace ALog