summaryrefslogtreecommitdiffstats
path: root/source/cMCLogger.cpp
blob: a084c62bff77c23195043471f788e3dfb2b17f75 (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

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

#include <cstdarg>
#include <time.h>
#include "cLog.h"





cMCLogger* cMCLogger::s_MCLogger = 0;

cMCLogger* cMCLogger::GetInstance()
{
	return s_MCLogger;
}

cMCLogger::cMCLogger()
{
	m_CriticalSection = new cCriticalSection();
	char c_Buffer[128];
	sprintf_s(c_Buffer, 128, "LOG_%d.txt", (int)time(0) );
	m_Log = new cLog(c_Buffer);
	m_Log->Log("--- Started Log ---");

	s_MCLogger = this;
}

cMCLogger::cMCLogger( char* a_File )
{
	m_CriticalSection = new cCriticalSection();
	m_Log = new cLog( a_File );
}

cMCLogger::~cMCLogger()
{
	m_Log->Log("--- Stopped Log ---");
	delete m_Log;
	delete m_CriticalSection;
	if( this == s_MCLogger )
		s_MCLogger = 0;
}

void cMCLogger::LogSimple(const char* a_Text, int a_LogType /* = 0 */ )
{
	switch( a_LogType )
	{
	case 0:
		Log(a_Text, 0);
		break;
	case 1:
		Info(a_Text, 0);
		break;
	case 2:
		Warn(a_Text, 0);
		break;
	case 3:
		Error(a_Text, 0);
		break;
	default:
		Log(a_Text, 0);
		break;
	}
}

void cMCLogger::Log(const char* a_Format, va_list a_ArgList)
{
	m_CriticalSection->Lock();
	SetColor( 0x7 );	 // 0x7 is default grey color
	m_Log->Log( a_Format, a_ArgList );
	m_CriticalSection->Unlock();
}

void cMCLogger::Info(const char* a_Format, va_list a_ArgList)
{
	m_CriticalSection->Lock();
// 	for( int i = 0; i < 16; i++)
// 	{
// 		for( int j = 0; j < 16; j++ )
// 		{
// 			SetConsoleTextAttribute( hConsole, i | (j<<4) );
// 			printf("0x%x", (i|j<<4));
// 		}
// 		printf("\n");
// 	}

	SetColor( 0xe );	 // 0xe is yellow
	m_Log->Log( a_Format, a_ArgList );
	m_CriticalSection->Unlock();
}

void cMCLogger::Warn(const char* a_Format, va_list a_ArgList)
{
	m_CriticalSection->Lock();
	SetColor( 0xc );	 // 0xc is red
	m_Log->Log( a_Format, a_ArgList );
	m_CriticalSection->Unlock();
}

void cMCLogger::Error(const char* a_Format, va_list a_ArgList)
{
	m_CriticalSection->Lock();
	SetColor( 0xc0 );	// 0xc0 is red bg and black text
	m_Log->Log( a_Format, a_ArgList );
	m_CriticalSection->Unlock();
}

void cMCLogger::SetColor( unsigned char a_Color )
{
#ifdef _WIN32
	HANDLE  hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
	SetConsoleTextAttribute( hConsole, a_Color );
#else
	(void)a_Color;
#endif
}


//////////////////////////////////////////////////////////////////////////
// Global functions
void LOG(const char* a_Format, ...)
{
	va_list argList;
	va_start(argList, a_Format);
	cMCLogger::GetInstance()->Log( a_Format, argList );
	va_end(argList);
}

void LOGINFO(const char* a_Format, ...)
{
	va_list argList;
	va_start(argList, a_Format);
	cMCLogger::GetInstance()->Info( a_Format, argList );
	va_end(argList);
}

void LOGWARN(const char* a_Format, ...)
{
	va_list argList;
	va_start(argList, a_Format);
	cMCLogger::GetInstance()->Warn( a_Format, argList );
	va_end(argList);
}

void LOGERROR(const char* a_Format, ...)
{
	va_list argList;
	va_start(argList, a_Format);
	cMCLogger::GetInstance()->Error( a_Format, argList );
	va_end(argList);
}