aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/utility
diff options
context:
space:
mode:
author hop311 <hop3114@gmail.com>2023-10-13 23:04:12 +0200
committer hop311 <hop3114@gmail.com>2023-10-14 12:05:06 +0200
commitd26f9c2fb5a9666822a0f702d76b764600a390d7 (patch)
treec2a13a44d4c8553f6e87193f5f437052cf1d7067 /src/openvic-simulation/utility
parentce6e70d079f4ab18cdfa082032dc3580ab233b0e (diff)
Further CLI and modifier reading work
Diffstat (limited to 'src/openvic-simulation/utility')
-rw-r--r--src/openvic-simulation/utility/Logger.cpp16
-rw-r--r--src/openvic-simulation/utility/Logger.hpp33
2 files changed, 28 insertions, 21 deletions
diff --git a/src/openvic-simulation/utility/Logger.cpp b/src/openvic-simulation/utility/Logger.cpp
index fca08a5..68c43dd 100644
--- a/src/openvic-simulation/utility/Logger.cpp
+++ b/src/openvic-simulation/utility/Logger.cpp
@@ -4,19 +4,19 @@
using namespace OpenVic;
-Logger::log_func_t Logger::info_func {};
-Logger::log_queue_t Logger::info_queue {};
-Logger::log_func_t Logger::warning_func {};
-Logger::log_queue_t Logger::warning_queue {};
-Logger::log_func_t Logger::error_func {};
-Logger::log_queue_t Logger::error_queue {};
+void Logger::set_logger_funcs() {
+ Logger::set_info_func([](std::string&& str) { std::cout << str; });
+ Logger::set_warning_func([](std::string&& str) { std::cerr << str; });
+ Logger::set_error_func([](std::string&& str) { std::cerr << str; });
+}
-char const* Logger::get_filename(char const* filepath) {
- if (filepath == nullptr) return nullptr;
+char const* Logger::get_filename(char const* filepath, char const* default_path) {
+ if (filepath == nullptr) return default_path;
char const* last_slash = filepath;
while (*filepath != '\0') {
if (*filepath == '\\' || *filepath == '/') last_slash = filepath + 1;
filepath++;
}
+ if (*last_slash == '\0') return default_path;
return last_slash;
}
diff --git a/src/openvic-simulation/utility/Logger.hpp b/src/openvic-simulation/utility/Logger.hpp
index 11cada5..d60e59e 100644
--- a/src/openvic-simulation/utility/Logger.hpp
+++ b/src/openvic-simulation/utility/Logger.hpp
@@ -32,7 +32,7 @@ namespace OpenVic {
};
#endif
- class Logger {
+ class Logger final {
using log_func_t = std::function<void(std::string&&)>;
using log_queue_t = std::queue<std::string>;
@@ -42,39 +42,46 @@ namespace OpenVic {
using source_location = OpenVic::source_location;
#endif
- static char const* get_filename(char const* filepath);
+ public:
+ static void set_logger_funcs();
+ static char const* get_filename(char const* filepath, char const* default_path = nullptr);
+
+ private:
+ struct log_channel_t {
+ log_func_t func;
+ log_queue_t queue;
+ };
template<typename... Ts>
struct log {
- log(log_func_t log_func, log_queue_t& log_queue, Ts&&... ts, source_location const& location) {
+ log(log_channel_t& log_channel, Ts&&... ts, source_location const& location) {
std::stringstream stream;
stream << "\n" << get_filename(location.file_name()) << "("
//<< location.line() << ") `" << location.function_name() << "`: ";
<< location.line() << "): ";
((stream << std::forward<Ts>(ts)), ...);
stream << std::endl;
- log_queue.push(stream.str());
- if (log_func) {
+ log_channel.queue.push(stream.str());
+ if (log_channel.func) {
do {
- log_func(std::move(log_queue.front()));
- log_queue.pop();
- } while (!log_queue.empty());
+ log_channel.func(std::move(log_channel.queue.front()));
+ log_channel.queue.pop();
+ } while (!log_channel.queue.empty());
}
}
};
#define LOG_FUNC(name) \
private: \
- static log_func_t name##_func; \
- static log_queue_t name##_queue; \
+ static inline log_channel_t name##_channel{}; \
public: \
- static void set_##name##_func(log_func_t log_func) { \
- name##_func = log_func; \
+ static inline void set_##name##_func(log_func_t log_func) { \
+ name##_channel.func = log_func; \
} \
template<typename... Ts> \
struct name { \
name(Ts&&... ts, source_location const& location = source_location::current()) { \
- log<Ts...>{ name##_func, name##_queue, std::forward<Ts>(ts)..., location }; \
+ log<Ts...>{ name##_channel, std::forward<Ts>(ts)..., location }; \
} \
}; \
template<typename... Ts> \