BsLog.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. #include "BsLog.h"
  2. #include "BsException.h"
  3. namespace BansheeEngine
  4. {
  5. LogEntry::LogEntry(const String& msg, UINT32 channel)
  6. :mMsg(msg), mChannel(channel)
  7. { }
  8. Log::Log()
  9. :mHash(0)
  10. {
  11. }
  12. Log::~Log()
  13. {
  14. clear();
  15. }
  16. void Log::logMsg(const String& message, UINT32 channel)
  17. {
  18. BS_LOCK_RECURSIVE_MUTEX(mMutex);
  19. mUnreadEntries.push(LogEntry(message, channel));
  20. }
  21. void Log::clear()
  22. {
  23. BS_LOCK_RECURSIVE_MUTEX(mMutex);
  24. mEntries.clear();
  25. while (!mUnreadEntries.empty())
  26. mUnreadEntries.pop();
  27. mHash++;
  28. }
  29. void Log::clear(UINT32 channel)
  30. {
  31. BS_LOCK_RECURSIVE_MUTEX(mMutex);
  32. Vector<LogEntry> newEntries;
  33. for(auto& entry : mEntries)
  34. {
  35. if (entry.getChannel() == channel)
  36. continue;
  37. newEntries.push_back(entry);
  38. }
  39. mEntries = newEntries;
  40. Queue<LogEntry> newUnreadEntries;
  41. while (!mUnreadEntries.empty())
  42. {
  43. LogEntry entry = mUnreadEntries.front();
  44. mUnreadEntries.pop();
  45. if (entry.getChannel() == channel)
  46. continue;
  47. newUnreadEntries.push(entry);
  48. }
  49. mUnreadEntries = newUnreadEntries;
  50. mHash++;
  51. }
  52. bool Log::getUnreadEntry(LogEntry& entry)
  53. {
  54. BS_LOCK_RECURSIVE_MUTEX(mMutex);
  55. if (mUnreadEntries.empty())
  56. return false;
  57. entry = mUnreadEntries.front();
  58. mUnreadEntries.pop();
  59. mEntries.push_back(entry);
  60. mHash++;
  61. return true;
  62. }
  63. bool Log::getLastEntry(LogEntry& entry)
  64. {
  65. if (mEntries.size() == 0)
  66. return false;
  67. entry = mEntries.back();
  68. return true;
  69. }
  70. Vector<LogEntry> Log::getEntries() const
  71. {
  72. BS_LOCK_RECURSIVE_MUTEX(mMutex);
  73. return mEntries;
  74. }
  75. Vector<LogEntry> Log::getAllEntries() const
  76. {
  77. Vector<LogEntry> entries;
  78. {
  79. BS_LOCK_RECURSIVE_MUTEX(mMutex);
  80. for (auto& entry : mEntries)
  81. entries.push_back(entry);
  82. Queue<LogEntry> unreadEntries = mUnreadEntries;
  83. while (!unreadEntries.empty())
  84. {
  85. entries.push_back(unreadEntries.front());
  86. unreadEntries.pop();
  87. }
  88. }
  89. return entries;
  90. }
  91. }