#pragma once #include #ifdef __linux__ #include #endif #include #include "fmt/color.h" #include "fmt/core.h" namespace lg { #ifdef __linux__ struct LogTime { timeval tv; }; #else struct LogTime { time_t tim; }; #endif // Logging API enum class level { trace = 0, debug = 1, info = 2, warn = 3, error = 4, die = 5, off_unless_die = 6, off = 7 }; namespace internal { // log implementation stuff, not to be called by the user void log_message(level log_level, LogTime& now, const char* message); void log_print(const char* message); void log_vprintf(const char* format, va_list arg_list); } // namespace internal void set_file(const std::string& filename, const bool should_rotate = true, const bool append = false, const std::string& dir = ""); void set_flush_level(level log_level); void set_file_level(level log_level); void set_stdout_level(level log_level); void set_max_debug_levels(); void disable_ansi_colors(); void initialize(); void finish(); template void log(level log_level, const std::string& format, Args&&... args) { LogTime now; #ifdef __linux__ gettimeofday(&now.tv, nullptr); #else now.tim = time(nullptr); #endif std::string formatted_message = fmt::format(fmt::runtime(format), std::forward(args)...); internal::log_message(log_level, now, formatted_message.c_str()); } template void print(const std::string& format, Args&&... args) { std::string formatted_message = fmt::format(fmt::runtime(format), std::forward(args)...); internal::log_print(formatted_message.c_str()); } template void print(const fmt::text_style& ts, const std::string& format, Args&&... args) { std::string formatted_message = fmt::format(ts, format, std::forward(args)...); internal::log_print(formatted_message.c_str()); } // same as print but uses the C printf instead of fmt void printstd(const char* format, va_list arg_list); template void trace(const std::string& format, Args&&... args) { log(level::trace, format, std::forward(args)...); } template void debug(const std::string& format, Args&&... args) { log(level::debug, format, std::forward(args)...); } template void info(const std::string& format, Args&&... args) { log(level::info, format, std::forward(args)...); } template void warn(const std::string& format, Args&&... args) { log(level::warn, format, std::forward(args)...); } template void error(const std::string& format, Args&&... args) { log(level::error, format, std::forward(args)...); } template void die(const std::string& format, Args&&... args) { log(level::die, format, std::forward(args)...); } } // namespace lg