-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConsole.cpp
More file actions
82 lines (65 loc) · 1.86 KB
/
Console.cpp
File metadata and controls
82 lines (65 loc) · 1.86 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
#include "Console.h"
#include <fstream>
Console& Console::Get()
{
static Console instance;
return instance;
}
std::string Console::FindLatestLog() const
{
char modulePath[MAX_PATH] = {0};
GetModuleFileNameA(nullptr, modulePath, MAX_PATH);
std::string dir(modulePath);
const size_t slash = dir.find_last_of("\\/");
if (slash == std::string::npos)
return std::string();
dir.erase(slash + 1);
const std::string pattern = dir + "*.log";
WIN32_FIND_DATAA find = {0};
HANDLE handle = FindFirstFileA(pattern.c_str(), &find);
if (handle == INVALID_HANDLE_VALUE)
return std::string();
std::string best;
FILETIME bestTime = {0};
do
{
if (find.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
continue;
if (CompareFileTime(&find.ftLastWriteTime, &bestTime) > 0)
{
bestTime = find.ftLastWriteTime;
best = dir + find.cFileName;
}
} while (FindNextFileA(handle, &find));
FindClose(handle);
return best;
}
void Console::Poll(std::string& out)
{
const std::string latest = FindLatestLog();
if (latest.empty())
return;
if (latest != m_path)
{
m_path = latest;
m_offset = 0;
}
std::ifstream in(m_path.c_str(), std::ios::binary);
if (!in)
return;
in.seekg(0, std::ios::end);
const long long size = static_cast<long long>(in.tellg());
if (size < m_offset)
m_offset = 0;
if (size <= m_offset)
return;
in.seekg(m_offset, std::ios::beg);
const std::streamsize toRead =
static_cast<std::streamsize>(size - m_offset);
std::string chunk;
chunk.resize(static_cast<size_t>(toRead));
in.read(&chunk[0], toRead);
chunk.resize(static_cast<size_t>(in.gcount()));
m_offset += static_cast<long long>(chunk.size());
out += chunk;
}