This repository was archived by the owner on Oct 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.cpp
More file actions
124 lines (81 loc) · 2.04 KB
/
Log.cpp
File metadata and controls
124 lines (81 loc) · 2.04 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
116
117
118
119
120
121
122
123
124
#include "Log.hpp"
HANDLE hFile;
void LogTime(string* log, DWORD lastError)
{
time_t _time = time(NULL);
tm ltm;
ZeroMemory(<m, sizeof(ltm));
localtime_s(<m, &_time);
log->append("[");
if (ltm.tm_mday < 10)
log->append("0");
log->append(to_string(ltm.tm_mday));
log->append(".");
if (1 + ltm.tm_mon < 10)
log->append("0");
log->append(to_string(1 + ltm.tm_mon));
log->append(".");
log->append(to_string(1900 + ltm.tm_year));
log->append(" ");
if (ltm.tm_hour < 10)
log->append("0");
log->append(to_string(ltm.tm_hour));
log->append(":");
if (ltm.tm_min < 10)
log->append("0");
log->append(to_string(ltm.tm_min));
log->append(":");
if (ltm.tm_sec < 10)
log->append("0");
log->append(to_string(ltm.tm_sec));
log->append(" LastError: ");
log->append(to_string(lastError));
if (lastError)
{
log->append("(");
// Retrieve the system error message for the last-error code
LPVOID lpMsgBuf;
FormatMessageA(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
lastError,
MAKELANGID(LANG_ENGLISH, SUBLANG_DEFAULT),
(LPSTR)&lpMsgBuf,
0, NULL);
for (size_t i = 0; i < LocalSize(lpMsgBuf); i++)
if (((LPSTR)lpMsgBuf)[i] == '\n'
|| ((LPSTR)lpMsgBuf)[i] == '\r')
((LPSTR)lpMsgBuf)[i] = 0x00;
log->append((LPSTR)lpMsgBuf);
log->append(")");
LocalFree(lpMsgBuf);
}
log->append("] ");
return;
}
void Log(const char* text)
{
DWORD lastError = GetLastError();
std::wstring fullpath;
std::string log;
wchar_t path[MAX_PATH];
ZeroMemory(path, sizeof(path));
if (!GetCurDir(path, MAX_PATH))
return;
fullpath.append(path);
fullpath.append(L"\\error.log");
if (!hFile
|| hFile == INVALID_HANDLE_VALUE)
{
hFile = CreateFileW(fullpath.c_str(), FILE_APPEND_DATA, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
if (hFile == INVALID_HANDLE_VALUE)
return;
}
LogTime(&log, lastError);
log.append(text);
log.append("\n");
WriteFile(hFile, log.c_str(), log.size(), 0, 0);
return;
}