Move logger to file and adjust path string in publish contoller

This commit is contained in:
Hepatica
2024-08-11 01:59:54 +02:00
parent 4e94641571
commit 1f3086c96c
5 changed files with 204 additions and 127 deletions

View File

@@ -7,77 +7,15 @@
#include "httplib.h"
#include "Controllers/PublishController.cpp"
//#include "Service/AuthorizationService.cpp"
#include "Service/Logger.cpp"
//#include "Service/FileProcessingService.cpp"
using namespace std;
enum LogLevel { DEBUG, INFO, WARNING, ERROR, CRITICAL };
class Logger {
public:
// Constructor: Opens the log file in append mode
Logger(const string& filename)
{
logFile.open(filename, ios::app);
if (!logFile.is_open()) {
cerr << "Error opening log file." << endl;
}
}
// Destructor: Closes the log file
~Logger() { logFile.close(); }
// Logs a message with a given log level
void log(LogLevel level, const string& message)
{
// Get current timestamp
time_t now = time(0);
tm* timeinfo = localtime(&now);
char timestamp[20];
strftime(timestamp, sizeof(timestamp),
"%Y-%m-%d %H:%M:%S", timeinfo);
// Create log entry
ostringstream logEntry;
logEntry << "[" << timestamp << "] "
<< levelToString(level) << ": " << message
<< endl;
// Output to console
cout << logEntry.str();
// Output to log file
if (logFile.is_open()) {
logFile << logEntry.str();
logFile.flush(); // Ensure immediate write to file
}
}
private:
ofstream logFile; // File stream for the log file
// Converts log level to a string for output
string levelToString(LogLevel level)
{
switch (level) {
case DEBUG:
return "DEBUG";
case INFO:
return "INFO";
case WARNING:
return "WARNING";
case ERROR:
return "ERROR";
case CRITICAL:
return "CRITICAL";
default:
return "UNKNOWN";
}
}
};
int main()
{
Logger logger("logfile.txt");
string logger_name = "logfile.txt";
Logger logger(logger_name);
std::cout << "SpCloud start\n";
@@ -96,8 +34,8 @@ int main()
httplib::Headers test = req.headers;
});
// Предполагается, что эти классы определены где-то еще
AuthorizationService authorization_service;
FileProcessingService file_processing;
PublishController publish_controller(svr, authorization_service, file_processing);