-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogger.cpp
More file actions
176 lines (144 loc) · 6.27 KB
/
Logger.cpp
File metadata and controls
176 lines (144 loc) · 6.27 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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <windows.h>
#include <ctime>
#include <filesystem>
#include <vector>
#include <QMessageBox>
#include <QDebug>
#include "Windows/Registry.hpp"
#include "Logger.hpp"
#include "Info.hpp"
using namespace std;
using namespace StringExt;
#pragma region ctor and dtor
Logger::Logger()
{
// Disable console if it is really opened.
ShowWindow(GetConsoleWindow(), SW_HIDE);
int i = 0;
vector<filesystem::directory_entry> files;
for (const auto& entry : filesystem::directory_iterator("..\\..\\Logs"))
if (entry.is_regular_file())
files.push_back(entry);
if (files.size() > MAX_LOGS_COUNT)
{
sort(files.begin(), files.end(), [](const filesystem::directory_entry& a, const filesystem::directory_entry& b)
{
return filesystem::last_write_time(a) > filesystem::last_write_time(b);
});
for (auto it = files.begin() + MAX_LOGS_COUNT; it != files.end(); ++it)
{
try
{
filesystem::remove(it->path());
}
catch (...)
{
}
}
}
LogFile.open(GetLogFileName());
// Due to Logger is a singleton, we must create check if folder Logs exists.
if (!LogFile.is_open())
{
filesystem::create_directory("..\\..\\Logs");
LogFile.open(string("..\\..\\") + GetLogFileName());
}
if (!LogFile.is_open())
QMessageBox::critical(nullptr, EXCEPTION_HEADER, "Unable to create log file; Make sure \"Logs\" folder are exists.");
string title = "C&C Generals and Generals Zero Hour hotkey editor";
string version = string("Version: ") + VERSION;
string authors = string("Authors: ") + AUTHORS;
Log(title); LogToConsole(title);
Log(version); LogToConsole(version);
Log(authors); LogToConsole(authors);
LogFile << endl; qDebug() << "";
LogSystemInformation();
Log(filesystem::current_path().c_str());
}
Logger::~Logger()
{
if (LogFile.is_open()) LogFile.close();
}
#pragma endregion
#pragma region Log methods
void Logger::LogSystemInformation()
{
// Write to log all necessary information about MS Windows
Log() << "Operation System Information" << endl;
Log() << "Version : "
<< Windows::Registry::GetWindowsVersion() << ' '
<< GetWindowsBit() << endl;
Log() << "Language : " << Windows::Registry::GetCurrentUserLanguage() << endl << endl;
// Write to log all information about processor type and memory size
Log() << "Hardware Information" << endl;
Log() << "Processor : " << Windows::Registry::GetProcessorInfo() << endl;
Log() << "Memory : " << Windows::Registry::GetMemoryInfo() << endl << endl;
// Write to log all games paths
Log() << "Software Information" << endl;
for (const auto& game : {Windows::Registry::Games::Generals, Windows::Registry::Games::GeneralsZeroHour})
{
if (Windows::Registry::GetPathToGame(game).empty())
Log() << "C&C: " << Windows::Registry::ToString(game) << " not installed" << endl;
else
Log() << "C&C: " << Windows::Registry::ToString(game) << " installed at ["
<< ToQString(Windows::Registry::GetPathToGame(game)).toStdString() << ']' << endl;
}
LogFile << endl;
}
const string Logger::GetCurrentTime() const
{
time_t timeStomp = time(nullptr);
tm timeNow = {};
localtime_s(&timeNow, &timeStomp);
char currentTime[128];
strftime(currentTime, sizeof(currentTime), "%Y-%m-%d %X", &timeNow);
stringstream ss;
ss << currentTime;
return ss.str();
}
const string Logger::GetLogFileName() const
{
time_t timeStomp = time(nullptr);
tm timeNow = {};
localtime_s(&timeNow, &timeStomp);
char currentTime[128];
strftime(currentTime, sizeof(currentTime), "%Y-%m-%d %H-%M-%S", &timeNow);
stringstream ss;
ss << "Logs\\Log " << currentTime << ".log";
return ss.str();
}
void Logger::LogToConsole(const char* msg) const { qDebug() << "[" << GetCurrentTime().c_str() << "]\t" << msg; }
void Logger::LogToConsole(const wchar_t* msg) const { qDebug() << "[" << GetCurrentTime().c_str() << "]\t" << msg; }
void Logger::LogToConsole(const std::string& msg) const { LogToConsole(msg.c_str()); }
void Logger::LogToConsole(const QString& msg) const { LogToConsole(msg.toStdString().c_str()); }
void Logger::LogToConsole(const std::stringstream& msg) const { LogToConsole(msg.str().c_str()); }
void Logger::LogToConsole(const std::wstringstream& msg) const { LogToConsole(msg.str().c_str()); }
void Logger::LogToConsole(const std::wstring& msg) const { LogToConsole(msg.c_str()); }
ofstream& Logger::Log()
{
LogFile << "[" << GetCurrentTime() << "]\t";
return LogFile;
}
void Logger::Log(const char* msg) { Log() << msg << endl; }
void Logger::Log(const string& msg) { Log(msg.c_str()); }
void Logger::Log(const QString& msg) { Log(msg.toStdString()); }
void Logger::Log(const stringstream& msg) { Log(msg.str()); }
void Logger::Log(const wstringstream& msg) { Log(msg.str()); }
void Logger::Log(const wstring& msg) { Log(msg.c_str()); }
void Logger::Log(const wchar_t* msg) { Log() << QString::fromStdWString(wstring{msg}).toStdString().c_str() << endl; }
void Logger::LogException(const char* msg)
{
LogFile << endl << endl;
Log("\t\t\t\tI'VE GOT A PRESENT FOR YA");
Log(msg);
}
#pragma endregion
#pragma region Support methods
const string Logger::GetWindowsBit() const
{
if (Windows::Registry::GetWindowsBit() == Windows::Registry::Arch::Win32)
return "32-bit";
else
return "64-bit";
}
#pragma endregion