-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_manager.cpp
More file actions
115 lines (97 loc) · 2.63 KB
/
log_manager.cpp
File metadata and controls
115 lines (97 loc) · 2.63 KB
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
#ifndef UNICODE
#define UNICODE
#define _UNICODE
#endif
#include "log_manager.hpp"
#include <knownfolders.h>
#include <share.h>
#include <shlobj.h>
#pragma comment(lib, "shell32")
#pragma comment(lib, "ole32")
FILE* g_log_file = nullptr;
std::wstring g_logPath;
namespace
{
CRITICAL_SECTION g_log_cs;
bool g_log_cs_initialized = false;
} // namespace
void InitLogger()
{
PWSTR appdata_path = nullptr;
HRESULT hr = SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, nullptr, &appdata_path);
if (SUCCEEDED(hr))
{
g_logPath.assign(appdata_path);
CoTaskMemFree(appdata_path);
g_logPath += L"\\CloudStreamingArgsDebugger\\debug.log";
std::wstring dir = g_logPath.substr(0, g_logPath.find_last_of(L"\\"));
CreateDirectoryW(dir.c_str(), nullptr);
}
else
{
wchar_t exePath[MAX_PATH] = L"\0";
GetModuleFileNameW(nullptr, exePath, MAX_PATH);
g_logPath.assign(exePath);
size_t pos = g_logPath.find_last_of(L"\\/");
if (pos != std::wstring::npos)
g_logPath.erase(pos + 1);
g_logPath += L"CloudStreamingArgsDebugger.log";
}
g_log_file = _wfsopen(g_logPath.c_str(), L"a+, ccs=UTF-16LE", _SH_DENYNO);
if (g_log_file && ftell(g_log_file) == 0)
{
fputwc(0xFEFF, g_log_file); // UTF-16LE BOM
}
InitializeCriticalSection(&g_log_cs);
g_log_cs_initialized = true;
}
void Log(const std::wstring& text)
{
if (!g_log_file || !g_log_cs_initialized)
return;
EnterCriticalSection(&g_log_cs);
SYSTEMTIME st;
GetLocalTime(&st);
std::wstring entry = L"[";
entry += std::to_wstring(st.wYear) + L"-";
if (st.wMonth < 10)
entry += L"0";
entry += std::to_wstring(st.wMonth) + L"-";
if (st.wDay < 10)
entry += L"0";
entry += std::to_wstring(st.wDay) + L" ";
if (st.wHour < 10)
entry += L"0";
entry += std::to_wstring(st.wHour) + L":";
if (st.wMinute < 10)
entry += L"0";
entry += std::to_wstring(st.wMinute) + L":";
if (st.wSecond < 10)
entry += L"0";
entry += std::to_wstring(st.wSecond) + L"] ";
entry += text + L"\n";
fputws(entry.c_str(), g_log_file);
fflush(g_log_file);
LeaveCriticalSection(&g_log_cs);
}
void LogSEH(const wchar_t* message)
{
if (!message)
return;
Log(std::wstring(message));
OutputDebugStringW(message);
OutputDebugStringW(L"\n");
}
void CloseLogger()
{
if (g_log_file)
{
fclose(g_log_file);
g_log_file = nullptr;
}
if (g_log_cs_initialized)
{
DeleteCriticalSection(&g_log_cs);
g_log_cs_initialized = false;
}
}