summaryrefslogtreecommitdiffstats
path: root/src/LogDispacher.cpp
blob: abca6a898e2b2b9ded5f816f348d2c5a4e333235 (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

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

#include "OSSupport/IsThread.h"



namespace Logger
{

	cLogDispacher & GetInstance(void)
	{
		static cLogDispacher Instance;
		return Instance;
	}

	void InitiateMultithreading()
	{
		GetInstance();
	}

	void cLogDispacher::LogSimple(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, "[%04lx|%02d:%02d:%02d] %s\n", cIsThread::GetCurrentID(), timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message.c_str());
		#else
		Printf(Line, "[%02d:%02d:%02d] %s\n", timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec, a_Message.c_str());
		#endif

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





	void cLogDispacher::Log(const char * a_Format, eLogLevel a_LogLevel, va_list a_ArgList)
	{
		AString Message;
		AppendVPrintf(Message, a_Format, a_ArgList);
		LogSimple(Message, a_LogLevel);
	}
	
	void cLogDispacher::AttachListener(Logger::cLoggerListener * a_Listener)
	{
		cCSLock Lock(m_CriticalSection);
		m_LogListeners.push_back(a_Listener);
	}
	
	void cLogDispacher::DetachListener(Logger::cLoggerListener * a_Listener)
	{
		cCSLock Lock(m_CriticalSection);
		m_LogListeners.erase(std::remove(m_LogListeners.begin(), m_LogListeners.end(), a_Listener));
	}
};



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

void LOG(const char* a_Format, ...)
{
	va_list argList;
	va_start(argList, a_Format);
	Logger::GetInstance().Log(a_Format, Logger::llRegular, argList);
	va_end(argList);
}

void LOGINFO(const char* a_Format, ...)
{
	va_list argList;
	va_start(argList, a_Format);
	Logger::GetInstance().Log( a_Format, Logger::llInfo, argList);
	va_end(argList);
}

void LOGWARN(const char* a_Format, ...)
{
	va_list argList;
	va_start(argList, a_Format);
	Logger::GetInstance().Log( a_Format, Logger::llWarning, argList);
	va_end(argList);
}

void LOGERROR(const char* a_Format, ...)
{
	va_list argList;
	va_start(argList, a_Format);
	Logger::GetInstance().Log( a_Format, Logger::llError, argList);
	va_end(argList);
}