Cuberite
A lightweight, fast and extensible game server for Minecraft
Logger.cpp
Go to the documentation of this file.
1 
2 #include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
3 #include "Logger.h"
4 
5 #include "OSSupport/IsThread.h"
6 #ifdef _WIN32
7  #include <time.h>
8 #endif
9 
10 
11 
12 
13 
14 static void WriteLogOpener(fmt::memory_buffer & Buffer)
15 {
16  const time_t rawtime = time(nullptr);
17 
18  struct tm timeinfo;
19 #ifdef _MSC_VER
20  localtime_s(&timeinfo, &rawtime);
21 #else
22  localtime_r(&rawtime, &timeinfo);
23 #endif
24 
25 #ifndef NDEBUG
26  const auto ThreadID = std::hash<std::thread::id>()(std::this_thread::get_id());
27  fmt::format_to(
28  Buffer, "[{0:04x}|{1:02d}:{2:02d}:{3:02d}] ",
29  ThreadID, timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec
30  );
31 #else
32  fmt::format_to(
33  Buffer, "[{0:02d}:{1:02d}:{2:02d}] ",
34  timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec
35  );
36 #endif
37 }
38 
39 
40 
41 
42 
44 {
45  static cLogger Instance;
46  return Instance;
47 }
48 
49 
50 
51 
52 
54 {
55  GetInstance();
56 }
57 
58 
59 
60 
61 
62 void cLogger::LogSimple(std::string_view a_Message, eLogLevel a_LogLevel)
63 {
64  fmt::memory_buffer Buffer;
65  WriteLogOpener(Buffer);
66  fmt::format_to(Buffer, "{0}\n", a_Message);
67  LogLine(std::string_view(Buffer.data(), Buffer.size()), a_LogLevel);
68 }
69 
70 
71 
72 
73 
74 void cLogger::LogLine(std::string_view a_Line, eLogLevel a_LogLevel)
75 {
77  for (size_t i = 0; i < m_LogListeners.size(); i++)
78  {
79  m_LogListeners[i]->Log(a_Line, a_LogLevel);
80  }
81 }
82 
83 
84 
85 
86 
87 void cLogger::LogPrintf(std::string_view a_Format, eLogLevel a_LogLevel, fmt::printf_args a_ArgList)
88 {
89  fmt::memory_buffer Buffer;
90  WriteLogOpener(Buffer);
91  fmt::vprintf(Buffer, fmt::to_string_view(a_Format), a_ArgList);
92  fmt::format_to(Buffer, "\n");
93 
94  LogLine(std::string_view(Buffer.data(), Buffer.size()), a_LogLevel);
95 }
96 
97 
98 
99 
100 
101 void cLogger::LogFormat(std::string_view a_Format, eLogLevel a_LogLevel, fmt::format_args a_ArgList)
102 {
103  fmt::memory_buffer Buffer;
104  WriteLogOpener(Buffer);
105  fmt::vformat_to(Buffer, a_Format, a_ArgList);
106  fmt::format_to(Buffer, "\n");
107 
108  LogLine(std::string_view(Buffer.data(), Buffer.size()), a_LogLevel);
109 }
110 
111 
112 
113 
114 
115 cLogger::cAttachment cLogger::AttachListener(std::unique_ptr<cListener> a_Listener)
116 {
117  auto nonOwning = a_Listener.get();
118  {
120  m_LogListeners.push_back(std::move(a_Listener));
121  }
122  return cAttachment{nonOwning};
123 }
124 
125 
126 
127 
128 
130 {
132  m_LogListeners.erase(
133  std::remove_if(
134  m_LogListeners.begin(),
135  m_LogListeners.end(),
136  [=](std::unique_ptr<cListener> & a_OtherListener) -> bool
137  {
138  return a_OtherListener.get() == a_Listener;
139  }
140  )
141  );
142 }
143 
144 
145 
146 
147 
149 // Global functions
150 
151 void Logger::LogFormat(std::string_view a_Format, eLogLevel a_LogLevel, fmt::format_args a_ArgList)
152 {
153  cLogger::GetInstance().LogFormat(a_Format, a_LogLevel, a_ArgList);
154 }
155 
156 
157 
158 
159 
160 void Logger::LogPrintf(std::string_view a_Format, eLogLevel a_LogLevel, fmt::printf_args a_ArgList)
161 {
162  cLogger::GetInstance().LogPrintf(a_Format, a_LogLevel, a_ArgList);
163 }
164 
165 
166 
167 
168 
169 void Logger::LogSimple(std::string_view a_Message, eLogLevel a_LogLevel)
170 {
171  cLogger::GetInstance().LogSimple(a_Message, a_LogLevel);
172 }
static void WriteLogOpener(fmt::memory_buffer &Buffer)
Definition: Logger.cpp:14
eLogLevel
Definition: LoggerSimple.h:6
void LogSimple(std::string_view a_Message, eLogLevel a_LogLevel)
Definition: Logger.cpp:169
void LogFormat(std::string_view a_Format, eLogLevel a_LogLevel, fmt::format_args a_ArgList)
Definition: Logger.cpp:151
void LogPrintf(std::string_view a_Format, eLogLevel a_LogLevel, fmt::printf_args a_ArgList)
Definition: Logger.cpp:160
Definition: Logger.h:6
void LogPrintf(std::string_view a_Format, eLogLevel a_LogLevel, fmt::printf_args a_ArgList)
Log a message formatted with a printf style formatting string.
Definition: Logger.cpp:87
void LogLine(std::string_view a_Line, eLogLevel a_LogLevel)
Definition: Logger.cpp:74
static cLogger & GetInstance(void)
Definition: Logger.cpp:43
void LogFormat(std::string_view a_Format, eLogLevel a_LogLevel, fmt::format_args a_ArgList)
Log a message formatted with a python style formatting string.
Definition: Logger.cpp:101
void DetachListener(cListener *a_Listener)
Definition: Logger.cpp:129
static void InitiateMultithreading()
Definition: Logger.cpp:53
std::vector< std::unique_ptr< cListener > > m_LogListeners
Definition: Logger.h:71
void LogSimple(std::string_view a_Message, eLogLevel a_LogLevel=eLogLevel::Regular)
Logs the simple text message at the specified log level.
Definition: Logger.cpp:62
cCriticalSection m_CriticalSection
Definition: Logger.h:70
cAttachment AttachListener(std::unique_ptr< cListener > a_Listener)
Definition: Logger.cpp:115
RAII for cCriticalSection - locks the CS on creation, unlocks on destruction.