aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-21 10:39:31 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-21 10:39:31 +0200
commit58dfdba241b501d4b1b9688b44ee775507ec325b (patch)
tree37e4943d8605770e971f83b84039e929be2d043f
parent77555730e3ddb811b9ce8470659663e3f1573de2 (diff)
add Config + wip rework color
-rw-r--r--src/crepe/api/CMakeLists.txt1
-rw-r--r--src/crepe/api/Config.h27
-rw-r--r--src/crepe/util/CMakeLists.txt1
-rw-r--r--src/crepe/util/color.cpp49
-rw-r--r--src/crepe/util/color.h79
-rw-r--r--src/crepe/util/log.cpp9
-rw-r--r--src/crepe/util/log.h11
-rw-r--r--src/example/script.cpp4
8 files changed, 137 insertions, 44 deletions
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 86623de..68c4b72 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -6,5 +6,6 @@ target_sources(crepe PUBLIC
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
# AudioSource.h
BehaviorScript.h
+ Config.h
)
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
new file mode 100644
index 0000000..d6ee8e8
--- /dev/null
+++ b/src/crepe/api/Config.h
@@ -0,0 +1,27 @@
+#pragma once
+
+#include "../util/log.h"
+
+namespace crepe::api {
+
+class Config {
+private:
+ Config() = default;
+public:
+ ~Config() = default;
+
+public:
+ static Config & get_instance() {
+ static Config instance;
+ return instance;
+ }
+
+public:
+ struct {
+ util::log_level level = util::log_level::INFO;
+ bool color = true;
+ } log;
+};
+
+}
+
diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt
index 100f028..d5dbd9f 100644
--- a/src/crepe/util/CMakeLists.txt
+++ b/src/crepe/util/CMakeLists.txt
@@ -1,4 +1,5 @@
target_sources(crepe PUBLIC
+ color.cpp
log.cpp
)
diff --git a/src/crepe/util/color.cpp b/src/crepe/util/color.cpp
new file mode 100644
index 0000000..3fec157
--- /dev/null
+++ b/src/crepe/util/color.cpp
@@ -0,0 +1,49 @@
+#include "color.h"
+#include "../api/Config.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);
+ return this->final.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..0b7c1e6 100644
--- a/src/crepe/util/color.h
+++ b/src/crepe/util/color.h
@@ -1,41 +1,46 @@
#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";
+#include <string>
+
+namespace crepe::util {
+
+class LogColor {
+public:
+ LogColor() = default;
+
+public:
+ const char * c_str(const char * content = NULL);
+ const std::string str(const std::string & content = "");
+
+public:
+ 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::color
diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp
index f91d52c..ba12ba5 100644
--- a/src/crepe/util/log.cpp
+++ b/src/crepe/util/log.cpp
@@ -4,17 +4,22 @@
#include <string>
#include "log.h"
+#include "../api/Config.h"
using namespace crepe::util;
static const char * const LOG_PREFIX[] = {
- [log_level::DEBUG] = "[DBG] ",
+ [log_level::TRACE] = "[TRACE] ",
+ [log_level::DEBUG] = "[DEBUG] ",
[log_level::INFO] = "[INFO] ",
[log_level::WARNING] = "[WARN] ",
- [log_level::ERROR] = "[ERR] ",
+ [log_level::ERROR] = "[ERROR] ",
};
static void va_logf(enum log_level level, va_list args, const std::string fmt) {
+ auto & cfg = crepe::api::Config::get_instance();
+ if (level < cfg.log.level) return;
+
va_list args_copy;
va_copy(args_copy, args);
diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h
index 2b0fbe1..d5494c7 100644
--- a/src/crepe/util/log.h
+++ b/src/crepe/util/log.h
@@ -6,17 +6,17 @@
#include "color.h"
// utility macros
-#define _crepe_logf_here(fmt, ...) \
- crepe::util::logf(util::log_level::DEBUG, "%s%s (%s:%d)" fmt "\n", \
+#define _crepe_logf_here(lvl, fmt, ...) \
+ crepe::util::logf(lvl, "%s%s (%s:%d)" 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", "")
+#define dbg_logf(fmt, ...) _crepe_logf_here(util::log_level::DEBUG, ": " fmt, __VA_ARGS__)
+#define dbg_log(str) _crepe_logf_here(util::log_level::DEBUG, ": %s", str)
+#define dbg_trace() _crepe_logf_here(util::log_level::TRACE, "%s", "")
// NOLINTEND
#endif
@@ -24,6 +24,7 @@
namespace crepe::util {
enum log_level {
+ TRACE,
DEBUG,
INFO,
WARNING,
diff --git a/src/example/script.cpp b/src/example/script.cpp
index a610b83..44e2922 100644
--- a/src/example/script.cpp
+++ b/src/example/script.cpp
@@ -8,6 +8,7 @@
#include <crepe/ScriptSystem.h>
#include <crepe/util/log.h>
+#include <crepe/api/Config.h>
#include <crepe/api/BehaviorScript.h>
using namespace crepe;
@@ -18,6 +19,9 @@ class MyScript : public api::BehaviorScript {
};
int main() {
+ auto & cfg = api::Config::get_instance();
+ cfg.log.level = util::log_level::TRACE;
+
dbg_trace();
auto obj = GameObject(0, "name", "tag", 0);