aboutsummaryrefslogtreecommitdiff
path: root/src/openvic-simulation/utility
diff options
context:
space:
mode:
author Hop311 <Hop3114@gmail.com>2023-10-15 16:26:39 +0200
committer GitHub <noreply@github.com>2023-10-15 16:26:39 +0200
commit3249e21104bab6002676abe00450e7bd3f682303 (patch)
treec2a13a44d4c8553f6e87193f5f437052cf1d7067 /src/openvic-simulation/utility
parent17847e16e14ec52eb48a6fd0d9dc36ee93e457db (diff)
parentd26f9c2fb5a9666822a0f702d76b764600a390d7 (diff)
Merge pull request #52 from OpenVicProject/tgc-compat
TGC compatibility fixes + other cleanup
Diffstat (limited to 'src/openvic-simulation/utility')
-rw-r--r--src/openvic-simulation/utility/Logger.cpp16
-rw-r--r--src/openvic-simulation/utility/Logger.hpp53
2 files changed, 38 insertions, 31 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 55e0862..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,42 +42,49 @@ 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; \
- public: \
- static void set_##name##_func(log_func_t log_func) { \
- name##_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 }; \
- } \
- }; \
- template<typename... Ts> \
+#define LOG_FUNC(name) \
+ private: \
+ static inline log_channel_t name##_channel{}; \
+ public: \
+ 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##_channel, std::forward<Ts>(ts)..., location }; \
+ } \
+ }; \
+ template<typename... Ts> \
name(Ts&&...) -> name<Ts...>;
LOG_FUNC(info)