mirror of
https://github.com/yawaflua/SpCloudCore.git
synced 2025-12-08 19:49:28 +02:00
fix logger
This commit is contained in:
@@ -14,71 +14,70 @@ 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;
|
||||
}
|
||||
}
|
||||
// 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(); }
|
||||
// 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);
|
||||
// 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;
|
||||
// Create log entry
|
||||
ostringstream logEntry;
|
||||
logEntry << "[" << timestamp << "] "
|
||||
<< levelToString(level) << ": " << message
|
||||
<< endl;
|
||||
|
||||
// Output to console
|
||||
cout << logEntry.str();
|
||||
// Output to console
|
||||
cout << logEntry.str();
|
||||
|
||||
// Output to log file
|
||||
if (logFile.is_open()) {
|
||||
logFile << logEntry.str();
|
||||
logFile
|
||||
.flush(); // Ensure immediate write to file
|
||||
}
|
||||
}
|
||||
// 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
|
||||
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";
|
||||
}
|
||||
}
|
||||
// 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"); //
|
||||
Logger logger("logfile.txt");
|
||||
|
||||
std::cout << "SpCloud start\n";
|
||||
|
||||
@@ -86,23 +85,21 @@ int main()
|
||||
|
||||
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";
|
||||
logger.log(INFO, "App was pinged.");
|
||||
|
||||
logger.log(INFO, "App was pinged.");
|
||||
|
||||
res.set_content("Pong", "text/plain");
|
||||
|
||||
httplib::Headers test = req.headers;
|
||||
|
||||
});
|
||||
|
||||
// Предполагается, что эти классы определены где-то еще
|
||||
AuthorizationService authorization_service;
|
||||
|
||||
FileProcessingService 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';
|
||||
|
||||
Reference in New Issue
Block a user