Skip to content

Commit daa0852

Browse files
committed
♻️ Refactor log source code and add a const to avoid a useless copy
1 parent 3f4aaf3 commit daa0852

File tree

1 file changed

+145
-147
lines changed

1 file changed

+145
-147
lines changed

Plugin/Utilities/src/log.cpp

Lines changed: 145 additions & 147 deletions
Original file line numberDiff line numberDiff line change
@@ -5,151 +5,149 @@
55
#define PROJECT_NAME ""
66
#endif
77

8-
extern "C" {
9-
10-
void GetLastLog(const char *&data, logType type) {
11-
Log::log().getMergedLog(data, type);
12-
}
13-
14-
Log &Log::log() {
15-
// Static are guarantee to be created only once and is thread safe
16-
// To be sure this is the only instance even across multiple processes
17-
// the declaration / initialisation must be declared in a compiled unit
18-
// ! This function should not be inlined !
19-
static Log sInstance{};
20-
return sInstance;
21-
}
22-
23-
Log::Log() {
24-
_logFile.open("log.txt", std::ios::out);
25-
26-
std::string projectName = PROJECT_NAME;
27-
28-
// get time now
29-
time_t now = time(NULL);
30-
tm now_tm = {};
31-
char str[26] = {};
32-
localtime_s(&now_tm, &now);
33-
asctime_s(str, 26, &now_tm);
34-
35-
_logFile << "Log file - Project : " << projectName << " - Begin at : " << str
36-
<< std::endl;
37-
38-
for (int i = 0; i < 128; i++)
39-
_logFile << "-";
40-
_logFile << std::endl;
41-
42-
_logInfoVector.reserve(MAX_LOG_SIZE);
43-
_logWarningVector.reserve(MAX_LOG_SIZE);
44-
_logErrorVector.reserve(MAX_LOG_SIZE);
45-
_logBufferSum.reserve(128);
46-
_infoPrintFunction = [](std::string s, std::fstream& logFile) {
47-
logFile << s << std::endl;
48-
std::cout << s << std::endl;
49-
};
50-
_warningPrintFunction = [](std::string s, std::fstream& logFile) {
51-
logFile << s << std::endl;
52-
std::cout << s << std::endl;
53-
};
54-
55-
_errorPrintFunction = [](std::string s, std::fstream& logFile) {
56-
logFile << s << std::endl;
57-
std::cout << s << std::endl;
58-
};
59-
}
60-
61-
Log::~Log() {
62-
std::string projectName = PROJECT_NAME;
63-
64-
for (int i = 0; i < 128; i++)
65-
_logFile << "-";
66-
_logFile << std::endl;
67-
68-
// get time now
69-
time_t now = time(NULL);
70-
tm now_tm = {};
71-
char str[26] = {};
72-
localtime_s(&now_tm, &now);
73-
asctime_s(str, 26, &now_tm);
74-
75-
_logFile << "End Log file - Project : " << projectName
76-
<< " - End at : " << str << std::endl;
77-
_logFile.close();
78-
}
79-
80-
81-
void Log::setInfoPrintFunction(std::function<void(std::string, std::fstream&)> printFunc) {
82-
_infoPrintFunction = printFunc;
83-
}
84-
85-
86-
void Log::setWarningPrintFunction(std::function<void(std::string, std::fstream&)> printFunc) {
87-
_warningPrintFunction = printFunc;
88-
}
89-
90-
91-
void Log::setErrorPrintFunction(std::function<void(std::string, std::fstream&)> printFunc) {
92-
_errorPrintFunction = printFunc;
93-
}
94-
95-
void Log::debugLog(const std::string text) {
96-
_infoPrintFunction(text, _logFile);
97-
98-
if (_logInfoVector.size() >= MAX_LOG_SIZE)
99-
_logInfoVector.pop_back();
100-
101-
_logInfoVector.push_back(text);
102-
}
103-
104-
void Log::debugLogWarning(const std::string text) {
105-
_warningPrintFunction("Warning " + text, _logFile);
106-
// _logFile << "Warning : " << text << std::endl;
107-
108-
if (_logWarningVector.size() >= MAX_LOG_SIZE)
109-
_logWarningVector.pop_back();
110-
111-
_logWarningVector.push_back(text);
112-
}
113-
114-
void Log::debugLogError(const std::string text) {
115-
_errorPrintFunction("Error " + text, _logFile);
116-
// _logFile << "Error : " << text << std::endl;
117-
if (_logErrorVector.size() >= MAX_LOG_SIZE)
118-
_logErrorVector.pop_back();
119-
120-
_logErrorVector.push_back(text);
121-
}
122-
123-
void Log::getMergedLog(const char *&data, logType logType) {
124-
switch (logType) {
125-
case logType::Info: {
126-
extractLog(_logInfoVector);
127-
break;
128-
}
129-
case logType::Warning: {
130-
extractLog(_logWarningVector);
131-
break;
132-
}
133-
case logType::Error: {
134-
extractLog(_logErrorVector);
135-
break;
136-
}
137-
default:
138-
debugLogError("Unknown log type");
139-
return;
140-
}
141-
142-
data = _logBufferSum.c_str();
143-
}
144-
145-
void Log::extractLog(std::vector<std::string> &logVector) {
146-
_logBufferSum.clear();
147-
_logBufferSum.reserve(128 * logVector.size());
148-
for (auto &&str : logVector) {
149-
_logBufferSum += str + "\n";
150-
}
151-
152-
// we'll clear log
153-
logVector.clear();
154-
}
8+
extern "C"
9+
{
10+
11+
void GetLastLog(const char*& data, logType type) { Log::log().getMergedLog(data, type); }
12+
13+
Log& Log::log()
14+
{
15+
// Static are guarantee to be created only once and is thread safe
16+
// To be sure this is the only instance even across multiple processes
17+
// the declaration / initialisation must be declared in a compiled unit
18+
// ! This function should not be inlined !
19+
static Log sInstance{};
20+
return sInstance;
21+
}
22+
23+
Log::Log()
24+
{
25+
_logFile.open("log.txt", std::ios::out);
26+
27+
std::string projectName = PROJECT_NAME;
28+
29+
// get time now
30+
time_t now = time(NULL);
31+
tm now_tm = {};
32+
char str[26] = {};
33+
localtime_s(&now_tm, &now);
34+
asctime_s(str, 26, &now_tm);
35+
36+
_logFile << "Log file - Project : " << projectName << " - Begin at : " << str << std::endl;
37+
38+
for (int i = 0; i < 128; i++) _logFile << "-";
39+
_logFile << std::endl;
40+
41+
_logInfoVector.reserve(MAX_LOG_SIZE);
42+
_logWarningVector.reserve(MAX_LOG_SIZE);
43+
_logErrorVector.reserve(MAX_LOG_SIZE);
44+
_logBufferSum.reserve(128);
45+
_infoPrintFunction = [](const std::string& msg, std::fstream& logFile) {
46+
logFile << msg << std::endl;
47+
std::cout << msg << std::endl;
48+
};
49+
_warningPrintFunction = [](const std::string& msg, std::fstream& logFile) {
50+
logFile << msg << std::endl;
51+
std::cout << msg << std::endl;
52+
};
53+
54+
_errorPrintFunction = [](const std::string& msg, std::fstream& logFile) {
55+
logFile << msg << std::endl;
56+
std::cout << msg << std::endl;
57+
};
58+
}
59+
60+
Log::~Log()
61+
{
62+
std::string projectName = PROJECT_NAME;
63+
64+
for (int i = 0; i < 128; i++) _logFile << "-";
65+
_logFile << std::endl;
66+
67+
// get time now
68+
time_t now = time(NULL);
69+
tm now_tm = {};
70+
char str[26] = {};
71+
localtime_s(&now_tm, &now);
72+
asctime_s(str, 26, &now_tm);
73+
74+
_logFile << "End Log file - Project : " << projectName << " - End at : " << str
75+
<< std::endl;
76+
_logFile.close();
77+
}
78+
79+
void Log::setInfoPrintFunction(std::function<void(std::string, std::fstream&)> printFunc)
80+
{
81+
_infoPrintFunction = printFunc;
82+
}
83+
84+
void Log::setWarningPrintFunction(std::function<void(std::string, std::fstream&)> printFunc)
85+
{
86+
_warningPrintFunction = printFunc;
87+
}
88+
89+
void Log::setErrorPrintFunction(std::function<void(std::string, std::fstream&)> printFunc)
90+
{
91+
_errorPrintFunction = printFunc;
92+
}
93+
94+
void Log::debugLog(const std::string text)
95+
{
96+
_infoPrintFunction(text, _logFile);
97+
98+
if (_logInfoVector.size() >= MAX_LOG_SIZE) _logInfoVector.pop_back();
99+
100+
_logInfoVector.push_back(text);
101+
}
102+
103+
void Log::debugLogWarning(const std::string text)
104+
{
105+
_warningPrintFunction("Warning " + text, _logFile);
106+
// _logFile << "Warning : " << text << std::endl;
107+
108+
if (_logWarningVector.size() >= MAX_LOG_SIZE) _logWarningVector.pop_back();
109+
110+
_logWarningVector.push_back(text);
111+
}
112+
113+
void Log::debugLogError(const std::string text)
114+
{
115+
_errorPrintFunction("Error " + text, _logFile);
116+
// _logFile << "Error : " << text << std::endl;
117+
if (_logErrorVector.size() >= MAX_LOG_SIZE) _logErrorVector.pop_back();
118+
119+
_logErrorVector.push_back(text);
120+
}
121+
122+
void Log::getMergedLog(const char*& data, logType logType)
123+
{
124+
switch (logType)
125+
{
126+
case logType::Info: {
127+
extractLog(_logInfoVector);
128+
break;
129+
}
130+
case logType::Warning: {
131+
extractLog(_logWarningVector);
132+
break;
133+
}
134+
case logType::Error: {
135+
extractLog(_logErrorVector);
136+
break;
137+
}
138+
default: debugLogError("Unknown log type"); return;
139+
}
140+
141+
data = _logBufferSum.c_str();
142+
}
143+
144+
void Log::extractLog(std::vector<std::string>& logVector)
145+
{
146+
_logBufferSum.clear();
147+
_logBufferSum.reserve(128 * logVector.size());
148+
for (auto&& str : logVector) { _logBufferSum += str + "\n"; }
149+
150+
// we'll clear log
151+
logVector.clear();
152+
}
155153
}

0 commit comments

Comments
 (0)