-
Notifications
You must be signed in to change notification settings - Fork 42
/
logging.h
52 lines (49 loc) · 1.41 KB
/
logging.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
#include <cstdio>
#include <cstdarg>
#include <vector>
#include "win32util.h"
class Log {
std::vector<std::shared_ptr<FILE>> m_streams;
public:
static Log &instance()
{
static Log self;
return self;
}
bool is_enabled() { return m_streams.size() != 0; }
void enable_stderr()
{
if (GetFileType(win32::get_handle(2)) != FILE_TYPE_UNKNOWN)
m_streams.push_back(std::shared_ptr<FILE>(stderr, [](FILE*){}));
}
void enable_file(const wchar_t *filename)
{
try {
FILE *fp = win32::wfopenx(filename, L"w");
_setmode(_fileno(fp), _O_U8TEXT);
std::setbuf(fp, 0);
m_streams.push_back(std::shared_ptr<FILE>(fp, std::fclose));
} catch (...) {}
}
void vwprintf(const wchar_t *fmt, va_list args)
{
int rc = _vscwprintf(fmt, args);
std::vector<wchar_t> buffer(rc + 1);
rc = _vsnwprintf(buffer.data(), buffer.size(), fmt, args);
OutputDebugStringW(buffer.data());
for (size_t i = 0; i < m_streams.size(); ++i)
std::fputws(buffer.data(), m_streams[i].get());
}
void wprintf(const wchar_t *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
vwprintf(fmt, ap);
va_end(ap);
}
private:
Log() {}
Log(const Log&);
Log& operator=(const Log&);
};
#define LOG(fmt, ...) Log::instance().wprintf(fmt, __VA_ARGS__)