-
Notifications
You must be signed in to change notification settings - Fork 1
/
Logger.cpp
85 lines (71 loc) · 2.84 KB
/
Logger.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
/**********************************************************************
OJ: An online judge server written with only C++ and MySQL.
Copyright (C) 2024 langningchen
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
**********************************************************************/
#include "Logger.hpp"
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
LOGGER::LOGGER() { SetLogFilename("Log.log"); }
LOGGER::~LOGGER() { fclose(LogFile); }
void LOGGER::SetLogFilename(std::string LogFilename) {
LogFile = fopen(LogFilename.c_str(), "a+");
if (LogFile == nullptr) {
LogFile = stdout;
this->LogFilename = "";
Error("Failed to open log file: " + LogFilename);
} else
this->LogFilename = LogFilename;
}
void LOGGER::Output(std::string Type, std::string Style, std::string Data) {
if (LogFile == nullptr)
LogFile = stdout;
if (Data.length() > 65535)
Data = Data.substr(0, 65535) + " ... (truncated)";
struct timeval CurrentSecond;
gettimeofday(&CurrentSecond, nullptr);
int MilliSecond = CurrentSecond.tv_usec / 1000;
char CurrentTime[80] = {0};
struct tm TempTime;
localtime_r(&CurrentSecond.tv_sec, &TempTime);
strftime(CurrentTime, sizeof(CurrentTime), "%Y-%m-%d %H:%M:%S", &TempTime);
char *Buffer = new char[Data.length() + 100];
if (Buffer == nullptr) {
std::cout << "Failed to allocate memory for log buffer" << std::endl;
return;
}
sprintf(Buffer, "\033[%sm%s[%s.%03d][%d-%d] %s\033[0m\n",
Style.c_str(), Type.c_str(),
CurrentTime, MilliSecond,
getpid(), gettid(),
Data.c_str());
OutputMutex.lock();
fprintf(LogFile, "%s", Buffer);
fflush(LogFile);
errno = 0;
if (Type == "W" || Type == "E")
printf("%s", Buffer);
OutputMutex.unlock();
delete[] Buffer;
}
void LOGGER::Debug(std::string Data) {
Output("D", "36", Data);
}
void LOGGER::Info(std::string Data) { Output("I", "32", Data); }
void LOGGER::Warning(std::string Data) { Output("W", "33", Data); }
void LOGGER::Error(std::string Data) { Output("E", "31", Data + ", " + std::to_string(errno) + ": " + std::string(strerror(errno))); }
void LOGGER::Fatal(std::string Data) {
Output("F", "1;4;5;31", Data);
exit(1);
}
LOGGER Logger;