-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogManager.cpp
More file actions
155 lines (122 loc) · 2.88 KB
/
LogManager.cpp
File metadata and controls
155 lines (122 loc) · 2.88 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
#include "LogManager.h"
#include <iostream>
#include <sstream>
#include <ctime>
#include <direct.h>
#include <Shlwapi.h>
LogManager LogManager::m_instance;
LogManager::LogManager()
{
m_xFile = nullptr;
bDisableFile = false;
m_hstdout = GetStdHandle(STD_OUTPUT_HANDLE);
m_logFilePath = "logs\\";
m_logFileName = GenerateFileName();
VerifyLogPath();
if(!bDisableFile)
OpenFile();
}
LogManager::~LogManager()
{
CloseFile();
}
bool LogManager::Write( std::string text, int color )
{
WriteCmd(text, color);
return (bDisableFile || WriteFile(text));
}
void LogManager::WriteCmd( std::string text, int color )
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(m_hstdout, &csbi);
SetConsoleTextAttribute(m_hstdout, color);
std::cout << text;
SetConsoleTextAttribute(m_hstdout, csbi.wAttributes);
}
bool LogManager::WriteFile( std::string text )
{
if(FileOpen()) {
(*m_xFile) << text;
m_xFile->flush();
return true;
}
return false;
}
std::string LogManager::GenerateFileName()
{
time_t t = time(0);
struct tm* now = new struct tm();
localtime_s(now, &t);
int mon = now->tm_mon+1;
std::stringstream str;
str << m_logFilePath
<<(now->tm_year+1900) << "-"
<< ((mon < 10) ? "0" : "") << mon << "-"
<< ((now->tm_mday < 10) ? "0" : "") << now->tm_mday << " "
<< ((now->tm_hour < 10) ? "0" : "") << now->tm_hour << "-"
<< ((now->tm_min < 10) ? "0" : "") << now->tm_min << "-"
<<((now->tm_sec < 10) ? "0" : "") << now->tm_sec << ".log";
delete now;
return str.str();
}
void LogManager::VerifyLogPath()
{
char* pathbuf = _getcwd(NULL, 0);
std::string logPath(pathbuf);
logPath += "\\" + m_logFilePath;
if(!PathIsDirectoryA(logPath.c_str())) {
if(_mkdir(logPath.c_str()) == -1) {
bDisableFile = true;
Log::Warn("Couldn't verify/create log path; disabling logging to file." + logPath);
}
}
}
bool LogManager::OpenFile()
{
if(m_xFile != nullptr) {
CloseFile();
}
std::string fileName = GenerateFileName();
m_xFile = new std::ofstream(fileName);
if(!m_xFile->is_open()) {
Log::Warn("Couldn't create log file " + fileName);
CloseFile();
return false;
}
Log::Success("LogFile: " + fileName);
return true;
}
void LogManager::CloseFile()
{
if(m_xFile != nullptr) {
if(m_xFile->is_open()) {
m_xFile->close();
}
delete m_xFile;
m_xFile = nullptr;
}
}
bool LogManager::FileOpen() {
return ( (m_xFile != nullptr && m_xFile->is_open()) );
}
bool Log::Write( std::string text, int color /*= COLOR_DEFAULT*/ )
{
return LogManager::GetInstance().Write(text,color);
}
bool Log::Writeln( std::string text, int color /*= COLOR_DEFAULT*/ )
{
text += "\n";
return Log::Write(text, color);
}
bool Log::Warn( std::string warn )
{
return Log::Writeln(warn, Log::COLOR_WARNING);
}
bool Log::Err( std::string err )
{
return Log::Writeln(err, Log::COLOR_ERROR);
}
bool Log::Success( std::string msg )
{
return Log::Writeln(msg, Log::COLOR_SUCCESS);
}