CmLog.h 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #pragma once
  2. #include "CmPrerequisitesUtil.h"
  3. #include "boost/signal.hpp"
  4. namespace CamelotEngine
  5. {
  6. /**
  7. * @brief Different log levels that log entries may be assigned.
  8. */
  9. enum LogLevel
  10. {
  11. LL_DEBUG = 0x1,
  12. LL_INFO = 0x2,
  13. LL_WARNING = 0x4,
  14. LL_ERROR = 0x8
  15. };
  16. /**
  17. * @brief A single log entry, usually used in QtLogModel as
  18. * a representation of a log entry in the Console window.
  19. */
  20. class CM_EXPORT LogEntry
  21. {
  22. public:
  23. LogEntry(const String& msg, LogLevel level);
  24. LogLevel getLevel(void) { return mLevel; }
  25. const String& getMessage(void) { return mMsg; }
  26. private:
  27. String mMsg;
  28. LogLevel mLevel;
  29. };
  30. /**
  31. * @brief Used for logging messages to a file. Can also send out callbacks to
  32. * registered classes when a message is received, so they can do with it as they wish.
  33. */
  34. class CM_EXPORT Log
  35. {
  36. public:
  37. /**
  38. * @brief Constructor.
  39. *
  40. * @param logFilePath Full pathname of the log file. Should have a .html extension.
  41. * @param autoSave (optional) Whether to save the log to disk whenever a new message
  42. * is added. (Useful when you want to make sure your log is up to
  43. * date after a crash). The log file will only be saved on exit, or
  44. * if you call save manually.
  45. * @param suppressFileOutput (optional) If true, log won't be saved to a file, even if you
  46. * call save manually.
  47. */
  48. Log(const String& logFilePath, bool autoSave = true, bool suppressFileOutput = false);
  49. ~Log();
  50. /**
  51. * @brief Logs a new message. If autoSave is enabled it will automatically save the message to
  52. * disk.
  53. *
  54. * @param message The message describing the log entry.
  55. * @param level Severity of the log entry.
  56. */
  57. void logMsg(const String& message, LogLevel level);
  58. /**
  59. * @brief Removes all log entries. If autoSave is enabled the file on disk will be cleared too.
  60. */
  61. void clear();
  62. /**
  63. * @brief Saves the log file to disk.
  64. */
  65. void save();
  66. private:
  67. std::vector<LogEntry*> mEntries;
  68. bool mAutoSave;
  69. bool mSuppressFileOutput;
  70. String mSaveFilePath;
  71. /**
  72. * @brief Called whenever a new entry is added.
  73. */
  74. void doOnEntryAdded(const LogEntry& entry);
  75. /************************************************************************/
  76. /* SIGNALS */
  77. /************************************************************************/
  78. public:
  79. /**
  80. * @brief Triggered when a new entry in the log is added.
  81. */
  82. boost::signal<void(const LogEntry&)> onEntryAdded;
  83. };
  84. }