diff options
Diffstat (limited to 'src/debug')
| -rw-r--r-- | src/debug/format.hpp | 51 | ||||
| -rw-r--r-- | src/debug/log.hpp | 37 |
2 files changed, 88 insertions, 0 deletions
diff --git a/src/debug/format.hpp b/src/debug/format.hpp new file mode 100644 index 0000000..f857f96 --- /dev/null +++ b/src/debug/format.hpp @@ -0,0 +1,51 @@ +#pragma once +#include <format> +#include <string> + +enum eFormatType : int8_t { + NONE, + TRACE, + LOG, + WARN, + ERR, + SYS, + NONEWLINE +}; + +namespace NFormatter { +inline std::string fmt(eFormatType type, std::string str) { + std::string prefix; + switch (type) { + case LOG: + prefix = "[LOG] "; + break; + case WARN: + prefix = "[WARN] "; + break; + case ERR: + prefix = "[ERROR] "; + break; + case SYS: + prefix = "[SYSTEM] "; + break; +#ifdef DEBUG + case TRACE: + prefix = "[DEBUG] "; + break; +#endif + default: + prefix = ""; + break; + } + + if (!str.empty() && str.back() != '\n' && type != NONEWLINE) + str += '\n'; + + return prefix + str; +} + +template <typename... Args> +std::string fmt(eFormatType type, std::format_string<Args...> fmt, Args &&...args) { + return NFormatter::fmt(type, std::format(fmt, std::forward<Args>(args)...)); +} +} // namespace NFormatter diff --git a/src/debug/log.hpp b/src/debug/log.hpp new file mode 100644 index 0000000..71a958e --- /dev/null +++ b/src/debug/log.hpp @@ -0,0 +1,37 @@ +#pragma once +#include "../io/IOManager.hpp" +#include "format.hpp" +#include <print> +#include <string> + +inline void log(FILE *_stream, eFormatType type, std::string str) { +#ifndef DEBUG + if (type == TRACE) + return; +#endif + if (g_pIOManager) + g_pIOManager->addLog({.type = type, .log = str}); + else + std::println(_stream, "{}", NFormatter::fmt(type, str)); +} + +template <typename... Args> +void log(FILE *_stream, eFormatType type, std::format_string<Args...> fmt, Args &&...args) { +#ifndef DEBUG + if (type == TRACE) + return; +#endif + if (g_pIOManager) + g_pIOManager->addLog({.type = type, .log = NFormatter::fmt(NONEWLINE, fmt, std::forward<Args>(args)...)}); + else + std::println(_stream, "{}", NFormatter::fmt(type, fmt, std::forward<Args>(args)...)); +} + +inline void log(eFormatType type, std::string str) { + log(type == ERR ? stderr : stdout, type, str); +} + +template <typename... Args> +void log(eFormatType type, std::format_string<Args...> fmt, Args &&...args) { + log(type == ERR ? stderr : stdout, type, fmt, std::forward<Args>(args)...); +} |
