sparkle/libs/utils/log.hpp
2025-01-20 03:09:27 +05:00

54 lines
1.7 KiB
C++

#ifndef UTILS_LOG_HPP
#define UTILS_LOG_HPP
#include <string>
#include <chrono>
#include <iostream>
#include <iomanip>
#include <ctime>
using std::setfill, std::setw;
enum level { INFO, WARNING, ERROR, CRITICAL };
enum type { WEBSOCKET, NETWORK, API };
class Log {
public:
static void create(level lvl, type t, const std::string& message) {
#ifdef DEBUG
std::string color;
switch (lvl) {
case INFO: color = "\033[34;1m"; break;
case WARNING: color = "\033[33;1m"; break;
case ERROR: color = "\033[31;1m"; break;
case CRITICAL: color = "\033[31;1;2m"; break;
default: color = "\033[0m"; break;
}
std::cout << color << "[" << getCurrentTime() << "][" << str(t) << "][" << str(lvl) << "] \033[0m" << message << "\033[0m" << std::endl;
#endif
}
private:
static std::string getCurrentTime() {
std::time_t now_c = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm* timer = std::localtime(&now_c);
std::ostringstream oss;
oss << setfill('0') << setw(2) << timer->tm_hour << ":" << setfill('0') << setw(2) << timer->tm_min << ":" << setfill('0') << setw(2) << timer->tm_sec;
return oss.str();
}
static std::string str(const level& lvl) {
switch (lvl) {
case INFO: return "INFO";
case WARNING: return "WARNING";
case ERROR: return "ERROR";
case CRITICAL: return "CRITICAL";
default: return "UNKNOWN";
}
}
static std::string str(const type& t) {
switch (t) {
case WEBSOCKET: return "WEBSOCKET";
case NETWORK: return "NETWORK";
case API: return "API";
default: return "UNKNOWN";
}
}
};
#endif