mirror of
https://github.com/yawaflua/SpCloudCore.git
synced 2025-12-08 19:49:28 +02:00
Add Logger
This commit is contained in:
@@ -5,24 +5,91 @@
|
||||
|
||||
#include "SpCloudMain.h"
|
||||
#include "httplib.h"
|
||||
#include "spdlog.h"
|
||||
#include "basic_file_sink.h"
|
||||
#include "Controllers/PublishController.cpp"
|
||||
//#include "Service/AuthorizationService.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"); //
|
||||
|
||||
std::cout << "SpCloud start\n";
|
||||
|
||||
httplib::Server svr;
|
||||
|
||||
logger.log(INFO, "Program started.");
|
||||
|
||||
svr.Get("/ping", [](const httplib::Request& req, httplib::Response& res)
|
||||
{
|
||||
std::cout << "Ping-\n";
|
||||
logger.log(INFO, "App was pinged.");
|
||||
|
||||
res.set_content("Pong", "text/plain");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user