1 | //************************************ bs::framework - Copyright 2018 Marko Pintera **************************************// |
---|---|
2 | //*********** Licensed under the MIT license. See LICENSE.md for full terms. This notice is not to be removed. ***********// |
3 | #include "Debug/BsLog.h" |
4 | #include "Error/BsException.h" |
5 | |
6 | namespace bs |
7 | { |
8 | Log::~Log() |
9 | { |
10 | clear(); |
11 | } |
12 | |
13 | void Log::logMsg(const String& message, UINT32 channel) |
14 | { |
15 | RecursiveLock lock(mMutex); |
16 | |
17 | mUnreadEntries.push(LogEntry(message, channel)); |
18 | } |
19 | |
20 | void Log::clear() |
21 | { |
22 | RecursiveLock lock(mMutex); |
23 | |
24 | mEntries.clear(); |
25 | |
26 | while (!mUnreadEntries.empty()) |
27 | mUnreadEntries.pop(); |
28 | |
29 | mHash++; |
30 | } |
31 | |
32 | void Log::clear(UINT32 channel) |
33 | { |
34 | RecursiveLock lock(mMutex); |
35 | |
36 | Vector<LogEntry> newEntries; |
37 | for(auto& entry : mEntries) |
38 | { |
39 | if (entry.getChannel() == channel) |
40 | continue; |
41 | |
42 | newEntries.push_back(entry); |
43 | } |
44 | |
45 | mEntries = newEntries; |
46 | |
47 | Queue<LogEntry> newUnreadEntries; |
48 | while (!mUnreadEntries.empty()) |
49 | { |
50 | LogEntry entry = mUnreadEntries.front(); |
51 | mUnreadEntries.pop(); |
52 | |
53 | if (entry.getChannel() == channel) |
54 | continue; |
55 | |
56 | newUnreadEntries.push(entry); |
57 | } |
58 | |
59 | mUnreadEntries = newUnreadEntries; |
60 | mHash++; |
61 | } |
62 | |
63 | bool Log::getUnreadEntry(LogEntry& entry) |
64 | { |
65 | RecursiveLock lock(mMutex); |
66 | |
67 | if (mUnreadEntries.empty()) |
68 | return false; |
69 | |
70 | entry = mUnreadEntries.front(); |
71 | mUnreadEntries.pop(); |
72 | mEntries.push_back(entry); |
73 | mHash++; |
74 | |
75 | return true; |
76 | } |
77 | |
78 | bool Log::getLastEntry(LogEntry& entry) |
79 | { |
80 | if (mEntries.size() == 0) |
81 | return false; |
82 | |
83 | entry = mEntries.back(); |
84 | return true; |
85 | } |
86 | |
87 | Vector<LogEntry> Log::getEntries() const |
88 | { |
89 | RecursiveLock lock(mMutex); |
90 | |
91 | return mEntries; |
92 | } |
93 | |
94 | Vector<LogEntry> Log::getAllEntries() const |
95 | { |
96 | Vector<LogEntry> entries; |
97 | { |
98 | RecursiveLock lock(mMutex); |
99 | |
100 | for (auto& entry : mEntries) |
101 | entries.push_back(entry); |
102 | |
103 | Queue<LogEntry> unreadEntries = mUnreadEntries; |
104 | while (!unreadEntries.empty()) |
105 | { |
106 | entries.push_back(unreadEntries.front()); |
107 | unreadEntries.pop(); |
108 | } |
109 | } |
110 | return entries; |
111 | } |
112 | } |
113 |