BsLog.cpp 2.1 KB

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