BsLog.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #pragma once
  2. #include "BsPrerequisitesUtil.h"
  3. namespace BansheeEngine
  4. {
  5. /** @addtogroup Debug
  6. * @{
  7. */
  8. /** A single log entry, containing a message and a channel the message was recorded on. */
  9. class BS_UTILITY_EXPORT LogEntry
  10. {
  11. public:
  12. LogEntry() { }
  13. LogEntry(const String& msg, UINT32 channel);
  14. /** Channel the message was recorded on. */
  15. UINT32 getChannel() const { return mChannel; }
  16. /** Text of the message. */
  17. const String& getMessage() const { return mMsg; }
  18. private:
  19. String mMsg;
  20. UINT32 mChannel;
  21. };
  22. /**
  23. * Used for logging messages. Can categorize messages according to channels, save the log to a file
  24. * and send out callbacks when a new message is added.
  25. *
  26. * @note Thread safe.
  27. */
  28. class BS_UTILITY_EXPORT Log
  29. {
  30. public:
  31. Log();
  32. ~Log();
  33. /**
  34. * Logs a new message.
  35. *
  36. * @param[in] message The message describing the log entry.
  37. * @param[in] channel Channel in which to store the log entry.
  38. */
  39. void logMsg(const String& message, UINT32 channel);
  40. /** Removes all log entries. */
  41. void clear();
  42. /** Removes all log entries in a specific channel. */
  43. void clear(UINT32 channel);
  44. /** Returns all existing log entries. */
  45. Vector<LogEntry> getEntries() const;
  46. /**
  47. * Returns the latest unread entry from the log queue, and removes the entry from the unread entries list.
  48. *
  49. * @param[out] entry Entry that was retrieved, or undefined if no entries exist.
  50. * @return True if an unread entry was retrieved, false otherwise.
  51. */
  52. bool getUnreadEntry(LogEntry& entry);
  53. /**
  54. * Returns the last available log entry.
  55. *
  56. * @param[out] entry Entry that was retrieved, or undefined if no entries exist.
  57. * @return True if an entry was retrieved, false otherwise.
  58. */
  59. bool getLastEntry(LogEntry& entry);
  60. /**
  61. * Returns a hash value that is modified whenever entries in the log change. This can be used for
  62. * checking for changes by external systems.
  63. */
  64. UINT64 getHash() const { return mHash; }
  65. private:
  66. friend class Debug;
  67. /** Returns all log entries, including those marked as unread. */
  68. Vector<LogEntry> getAllEntries() const;
  69. Vector<LogEntry> mEntries;
  70. Queue<LogEntry> mUnreadEntries;
  71. UINT64 mHash;
  72. BS_RECURSIVE_MUTEX(mMutex);
  73. };
  74. /** @} */
  75. }