aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/crepe/util')
-rw-r--r--src/crepe/util/CMakeLists.txt3
-rw-r--r--src/crepe/util/color.cpp91
-rw-r--r--src/crepe/util/color.h88
-rw-r--r--src/crepe/util/fmt.cpp33
-rw-r--r--src/crepe/util/fmt.h10
-rw-r--r--src/crepe/util/log.cpp53
-rw-r--r--src/crepe/util/log.h24
7 files changed, 228 insertions, 74 deletions
diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt
index 100f028..bbeaad9 100644
--- a/src/crepe/util/CMakeLists.txt
+++ b/src/crepe/util/CMakeLists.txt
@@ -1,9 +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
index 066c9d3..91e1abe 100644
--- a/src/crepe/util/color.h
+++ b/src/crepe/util/color.h
@@ -1,41 +1,51 @@
#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
+#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
index f91d52c..6bcc4ae 100644
--- a/src/crepe/util/log.cpp
+++ b/src/crepe/util/log.cpp
@@ -3,48 +3,51 @@
#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 const char * const LOG_PREFIX[] = {
- [log_level::DEBUG] = "[DBG] ",
- [log_level::INFO] = "[INFO] ",
- [log_level::WARNING] = "[WARN] ",
- [log_level::ERROR] = "[ERR] ",
-};
-
-static void va_logf(enum log_level level, va_list args, const std::string fmt) {
- va_list args_copy;
- va_copy(args_copy, args);
-
- // prepend log level and ensure newline
- std::string format_fixed = LOG_PREFIX[level] + fmt;
- if (!format_fixed.ends_with("\n")) format_fixed += "\n";
-
- size_t sz = vsnprintf(NULL, 0, format_fixed.c_str(), args_copy) + 1;
- char * msg = (char *) malloc(sz);
- va_end(args_copy);
+static void log(LogLevel level, const string msg) {
+ auto & cfg = crepe::api::Config::get_instance();
+ if (level < cfg.log.level) return;
- vsnprintf(msg, sz, format_fixed.c_str(), args);
+ string out = log_prefix(level) + msg;
+ if (!out.ends_with("\n")) out += "\n";
// TODO: also log to file or smth
- printf("%s", msg);
+ printf("%s", out.c_str());
fflush(stdout);
-
- free(msg);
}
void crepe::util::logf(const char * fmt, ...) {
va_list args;
va_start(args, fmt);
- va_logf(crepe::util::log_level::DEBUG, args, fmt);
+ log(LogLevel::DEBUG, va_stringf(args, fmt));
va_end(args);
}
-void crepe::util::logf(log_level level, const char * fmt, ...) {
+void crepe::util::logf(LogLevel level, const char * fmt, ...) {
va_list args;
va_start(args, fmt);
- va_logf(level, 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
index 2b0fbe1..308ba96 100644
--- a/src/crepe/util/log.h
+++ b/src/crepe/util/log.h
@@ -6,24 +6,28 @@
#include "color.h"
// utility macros
-#define _crepe_logf_here(fmt, ...) \
- crepe::util::logf(util::log_level::DEBUG, "%s%s (%s:%d)" fmt "\n", \
- crepe::util::color::FG_WHITE, __PRETTY_FUNCTION__, \
- __FILE_NAME__, __LINE__, crepe::util::color::RESET, \
- __VA_ARGS__)
+#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(": " fmt, __VA_ARGS__)
-#define dbg_log(str) _crepe_logf_here(": %s", str)
-#define dbg_trace() _crepe_logf_here("%s", "")
+#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 log_level {
+enum LogLevel {
+ TRACE,
DEBUG,
INFO,
WARNING,
@@ -31,6 +35,6 @@ enum log_level {
};
void logf(const char * fmt, ...);
-void logf(enum log_level level, const char * fmt, ...);
+void logf(enum LogLevel level, const char * fmt, ...);
} // namespace crepe::util