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 | #pragma once |
4 | |
5 | #include "Prerequisites/BsPrerequisitesUtil.h" |
6 | #include "Utility/BsTime.h" |
7 | |
8 | namespace bs |
9 | { |
10 | /** @addtogroup Debug |
11 | * @{ |
12 | */ |
13 | |
14 | /** A single log entry, containing a message and a channel the message was recorded on. */ |
15 | class BS_UTILITY_EXPORT LogEntry |
16 | { |
17 | public: |
18 | LogEntry() = default; |
19 | LogEntry(String msg, UINT32 channel) |
20 | :mMsg(std::move(msg)), mChannel(channel), mLocalTime(gTime().getCurrentTimeString(false)) |
21 | { } |
22 | |
23 | /** Channel the message was recorded on. */ |
24 | UINT32 getChannel() const { return mChannel; } |
25 | |
26 | /** Text of the message. */ |
27 | const String& getMessage() const { return mMsg; } |
28 | |
29 | /** Local time of message being registered as a text */ |
30 | const String& getLocalTime() const { return mLocalTime; } |
31 | |
32 | private: |
33 | String mMsg; |
34 | UINT32 mChannel; |
35 | String mLocalTime; |
36 | }; |
37 | |
38 | /** |
39 | * Used for logging messages. Can categorize messages according to channels, save the log to a file |
40 | * and send out callbacks when a new message is added. |
41 | * |
42 | * @note Thread safe. |
43 | */ |
44 | class BS_UTILITY_EXPORT Log |
45 | { |
46 | public: |
47 | Log() = default; |
48 | ~Log(); |
49 | |
50 | /** |
51 | * Logs a new message. |
52 | * |
53 | * @param[in] message The message describing the log entry. |
54 | * @param[in] channel Channel in which to store the log entry. |
55 | */ |
56 | void logMsg(const String& message, UINT32 channel); |
57 | |
58 | /** Removes all log entries. */ |
59 | void clear(); |
60 | |
61 | /** Removes all log entries in a specific channel. */ |
62 | void clear(UINT32 channel); |
63 | |
64 | /** Returns all existing log entries. */ |
65 | Vector<LogEntry> getEntries() const; |
66 | |
67 | /** |
68 | * Returns the latest unread entry from the log queue, and removes the entry from the unread entries list. |
69 | * |
70 | * @param[out] entry Entry that was retrieved, or undefined if no entries exist. |
71 | * @return True if an unread entry was retrieved, false otherwise. |
72 | */ |
73 | bool getUnreadEntry(LogEntry& entry); |
74 | |
75 | /** |
76 | * Returns the last available log entry. |
77 | * |
78 | * @param[out] entry Entry that was retrieved, or undefined if no entries exist. |
79 | * @return True if an entry was retrieved, false otherwise. |
80 | */ |
81 | bool getLastEntry(LogEntry& entry); |
82 | |
83 | /** |
84 | * Returns a hash value that is modified whenever entries in the log change. This can be used for |
85 | * checking for changes by external systems. |
86 | */ |
87 | UINT64 getHash() const { return mHash; } |
88 | |
89 | private: |
90 | friend class Debug; |
91 | |
92 | /** Returns all log entries, including those marked as unread. */ |
93 | Vector<LogEntry> getAllEntries() const; |
94 | |
95 | Vector<LogEntry> mEntries; |
96 | Queue<LogEntry> mUnreadEntries; |
97 | UINT64 mHash = 0; |
98 | mutable RecursiveMutex mMutex; |
99 | }; |
100 | |
101 | /** @} */ |
102 | } |
103 | |