blob: a857be8b1b84f7cbc8e01eb223c668e5143f9e93 (
plain)
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
|
#include "../include/logger.hpp"
// Private----------------------------------------------------------------------
std::string Logger::getDate() {
t = time(nullptr);
now = localtime(&t);
return "(" + days[now->tm_wday] + " " + months[now->tm_mon] + " "
+ std::to_string(now->tm_mday) + " " + std::to_string(now->tm_year + 1900) + ")";
}
std::string Logger::getTime() {
t = time(nullptr);
now = localtime(&t);
std::string hour = std::to_string(now->tm_hour);
std::string min = std::to_string(now->tm_min);
std::string sec = std::to_string(now->tm_sec);
//string hour = "0" + to_string(now->tm_hour);
if (now->tm_hour < 10) {
hour = "0" + std::to_string(now->tm_hour);
}
if (now->tm_min < 10) {
min = "0" + std::to_string(now->tm_min);
}
if (now->tm_sec < 10) {
sec = "0" + std::to_string(now->tm_sec);
}
return hour + ":" + min +
":" + sec;
}
// Public----------------------------------------------------------------------
Logger& Logger::Get() {
static Logger loggerSingleton;
return loggerSingleton;
}
//Logger Logger::loggerSingleton;
bool Logger::openLog(std::string _filename) {
filename = _filename;
if (file_open) {
return false;
}
file.open(filename, std::ios::in | std::ios::out | std::ios::app);
if (!file) {
return false;
}
file_open = true;
std::string date = getDate();
std::string timestamp = getTime();
file << timestamp << infoTag << "Log Start---- " << date << std::endl;
return true;
}
void Logger::closeLog() {
std::string timestamp = getTime();
file << timestamp << infoTag << "Log End----\n\n";
file.close();
file_open = false;
}
bool Logger::log(std::string data) {
if (!file) {
return false;
}
if (!file_open) {
return false;
}
std::string timestamp = getTime();
file << timestamp << infoTag << data << std::endl;
return true;
}
bool Logger::logErr(std::string data) {
if (!file) {
return false;
}
if (!file_open) {
return false;
}
std::string timestamp = getTime();
file << timestamp << errorTag << data << std::endl;
return true;
}
bool Logger::printLog() {
if (file.is_open()) {
std::cout << "Log still open. Please close Log." << std::endl;
return false;
}
file.open(filename, std::ios::in);
if (!file.is_open()) {
return false;
}
std::string line;
while(getline(file, line)) {
std::cout << line << std::endl;
}
file.close();
return true;
}
|