feat: update logger with new features

This commit is contained in:
2026-03-29 12:38:06 +08:00
parent 2cfbd1a03b
commit 01d6e91da9
11 changed files with 86 additions and 40 deletions

View File

@@ -6,10 +6,10 @@ namespace Assert {
std::string_view message = ""
) {
LOG::error("Assertion failed: {} at {}: {} in function {}",
Logger::error("Assertion failed: {} at {}: {} in function {}",
condition, file, line, func);
if (message.size()) {
LOG::error("Message: {}", message);
Logger::error("Message: {}", message);
}
std::abort();

View File

@@ -2,16 +2,25 @@
#include <iostream>
#include <chrono>
#include <format>
#include <source_location>
#include <string>
namespace LOG {
namespace Logger {
enum class Level {
TRACE,
DEBUG,
INFO,
ERROR,
WARN
};
template<typename... Args>
void info(const char * fmt, Args&&... args) {
inline void info(std::format_string<Args...> fmt, Args&&... args) {
auto now_time = std::chrono::
time_point_cast<std::chrono::seconds>
(std::chrono::system_clock::now());
std::string msg = std::vformat(fmt, std::make_format_args(args...));
std::string msg = std::vformat(fmt.get(), std::make_format_args(args...));
std::cout << "\033[1;32m"
<< std::format("[INFO][{:%Y-%m-%d %H:%M:%S}]", now_time)
<< msg
@@ -20,11 +29,11 @@ namespace LOG {
}
template<typename... Args>
void error(const char * fmt, Args&&... args) {
inline void error(std::format_string<Args...> fmt, Args&&... args) {
auto now_time = std::chrono::
time_point_cast<std::chrono::seconds>
(std::chrono::system_clock::now());
std::string msg = std::vformat(fmt, std::make_format_args(args...));
std::string msg = std::vformat(fmt.get(), std::make_format_args(args...));
std::cerr << "\033[1;31m"
<< std::format("[ERROR][{:%Y-%m-%d %H:%M:%S}]", now_time)
<< msg
@@ -34,11 +43,11 @@ namespace LOG {
}
template<typename... Args>
void warn(const char * fmt, Args&&... args) {
inline void warn(std::format_string<Args...> fmt, Args&&... args) {
auto now_time = std::chrono::
time_point_cast<std::chrono::seconds>
(std::chrono::system_clock::now());
std::string msg = std::vformat(fmt, std::make_format_args(args...));
std::string msg = std::vformat(fmt.get(), std::make_format_args(args...));
std::cout << "\033[1;33m"
<< std::format("[WARN][{:%Y-%m-%d %H:%M:%S}]", now_time)
<< msg
@@ -46,4 +55,41 @@ namespace LOG {
<< std::endl;
}
template<typename... Args>
inline void log(Level level, std::source_location loc, std::format_string<Args...> fmt, Args&&... args) {
auto now_time = std::chrono::
time_point_cast<std::chrono::seconds>
(std::chrono::system_clock::now());
std::string msg = std::vformat(fmt.get(), std::make_format_args(args...));
switch (level) {
case Logger::Level::TRACE:
std::cout << "\033[1;34m"
<< std::format("[TRACE][{:%Y-%m-%d %H:%M:%S}]", now_time)
<< "[" << loc.file_name() << ":" << loc.line() << "]"
<< "[" << loc.function_name() << "]"
<< msg
<< "\033[0m"
<< "\n";
break;
case Logger::Level::DEBUG:
std::cout << "\033[1;34m"
<< std::format("[DEBUG][{:%Y-%m-%d %H:%M:%S}]", now_time)
<< msg
<< "\033[0m"
<< "\n";
break;
case Logger::Level::INFO:
info(fmt, std::forward<Args...>(args)...);
break;
case Logger::Level::WARN:
warn(fmt, std::forward<Args...>(args)...);
break;
case Logger::Level::ERROR:
error(fmt, std::forward<Args...>(args)...);
break;
}
}
}