fix logger

This commit is contained in:
Hepatica
2024-08-10 06:37:28 +02:00
parent 500e394507
commit 4e94641571

View File

@@ -14,71 +14,70 @@ enum LogLevel { DEBUG, INFO, WARNING, ERROR, CRITICAL };
class Logger { class Logger {
public: public:
// Constructor: Opens the log file in append mode // Constructor: Opens the log file in append mode
Logger(const string& filename) Logger(const string& filename)
{ {
logFile.open(filename, ios::app); logFile.open(filename, ios::app);
if (!logFile.is_open()) { if (!logFile.is_open()) {
cerr << "Error opening log file." << endl; cerr << "Error opening log file." << endl;
} }
} }
// Destructor: Closes the log file // Destructor: Closes the log file
~Logger() { logFile.close(); } ~Logger() { logFile.close(); }
// Logs a message with a given log level // Logs a message with a given log level
void log(LogLevel level, const string& message) void log(LogLevel level, const string& message)
{ {
// Get current timestamp // Get current timestamp
time_t now = time(0); time_t now = time(0);
tm* timeinfo = localtime(&now); tm* timeinfo = localtime(&now);
char timestamp[20]; char timestamp[20];
strftime(timestamp, sizeof(timestamp), strftime(timestamp, sizeof(timestamp),
"%Y-%m-%d %H:%M:%S", timeinfo); "%Y-%m-%d %H:%M:%S", timeinfo);
// Create log entry // Create log entry
ostringstream logEntry; ostringstream logEntry;
logEntry << "[" << timestamp << "] " logEntry << "[" << timestamp << "] "
<< levelToString(level) << ": " << message << levelToString(level) << ": " << message
<< endl; << endl;
// Output to console // Output to console
cout << logEntry.str(); cout << logEntry.str();
// Output to log file // Output to log file
if (logFile.is_open()) { if (logFile.is_open()) {
logFile << logEntry.str(); logFile << logEntry.str();
logFile logFile.flush(); // Ensure immediate write to file
.flush(); // Ensure immediate write to file }
} }
}
private: private:
ofstream logFile; // File stream for the log file ofstream logFile; // File stream for the log file
// Converts log level to a string for output // Converts log level to a string for output
string levelToString(LogLevel level) string levelToString(LogLevel level)
{ {
switch (level) { switch (level) {
case DEBUG: case DEBUG:
return "DEBUG"; return "DEBUG";
case INFO: case INFO:
return "INFO"; return "INFO";
case WARNING: case WARNING:
return "WARNING"; return "WARNING";
case ERROR: case ERROR:
return "ERROR"; return "ERROR";
case CRITICAL: case CRITICAL:
return "CRITICAL"; return "CRITICAL";
default: default:
return "UNKNOWN"; return "UNKNOWN";
} }
} }
}; };
int main() int main()
{ {
Logger logger("logfile.txt"); // Logger logger("logfile.txt");
std::cout << "SpCloud start\n"; std::cout << "SpCloud start\n";
@@ -86,23 +85,21 @@ int main()
logger.log(INFO, "Program started."); logger.log(INFO, "Program started.");
svr.Get("/ping", [](const httplib::Request& req, httplib::Response& res) svr.Get("/ping", [&](const httplib::Request& req, httplib::Response& res)
{ {
std::cout << "Ping-\n"; std::cout << "Ping-\n";
logger.log(INFO, "App was pinged.");
logger.log(INFO, "App was pinged.");
res.set_content("Pong", "text/plain"); res.set_content("Pong", "text/plain");
httplib::Headers test = req.headers; httplib::Headers test = req.headers;
}); });
// Предполагается, что эти классы определены где-то еще
AuthorizationService authorization_service; AuthorizationService authorization_service;
FileProcessingService file_processing; FileProcessingService file_processing;
//PublishController publish_controller(svr, authorization_service, file_processing);
PublishController publish_controller(svr, authorization_service, file_processing); PublishController publish_controller(svr, authorization_service, file_processing);
std::cout << "Server is running at http://localhost:8080" << '\n'; std::cout << "Server is running at http://localhost:8080" << '\n';