-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlog.cpp
58 lines (46 loc) · 1.45 KB
/
log.cpp
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
#include "log.h"
std::ofstream logStream;
string GetCurrentTimeString()
{
const auto currentTime = std::time(nullptr);
const auto localTime = std::localtime(¤tTime);
char buffer[10];
std::strftime(buffer, sizeof buffer, "%H:%M:%S", localTime);
return { buffer };
}
string GetModuleFilePath()
{
HMODULE handle;
if (!GetModuleHandleExA(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT, reinterpret_cast<LPCSTR>(&GetModuleFilePath), &handle))
{
MessageBoxA(nullptr, std::format("GetModuleHandleExA failed (0x{:X})", GetLastError()).c_str(), "Save-Unfinished-Ships", MB_OK | MB_ICONERROR);
return "";
}
char path[MAX_PATH];
if (!GetModuleFileNameA(handle, path, sizeof path))
{
MessageBoxA(nullptr, std::format("GetModuleFileNameA failed (0x{:X})", GetLastError()).c_str(), "Save-Unfinished-Ships", MB_OK | MB_ICONERROR);
return "";
}
return path;
}
string GetLogFilePath()
{
auto path = GetModuleFilePath();
if (path.empty())
return "";
path = path.replace(path.find(".dll"), 4, ".log");
return path;
}
bool SetupLog()
{
const auto path = GetLogFilePath();
if (path.empty())
return false;
logStream = std::ofstream(path.c_str(), std::ios_base::out | std::ios_base::trunc);
return true;
}
void Log(const string& text)
{
logStream << "[" << GetCurrentTimeString() << "] " << text << std::endl;
}