summaryrefslogtreecommitdiffstats
path: root/src/Logger.cpp
blob: a52ca471da4c75c72add4c1fd8a1677a94f89da8 (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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

#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





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





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





void cLogger::LogSimple(const AString & a_Message, eLogLevel a_LogLevel)
{
	time_t rawtime;
	time(&rawtime);

	struct tm * timeinfo;
	#ifdef _MSC_VER
		struct tm timeinforeal;
		timeinfo = &timeinforeal;
		localtime_s(timeinfo, &rawtime);
	#else
		timeinfo = localtime(&rawtime);
	#endif

	AString Line;
	#ifdef _DEBUG
		Printf(Line, "[%04llx|%02d:%02d:%02d] %s\n", static_cast<UInt64>(std::hash<std::thread::id>()(std::this_thread::get_id())), timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message);
	#else
		Printf(Line, "[%02d:%02d:%02d] %s\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message);
	#endif


	cCSLock Lock(m_CriticalSection);
	for (size_t i = 0; i < m_LogListeners.size(); i++)
	{
		m_LogListeners[i]->Log(Line, a_LogLevel);
	}
}





void cLogger::vLogPrintf(const char * a_Format, eLogLevel a_LogLevel, fmt::printf_args a_ArgList)
{
	LogSimple(vPrintf(a_Format, a_ArgList), a_LogLevel);
}





void cLogger::vLogFormat(const char * a_Format, eLogLevel a_LogLevel, fmt::format_args a_ArgList)
{
	LogSimple(fmt::vformat(a_Format, a_ArgList), 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 vFLOG(const char * a_Format, fmt::format_args a_ArgList)
{
	cLogger::GetInstance().vLogFormat(a_Format, cLogger::llRegular, a_ArgList);
}





void vFLOGINFO(const char * a_Format, fmt::format_args a_ArgList)
{
	cLogger::GetInstance().vLogFormat( a_Format, cLogger::llInfo, a_ArgList);
}





void vFLOGWARNING(const char * a_Format, fmt::format_args a_ArgList)
{
	cLogger::GetInstance().vLogFormat( a_Format, cLogger::llWarning, a_ArgList);
}





void vFLOGERROR(const char * a_Format, fmt::format_args a_ArgList)
{
	cLogger::GetInstance().vLogFormat(a_Format, cLogger::llError, a_ArgList);
}





void vLOG(const char * a_Format, fmt::printf_args a_ArgList)
{
	cLogger::GetInstance().vLogPrintf(a_Format, cLogger::llRegular, a_ArgList);
}





void vLOGINFO(const char * a_Format, fmt::printf_args a_ArgList)
{
	cLogger::GetInstance().vLogPrintf(a_Format, cLogger::llInfo, a_ArgList);
}





void vLOGWARNING(const char * a_Format, fmt::printf_args a_ArgList)
{
	cLogger::GetInstance().vLogPrintf(a_Format, cLogger::llWarning, a_ArgList);
}





void vLOGERROR(const char * a_Format, fmt::printf_args a_ArgList)
{
	cLogger::GetInstance().vLogPrintf( a_Format, cLogger::llError, a_ArgList);
}