sparkle/libs/utils/log.hpp

66 lines
2.5 KiB
C++
Raw Normal View History

2025-01-19 04:35:20 +05:00
#ifndef UTILS_LOG_HPP
#define UTILS_LOG_HPP
2025-01-14 05:18:08 +05:00
#include <string>
#include <chrono>
#include <iostream>
#include <iomanip>
#include <ctime>
2025-01-30 17:21:08 +05:00
#include <utils/types.hpp>
2025-01-30 23:05:59 +05:00
enum level : unsigned char { INFO, WARNING, ERROR, CRITICAL };
enum type : unsigned char { WEBSOCKET, NETWORK, API };
2025-01-19 04:35:20 +05:00
class Log {
2025-01-14 05:18:08 +05:00
public:
2025-01-30 23:05:59 +05:00
_maybe_unused static void create(_maybe_unused const level& lvl, _maybe_unused const type& t, _maybe_unused const std::string& message) {
2025-01-19 04:35:20 +05:00
#ifdef DEBUG
2025-01-14 05:18:08 +05:00
std::string color;
switch (lvl) {
2025-01-25 11:24:56 +05:00
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;
2025-01-14 05:18:08 +05:00
}
2025-01-18 23:31:16 +05:00
std::cout << color << "[" << getCurrentTime() << "][" << str(t) << "][" << str(lvl) << "] \033[0m" << message << "\033[0m" << std::endl;
2025-01-19 04:35:20 +05:00
#endif
2025-01-14 05:18:08 +05:00
}
2025-01-26 23:33:52 +05:00
static void force_create(const level& lvl, const type& t, const std::string& message) {
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;
}
2025-01-30 23:05:59 +05:00
std::cout << color << "[" << getCurrentTime() << "][" << str(t) << "][" << str(lvl) << "] \033[0m" << message << "\033[0m" << '\n';
2025-01-26 23:33:52 +05:00
}
2025-01-14 05:18:08 +05:00
private:
2025-01-30 23:05:59 +05:00
static auto getCurrentTime() -> const std::string {
2025-01-14 05:18:08 +05:00
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;
2025-01-25 11:24:56 +05:00
oss << std::setfill('0') << std::setw(2)
<< timer->tm_hour << ":" << std::setfill('0') << std::setw(2)
<< timer->tm_min << ":" << std::setfill('0') << std::setw(2)
<< timer->tm_sec;
2025-01-14 05:18:08 +05:00
return oss.str();
}
2025-01-30 23:05:59 +05:00
static auto str(const level& lvl) noexcept -> std::string {
2025-01-14 05:18:08 +05:00
switch (lvl) {
case INFO: return "INFO";
case WARNING: return "WARNING";
case ERROR: return "ERROR";
2025-01-18 23:31:16 +05:00
case CRITICAL: return "CRITICAL";
2025-01-14 05:18:08 +05:00
default: return "UNKNOWN";
}
}
2025-01-30 23:05:59 +05:00
static auto str(const type& t) -> std::string {
2025-01-14 05:18:08 +05:00
switch (t) {
case WEBSOCKET: return "WEBSOCKET";
case NETWORK: return "NETWORK";
2025-01-18 23:31:16 +05:00
case API: return "API";
2025-01-14 05:18:08 +05:00
default: return "UNKNOWN";
}
}
};
2025-01-30 23:05:59 +05:00
#endif