aboutsummaryrefslogtreecommitdiff
path: root/src/crepe
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-10-21 11:17:23 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-10-21 11:17:23 +0200
commite58c98e224afcf4a28078344e0cd5f8c0e10961f (patch)
treed165add07884e842321a6b7af85ee47c1b07355e /src/crepe
parent157cdabe941be14ca72022e7a7c8c55a582a7c1c (diff)
add logging example + finish log color config integration
Diffstat (limited to 'src/crepe')
-rw-r--r--src/crepe/api/Config.h13
-rw-r--r--src/crepe/util/color.cpp13
-rw-r--r--src/crepe/util/color.h5
-rw-r--r--src/crepe/util/log.cpp25
-rw-r--r--src/crepe/util/log.h12
5 files changed, 48 insertions, 20 deletions
diff --git a/src/crepe/api/Config.h b/src/crepe/api/Config.h
index d6ee8e8..f47f45b 100644
--- a/src/crepe/api/Config.h
+++ b/src/crepe/api/Config.h
@@ -11,14 +11,27 @@ 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/color.cpp b/src/crepe/util/color.cpp
index 3fec157..bdf5809 100644
--- a/src/crepe/util/color.cpp
+++ b/src/crepe/util/color.cpp
@@ -1,4 +1,7 @@
+#include <cstdarg>
+
#include "color.h"
+#include "fmt.h"
#include "../api/Config.h"
using namespace crepe::util;
@@ -16,10 +19,18 @@ const string LogColor::str(const string & content) {
}
const char * LogColor::c_str(const char * content) {
- this->final = this->str(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;
diff --git a/src/crepe/util/color.h b/src/crepe/util/color.h
index 0b7c1e6..66f3a28 100644
--- a/src/crepe/util/color.h
+++ b/src/crepe/util/color.h
@@ -9,10 +9,15 @@ 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:
diff --git a/src/crepe/util/log.cpp b/src/crepe/util/log.cpp
index e3e9f1f..a2672a3 100644
--- a/src/crepe/util/log.cpp
+++ b/src/crepe/util/log.cpp
@@ -8,21 +8,24 @@
#include "../api/Config.h"
using namespace crepe::util;
+using namespace std;
+
+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 const char * const LOG_PREFIX[] = {
- [log_level::TRACE] = "[TRACE] ",
- [log_level::DEBUG] = "[DEBUG] ",
- [log_level::INFO] = "[INFO] ",
- [log_level::WARNING] = "[WARN] ",
- [log_level::ERROR] = "[ERROR] ",
-};
-
-static void log(enum log_level level, const std::string msg) {
+static void log(enum log_level level, const string msg) {
auto & cfg = crepe::api::Config::get_instance();
if (level < cfg.log.level) return;
- using namespace std;
- string final = string(LOG_PREFIX[level]) + msg;
+ string final = log_prefix(level) + msg;
if (!final.ends_with("\n")) final += "\n";
// TODO: also log to file or smth
diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h
index c5d8665..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(lvl, fmt, ...) \
- crepe::util::logf(lvl, "%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(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", "")
+#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