-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.h
173 lines (155 loc) · 3.09 KB
/
logger.h
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
#pragma once
#include <cstdio>
#include <iostream>
#include <mutex>
#include <string>
#include <cstdarg>
#define TMP_BUFFER_SIZE 1024
class LOG {
private:
enum OUTTYPE{ COUT, FILE, STREAM };
std::string strPath;
std::mutex mtxLog;
std::FILE *pFile;
std::ostream *pStream;
char tmpBuffer[TMP_BUFFER_SIZE] = {0};
OUTTYPE logType;
// private constructor for the singleton class
LOG()
{
logType = COUT;
pFile = nullptr;
pStream = nullptr;
} // singleton
// logs a string to the current output method
void LogString(const char *str)
{
mtxLog.lock();
switch (logType) {
case COUT:
std::cout << str << std::endl;
break;
case FILE:
if (pFile != nullptr) {
std::fputs(str, pFile);
}
break;
case STREAM:
if (pStream != nullptr)
{
*pStream << str << std::endl;
}
break;
}
mtxLog.unlock();
}
// logs a formatted string to the output method
void LogStringFormat(const char *str,...)
{
mtxLog.lock();
va_list arVa;
va_start(arVa, str);
switch(logType){
case COUT:
vsnprintf(tmpBuffer, TMP_BUFFER_SIZE, str, arVa);
std::cout << tmpBuffer << std::endl;
break;
case FILE:
if (pFile != nullptr) {
std::vfprintf(pFile, str, arVa);
std::fputs(str, pFile);
}
break;
case STREAM:
if (pStream != nullptr)
{
vsnprintf(tmpBuffer, TMP_BUFFER_SIZE, str, arVa);
*pStream << tmpBuffer << std::endl;
}
break;
}
va_end(arVa);
mtxLog.unlock();
}
// sets the log output to the file specified
void SetLogPath(const char *str)
{
mtxLog.lock();
cleanupOutputs();
logType = FILE;
strPath = str;
pFile = fopen(str, "a");
mtxLog.unlock();
}
// sets the log output the the stream passed in
// the caller is responsible for cleaning up the stream
void SetLogStreamer(std::ostream* stream)
{
mtxLog.lock();
cleanupOutputs();
logType = STREAM;
pStream = stream;
mtxLog.unlock();
}
// sets the output to std::cout
void SetStdOut()
{
mtxLog.lock();
cleanupOutputs();
logType = COUT;
mtxLog.unlock();
}
// basic cleanup
void cleanupOutputs()
{
if (pStream)
{
pStream = nullptr;
}
if (pFile)
{
fclose(pFile);
pFile = nullptr;
}
}
// get the singleton instance
static LOG* getInstance()
{
static LOG logger;
return &logger;
}
public:
// destructor
~LOG()
{
if (pFile)
{
fclose(pFile);
}
}
// the following are static versions of the internal functions
// this was done for convenience to the caller
static void SETSTDOUT()
{
LOG::getInstance()->SetStdOut();
}
static void SETLOGSTREAM(std::ostream *stream)
{
LOG::getInstance()->SetLogStreamer(stream);
}
static void SETLOGPATH(const char *str)
{
LOG::getInstance()->SetLogPath(str);
}
static void LOGSTR(const char *str)
{
LOG::getInstance()->LogString(str);
}
static void LOGSTRF(const char *str, ...)
{
va_list arVa;
va_start(arVa, str);
LOG::getInstance()->LogStringFormat(str,arVa);
va_end(arVa);
}
};