| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091 |
- export module Core.Logger;
- import Core.TerminalConstants;
- import Core.Meta;
- import Core.ToString;
- import Core.Std;
- import Core.StringJoin;
- #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) noexcept;
- void setReportHandler(ReportHandler h, void* data) noexcept;
- struct FormatLocation final {
- const char* format;
- std::source_location location;
- FormatLocation(const char* f, SOURCE) noexcept :
- format(f), location(sl) {
- }
- };
- template<typename... Args>
- void report(
- LogLevel l, const FormatLocation& format, Args&&... args) noexcept {
- String<512> s;
- s.addFormat(format.format, Core::forward<Args>(args)...);
- callReportHandler(l, s, format.location);
- }
- const char* getShortFileName(const char* s) noexcept;
- template<typename... Args>
- void log(
- LogLevel l, const char* prefix, const FormatLocation& format,
- Args&&... args) noexcept {
- 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) noexcept {
- if constexpr(LOG_LEVEL >= 1) {
- log(LogLevel::ERROR, mergeStrings(Terminal::FG_RED, "[ERROR] "),
- format, forward<Args>(args)...);
- }
- }
- template<typename... Args>
- void logWarning(const FormatLocation& format, Args&&... args) noexcept {
- if constexpr(LOG_LEVEL >= 2) {
- log(LogLevel::WARNING,
- mergeStrings(Terminal::FG_BRIGHT_YELLOW, "[WARNING] "), format,
- forward<Args>(args)...);
- }
- }
- template<typename... Args>
- void logInfo(const FormatLocation& format, Args&&... args) noexcept {
- if constexpr(LOG_LEVEL >= 3) {
- log(LogLevel::INFO, mergeStrings(Terminal::BOLD, "[INFO] "), format,
- forward<Args>(args)...);
- }
- }
- template<typename... Args>
- void logDebug(const FormatLocation& format, Args&&... args) noexcept {
- if constexpr(LOG_LEVEL >= 4) {
- log(LogLevel::DEBUG,
- mergeStrings(
- Terminal::FG_BRIGHT_BLACK, Terminal::BOLD, "[DEBUG] "),
- format, forward<Args>(args)...);
- }
- }
- }
|