diff options
author | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-10-23 21:15:58 +0200 |
---|---|---|
committer | WBoerenkamps <wrj.boerenkamps@student.avans.nl> | 2024-10-23 21:15:58 +0200 |
commit | b5e83d076f356c6d01b7bbc1f033db4850356c0d (patch) | |
tree | c4b11f86c6ab1685e46fab9d674377a39e612fd7 /src/crepe/util | |
parent | 51c8a51b53a850265955a3e4bc45b40ad3f8c477 (diff) | |
parent | 04a040e28ade412ea5b1767bf77eed3956121973 (diff) |
pull origin master
Diffstat (limited to 'src/crepe/util')
-rw-r--r-- | src/crepe/util/CMakeLists.txt | 11 | ||||
-rw-r--r-- | src/crepe/util/color.h | 41 | ||||
-rw-r--r-- | src/crepe/util/fmt.cpp | 33 | ||||
-rw-r--r-- | src/crepe/util/fmt.h | 10 | ||||
-rw-r--r-- | src/crepe/util/log.cpp | 40 | ||||
-rw-r--r-- | src/crepe/util/log.h | 36 |
6 files changed, 171 insertions, 0 deletions
diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt new file mode 100644 index 0000000..e2cffaf --- /dev/null +++ b/src/crepe/util/CMakeLists.txt @@ -0,0 +1,11 @@ +target_sources(crepe PUBLIC + log.cpp + fmt.cpp +) + +target_sources(crepe PUBLIC FILE_SET HEADERS FILES + color.h + log.h + fmt.h +) + diff --git a/src/crepe/util/color.h b/src/crepe/util/color.h new file mode 100644 index 0000000..066c9d3 --- /dev/null +++ b/src/crepe/util/color.h @@ -0,0 +1,41 @@ +#pragma once + +namespace crepe::util::color { + +constexpr const char * RESET = "\e[0m"; + +constexpr const char * FG_BLACK = "\e[30m"; +constexpr const char * FG_RED = "\e[31m"; +constexpr const char * FG_GREEN = "\e[32m"; +constexpr const char * FG_YELLOW = "\e[33m"; +constexpr const char * FG_BLUE = "\e[34m"; +constexpr const char * FG_MAGENTA = "\e[35m"; +constexpr const char * FG_CYAN = "\e[36m"; +constexpr const char * FG_WHITE = "\e[37m"; +constexpr const char * BG_BLACK = "\e[40m"; +constexpr const char * BG_RED = "\e[41m"; +constexpr const char * BG_GREEN = "\e[42m"; +constexpr const char * BG_YELLOW = "\e[43m"; +constexpr const char * BG_BLUE = "\e[44m"; +constexpr const char * BG_MAGENTA = "\e[45m"; +constexpr const char * BG_CYAN = "\e[46m"; +constexpr const char * BG_WHITE = "\e[47m"; + +constexpr const char * FG_BLACK_BRIGHT = "\e[90m"; +constexpr const char * FG_RED_BRIGHT = "\e[91m"; +constexpr const char * FG_GREEN_BRIGHT = "\e[92m"; +constexpr const char * FG_YELLOW_BRIGHT = "\e[93m"; +constexpr const char * FG_BLUE_BRIGHT = "\e[94m"; +constexpr const char * FG_MAGENTA_BRIGHT = "\e[95m"; +constexpr const char * FG_CYAN_BRIGHT = "\e[96m"; +constexpr const char * FG_WHITE_BRIGHT = "\e[97m"; +constexpr const char * BG_BLACK_BRIGHT = "\e[100m"; +constexpr const char * BG_RED_BRIGHT = "\e[101m"; +constexpr const char * BG_GREEN_BRIGHT = "\e[102m"; +constexpr const char * BG_YELLOW_BRIGHT = "\e[103m"; +constexpr const char * BG_BLUE_BRIGHT = "\e[104m"; +constexpr const char * BG_MAGENTA_BRIGHT = "\e[105m"; +constexpr const char * BG_CYAN_BRIGHT = "\e[106m"; +constexpr const char * BG_WHITE_BRIGHT = "\e[107m"; + +} // namespace crepe::util::color diff --git a/src/crepe/util/fmt.cpp b/src/crepe/util/fmt.cpp new file mode 100644 index 0000000..8ef1164 --- /dev/null +++ b/src/crepe/util/fmt.cpp @@ -0,0 +1,33 @@ +#include <cstdarg> +#include <cstdio> +#include <string> + +#include "fmt.h" + +using namespace std; + +string crepe::util::va_stringf(va_list args, const char * fmt) { + va_list args_copy; + va_copy(args_copy, args); + + size_t sz = vsnprintf(NULL, 0, fmt, args_copy) + 1; + char * msg = (char *) malloc(sz); + va_end(args_copy); + + vsnprintf(msg, sz, fmt, args); + + string out = msg; + free(msg); + + va_end(args); + + return out; +} + +string crepe::util::stringf(const char * fmt, ...) { + va_list args; + va_start(args, fmt); + string out = va_stringf(args, fmt); + va_end(args); + return out; +} diff --git a/src/crepe/util/fmt.h b/src/crepe/util/fmt.h new file mode 100644 index 0000000..44c426f --- /dev/null +++ b/src/crepe/util/fmt.h @@ -0,0 +1,10 @@ +#pragma once + +#include <string> + +namespace crepe::util { + +std::string va_stringf(va_list args, const char * fmt); +std::string stringf(const char * fmt, ...); + +} // namespace crepe::util diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp new file mode 100644 index 0000000..0c2ce1e --- /dev/null +++ b/src/crepe/util/log.cpp @@ -0,0 +1,40 @@ +#include <cstdarg> +#include <cstdio> +#include <cstdlib> +#include <string> + +#include "fmt.h" +#include "log.h" + +using namespace crepe::util; + +static const char * const LOG_PREFIX[] = { + [log_level::DEBUG] = "[DBG] ", + [log_level::INFO] = "[INFO] ", + [log_level::WARNING] = "[WARN] ", + [log_level::ERROR] = "[ERR] ", +}; + +static void log(enum log_level level, const std::string & msg) { + using namespace std; + string out = string(LOG_PREFIX[level]) + msg; + if (!out.ends_with("\n")) out += "\n"; + + // TODO: also log to file or smth + printf("%s", out.c_str()); + fflush(stdout); +} + +void crepe::util::logf(const char * fmt, ...) { + va_list args; + va_start(args, fmt); + log(log_level::DEBUG, va_stringf(args, fmt)); + va_end(args); +} + +void crepe::util::logf(log_level level, const char * fmt, ...) { + va_list args; + va_start(args, fmt); + log(level, va_stringf(args, fmt)); + va_end(args); +} diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h new file mode 100644 index 0000000..3e36f0a --- /dev/null +++ b/src/crepe/util/log.h @@ -0,0 +1,36 @@ +#pragma once + +// allow user to disable debug macros +#ifndef CREPE_DISABLE_MACROS + +#include "color.h" + +// utility macros +#define _crepe_logf_here(fmt, ...) \ + crepe::util::logf(util::log_level::DEBUG, "%s%s (%s:%d)%s" fmt "\n", \ + crepe::util::color::FG_WHITE, __PRETTY_FUNCTION__, \ + __FILE_NAME__, __LINE__, crepe::util::color::RESET, \ + __VA_ARGS__) + +// very illegal global function-style macros +// NOLINTBEGIN +#define dbg_logf(fmt, ...) _crepe_logf_here(": " fmt, __VA_ARGS__) +#define dbg_log(str) _crepe_logf_here("%s: " str, "") +#define dbg_trace() _crepe_logf_here("%s", "") +// NOLINTEND + +#endif + +namespace crepe::util { + +enum log_level { + DEBUG, + INFO, + WARNING, + ERROR, +}; + +void logf(const char * fmt, ...); +void logf(enum log_level level, const char * fmt, ...); + +} // namespace crepe::util |