Berserk
Logger.hpp
Go to the documentation of this file.
1 /**********************************************************************************/
2 /* This file is part of Berserk Engine project */
3 /* https://github.com/EgorOrachyov/Berserk */
4 /**********************************************************************************/
5 /* MIT License */
6 /* */
7 /* Copyright (c) 2018 - 2021 Egor Orachyov */
8 /* */
9 /* Permission is hereby granted, free of charge, to any person obtaining a copy */
10 /* of this software and associated documentation files (the "Software"), to deal */
11 /* in the Software without restriction, including without limitation the rights */
12 /* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell */
13 /* copies of the Software, and to permit persons to whom the Software is */
14 /* furnished to do so, subject to the following conditions: */
15 /* */
16 /* The above copyright notice and this permission notice shall be included in all */
17 /* copies or substantial portions of the Software. */
18 /* */
19 /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR */
20 /* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, */
21 /* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE */
22 /* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER */
23 /* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, */
24 /* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE */
25 /* SOFTWARE. */
26 /**********************************************************************************/
27 
28 #ifndef BERSERK_LOGGER_HPP
29 #define BERSERK_LOGGER_HPP
30 
31 #include <core/Config.hpp>
32 #include <core/Typedefs.hpp>
33 #include <core/string/String.hpp>
34 
35 #include <atomic>
36 #include <functional>
37 #include <mutex>
38 #include <queue>
39 #include <sstream>
40 #include <vector>
41 
43 
53 class Logger final {
54 public:
56  enum class Level {
58  Info = 0,
60  Warning = 1,
62  Error = 2
63  };
64 
66  struct Entry {
68  String function;
70  size_t line;
72  };
73 
74  using Listener = std::function<void(const Entry &)>;
75  static const size_t DEFAULT_SIZE = 100;
76 
77  BRK_API Logger() = default;
78 
79  BRK_API void SetLevel(Level level);
80  BRK_API void SetSize(size_t size = DEFAULT_SIZE);
81  BRK_API void SetActive(bool active = true);
82  BRK_API void AddListener(Listener listener);
83 
84  BRK_API void Log(Level level, String message, String function = "", String file = "", size_t line = 0);
85  BRK_API void LogInfo(String message, String function = "", String file = "", size_t line = 0);
86  BRK_API void LogWarning(String message, String function = "", String file = "", size_t line = 0);
87  BRK_API void LogError(String message, String function = "", String file = "", size_t line = 0);
88 
89  BRK_API Level GetLevel() const;
90  BRK_API bool IsActive() const;
91  BRK_API bool ShouldLog(Level level) const;
92 
93  BRK_API static Logger &Instance();
94 
95 private:
96  void Shrink();
97  void AddEntry(Entry &&entry);
98 
99 private:
100  std::queue<Entry> mEntries;
101  std::vector<Listener> mListeners;
102  std::atomic<Level> mLevel{Level::Info};
103  std::atomic_bool mActive{true};
104  size_t mSize = DEFAULT_SIZE;
105 
106  mutable std::mutex mMutex;
107  static Logger gLogger;
108 };
109 
110 namespace {
112  inline const char *LoggerLevelToStr(Logger::Level level) {
113  switch (level) {
114  case Logger::Level::Info:
115  return "Info";
117  return "Warning";
119  return "Error";
120  default:
121  return "Unknown";
122  }
123  }
124 }// namespace
125 
126 #define BRK_LOG(log, level, message) \
127  do { \
128  (log).Log(level, message, __FUNCTION__, __FILE__, static_cast<BRK_NS ::size_t>(__LINE__)); \
129  } while (false);
130 
131 #define BRK_LOG_MESSAGE(log, level, message) \
132  if ((log).ShouldLog(level)) { \
133  std::stringstream ss; \
134  ss << message; \
135  BRK_LOG(log, level, ss.str()); \
136  } else { \
137  }
138 
140 #define BRK_INFO(message) \
141  BRK_LOG_MESSAGE(BRK_NS ::Logger::Instance(), BRK_NS ::Logger::Level::Info, message)
142 
144 #define BRK_WARNING(message) \
145  BRK_LOG_MESSAGE(BRK_NS ::Logger::Instance(), BRK_NS ::Logger::Level::Warning, message)
146 
148 #define BRK_ERROR(message) \
149  BRK_LOG_MESSAGE(BRK_NS ::Logger::Instance(), BRK_NS ::Logger::Level::Error, message)
150 
156 
157 #endif//BERSERK_LOGGER_HPP
#define BRK_NS_END
Definition: Config.hpp:48
#define BRK_API
Definition: Config.hpp:32
Simpler engine logger class to maintain text log.
Definition: Logger.hpp:53
BRK_API void LogInfo(String message, String function="", String file="", size_t line=0)
Definition: Logger.cpp:59
BRK_API void SetLevel(Level level)
Definition: Logger.cpp:35
BRK_API void SetSize(size_t size=DEFAULT_SIZE)
Definition: Logger.cpp:39
BRK_API bool ShouldLog(Level level) const
Definition: Logger.cpp:102
BRK_API void LogWarning(String message, String function="", String file="", size_t line=0)
Definition: Logger.cpp:64
BRK_API bool IsActive() const
Definition: Logger.cpp:98
BRK_API void Log(Level level, String message, String function="", String file="", size_t line=0)
Definition: Logger.cpp:54
BRK_API void SetActive(bool active=true)
Definition: Logger.cpp:45
static const size_t DEFAULT_SIZE
Definition: Logger.hpp:75
static BRK_API Logger & Instance()
Definition: Logger.cpp:106
BRK_API Logger()=default
Level
Level of accepted messages.
Definition: Logger.hpp:56
std::function< void(const Entry &)> Listener
Definition: Logger.hpp:74
BRK_API void AddListener(Listener listener)
Definition: Logger.cpp:49
BRK_API Level GetLevel() const
Definition: Logger.cpp:94
BRK_API void LogError(String message, String function="", String file="", size_t line=0)
Definition: Logger.cpp:69
Utf-8 encoded std based default string class.
Definition: GLDevice.cpp:46
Log entry.
Definition: Logger.hpp:66
size_t line
Definition: Logger.hpp:70
String message
Definition: Logger.hpp:67
Level level
Definition: Logger.hpp:71
String file
Definition: Logger.hpp:69