summaryrefslogtreecommitdiffstats
path: root/src/Logger.cpp
blob: 438fba5e9ff2ffcb87bc300289eb2f48e0021600 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168

#include "Globals.h"  // NOTE: MSVC stupidness requires this to be the same across all modules
#include "Logger.h"

#include "OSSupport/IsThread.h"
#ifdef _WIN32
#include <time.h>
#endif





static void WriteLogOpener(fmt::memory_buffer & Buffer)
{
	const time_t rawtime = time(nullptr);

	struct tm timeinfo;
#ifdef _MSC_VER
	localtime_s(&timeinfo, &rawtime);
#else
	localtime_r(&rawtime, &timeinfo);
#endif

#ifndef NDEBUG
	const auto ThreadID = std::hash<std::thread::id>()(std::this_thread::get_id());
	fmt::format_to(
		Buffer,
		"[{0:04x}|{1:02d}:{2:02d}:{3:02d}] ",
		ThreadID,
		timeinfo.tm_hour,
		timeinfo.tm_min,
		timeinfo.tm_sec
	);
#else
	fmt::format_to(Buffer, "[{0:02d}:{1:02d}:{2:02d}] ", timeinfo.tm_hour, timeinfo.tm_min, timeinfo.tm_sec);
#endif
}





cLogger & cLogger::GetInstance(void)
{
	static cLogger Instance;
	return Instance;
}





void cLogger::InitiateMultithreading()
{
	GetInstance();
}





void cLogger::LogSimple(std::string_view a_Message, eLogLevel a_LogLevel)
{
	fmt::memory_buffer Buffer;
	WriteLogOpener(Buffer);
	fmt::format_to(Buffer, "{0}\n", a_Message);
	LogLine(std::string_view(Buffer.data(), Buffer.size()), a_LogLevel);
}





void cLogger::LogLine(std::string_view a_Line, eLogLevel a_LogLevel)
{
	cCSLock Lock(m_CriticalSection);
	for (size_t i = 0; i < m_LogListeners.size(); i++)
	{
		m_LogListeners[i]->Log(a_Line, a_LogLevel);
	}
}





void cLogger::LogPrintf(std::string_view a_Format, eLogLevel a_LogLevel, fmt::printf_args a_ArgList)
{
	fmt::memory_buffer Buffer;
	WriteLogOpener(Buffer);
	fmt::vprintf(Buffer, fmt::to_string_view(a_Format), a_ArgList);
	fmt::format_to(Buffer, "\n");

	LogLine(std::string_view(Buffer.data(), Buffer.size()), a_LogLevel);
}





void cLogger::LogFormat(std::string_view a_Format, eLogLevel a_LogLevel, fmt::format_args a_ArgList)
{
	fmt::memory_buffer Buffer;
	WriteLogOpener(Buffer);
	fmt::vformat_to(Buffer, a_Format, a_ArgList);
	fmt::format_to(Buffer, "\n");

	LogLine(std::string_view(Buffer.data(), Buffer.size()), a_LogLevel);
}





cLogger::cAttachment cLogger::AttachListener(std::unique_ptr<cListener> a_Listener)
{
	auto nonOwning = a_Listener.get();
	{
		cCSLock Lock(m_CriticalSection);
		m_LogListeners.push_back(std::move(a_Listener));
	}
	return cAttachment {nonOwning};
}





void cLogger::DetachListener(cListener * a_Listener)
{
	cCSLock Lock(m_CriticalSection);
	m_LogListeners.erase(std::remove_if(
		m_LogListeners.begin(),
		m_LogListeners.end(),
		[=](std::unique_ptr<cListener> & a_OtherListener) -> bool { return a_OtherListener.get() == a_Listener; }
	));
}





////////////////////////////////////////////////////////////////////////////////
// Global functions

void Logger::LogFormat(std::string_view a_Format, eLogLevel a_LogLevel, fmt::format_args a_ArgList)
{
	cLogger::GetInstance().LogFormat(a_Format, a_LogLevel, a_ArgList);
}





void Logger::LogPrintf(std::string_view a_Format, eLogLevel a_LogLevel, fmt::printf_args a_ArgList)
{
	cLogger::GetInstance().LogPrintf(a_Format, a_LogLevel, a_ArgList);
}





void Logger::LogSimple(std::string_view a_Message, eLogLevel a_LogLevel)
{
	cLogger::GetInstance().LogSimple(a_Message, a_LogLevel);
}