summaryrefslogtreecommitdiff
path: root/plugins/MirFox/src/MirFoxCommons/MirFoxCommons_logger.cpp
blob: c9c50a4b8a702f7325000207401f2baa832b472a (plain)
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
#include "MirFoxCommons_pch.h"
#include "MirFoxCommons_logger.h"


/*static*/ MFLogger * MFLogger::m_pOnlyOneInstance;


MFLogger::MFLogger()
{
	logFunction = NULL;
	memcpy(m_prefix, L"      ", PREFIX_SIZE * sizeof(m_prefix[0]));
}


void
MFLogger::initLogger(LogFunction logFunction_p){

	logFunction = logFunction_p;
	InitializeCriticalSection(&logCs);

}

void
MFLogger::set6CharsPrefix(const wchar_t* prefix){
	size_t len = wcslen(prefix);
	memcpy(m_prefix, L"      ", PREFIX_SIZE * sizeof(m_prefix[0]));
	memcpy(m_prefix, prefix, len * sizeof(wchar_t));
}


void
MFLogger::releaseLogger(){

	logFunction = NULL;
	DeleteCriticalSection(&logCs);

}


void
MFLogger::log(const wchar_t* szText){

	size_t len = wcslen(szText) + 1;
	wchar_t* buffer = new wchar_t[(PREFIX_SIZE + len) * sizeof(wchar_t)];
	memcpy(buffer, m_prefix, PREFIX_SIZE * sizeof(m_prefix[0]));
	memcpy(buffer + PREFIX_SIZE , szText, len * sizeof(wchar_t));
	log_int(buffer);
	delete buffer;

}


void
MFLogger::log_p(const wchar_t* szText, ...){

	va_list args;
	va_start(args, szText);
	int len = (PREFIX_SIZE + _vscwprintf(szText, args ) + 1); // _vscprintf doesn't count terminating '\0'
	wchar_t* buffer = new wchar_t[len * sizeof(wchar_t)];
	vswprintf_s(buffer + PREFIX_SIZE, len, szText, args);
	va_end(args);
	memcpy(buffer, m_prefix, PREFIX_SIZE * sizeof(m_prefix[0]));
	log_int(buffer);
	delete buffer;

}


void
MFLogger::log_d(const wchar_t* szText){

	#ifdef _DEBUG
	size_t len = wcslen(szText) + 1;
	wchar_t* buffer = new wchar_t[(PREFIX_SIZE + len) * sizeof(wchar_t)];
	memcpy(buffer, m_prefix, PREFIX_SIZE * sizeof(m_prefix[0]));
	memcpy(buffer + PREFIX_SIZE , szText, len * sizeof(wchar_t));
	log_int(buffer);
	delete buffer;
	#endif //_DEBUG

}


void
MFLogger::log_dp(const wchar_t* szText, ...){

	#ifdef _DEBUG
	va_list args;
	va_start(args, szText);
	int len = (PREFIX_SIZE + _vscwprintf(szText, args ) + 1); // _vscprintf doesn't count terminating '\0'
	wchar_t* buffer = new wchar_t[len * sizeof(wchar_t)];
	vswprintf_s(buffer + PREFIX_SIZE, len, szText, args);
	va_end(args);
	memcpy(buffer, m_prefix, PREFIX_SIZE * sizeof(m_prefix[0]));
	log_int(buffer);
	delete buffer;
	#endif //_DEBUG

}


void
MFLogger::log_int(const wchar_t* szText){

	if (!logFunction){
		return;
	}

	EnterCriticalSection(&logCs);
	(*logFunction)(szText);
	LeaveCriticalSection(&logCs);

}