aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/crepe/api/CMakeLists.txt1
-rw-r--r--src/crepe/api/Config.h40
-rw-r--r--src/crepe/util/CMakeLists.txt1
-rw-r--r--src/crepe/util/color.cpp60
-rw-r--r--src/crepe/util/color.h84
-rw-r--r--src/crepe/util/log.cpp27
-rw-r--r--src/crepe/util/log.h13
-rw-r--r--src/example/CMakeLists.txt1
-rw-r--r--src/example/log.cpp25
-rw-r--r--src/example/script.cpp4
10 files changed, 201 insertions, 55 deletions
diff --git a/src/crepe/api/CMakeLists.txt b/src/crepe/api/CMakeLists.txt
index 6b337be..2181b30 100644
--- a/src/crepe/api/CMakeLists.txt
+++ b/src/crepe/api/CMakeLists.txt
@@ -7,6 +7,7 @@ target_sources(crepe PUBLIC
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
# AudioSource.h
BehaviorScript.h
+ Config.h
Script.h
)
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
new file mode 100644
index 0000000..f47f45b
--- /dev/null
+++ b/src/crepe/api/Config.h
@@ -0,0 +1,40 @@
+#pragma once
+
+#include "../util/log.h"
+
+namespace crepe::api {
+
+class Config {
+private:
+ Config() = default;
+public:
+ ~Config() = default;
+
+public:
+ //! Retrieve handle to global Config instance
+ static Config & get_instance() {
+ static Config instance;
+ return instance;
+ }
+
+public:
+ //! Logging-related settings
+ struct {
+ /**
+ * \brief Log level
+ *
+ * Only messages with equal or higher priority than this value will be
+ * logged.
+ */
+ util::log_level level = util::log_level::INFO;
+ /**
+ * \brief Colored log output
+ *
+ * Enables log coloring using ANSI escape codes.
+ */
+ bool color = true;
+ } log;
+};
+
+}
+
diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt
index e2cffaf..bbeaad9 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
fmt.cpp
)
diff --git a/src/crepe/util/color.cpp b/src/crepe/util/color.cpp
new file mode 100644
index 0000000..bdf5809
--- /dev/null
+++ b/src/crepe/util/color.cpp
@@ -0,0 +1,60 @@
+#include <cstdarg>
+
+#include "color.h"
+#include "fmt.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 == 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..66f3a28 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";
+#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::color
diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp
index 0c2ce1e..ce0c07a 100644
--- a/src/crepe/util/log.cpp
+++ b/src/crepe/util/log.cpp
@@ -5,19 +5,27 @@
#include "fmt.h"
#include "log.h"
+#include "../api/Config.h"
using namespace crepe::util;
+using namespace std;
-static const char * const LOG_PREFIX[] = {
- [log_level::DEBUG] = "[DBG] ",
- [log_level::INFO] = "[INFO] ",
- [log_level::WARNING] = "[WARN] ",
- [log_level::ERROR] = "[ERR] ",
-};
+string log_prefix(log_level level) {
+ switch (level) {
+ case log_level::TRACE: return LogColor().fg_white().str("[TRACE]") + " ";
+ case log_level::DEBUG: return LogColor().fg_magenta().str("[DEBUG]") + " ";
+ case log_level::INFO: return LogColor().fg_blue().str("[INFO]") + " ";
+ case log_level::WARNING: return LogColor().fg_yellow().str("[WARN]") + " ";
+ case log_level::ERROR: return LogColor().fg_red().str("[ERROR]") + " ";
+ }
+ return "";
+}
+
+static void log(enum log_level level, const string msg) {
+ auto & cfg = crepe::api::Config::get_instance();
+ if (level < cfg.log.level) return;
-static void log(enum log_level level, const std::string & msg) {
- using namespace std;
- string out = string(LOG_PREFIX[level]) + msg;
+ string out = log_prefix(level) + msg;
if (!out.ends_with("\n")) out += "\n";
// TODO: also log to file or smth
@@ -38,3 +46,4 @@ void crepe::util::logf(log_level level, const char * 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 3e36f0a..bcc06a5 100644
--- a/src/crepe/util/log.h
+++ b/src/crepe/util/log.h
@@ -6,17 +6,13 @@
#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__)
+#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::log_level::DEBUG, ": " fmt, __VA_ARGS__)
+#define dbg_log(str) _crepe_logf_here(crepe::util::log_level::DEBUG, "%s: " str, "")
+#define dbg_trace() _crepe_logf_here(crepe::util::log_level::TRACE, "%s", "")
// NOLINTEND
#endif
@@ -24,6 +20,7 @@
namespace crepe::util {
enum log_level {
+ TRACE,
DEBUG,
INFO,
WARNING,
diff --git a/src/example/CMakeLists.txt b/src/example/CMakeLists.txt
index 6df4ce7..4ef1cd1 100644
--- a/src/example/CMakeLists.txt
+++ b/src/example/CMakeLists.txt
@@ -15,4 +15,5 @@ endfunction()
add_example(audio_internal)
add_example(components_internal)
add_example(script)
+add_example(log)
diff --git a/src/example/log.cpp b/src/example/log.cpp
new file mode 100644
index 0000000..2d693d1
--- /dev/null
+++ b/src/example/log.cpp
@@ -0,0 +1,25 @@
+/** \file
+ *
+ * Standalone example for usage of the logging functions
+ */
+
+#include <crepe/util/log.h>
+#include <crepe/api/Config.h>
+
+using namespace crepe;
+using namespace crepe::util;
+
+int main() {
+ auto & cfg = api::Config::get_instance();
+ // make sure all log messages get printed
+ cfg.log.level = util::log_level::TRACE;
+
+ dbg_trace();
+ dbg_logf("cfg.log.color is equal to %d", cfg.log.color);
+ logf(log_level::INFO, "info message!");
+ logf(log_level::WARNING, "very scary warning");
+ logf(log_level::ERROR, "fatal error!!!");
+
+ return 0;
+}
+
diff --git a/src/example/script.cpp b/src/example/script.cpp
index cc585db..97e99b3 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>
#include <crepe/api/Script.h>
@@ -20,7 +21,8 @@ class MyScript : public Script {
};
int main() {
- dbg_trace();
+ auto & cfg = api::Config::get_instance();
+ cfg.log.level = util::log_level::TRACE;
auto obj = GameObject(0, "name", "tag", 0);
obj.add_component<BehaviorScript>().set_script<MyScript>();