logmessage.h 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /**
  2. @file
  3. @author Stefan Frings
  4. */
  5. #ifndef LOGMESSAGE_H
  6. #define LOGMESSAGE_H
  7. #include <QtGlobal>
  8. #include <QDateTime>
  9. #include <QHash>
  10. #include "logglobal.h"
  11. namespace stefanfrings {
  12. /**
  13. Represents a single log message together with some data
  14. that are used to decorate the log message.
  15. The following variables may be used in the message and in msgFormat:
  16. - {timestamp} Date and time of creation
  17. - {typeNr} Type of the message in numeric format (0-3)
  18. - {type} Type of the message in string format (DEBUG, WARNING, CRITICAL, FATAL)
  19. - {thread} ID number of the thread
  20. - {msg} Message text
  21. - {xxx} For any user-defined logger variable
  22. Plus some new variables since QT 5.0, only filled when compiled in debug mode:
  23. - {file} Filename where the message was generated
  24. - {function} Function where the message was generated
  25. - {line} Line number where the message was generated
  26. */
  27. class DECLSPEC LogMessage
  28. {
  29. Q_DISABLE_COPY(LogMessage)
  30. public:
  31. /**
  32. Constructor. All parameters are copied, so that later changes to them do not
  33. affect this object.
  34. @param type Type of the message
  35. @param message Message text
  36. @param logVars Logger variables, 0 is allowed
  37. @param file Name of the source file where the message was generated
  38. @param function Name of the function where the message was generated
  39. @param line Line Number of the source file, where the message was generated
  40. */
  41. LogMessage(const QtMsgType type, const QString& message, const QHash<QString,QString>* logVars,
  42. const QString &file, const QString &function, const int line);
  43. /**
  44. Returns the log message as decorated string.
  45. @param msgFormat Format of the decoration. May contain variables and static text,
  46. e.g. "{timestamp} {type} thread={thread}: {msg}".
  47. @param timestampFormat Format of timestamp, e.g. "dd.MM.yyyy hh:mm:ss.zzz", see QDateTime::toString().
  48. @see QDatetime for a description of the timestamp format pattern
  49. */
  50. QString toString(const QString& msgFormat, const QString& timestampFormat) const;
  51. /**
  52. Get the message type.
  53. */
  54. QtMsgType getType() const;
  55. private:
  56. /** Logger variables */
  57. QHash<QString,QString> logVars;
  58. /** Date and time of creation */
  59. QDateTime timestamp;
  60. /** Type of the message */
  61. QtMsgType type;
  62. /** ID number of the thread */
  63. Qt::HANDLE threadId;
  64. /** Message text */
  65. QString message;
  66. /** Filename where the message was generated */
  67. QString file;
  68. /** Function name where the message was generated */
  69. QString function;
  70. /** Line number where the message was generated */
  71. int line;
  72. };
  73. } // end of namespace
  74. #endif // LOGMESSAGE_H