From 18c0b318fe9266422a15d96c3f8060560c7d3152 Mon Sep 17 00:00:00 2001 From: MistEO Date: Tue, 31 Oct 2023 16:15:56 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96log=E6=89=93=E5=8D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/MaaUtils/Logger/Logger.cpp | 7 ++-- source/include/Utils/Logger.h | 53 +++++++++++++++---------------- 2 files changed, 30 insertions(+), 30 deletions(-) diff --git a/source/MaaUtils/Logger/Logger.cpp b/source/MaaUtils/Logger/Logger.cpp index 787e8c371..a6685960e 100644 --- a/source/MaaUtils/Logger/Logger.cpp +++ b/source/MaaUtils/Logger/Logger.cpp @@ -20,9 +20,9 @@ constexpr Logger::separator Logger::separator::comma(","); static constexpr std::string_view kSplitLine = "-----------------------------"; -void Logger::LogStream::print_color() +std::string Logger::LogStream::stdout_string() { - std::string_view color; + std::string color; switch (lv_) { case level::fatal: @@ -39,7 +39,8 @@ void Logger::LogStream::print_color() case level::trace: break; } - stdout_buf_ << color; + + return color + utf8_to_crt(buffer_.str()) + "\033[0m"; } constexpr std::string_view Logger::LogStream::level_str() diff --git a/source/include/Utils/Logger.h b/source/include/Utils/Logger.h index 0369bc966..db344d0b4 100644 --- a/source/include/Utils/Logger.h +++ b/source/include/Utils/Logger.h @@ -74,7 +74,6 @@ class MAA_UTILS_API Logger 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(std::boolalpha); stream_props(std::forward(args)...); } LogStream(const LogStream&) = delete; @@ -84,10 +83,9 @@ class MAA_UTILS_API Logger std::unique_lock lock(mutex_); if (stdout_) { - stdout_buf_ << "\033[0m"; - std::cout << utf8_to_crt(stdout_buf_.str()) << std::endl; + std::cout << stdout_string() << std::endl; } - stream_ << std::move(ofs_buffer_).str() << std::endl; + stream_ << std::move(buffer_).str() << std::endl; } template @@ -121,6 +119,9 @@ class MAA_UTILS_API Logger } 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(); } @@ -135,19 +136,12 @@ class MAA_UTILS_API Logger { auto&& content = string_converter_(std::forward(value)); - if (stdout_) { - stdout_buf_ << content << sep_.str; - } - ofs_buffer_ << std::forward(content) << sep_.str; + buffer_ << std::forward(content) << sep_.str; } template void stream_props(args_t&&... args) { - if (stdout_) { - print_color(); - } - #ifdef _WIN32 int pid = _getpid(); #else @@ -162,7 +156,7 @@ class MAA_UTILS_API Logger stream(props); } - void print_color(); + std::string stdout_string(); constexpr std::string_view level_str(); private: @@ -172,8 +166,7 @@ class MAA_UTILS_API Logger const bool stdout_ = false; separator sep_ = separator::space; - std::stringstream stdout_buf_; - std::stringstream ofs_buffer_; + std::stringstream buffer_; }; public: @@ -186,14 +179,19 @@ class MAA_UTILS_API Logger Logger& operator=(Logger&&) = delete; template - auto trace(args_t&&... args) + auto fatal(args_t&&... args) { - return stream(level::trace, std::forward(args)...); + return stream(level::fatal, std::forward(args)...); } template - auto debug(args_t&&... args) + auto error(args_t&&... args) { - return stream(level::debug, std::forward(args)...); + return stream(level::error, std::forward(args)...); + } + template + auto warn(args_t&&... args) + { + return stream(level::warn, std::forward(args)...); } template auto info(args_t&&... args) @@ -201,14 +199,14 @@ class MAA_UTILS_API Logger return stream(level::info, std::forward(args)...); } template - auto warn(args_t&&... args) + auto debug(args_t&&... args) { - return stream(level::warn, std::forward(args)...); + return stream(level::debug, std::forward(args)...); } template - auto error(args_t&&... args) + auto trace(args_t&&... args) { - return stream(level::error, std::forward(args)...); + return stream(level::trace, std::forward(args)...); } void start_logging(std::filesystem::path dir); @@ -318,11 +316,12 @@ inline constexpr std::string_view pertty_file(std::string_view file) #endif #define LOG_ARGS MAA_FILE, MAA_LINE, MAA_FUNCTION -#define LogTrace MAA_NS::Logger::get_instance().trace(LOG_ARGS) -#define LogDebug MAA_NS::Logger::get_instance().debug(LOG_ARGS) -#define LogInfo MAA_NS::Logger::get_instance().info(LOG_ARGS) -#define LogWarn MAA_NS::Logger::get_instance().warn(LOG_ARGS) +#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 _Cat_(a, b) a##b #define _Cat(a, b) _Cat_(a, b)