diff options
author | Hop311 <Hop3114@gmail.com> | 2023-05-22 17:54:15 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-05-22 17:54:15 +0200 |
commit | 08ec6fe5fbf52d814d88c235aac84bb95ad4e322 (patch) | |
tree | 136a221eb5e7c895c8219778b3d206f2ed9e8e7f /src/openvic/utility/Logger.hpp | |
parent | 15e960f93ced8c94a6a45ebb2b44d0705ff7f8f6 (diff) | |
parent | 7874702f30d5855319faf197b10aed31f07f5e27 (diff) |
Merge pull request #5 from OpenVicProject/bmp
BMP palette parser + code style cleanup
Diffstat (limited to 'src/openvic/utility/Logger.hpp')
-rw-r--r-- | src/openvic/utility/Logger.hpp | 80 |
1 files changed, 80 insertions, 0 deletions
diff --git a/src/openvic/utility/Logger.hpp b/src/openvic/utility/Logger.hpp new file mode 100644 index 0000000..ac82445 --- /dev/null +++ b/src/openvic/utility/Logger.hpp @@ -0,0 +1,80 @@ +#pragma once + +#include <functional> +#include <sstream> +#ifdef __cpp_lib_source_location +#include <source_location> +#endif + +namespace OpenVic { + +#ifndef __cpp_lib_source_location +#include <string> + // Implementation of std::source_location for compilers that do not support it + // Note: uses non-standard extensions that are supported by Clang, GCC, and MSVC + // https://clang.llvm.org/docs/LanguageExtensions.html#source-location-builtins + // https://stackoverflow.com/a/67970107 + class source_location { + std::string _file; + int _line; + std::string _function; + + public: + source_location(std::string f, int l, std::string n) : _file(f), _line(l), _function(n) {} + static source_location current(std::string f = __builtin_FILE(), int l = __builtin_LINE(), std::string n = __builtin_FUNCTION()) { + return source_location(f, l, n); + } + + inline char const* file_name() const { return _file.c_str(); } + inline int line() const { return _line; } + inline char const* function_name() const { return _function.c_str(); } + }; +#endif + + class Logger { + using log_func_t = std::function<void(std::string&&)>; + +#ifdef __cpp_lib_source_location + using source_location = std::source_location; +#else + using source_location = OpenVic::source_location; +#endif + + static char const* get_filename(char const* filepath); + + template<typename... Ts> + struct log { + log(log_func_t log_func, Ts&&... ts, source_location const& location) { + if (log_func) { + std::stringstream stream; + stream << "\n" << get_filename(location.file_name()) << "(" + << location.line() << ") `" << location.function_name() << "`: "; + ((stream << std::forward<Ts>(ts)), ...); + stream << std::endl; + log_func(stream.str()); + } + } + }; + +#define LOG_FUNC(name) \ + private: \ + static log_func_t name##_func; \ + 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, std::forward<Ts>(ts)..., location }; \ + } \ + }; \ + template <typename... Ts> \ + name(Ts&&...) -> name<Ts...>; + + LOG_FUNC(info) + LOG_FUNC(error) + +#undef LOG_FUNC + }; +} |