diff options
author | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-31 18:41:30 +0100 |
---|---|---|
committer | Loek Le Blansch <loek@pipeframe.xyz> | 2024-10-31 18:41:30 +0100 |
commit | 8e3367b186e60eb1e33bf58a066823cb00a7566e (patch) | |
tree | c4038a31993767276efec5fa1b1a37dff3b79465 /src/crepe/util | |
parent | b7df77d6cc26cb9ee46891d7108f01734b3104dd (diff) | |
parent | 35ef3ba91ce9e00466508f2388f4c1dd2321b505 (diff) |
Merge branch 'master' into poc/audio-miniaudio
Diffstat (limited to 'src/crepe/util')
-rw-r--r-- | src/crepe/util/CMakeLists.txt | 12 | ||||
-rw-r--r-- | src/crepe/util/color.cpp | 91 | ||||
-rw-r--r-- | src/crepe/util/color.h | 51 | ||||
-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 | 53 | ||||
-rw-r--r-- | src/crepe/util/log.h | 40 |
7 files changed, 290 insertions, 0 deletions
diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt new file mode 100644 index 0000000..bbeaad9 --- /dev/null +++ b/src/crepe/util/CMakeLists.txt @@ -0,0 +1,12 @@ +target_sources(crepe PUBLIC + color.cpp + 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.cpp b/src/crepe/util/color.cpp new file mode 100644 index 0000000..a7bbc81 --- /dev/null +++ b/src/crepe/util/color.cpp @@ -0,0 +1,91 @@ +#include <cstdarg> + +#include "../api/Config.h" +#include "color.h" +#include "fmt.h" + +using namespace crepe::util; +using namespace std; + +static constexpr const char * RESET_CODE = "\e[0m"; + +const string LogColor::str(const string & content) { + auto & cfg = api::Config::get_instance(); + string out = content; + if (cfg.log.color) out = this->code + out; + if (content.size() == 0) return out; + if (cfg.log.color) out = out + RESET_CODE; + return out; +} + +const char * LogColor::c_str(const char * content) { + this->final = this->str(content == NULL ? "" : content); + return this->final.c_str(); +} + +const char * LogColor::fmt(const char * fmt, ...) { + va_list args; + va_start(args, fmt); + string content = va_stringf(args, fmt); + va_end(args); + return this->c_str(content.c_str()); +} + +LogColor & LogColor::add_code(unsigned int code) { + this->code += stringf("\e[%dm", code); + return *this; +} + +LogColor & LogColor::reset() { + this->code = RESET_CODE; + return *this; +} + +LogColor & LogColor::fg_black(bool bright) { + return this->add_code(bright ? 90 : 30); +} +LogColor & LogColor::fg_red(bool bright) { + return this->add_code(bright ? 91 : 31); +} +LogColor & LogColor::fg_green(bool bright) { + return this->add_code(bright ? 92 : 32); +} +LogColor & LogColor::fg_yellow(bool bright) { + return this->add_code(bright ? 93 : 33); +} +LogColor & LogColor::fg_blue(bool bright) { + return this->add_code(bright ? 94 : 34); +} +LogColor & LogColor::fg_magenta(bool bright) { + return this->add_code(bright ? 95 : 35); +} +LogColor & LogColor::fg_cyan(bool bright) { + return this->add_code(bright ? 96 : 36); +} +LogColor & LogColor::fg_white(bool bright) { + return this->add_code(bright ? 97 : 37); +} +LogColor & LogColor::bg_black(bool bright) { + return this->add_code(bright ? 100 : 40); +} +LogColor & LogColor::bg_red(bool bright) { + return this->add_code(bright ? 101 : 41); +} +LogColor & LogColor::bg_green(bool bright) { + return this->add_code(bright ? 102 : 42); +} +LogColor & LogColor::bg_yellow(bool bright) { + return this->add_code(bright ? 103 : 43); +} +LogColor & LogColor::bg_blue(bool bright) { + return this->add_code(bright ? 104 : 44); +} +LogColor & LogColor::bg_magenta(bool bright) { + return this->add_code(bright ? 105 : 45); +} +LogColor & LogColor::bg_cyan(bool bright) { + return this->add_code(bright ? 106 : 46); +} +LogColor & LogColor::bg_white(bool bright) { + return this->add_code(bright ? 107 : 47); +} diff --git a/src/crepe/util/color.h b/src/crepe/util/color.h new file mode 100644 index 0000000..91e1abe --- /dev/null +++ b/src/crepe/util/color.h @@ -0,0 +1,51 @@ +#pragma once + +#include <string> + +namespace crepe::util { + +class LogColor { +public: + LogColor() = default; + +public: + //! get color code as c-style string (or color content string) + const char * c_str(const char * content = NULL); + //! color printf-style format string + const char * fmt(const char * fmt, ...); + //! get color code as stl string (or color content string) + const std::string str(const std::string & content = ""); + +public: + //! reset color to default foreground and background color + LogColor & reset(); + +public: + LogColor & fg_black(bool bright = false); + LogColor & fg_red(bool bright = false); + LogColor & fg_green(bool bright = false); + LogColor & fg_yellow(bool bright = false); + LogColor & fg_blue(bool bright = false); + LogColor & fg_magenta(bool bright = false); + LogColor & fg_cyan(bool bright = false); + LogColor & fg_white(bool bright = false); + +public: + LogColor & bg_black(bool bright = false); + LogColor & bg_red(bool bright = false); + LogColor & bg_green(bool bright = false); + LogColor & bg_yellow(bool bright = false); + LogColor & bg_blue(bool bright = false); + LogColor & bg_magenta(bool bright = false); + LogColor & bg_cyan(bool bright = false); + LogColor & bg_white(bool bright = false); + +private: + LogColor & add_code(unsigned int code); + +private: + std::string code = ""; + std::string final = ""; +}; + +} // namespace crepe::util 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..6bcc4ae --- /dev/null +++ b/src/crepe/util/log.cpp @@ -0,0 +1,53 @@ +#include <cstdarg> +#include <cstdio> +#include <cstdlib> +#include <string> + +#include "../api/Config.h" +#include "fmt.h" +#include "log.h" + +using namespace crepe::util; +using namespace std; + +string log_prefix(LogLevel level) { + switch (level) { + case LogLevel::TRACE: + return LogColor().fg_white().str("[TRACE]") + " "; + case LogLevel::DEBUG: + return LogColor().fg_magenta().str("[DEBUG]") + " "; + case LogLevel::INFO: + return LogColor().fg_blue().str("[INFO]") + " "; + case LogLevel::WARNING: + return LogColor().fg_yellow().str("[WARN]") + " "; + case LogLevel::ERROR: + return LogColor().fg_red().str("[ERROR]") + " "; + } + return ""; +} + +static void log(LogLevel level, const string msg) { + auto & cfg = crepe::api::Config::get_instance(); + if (level < cfg.log.level) return; + + string out = 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(LogLevel::DEBUG, va_stringf(args, fmt)); + va_end(args); +} + +void crepe::util::logf(LogLevel 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..308ba96 --- /dev/null +++ b/src/crepe/util/log.h @@ -0,0 +1,40 @@ +#pragma once + +// allow user to disable debug macros +#ifndef CREPE_DISABLE_MACROS + +#include "color.h" + +// utility macros +#define _crepe_logf_here(level, format, ...) \ + crepe::util::logf( \ + level, "%s" format, \ + crepe::util::LogColor().fg_white(false).fmt( \ + "%s (%s:%d)", __PRETTY_FUNCTION__, __FILE_NAME__, __LINE__), \ + __VA_ARGS__) + +// very illegal global function-style macros +// NOLINTBEGIN +#define dbg_logf(fmt, ...) \ + _crepe_logf_here(crepe::util::LogLevel::DEBUG, ": " fmt, __VA_ARGS__) +#define dbg_log(str) \ + _crepe_logf_here(crepe::util::LogLevel::DEBUG, "%s: " str, "") +#define dbg_trace() _crepe_logf_here(crepe::util::LogLevel::TRACE, "%s", "") +// NOLINTEND + +#endif + +namespace crepe::util { + +enum LogLevel { + TRACE, + DEBUG, + INFO, + WARNING, + ERROR, +}; + +void logf(const char * fmt, ...); +void logf(enum LogLevel level, const char * fmt, ...); + +} // namespace crepe::util |