| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- export module Core.Logger;
- import Core.TerminalConstants;
- import Core.Meta;
- import Core.ToString;
- import Core.Std;
- #define SOURCE const std::source_location& sl = std::source_location::current()
- export namespace Core {
- enum class LogLevel { NONE, ERROR, WARNING, INFO, DEBUG };
- extern LogLevel logLevel;
- using ReportHandler = void (*)(
- LogLevel l, const std::source_location& sl, void* data,
- const char* message);
- void callReportHandler(LogLevel l, const char* report, SOURCE);
- void setReportHandler(ReportHandler h, void* data);
- struct FormatLocation final {
- const char* format;
- std::source_location location;
- FormatLocation(const char* f, SOURCE) : format(f), location(sl) {
- }
- };
- template<typename... Args>
- void report(LogLevel l, const FormatLocation& format, Args&&... args) {
- String<512> s;
- s.addFormat(format.format, Core::forward<Args>(args)...);
- callReportHandler(l, s, format.location);
- }
- const char* getShortFileName(const char* s);
- template<typename... Args>
- void log(
- LogLevel l, const char* prefix, const FormatLocation& format,
- Args&&... args) {
- if(logLevel < l) {
- return;
- }
- const char* file = getShortFileName(format.location.file_name());
- fputs(prefix, stdout);
- print("{}:{} | ", file, format.location.line());
- String<512> s;
- s.addFormat(format.format, Core::forward<Args>(args)...);
- s.print();
- puts(Terminal::RESET);
- }
- template<typename... Args>
- void logError(const FormatLocation& format, Args&&... args) {
- if constexpr(LOG_LEVEL >= 1) {
- String<32> s;
- s.addFormat("{}[ERROR] ", Terminal::FG_RED);
- log(LogLevel::ERROR, s, format, forward<Args>(args)...);
- }
- }
- template<typename... Args>
- void logWarning(const FormatLocation& format, Args&&... args) {
- if constexpr(LOG_LEVEL >= 2) {
- String<32> s;
- s.addFormat("{}[WARNING] ", Terminal::FG_BRIGHT_YELLOW);
- log(LogLevel::WARNING, s, format, forward<Args>(args)...);
- }
- }
- template<typename... Args>
- void logInfo(const FormatLocation& format, Args&&... args) {
- if constexpr(LOG_LEVEL >= 3) {
- String<32> s;
- s.addFormat("{}[INFO] ", Terminal::BOLD);
- log(LogLevel::INFO, s, format, forward<Args>(args)...);
- }
- }
- template<typename... Args>
- void logDebug(const FormatLocation& format, Args&&... args) {
- if constexpr(LOG_LEVEL >= 4) {
- String<32> s;
- s.addFormat(
- "{}{}[DEBUG] ", Terminal::BOLD, Terminal::FG_BRIGHT_BLACK);
- log(LogLevel::DEBUG, s, format, forward<Args>(args)...);
- }
- }
- }
|