diff --git a/source/MaaAdbControlUnit/UnitBase.cpp b/source/MaaAdbControlUnit/UnitBase.cpp index 38a3cb9d0..1bb678c8a 100644 --- a/source/MaaAdbControlUnit/UnitBase.cpp +++ b/source/MaaAdbControlUnit/UnitBase.cpp @@ -76,10 +76,10 @@ std::optional UnitBase::command(const Argv::value& cmd, bool recv_b LogDebug << VAR(scmd) << VAR(ret) << VAR(pipe_data.size()) << VAR(sock_data.size()) << VAR(duration); if (!pipe_data.empty() && pipe_data.size() < 4096) { - LogDebug << Logger::separator::newline << "stdout output:" << pipe_data; + LogDebug << MAA_LOG_NS::separator::newline << "stdout output:" << pipe_data; } if (recv_by_socket && !sock_data.empty() && sock_data.size() < 4096) { - LogDebug << Logger::separator::newline << "socket output:" << sock_data; + LogDebug << MAA_LOG_NS::separator::newline << "socket output:" << sock_data; } if (ret != 0) { diff --git a/source/MaaFramework/Instance/InstanceMgr.cpp b/source/MaaFramework/Instance/InstanceMgr.cpp index 4f275ae6b..e62c02243 100644 --- a/source/MaaFramework/Instance/InstanceMgr.cpp +++ b/source/MaaFramework/Instance/InstanceMgr.cpp @@ -310,7 +310,7 @@ bool InstanceMgr::run_task(TaskId id, TaskPtr task_ptr) status_.clear(); - Logger::get_instance().flush(); + MAA_LOG_NS::Logger::get_instance().flush(); return ret; } diff --git a/source/MaaFramework/Option/GlobalOptionMgr.cpp b/source/MaaFramework/Option/GlobalOptionMgr.cpp index 26a587c5a..808d99e9d 100644 --- a/source/MaaFramework/Option/GlobalOptionMgr.cpp +++ b/source/MaaFramework/Option/GlobalOptionMgr.cpp @@ -33,7 +33,7 @@ bool GlobalOptionMgr::set_log_dir(MaaOptionValue value, MaaOptionValueSize val_s LogInfo << "Set log path" << VAR(log_dir_); - Logger::get_instance().start_logging(log_dir_); + MAA_LOG_NS::Logger::get_instance().start_logging(log_dir_); return true; } @@ -83,7 +83,7 @@ bool GlobalOptionMgr::set_stdout_level(MaaOptionValue value, MaaOptionValueSize LogInfo << "Set log stdout level" << VAR(level); - Logger::get_instance().set_stdout_level(level); + MAA_LOG_NS::Logger::get_instance().set_stdout_level(level); return true; } diff --git a/source/MaaUtils/Logger/Logger.cpp b/source/MaaUtils/Logger/Logger.cpp index e6dd13670..f73982b1e 100644 --- a/source/MaaUtils/Logger/Logger.cpp +++ b/source/MaaUtils/Logger/Logger.cpp @@ -10,17 +10,17 @@ #pragma message("MaaUtils MAA_VERSION: " MAA_VERSION) -MAA_NS_BEGIN +MAA_LOG_NS_BEGIN -constexpr Logger::separator Logger::separator::none(""); -constexpr Logger::separator Logger::separator::space(" "); -constexpr Logger::separator Logger::separator::tab("\t"); -constexpr Logger::separator Logger::separator::newline("\n"); -constexpr Logger::separator Logger::separator::comma(","); +constexpr separator separator::none(""); +constexpr separator separator::space(" "); +constexpr separator separator::tab("\t"); +constexpr separator separator::newline("\n"); +constexpr separator separator::comma(","); static constexpr std::string_view kSplitLine = "-----------------------------"; -std::string Logger::LogStream::stdout_string() +std::string LogStream::stdout_string() { std::string color; @@ -39,11 +39,11 @@ std::string Logger::LogStream::stdout_string() case level::trace: break; } - + return color + utf8_to_crt(buffer_.str()) + "\033[0m"; } -std::string_view Logger::LogStream::level_str() +std::string_view LogStream::level_str() { switch (lv_) { case level::fatal: @@ -179,9 +179,9 @@ void Logger::log_proc_info() internal_dbg() << kSplitLine; } -Logger::LogStream Logger::internal_dbg() +LogStream Logger::internal_dbg() { return debug("Logger"); } -MAA_NS_END +MAA_LOG_NS_END diff --git a/source/include/Utils/Logger.h b/source/include/Utils/Logger.h index ee8453196..975b9b88f 100644 --- a/source/include/Utils/Logger.h +++ b/source/include/Utils/Logger.h @@ -25,7 +25,13 @@ #include "Ranges.hpp" #include "Time.hpp" -MAA_NS_BEGIN +#define MAA_LOG_NS MAA_NS::LogNS +#define MAA_LOG_NS_BEGIN \ + namespace MAA_LOG_NS \ + { +#define MAA_LOG_NS_END } + +MAA_LOG_NS_BEGIN #ifdef __GNUC__ std::ostream& operator<<(std::ostream& os, const std::chrono::milliseconds& ms); @@ -36,136 +42,36 @@ std::ostream& operator<<(std::ostream& os, const std::optional& v); template concept has_output_operator = requires { std::declval() << std::declval(); }; -class MAA_UTILS_API Logger +enum class level { -public: - static constexpr std::string_view kLogFilename = "maa.log"; - static constexpr std::string_view kLogbakFilename = "maa.bak.log"; - -public: - 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; - }; - -public: - 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: - struct MAA_UTILS_API 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) { - std::stringstream ss; - if constexpr (std::same_as>) { - ss << std::boolalpha; - } - ss << std::forward(value); - return std::move(ss).str(); - } - else { - return json::serialize(std::forward(value), *this).to_string(); - } - } - } string_converter_; - - template - void stream(T&& value) - { - buffer_ << string_converter_(std::forward(value)) << sep_.str; - } + fatal = MaaLoggingLevel_Fatal, + error = MaaLoggingLevel_Error, + warn = MaaLoggingLevel_Warn, + info = MaaLoggingLevel_Info, + debug = MaaLoggingLevel_Debug, + trace = MaaLoggingLevel_Trace, +}; - 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())); +struct MAA_UTILS_API separator +{ + constexpr separator(std::string_view s) noexcept : str(s) {} - 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); - } + static const separator none; + static const separator space; + static const separator tab; + static const separator newline; + static const separator comma; - std::string stdout_string(); - std::string_view level_str(); + std::string_view str; +}; - private: - std::mutex& mutex_; - std::ofstream& stream_; - const level lv_ = level::error; - const bool stdout_ = false; +class LogStream; - separator sep_ = separator::space; - std::stringstream buffer_; - }; +class MAA_UTILS_API Logger +{ +public: + static constexpr std::string_view kLogFilename = "maa.log"; + static constexpr std::string_view kLogbakFilename = "maa.bak.log"; public: static Logger& get_instance(); @@ -242,8 +148,112 @@ class MAA_UTILS_API Logger std::mutex trace_mutex_; }; -namespace LogUtils +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 ScopeEnterHelper { public: @@ -252,10 +262,10 @@ class ScopeEnterHelper {} ~ScopeEnterHelper() { stream_ << "| enter"; } - Logger::LogStream& operator()() { return stream_; } + LogStream& operator()() { return stream_; } private: - Logger::LogStream stream_; + LogStream stream_; }; template @@ -274,7 +284,6 @@ class ScopeLeaveHelper std::tuple args_; std::chrono::time_point start_ = std::chrono::steady_clock::now(); }; -} #ifdef __GNUC__ inline std::ostream& operator<<(std::ostream& os, const std::chrono::milliseconds& ms) @@ -301,11 +310,13 @@ inline constexpr std::string_view pertty_file(std::string_view file) return file.substr(pos + 1, file.size()); } +MAA_LOG_NS_END + #define STRINGIZE(x) STRINGIZE2(x) #define STRINGIZE2(x) #x #define LINE_STRING STRINGIZE(__LINE__) -#define MAA_FILE MAA_NS::pertty_file(__FILE__) +#define MAA_FILE MAA_LOG_NS::pertty_file(__FILE__) #define MAA_LINE std::string_view("L" LINE_STRING) #ifdef _MSC_VER #define MAA_FUNCTION std::string_view(__FUNCTION__) @@ -314,25 +325,23 @@ inline constexpr std::string_view pertty_file(std::string_view file) #endif #define LOG_ARGS MAA_FILE, MAA_LINE, MAA_FUNCTION -#define LogFatal MAA_NS::Logger::get_instance().fatal(LOG_ARGS) -#define LogError MAA_NS::Logger::get_instance().error(LOG_ARGS) -#define LogWarn MAA_NS::Logger::get_instance().warn(LOG_ARGS) -#define LogInfo MAA_NS::Logger::get_instance().info(LOG_ARGS) -#define LogDebug MAA_NS::Logger::get_instance().debug(LOG_ARGS) -#define LogTrace MAA_NS::Logger::get_instance().trace(LOG_ARGS) +#define LogFatal MAA_LOG_NS::Logger::get_instance().fatal(LOG_ARGS) +#define LogError MAA_LOG_NS::Logger::get_instance().error(LOG_ARGS) +#define LogWarn MAA_LOG_NS::Logger::get_instance().warn(LOG_ARGS) +#define LogInfo MAA_LOG_NS::Logger::get_instance().info(LOG_ARGS) +#define LogDebug MAA_LOG_NS::Logger::get_instance().debug(LOG_ARGS) +#define LogTrace MAA_LOG_NS::Logger::get_instance().trace(LOG_ARGS) #define _Cat_(a, b) a##b #define _Cat(a, b) _Cat_(a, b) #define _CatVarNameWithLine(Var) _Cat(Var, __LINE__) #define LogScopeHeplerName _CatVarNameWithLine(log_scope_) -#define LogFunc \ - MAA_NS::LogUtils::ScopeLeaveHelper LogScopeHeplerName(LOG_ARGS); \ - MAA_NS::LogUtils::ScopeEnterHelper(LOG_ARGS)() +#define LogFunc \ + MAA_LOG_NS::ScopeLeaveHelper LogScopeHeplerName(LOG_ARGS); \ + MAA_LOG_NS::ScopeEnterHelper(LOG_ARGS)() #define VAR_RAW(x) "[" << #x << "=" << (x) << "] " -#define VAR(x) MAA_NS::Logger::separator::none << VAR_RAW(x) << MAA_NS::Logger::separator::space +#define VAR(x) MAA_LOG_NS::separator::none << VAR_RAW(x) << MAA_LOG_NS::separator::space #define VAR_VOIDP_RAW(x) "[" << #x << "=" << ((void*)x) << "] " -#define VAR_VOIDP(x) MAA_NS::Logger::separator::none << VAR_VOIDP_RAW(x) << MAA_NS::Logger::separator::space - -MAA_NS_END +#define VAR_VOIDP(x) MAA_LOG_NS::separator::none << VAR_VOIDP_RAW(x) << MAA_LOG_NS::separator::space