diff --git a/source/include/Conf/Conf.h b/source/include/Conf/Conf.h index 7f582f1a2..0cca69ddf 100644 --- a/source/include/Conf/Conf.h +++ b/source/include/Conf/Conf.h @@ -85,6 +85,12 @@ { #define MAA_PLATFORM_NS_END } +#define MAA_LOG_NS MAA_NS::LogNS +#define MAA_LOG_NS_BEGIN \ + namespace MAA_LOG_NS \ + { +#define MAA_LOG_NS_END } + /* MaaFramwork */ #define MAA_RES_NS MAA_NS::ResourceNS diff --git a/source/include/Utils/Logger.h b/source/include/Utils/Logger.h index 47240a53e..efcef8a90 100644 --- a/source/include/Utils/Logger.h +++ b/source/include/Utils/Logger.h @@ -1,176 +1,11 @@ #pragma once -#include -#include -#include -#include #include -#include -#include -#include -#if defined(__APPLE__) || defined(__linux__) -#include -#endif - -#include - -#include "Conf/Conf.h" -#include "MaaFramework/MaaDef.h" -#include "MaaFramework/MaaPort.h" - -#include "Format.hpp" -#include "Locale.hpp" -#include "Platform.h" -#include "Ranges.hpp" -#include "Time.hpp" - -#define MAA_LOG_NS MAA_NS::LogNS -#define MAA_LOG_NS_BEGIN \ - namespace MAA_LOG_NS \ - { -#define MAA_LOG_NS_END } +#include "LoggerUtils.h" MAA_LOG_NS_BEGIN -#ifdef __GNUC__ -std::ostream& operator<<(std::ostream& os, const std::chrono::milliseconds& ms); -#endif -template -std::ostream& operator<<(std::ostream& os, const std::optional& v); - -template -concept has_output_operator = requires { std::declval() << std::declval(); }; - -enum class level -{ - fatal = MaaLoggingLevel_Fatal, - error = MaaLoggingLevel_Error, - warn = MaaLoggingLevel_Warn, - info = MaaLoggingLevel_Info, - debug = MaaLoggingLevel_Debug, - trace = MaaLoggingLevel_Trace, -}; - -struct MAA_UTILS_API separator -{ - constexpr separator(std::string_view s) noexcept : str(s) {} - - static const separator none; - static const separator space; - static const separator tab; - static const separator newline; - static const separator comma; - - std::string_view str; -}; - -struct StringConverter -{ - template - static constexpr bool is_convertible = std::same_as> || - std::same_as> || has_output_operator; - - template - std::string operator()(T&& value) const - { - if constexpr (std::same_as>) { - return path_to_utf8_string(std::forward(value)); - } - else if constexpr (std::same_as>) { - return from_u16(std::forward(value)); - } - else if constexpr (has_output_operator) { - return to_stringstream(std::forward(value)); - } - else { - return json::serialize(std::forward(value), *this).to_string(); - } - } - - template - std::string to_stringstream(T&& value) const - { - std::stringstream ss; - if constexpr (std::same_as>) { - ss << std::boolalpha; - } - ss << std::forward(value); - return std::move(ss).str(); - } -}; - -class MAA_UTILS_API LogStream -{ -public: - template - LogStream(std::mutex& m, std::ofstream& s, level lv, bool std_out, args_t&&... args) - : mutex_(m), stream_(s), lv_(lv), stdout_(std_out) - { - stream_props(std::forward(args)...); - } - LogStream(const LogStream&) = delete; - LogStream(LogStream&&) noexcept = default; - ~LogStream() - { - std::unique_lock lock(mutex_); - - if (stdout_) { - std::cout << stdout_string() << std::endl; - } - stream_ << std::move(buffer_).str() << std::endl; - } - - template - LogStream& operator<<(T&& value) - { - if constexpr (std::is_same_v, separator>) { - sep_ = std::forward(value); - } - else { - stream(std::forward(value)); - } - return *this; - } - -private: - template - void stream(T&& value) - { - buffer_ << string_converter_(std::forward(value)) << sep_.str; - } - - template - void stream_props(args_t&&... args) - { -#ifdef _WIN32 - int pid = _getpid(); -#else - int pid = ::getpid(); -#endif - auto tid = static_cast(std::hash {}(std::this_thread::get_id())); - - std::string props = MAA_FMT::format("[{}][{}][Px{}][Tx{}]", format_now(), level_str(), pid, tid); - for (auto&& arg : { args... }) { - props += MAA_FMT::format("[{}]", arg); - } - stream(props); - } - - std::string stdout_string(); - std::string_view level_str(); - -private: - std::mutex& mutex_; - std::ofstream& stream_; - const level lv_ = level::error; - const bool stdout_ = false; - const StringConverter string_converter_; - - separator sep_ = separator::space; - std::stringstream buffer_; -}; - class MAA_UTILS_API Logger { public: @@ -283,25 +118,6 @@ class ScopeLeaveHelper std::chrono::time_point start_ = std::chrono::steady_clock::now(); }; -#ifdef __GNUC__ -inline std::ostream& operator<<(std::ostream& os, const std::chrono::milliseconds& ms) -{ - return os << ms.count() << "ms"; -} -#endif - -template -std::ostream& operator<<(std::ostream& os, const std::optional& v) -{ - if (v) { - os << *v; - } - else { - os << ""; - } - return os; -} - inline constexpr std::string_view pertty_file(std::string_view file) { size_t pos = file.find_last_of(std::filesystem::path::preferred_separator); diff --git a/source/include/Utils/LoggerUtils.h b/source/include/Utils/LoggerUtils.h new file mode 100644 index 000000000..f0e8cbe4c --- /dev/null +++ b/source/include/Utils/LoggerUtils.h @@ -0,0 +1,180 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include + +#if defined(__APPLE__) || defined(__linux__) +#include +#endif + +#include + +#include "Conf/Conf.h" +#include "MaaFramework/MaaDef.h" +#include "MaaFramework/MaaPort.h" + +#include "Format.hpp" +#include "Locale.hpp" +#include "Platform.h" +#include "Ranges.hpp" +#include "Time.hpp" + +MAA_LOG_NS_BEGIN + +#ifdef __GNUC__ +inline std::ostream& operator<<(std::ostream& os, const std::chrono::milliseconds& ms) +{ + return os << ms.count() << "ms"; +} +#endif + +template +inline std::ostream& operator<<(std::ostream& os, const std::optional& v) +{ + if (v) { + os << *v; + } + else { + os << ""; + } + return os; +} + +enum class level +{ + fatal = MaaLoggingLevel_Fatal, + error = MaaLoggingLevel_Error, + warn = MaaLoggingLevel_Warn, + info = MaaLoggingLevel_Info, + debug = MaaLoggingLevel_Debug, + trace = MaaLoggingLevel_Trace, +}; + +struct MAA_UTILS_API separator +{ + constexpr separator(std::string_view s) noexcept : str(s) {} + + static const separator none; + static const separator space; + static const separator tab; + static const separator newline; + static const separator comma; + + std::string_view str; +}; + +template +concept has_output_operator = requires { std::declval() << std::declval(); }; + +struct StringConverter +{ + template + static constexpr bool is_convertible = std::same_as> || + std::same_as> || has_output_operator; + + template + std::string operator()(T&& value) const + { + if constexpr (std::same_as>) { + return path_to_utf8_string(std::forward(value)); + } + else if constexpr (std::same_as>) { + return from_u16(std::forward(value)); + } + else if constexpr (has_output_operator) { + return to_stringstream(std::forward(value)); + } + else { + return json::serialize(std::forward(value), *this).to_string(); + } + } + + template + std::string to_stringstream(T&& value) const + { + std::stringstream ss; + if constexpr (std::same_as>) { + ss << std::boolalpha; + } + ss << std::forward(value); + return std::move(ss).str(); + } +}; + +class MAA_UTILS_API LogStream +{ +public: + template + LogStream(std::mutex& m, std::ofstream& s, level lv, bool std_out, args_t&&... args) + : mutex_(m), stream_(s), lv_(lv), stdout_(std_out) + { + stream_props(std::forward(args)...); + } + LogStream(const LogStream&) = delete; + LogStream(LogStream&&) noexcept = default; + ~LogStream() + { + std::unique_lock lock(mutex_); + + if (stdout_) { + std::cout << stdout_string() << std::endl; + } + stream_ << std::move(buffer_).str() << std::endl; + } + + template + LogStream& operator<<(T&& value) + { + if constexpr (std::is_same_v, separator>) { + sep_ = std::forward(value); + } + else { + stream(std::forward(value)); + } + return *this; + } + +private: + template + void stream(T&& value) + { + buffer_ << string_converter_(std::forward(value)) << sep_.str; + } + + template + void stream_props(args_t&&... args) + { +#ifdef _WIN32 + int pid = _getpid(); +#else + int pid = ::getpid(); +#endif + auto tid = static_cast(std::hash {}(std::this_thread::get_id())); + + std::string props = MAA_FMT::format("[{}][{}][Px{}][Tx{}]", format_now(), level_str(), pid, tid); + for (auto&& arg : { args... }) { + props += MAA_FMT::format("[{}]", arg); + } + stream(props); + } + + std::string stdout_string(); + std::string_view level_str(); + +private: + std::mutex& mutex_; + std::ofstream& stream_; + const level lv_ = level::fatal; + const bool stdout_ = false; + const StringConverter string_converter_; + + separator sep_ = separator::space; + std::stringstream buffer_; +}; + +MAA_LOG_NS_END