CmLog.h 2.5 KB

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