aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/util
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-11-14 11:26:12 +0100
committerLoek Le Blansch <loek@pipeframe.xyz>2024-11-14 11:26:12 +0100
commit01c09a196c3f3e5cefaa4119a95a1cdeb7b9c263 (patch)
tree5667369a85bd06b683c67de42bf0311c2647912b /src/crepe/util
parent6e13510f3c6d4155707f748d237bb1fa05243450 (diff)
parent8600b8a29351aae26ec7b22f84aeeef92d8cb421 (diff)
merge `loek/cleanup` into `loek/audio`
Diffstat (limited to 'src/crepe/util')
-rw-r--r--src/crepe/util/CMakeLists.txt7
-rw-r--r--src/crepe/util/Log.cpp (renamed from src/crepe/util/log.cpp)33
-rw-r--r--src/crepe/util/Log.h85
-rw-r--r--src/crepe/util/Log.hpp18
-rw-r--r--src/crepe/util/LogColor.cpp19
-rw-r--r--src/crepe/util/LogColor.h29
-rw-r--r--src/crepe/util/fmt.cpp35
-rw-r--r--src/crepe/util/fmt.h10
-rw-r--r--src/crepe/util/log.h39
9 files changed, 135 insertions, 140 deletions
diff --git a/src/crepe/util/CMakeLists.txt b/src/crepe/util/CMakeLists.txt
index 0fa4343..4be738a 100644
--- a/src/crepe/util/CMakeLists.txt
+++ b/src/crepe/util/CMakeLists.txt
@@ -1,13 +1,12 @@
target_sources(crepe PUBLIC
LogColor.cpp
- log.cpp
- fmt.cpp
+ Log.cpp
)
target_sources(crepe PUBLIC FILE_SET HEADERS FILES
LogColor.h
- log.h
- fmt.h
+ Log.h
+ Log.hpp
Proxy.h
Proxy.hpp
)
diff --git a/src/crepe/util/log.cpp b/src/crepe/util/Log.cpp
index 4a8f8e8..e583734 100644
--- a/src/crepe/util/log.cpp
+++ b/src/crepe/util/Log.cpp
@@ -4,50 +4,35 @@
#include <string>
#include "../api/Config.h"
-#include "fmt.h"
-#include "log.h"
+#include "Log.h"
using namespace crepe;
using namespace std;
-string log_prefix(LogLevel level) {
+string Log::prefix(const Level & level) {
switch (level) {
- case LogLevel::TRACE:
+ case Level::TRACE:
return LogColor().fg_white().str("[TRACE]") + " ";
- case LogLevel::DEBUG:
+ case Level::DEBUG:
return LogColor().fg_magenta().str("[DEBUG]") + " ";
- case LogLevel::INFO:
+ case Level::INFO:
return LogColor().fg_blue().str("[INFO]") + " ";
- case LogLevel::WARNING:
+ case Level::WARNING:
return LogColor().fg_yellow().str("[WARN]") + " ";
- case LogLevel::ERROR:
+ case Level::ERROR:
return LogColor().fg_red().str("[ERROR]") + " ";
}
return "";
}
-static void log(LogLevel level, const string msg) {
+void Log::log(const Level & level, const string & msg) {
auto & cfg = Config::get_instance();
if (level < cfg.log.level) return;
- string out = log_prefix(level) + msg;
+ string out = Log::prefix(level) + msg;
if (!out.ends_with("\n")) out += "\n";
// TODO: also log to file or smth
fwrite(out.c_str(), 1, out.size(), stdout);
fflush(stdout);
}
-
-void crepe::logf(const char * fmt, ...) {
- va_list args;
- va_start(args, fmt);
- log(LogLevel::DEBUG, va_stringf(args, fmt));
- va_end(args);
-}
-
-void crepe::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..01452b2
--- /dev/null
+++ b/src/crepe/util/Log.h
@@ -0,0 +1,85 @@
+#pragma once
+
+#include <format>
+
+// allow user to disable debug macros
+#ifndef CREPE_DISABLE_MACROS
+
+#include "LogColor.h"
+
+// utility macros
+#define _crepe_logf_here(level, fmt, ...) \
+ crepe::Log::logf( \
+ level, "{}" fmt, \
+ crepe::LogColor().fg_white(false).str(std::format( \
+ "{} ({}:{})", __PRETTY_FUNCTION__, __FILE_NAME__, __LINE__)), \
+ __VA_ARGS__)
+
+// very illegal global function-style macros
+// NOLINTBEGIN
+#define dbg_logf(fmt, ...) \
+ _crepe_logf_here(crepe::Log::Level::DEBUG, ": " fmt, __VA_ARGS__)
+#define dbg_log(str) _crepe_logf_here(crepe::Log::Level::DEBUG, ": {}", str)
+#define dbg_trace() _crepe_logf_here(crepe::Log::Level::TRACE, "", "")
+// NOLINTEND
+
+#endif
+
+namespace crepe {
+
+/**
+ * \brief Logging utility
+ *
+ * This class is used to output log messages to the console and/or log files.
+ */
+class Log {
+public:
+ //! Log message severity
+ enum Level {
+ TRACE, //< Include (internal) function calls
+ DEBUG, //< Include dbg_logf output
+ INFO, //< General-purpose messages
+ WARNING, //< Non-fatal errors
+ ERROR, //< Fatal errors
+ };
+
+ /**
+ * \brief Log a formatted message
+ *
+ * \param level Message severity
+ * \param msg Formatted message
+ */
+ static void log(const Level & level, const std::string & msg);
+
+ /**
+ * \brief Format a message and log it
+ *
+ * \param level Message severity
+ * \param fmt Message format
+ * \param args Format arguments
+ */
+ template <class... Args>
+ static void logf(const Level & level, std::format_string<Args...> fmt,
+ Args &&... args);
+
+ /**
+ * \brief Format a message and log it (with default severity \c INFO)
+ *
+ * \param fmt Message format
+ * \param args Format arguments
+ */
+ template <class... Args>
+ static void logf(std::format_string<Args...> fmt, Args &&... args);
+
+private:
+ /**
+ * \brief Output a message prefix depending on the log level
+ *
+ * \param level Message severity
+ */
+ static std::string prefix(const Level & level);
+};
+
+} // namespace crepe
+
+#include "Log.hpp"
diff --git a/src/crepe/util/Log.hpp b/src/crepe/util/Log.hpp
new file mode 100644
index 0000000..651f076
--- /dev/null
+++ b/src/crepe/util/Log.hpp
@@ -0,0 +1,18 @@
+#pragma once
+
+#include "Log.h"
+
+namespace crepe {
+
+template <class... Args>
+void Log::logf(std::format_string<Args...> fmt, Args &&... args) {
+ Log::logf(Level::INFO, fmt, std::forward<Args>(args)...);
+}
+
+template <class... Args>
+void Log::logf(const Level & level, std::format_string<Args...> fmt,
+ Args &&... args) {
+ Log::log(level, std::format(fmt, std::forward<Args>(args)...));
+}
+
+} // namespace crepe
diff --git a/src/crepe/util/LogColor.cpp b/src/crepe/util/LogColor.cpp
index b5fe3ea..ae44d72 100644
--- a/src/crepe/util/LogColor.cpp
+++ b/src/crepe/util/LogColor.cpp
@@ -3,14 +3,12 @@
#include "../api/Config.h"
#include "LogColor.h"
-#include "fmt.h"
-
using namespace crepe;
using namespace std;
static constexpr const char * RESET_CODE = "\e[0m";
-const string LogColor::str(const string & content) {
+const string LogColor::str(const string & content) const {
auto & cfg = Config::get_instance();
string out = content;
if (cfg.log.color) out = this->code + out;
@@ -19,21 +17,8 @@ const string LogColor::str(const string & content) {
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);
+ this->code += format("\e[{}m", code);
return *this;
}
diff --git a/src/crepe/util/LogColor.h b/src/crepe/util/LogColor.h
index c1170cb..4b65127 100644
--- a/src/crepe/util/LogColor.h
+++ b/src/crepe/util/LogColor.h
@@ -4,20 +4,19 @@
namespace crepe {
+/**
+ * \brief Utility class for coloring text using ANSI escape codes
+ *
+ * \note Most methods in this class return a reference to \c this, which may be
+ * used to chain multiple display attributes.
+ */
class LogColor {
public:
- LogColor() = default;
+ //! Get color code as stl string (or color content string)
+ const std::string str(const std::string & content = "") const;
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
+ //! Reset color to default foreground and background color
LogColor & reset();
public:
@@ -41,11 +40,19 @@ public:
LogColor & bg_white(bool bright = false);
private:
+ /**
+ * \brief Append SGR escape sequence to \c this->code
+ *
+ * \param code SGR attribute number
+ *
+ * See <https://en.wikipedia.org/wiki/ANSI_escape_code> for magic number
+ * reference.
+ */
LogColor & add_code(unsigned int code);
private:
+ //! Color escape sequence
std::string code = "";
- std::string final = "";
};
} // namespace crepe
diff --git a/src/crepe/util/fmt.cpp b/src/crepe/util/fmt.cpp
deleted file mode 100644
index 4b50da8..0000000
--- a/src/crepe/util/fmt.cpp
+++ /dev/null
@@ -1,35 +0,0 @@
-#include <cstdarg>
-#include <cstdio>
-#include <string>
-
-#include "fmt.h"
-
-using namespace std;
-
-string crepe::va_stringf(va_list args, const char * fmt) {
- string out;
-
- va_list args_copy;
- va_copy(args_copy, args);
- size_t length = vsnprintf(NULL, 0, fmt, args_copy);
- // resize to include terminating null byte
- out.resize(length + 1);
- va_end(args_copy);
-
- // vsnprintf adds terminating null byte
- vsnprintf(out.data(), out.size(), fmt, args);
- // resize to actual length
- out.resize(length);
-
- va_end(args);
-
- return out;
-}
-
-string crepe::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
deleted file mode 100644
index e319e6e..0000000
--- a/src/crepe/util/fmt.h
+++ /dev/null
@@ -1,10 +0,0 @@
-#pragma once
-
-#include <string>
-
-namespace crepe {
-
-std::string va_stringf(va_list args, const char * fmt);
-std::string stringf(const char * fmt, ...);
-
-} // namespace crepe
diff --git a/src/crepe/util/log.h b/src/crepe/util/log.h
deleted file mode 100644
index 5a1cf00..0000000
--- a/src/crepe/util/log.h
+++ /dev/null
@@ -1,39 +0,0 @@
-#pragma once
-
-// allow user to disable debug macros
-#ifndef CREPE_DISABLE_MACROS
-
-#include "LogColor.h"
-
-// utility macros
-#define _crepe_logf_here(level, format, ...) \
- crepe::logf( \
- level, "%s" format, \
- crepe::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::LogLevel::DEBUG, ": " fmt, __VA_ARGS__)
-#define dbg_log(str) _crepe_logf_here(crepe::LogLevel::DEBUG, "%s: " str, "")
-#define dbg_trace() _crepe_logf_here(crepe::LogLevel::TRACE, "%s", "")
-// NOLINTEND
-
-#endif
-
-namespace crepe {
-
-enum LogLevel {
- TRACE,
- DEBUG,
- INFO,
- WARNING,
- ERROR,
-};
-
-void logf(const char * fmt, ...);
-void logf(enum LogLevel level, const char * fmt, ...);
-
-} // namespace crepe