-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRedLog.cpp
185 lines (150 loc) Β· 3.84 KB
/
RedLog.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
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
177
178
179
180
181
182
183
184
#include "RedLog.h"
#include <string>
//
// Local functions.
//
/**
* @brief GetCurrentTime
*
* @return String with time.
*/
static inline std::string _GetCurrentTime() {
time_t now = time(nullptr);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%d-%m-%Y %X", &tstruct);
return std::string(buf);
}
namespace Red {
/**
* @brief RedLog
*
* Base ctor.
*/
RedLog::RedLog() {}
/**
* @brief RedLog
*
* Ctor.
*
* @param path Path to log file.
*/
RedLog::RedLog(std::string& path) {
m_LogFile.open(path, std::ios::app);
}
/**
* @brief OpenFile
*
* Uses to open log file.
*
* @param path Path to log file.
*/
void RedLog::OpenFile(std::string& path) {
m_LogFile.open(path, std::ios::app);
}
/**
* @brief FileExists
*
* Uses to check log file for existance.
*
* @param path Path to log file.
*
* @return True if log file exists, false if not.
*/
bool RedLog::FileExists(std::string& path) {
std::ifstream is(path);
return is.is_open() ? true : false;
}
/**
* @brief CreateLogFile
*
* Uses to create log file.
*
* @param path Path to log file.
*
* @return True if file was created, false if not.
*/
bool RedLog::CreateLogFile(std::string& path) {
// Creating a log file.
std::ofstream of(path);
if (!of.is_open()){
return false;
}
of << "############################" << "\n";
of << " RedLog version: " << REDLOG_VERSION << "\n";
of << "############################" << "\n";
of << "Created: " + _GetCurrentTime() << "\n";
of << "############################" << "\n";
of.close();
// Checking file for existance.
std::ifstream is(path);
if (is.is_open()) {
return true;
} else {
return false;
}
}
/**
* @brief DeleteFile
*
* Uses to delete log file.
*
* @param path Path to log file.
*
* @return True if file was deleted successfully, false if not.
*/
bool RedLog::DeleteFile(std::string& path) {
return std::remove(path.c_str()) == 0 ? true : false;
}
/**
* @brief NewNote
*
* Uses to add new line to log file.
*
* Date and time will be added automatically.
*
* @param group Group of module you logging.
* @param note String which will be added to log file.
*/
void RedLog::NewNote(std::string *group, std::string *note) {
// Getting necessary variables.
std::string time = _GetCurrentTime();
std::string res = "\n";
// Creating string.
/// Inserting time.
res += "[" + time + "]";
/// Insering group if needed.
if (*group != REDLOG_NO_GROUP) {
res += "(" + *group + ")";
}
/// Inserting our note.
res += " " + *note + ".";
m_LogFile << res;
}
/**
* @brief NewNote
*
* Uses to add new line to log file.
*
* Date and time will be added automatically.
*
* @param group Group of module you logging.
* @param note String which will be added to log file.
*/
void RedLog::NewNote(std::string group, std::string note) {
// Getting necessary variables.
std::string time = _GetCurrentTime();
std::string res = "\n";
// Creating string.
/// Inserting time.
res += "[" + time + "]";
/// Insering group if needed.
if (group != REDLOG_NO_GROUP) {
res += "(" + group + ")";
}
/// Inserting our note.
res += " " + note + ".";
m_LogFile << res;
}
}