-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlogging.h
More file actions
58 lines (52 loc) · 972 Bytes
/
logging.h
File metadata and controls
58 lines (52 loc) · 972 Bytes
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
#pragma once
#include <fstream>
#include <stdarg.h>
class Log
{
public:
Log() {}
~Log()
{
if (LOG.is_open())
{
LOG << std::endl;
}
}
template <typename T>
Log& operator<<(const T& t)
{
if (LOG.is_open())
{
LOG << t;
}
return *this;
}
private:
static std::ofstream LOG;
};
static std::ostream& operator<<(std::ostream& os, const wchar_t* wchr)
{
std::wstring ws(wchr);
return os << std::string(ws.begin(), ws.end()).c_str();
}
static void logf(char* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
auto size = vsnprintf(nullptr, 0, fmt, ap);
std::string output(size + 1, '\0');
vsprintf_s(&output[0], size + 1, fmt, ap);
Log() << output.c_str();
va_end(ap);
}
static void logf(wchar_t* fmt, ...)
{
va_list ap;
va_start(ap, fmt);
#pragma warning(suppress: 4996)
auto size = _vsnwprintf(nullptr, 0, fmt, ap);
std::wstring output(size + 1, '\0');
vswprintf_s(&output[0], size + 1, fmt, ap);
Log() << output.c_str();
va_end(ap);
}