From 45b287cb5327ff415d4c08b32ac0445195b014fa Mon Sep 17 00:00:00 2001 From: MistEO Date: Fri, 9 Sep 2022 23:06:21 +0800 Subject: [PATCH 1/8] =?UTF-8?q?feat:=20=E5=88=9D=E7=89=88=20Logger::separa?= =?UTF-8?q?tor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MeoAssistant/Controller.cpp | 2 +- src/MeoAssistant/Logger.hpp | 57 ++++++++++++++++++++++----------- 2 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/MeoAssistant/Controller.cpp b/src/MeoAssistant/Controller.cpp index 408aa4b7d9..5f3f587061 100644 --- a/src/MeoAssistant/Controller.cpp +++ b/src/MeoAssistant/Controller.cpp @@ -422,7 +422,7 @@ std::optional asst::Controller::call_command(const std::string& cmd Log.info("Call `", cmd, "` ret", exit_ret, ", cost", duration, "ms , stdout size:", pipe_data.size(), ", socket size:", sock_data.size()); if (!pipe_data.empty() && pipe_data.size() < 4096) { - Log.trace("output:\n", pipe_data); + Log.trace("output:", Logger::separator("\n"), pipe_data); } if (!exit_ret) { diff --git a/src/MeoAssistant/Logger.hpp b/src/MeoAssistant/Logger.hpp index 26a52c6970..668142eb90 100644 --- a/src/MeoAssistant/Logger.hpp +++ b/src/MeoAssistant/Logger.hpp @@ -17,6 +17,13 @@ namespace asst { class Logger : public SingletonHolder { + public: + struct separator + { + separator(std::string_view str) : str(str) {} + std::string_view str; + }; + public: virtual ~Logger() override { flush(); } @@ -35,7 +42,7 @@ namespace asst { #ifdef ASST_DEBUG std::string_view level = "DEB"; - log(level, std::forward(args)...); + log(level, DefaultSeparator, std::forward(args)...); #endif } @@ -43,30 +50,30 @@ namespace asst inline void trace(Args&&... args) { std::string_view level = "TRC"; - log(level, std::forward(args)...); + log(level, DefaultSeparator, std::forward(args)...); } template inline void info(Args&&... args) { std::string_view level = "INF"; - log(level, std::forward(args)...); + log(level, DefaultSeparator, std::forward(args)...); } template inline void warn(Args&&... args) { std::string_view level = "WRN"; - log(level, std::forward(args)...); + log(level, DefaultSeparator, std::forward(args)...); } template inline void error(Args&&... args) { std::string_view level = "ERR"; - log(level, std::forward(args)...); + log(level, DefaultSeparator, std::forward(args)...); } template inline void log_with_custom_level(std::string_view level, Args&&... args) { - log(level, std::forward(args)...); + log(level, DefaultSeparator, std::forward(args)...); } void flush() { @@ -111,7 +118,7 @@ namespace asst } template - void log(std::string_view level, Args&&... args) + void log(std::string_view level, const separator& sep, Args&&... args) { std::unique_lock trace_lock(m_trace_mutex); @@ -134,13 +141,13 @@ namespace asst m_ofs = std::ofstream(m_log_path, std::ios::out | std::ios::app); } #ifdef ASST_DEBUG - stream_put_line(m_ofs, buff, args...); + stream_put_line(m_ofs, sep, buff, args...); #else - stream_put_line(m_ofs, buff, std::forward(args)...); + stream_put_line(m_ofs, sep, buff, std::forward(args)...); #endif #ifdef ASST_DEBUG - stream_put_line(std::cout, buff, std::forward(args)...); + stream_put_line(std::cout, sep, buff, std::forward(args)...); #endif } @@ -181,7 +188,7 @@ namespace asst for (const auto& elem : std::forward(v)) { s << comma; stream_put(s, elem); - comma = ","; + comma = ", "; } s << "]"; return s; @@ -202,18 +209,31 @@ namespace asst template struct stream_put_line_impl { - static constexpr Stream& apply(Stream& s) + static constexpr Stream& apply(Stream& s, const separator& sep) { + std::ignore = sep; s << std::endl; return s; } }; + template + struct stream_put_line_impl + { + static constexpr Stream& apply(Stream& s, const separator& sep, const separator& new_sep, Rest&&... rs) + { + std::ignore = sep; + stream_put_line_impl::apply(s, new_sep, std::forward(rs)...); + return s; + } + }; + template struct stream_put_line_impl { - static constexpr Stream& apply(Stream& s, Only&& only) + static constexpr Stream& apply(Stream& s, const separator& sep, Only&& only) { + std::ignore = sep; stream_put(s, std::forward(only)); s << std::endl; return s; @@ -223,21 +243,22 @@ namespace asst template struct stream_put_line_impl { - static constexpr Stream& apply(Stream& s, First&& f, Rest&&... rs) + static constexpr Stream& apply(Stream& s, const separator& sep, First&& f, Rest&&... rs) { stream_put(s, std::forward(f)); - s << " "; - stream_put_line_impl::apply(s, std::forward(rs)...); + s << sep.str; + stream_put_line_impl::apply(s, sep, std::forward(rs)...); return s; } }; template - Stream& stream_put_line(Stream& s, Args&&... args) + Stream& stream_put_line(Stream& s, const separator& sep, Args&&... args) { - return stream_put_line_impl::apply(s, std::forward(args)...); + return stream_put_line_impl::apply(s, sep, std::forward(args)...); } + inline static const separator DefaultSeparator { " " }; inline static std::filesystem::path m_directory; std::filesystem::path m_log_path = m_directory / "asst.log"; From ca7faa00f8a1d08f106273a4119cee7ac55927a8 Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 10 Sep 2022 00:23:53 +0800 Subject: [PATCH 2/8] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8D=20Logger::separat?= =?UTF-8?q?or=20=E8=B0=83=E7=94=A8=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MeoAssistant/Logger.hpp | 44 ++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 23 deletions(-) diff --git a/src/MeoAssistant/Logger.hpp b/src/MeoAssistant/Logger.hpp index 668142eb90..731a659e29 100644 --- a/src/MeoAssistant/Logger.hpp +++ b/src/MeoAssistant/Logger.hpp @@ -203,11 +203,11 @@ namespace asst } } - template + template struct stream_put_line_impl; - template - struct stream_put_line_impl + template + struct stream_put_line_impl { static constexpr Stream& apply(Stream& s, const separator& sep) { @@ -217,37 +217,35 @@ namespace asst } }; - template - struct stream_put_line_impl - { - static constexpr Stream& apply(Stream& s, const separator& sep, const separator& new_sep, Rest&&... rs) - { - std::ignore = sep; - stream_put_line_impl::apply(s, new_sep, std::forward(rs)...); - return s; - } - }; - - template - struct stream_put_line_impl + template + struct stream_put_line_impl { static constexpr Stream& apply(Stream& s, const separator& sep, Only&& only) { - std::ignore = sep; + if constexpr (!IsFirstWord) { + s << sep.str; + } stream_put(s, std::forward(only)); s << std::endl; return s; } }; - template - struct stream_put_line_impl + template + struct stream_put_line_impl { static constexpr Stream& apply(Stream& s, const separator& sep, First&& f, Rest&&... rs) { - stream_put(s, std::forward(f)); - s << sep.str; - stream_put_line_impl::apply(s, sep, std::forward(rs)...); + if constexpr (std::same_as>) { + stream_put_line_impl::apply(s, f, std::forward(rs)...); + } + else { + if constexpr (!IsFirstWord) { + s << sep.str; + } + stream_put(s, std::forward(f)); + stream_put_line_impl::apply(s, sep, std::forward(rs)...); + } return s; } }; @@ -255,7 +253,7 @@ namespace asst template Stream& stream_put_line(Stream& s, const separator& sep, Args&&... args) { - return stream_put_line_impl::apply(s, sep, std::forward(args)...); + return stream_put_line_impl::apply(s, sep, std::forward(args)...); } inline static const separator DefaultSeparator { " " }; From 3dfb9b589fc8fb3c097dd287d691b277744b850b Mon Sep 17 00:00:00 2001 From: Horror Proton <107091537+horror-proton@users.noreply.github.com> Date: Sat, 10 Sep 2022 00:40:53 +0800 Subject: [PATCH 3/8] feat: WIP Logger::separator --- src/MeoAssistant/Logger.hpp | 48 +++++++++++++------------------------ 1 file changed, 17 insertions(+), 31 deletions(-) diff --git a/src/MeoAssistant/Logger.hpp b/src/MeoAssistant/Logger.hpp index 731a659e29..d14d8f73f4 100644 --- a/src/MeoAssistant/Logger.hpp +++ b/src/MeoAssistant/Logger.hpp @@ -203,57 +203,43 @@ namespace asst } } - template + template struct stream_put_line_impl; - template - struct stream_put_line_impl + template + struct stream_put_line_impl { - static constexpr Stream& apply(Stream& s, const separator& sep) + static constexpr Stream& apply(Stream& s, const separator&) { - std::ignore = sep; s << std::endl; return s; } }; - template - struct stream_put_line_impl + template + struct stream_put_line_impl { - static constexpr Stream& apply(Stream& s, const separator& sep, Only&& only) + static constexpr Stream& apply(Stream& s, [[maybe_unused]] const separator& sep, First&& f, Rest&&... rs) { - if constexpr (!IsFirstWord) { - s << sep.str; - } - stream_put(s, std::forward(only)); - s << std::endl; - return s; - } - }; - - template - struct stream_put_line_impl - { - static constexpr Stream& apply(Stream& s, const separator& sep, First&& f, Rest&&... rs) - { - if constexpr (std::same_as>) { - stream_put_line_impl::apply(s, f, std::forward(rs)...); + if constexpr (std::same_as, separator>) { + stream_put_line_impl::apply(s, std::forward(f), + std::forward(rs)...); } else { - if constexpr (!IsFirstWord) { - s << sep.str; - } + s << sep.str; stream_put(s, std::forward(f)); - stream_put_line_impl::apply(s, sep, std::forward(rs)...); + stream_put_line_impl::apply(s, sep, std::forward(rs)...); } return s; } }; - template - Stream& stream_put_line(Stream& s, const separator& sep, Args&&... args) + template + Stream& stream_put_line(Stream& s, const separator& sep, First&& a0, Args&&... args) { - return stream_put_line_impl::apply(s, sep, std::forward(args)...); + stream_put(s, std::forward(a0)); + stream_put_line_impl::apply(s, sep, std::forward(args)...); + return s; } inline static const separator DefaultSeparator { " " }; From 681469f17fea3f76ff61c54fa050c8a20b091a17 Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 10 Sep 2022 00:56:14 +0800 Subject: [PATCH 4/8] perf: Remove redundant parameters for logger --- src/MeoAssistant/Logger.hpp | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/src/MeoAssistant/Logger.hpp b/src/MeoAssistant/Logger.hpp index d14d8f73f4..de391d843e 100644 --- a/src/MeoAssistant/Logger.hpp +++ b/src/MeoAssistant/Logger.hpp @@ -42,7 +42,7 @@ namespace asst { #ifdef ASST_DEBUG std::string_view level = "DEB"; - log(level, DefaultSeparator, std::forward(args)...); + log(level, std::forward(args)...); #endif } @@ -50,30 +50,30 @@ namespace asst inline void trace(Args&&... args) { std::string_view level = "TRC"; - log(level, DefaultSeparator, std::forward(args)...); + log(level, std::forward(args)...); } template inline void info(Args&&... args) { std::string_view level = "INF"; - log(level, DefaultSeparator, std::forward(args)...); + log(level, std::forward(args)...); } template inline void warn(Args&&... args) { std::string_view level = "WRN"; - log(level, DefaultSeparator, std::forward(args)...); + log(level, std::forward(args)...); } template inline void error(Args&&... args) { std::string_view level = "ERR"; - log(level, DefaultSeparator, std::forward(args)...); + log(level, std::forward(args)...); } template inline void log_with_custom_level(std::string_view level, Args&&... args) { - log(level, DefaultSeparator, std::forward(args)...); + log(level, std::forward(args)...); } void flush() { @@ -118,7 +118,7 @@ namespace asst } template - void log(std::string_view level, const separator& sep, Args&&... args) + void log(std::string_view level, Args&&... args) { std::unique_lock trace_lock(m_trace_mutex); @@ -141,13 +141,13 @@ namespace asst m_ofs = std::ofstream(m_log_path, std::ios::out | std::ios::app); } #ifdef ASST_DEBUG - stream_put_line(m_ofs, sep, buff, args...); + stream_put_line(m_ofs, buff, args...); #else - stream_put_line(m_ofs, sep, buff, std::forward(args)...); + stream_put_line(m_ofs, buff, std::forward(args)...); #endif #ifdef ASST_DEBUG - stream_put_line(std::cout, sep, buff, std::forward(args)...); + stream_put_line(std::cout, buff, std::forward(args)...); #endif } @@ -235,14 +235,14 @@ namespace asst }; template - Stream& stream_put_line(Stream& s, const separator& sep, First&& a0, Args&&... args) + Stream& stream_put_line(Stream& s, First&& a0, Args&&... args) { stream_put(s, std::forward(a0)); - stream_put_line_impl::apply(s, sep, std::forward(args)...); + static const separator default_sep(" "); + stream_put_line_impl::apply(s, default_sep, std::forward(args)...); return s; } - inline static const separator DefaultSeparator { " " }; inline static std::filesystem::path m_directory; std::filesystem::path m_log_path = m_directory / "asst.log"; From c41843b8281932fd61d009c5caf6b0cffa14fab6 Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 10 Sep 2022 01:24:15 +0800 Subject: [PATCH 5/8] =?UTF-8?q?perf:=20=E6=B7=BB=E5=8A=A0separator?= =?UTF-8?q?=E7=9A=84=E9=9D=99=E6=80=81=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MeoAssistant/Controller.cpp | 2 +- src/MeoAssistant/Logger.hpp | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/MeoAssistant/Controller.cpp b/src/MeoAssistant/Controller.cpp index 5f3f587061..d9a7aa398d 100644 --- a/src/MeoAssistant/Controller.cpp +++ b/src/MeoAssistant/Controller.cpp @@ -422,7 +422,7 @@ std::optional asst::Controller::call_command(const std::string& cmd Log.info("Call `", cmd, "` ret", exit_ret, ", cost", duration, "ms , stdout size:", pipe_data.size(), ", socket size:", sock_data.size()); if (!pipe_data.empty() && pipe_data.size() < 4096) { - Log.trace("output:", Logger::separator("\n"), pipe_data); + Log.trace("output:", Logger::separator::newline, pipe_data); } if (!exit_ret) { diff --git a/src/MeoAssistant/Logger.hpp b/src/MeoAssistant/Logger.hpp index de391d843e..0612f30236 100644 --- a/src/MeoAssistant/Logger.hpp +++ b/src/MeoAssistant/Logger.hpp @@ -20,7 +20,16 @@ namespace asst public: struct separator { - separator(std::string_view str) : str(str) {} + separator() = default; + separator(std::string_view s) : str(s) {} + separator& operator=(std::string_view s) { 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; }; @@ -238,8 +247,7 @@ namespace asst Stream& stream_put_line(Stream& s, First&& a0, Args&&... args) { stream_put(s, std::forward(a0)); - static const separator default_sep(" "); - stream_put_line_impl::apply(s, default_sep, std::forward(args)...); + stream_put_line_impl::apply(s, separator::space, std::forward(args)...); return s; } @@ -251,6 +259,12 @@ namespace asst std::ofstream m_ofs; }; + inline const Logger::separator Logger::separator::none; + inline const Logger::separator Logger::separator::space(" "); + inline const Logger::separator Logger::separator::tab("\t"); + inline const Logger::separator Logger::separator::newline("\n"); + inline const Logger::separator Logger::separator::comma(","); + class LoggerAux { public: From d6d1b0325c71d100ea861ab3b839b1dd83f265d4 Mon Sep 17 00:00:00 2001 From: Horror Proton <107091537+horror-proton@users.noreply.github.com> Date: Sat, 10 Sep 2022 02:52:31 +0800 Subject: [PATCH 6/8] perf: add qualifiers --- src/MeoAssistant/Logger.hpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/MeoAssistant/Logger.hpp b/src/MeoAssistant/Logger.hpp index 0612f30236..ae0dd55013 100644 --- a/src/MeoAssistant/Logger.hpp +++ b/src/MeoAssistant/Logger.hpp @@ -21,8 +21,12 @@ namespace asst struct separator { separator() = default; - separator(std::string_view s) : str(s) {} - separator& operator=(std::string_view s) { str = s; } + constexpr explicit separator(std::string_view s) noexcept : str(s) {} + separator& operator=(std::string_view s) noexcept + { + str = s; + return *this; + } static const separator none; static const separator space; From 5b95481063f47486b4eaeacf4b4ff4641bdb6d64 Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 10 Sep 2022 21:37:43 +0800 Subject: [PATCH 7/8] perf: add logger::level (cherry picked from commit ae317affff09d7c9c94920d6f5618706b070797c) --- src/MeoAssistant/AsstHttp.hpp | 6 ++-- src/MeoAssistant/Logger.hpp | 64 ++++++++++++++++++++++++----------- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/MeoAssistant/AsstHttp.hpp b/src/MeoAssistant/AsstHttp.hpp index 718b73b6a8..d841945816 100644 --- a/src/MeoAssistant/AsstHttp.hpp +++ b/src/MeoAssistant/AsstHttp.hpp @@ -23,7 +23,7 @@ namespace asst::http std::string_view m_body; std::unordered_map m_headers; - bool _analyze_status_line(const std::string_view& status_line) + bool _analyze_status_line(std::string_view status_line) { size_t _word_count = 0; for (const auto& word : utils::string_split(status_line, " ")) { @@ -57,7 +57,7 @@ namespace asst::http m_last_error = "status line too short"; return false; } - bool _analyze_headers_line(const std::string_view& status_line) + bool _analyze_headers_line(std::string_view status_line) { size_t _colon_pos = status_line.find(':'); if (_colon_pos == status_line.npos) { @@ -123,7 +123,7 @@ namespace asst::http std::string_view protocol_version() const noexcept { return m_protocol_version; } std::string_view status_code_info() const noexcept { return m_status_code_info; } std::string_view body() const noexcept { return m_body; } - std::optional find_header(const std::string_view& key) const + std::optional find_header(std::string_view key) const { if (auto iter = m_headers.find(key); iter != m_headers.cend()) { return iter->second; diff --git a/src/MeoAssistant/Logger.hpp b/src/MeoAssistant/Logger.hpp index ae0dd55013..703a582460 100644 --- a/src/MeoAssistant/Logger.hpp +++ b/src/MeoAssistant/Logger.hpp @@ -20,9 +20,13 @@ namespace asst public: struct separator { - separator() = default; + constexpr separator() = default; + constexpr separator(const separator&) = default; + constexpr separator(separator&&) noexcept = default; constexpr explicit separator(std::string_view s) noexcept : str(s) {} - separator& operator=(std::string_view s) noexcept + constexpr separator& operator=(const separator&) = default; + constexpr separator& operator=(separator&&) noexcept = default; + constexpr separator& operator=(std::string_view s) noexcept { str = s; return *this; @@ -37,6 +41,28 @@ namespace asst std::string_view str; }; + struct level + { + constexpr level(const level&) = default; + constexpr level(level&&) noexcept = default; + constexpr explicit level(std::string_view s) noexcept : str(s) {} + constexpr level& operator=(const level&) = default; + constexpr level& operator=(level&&) noexcept = default; + constexpr level& operator=(std::string_view s) noexcept + { + str = s; + return *this; + } + + static const level debug; + static const level trace; + static const level info; + static const level warn; + static const level error; + + std::string_view str; + }; + public: virtual ~Logger() override { flush(); } @@ -54,39 +80,34 @@ namespace asst inline void debug([[maybe_unused]] Args&&... args) { #ifdef ASST_DEBUG - std::string_view level = "DEB"; - log(level, std::forward(args)...); + log(level::debug, std::forward(args)...); #endif } template inline void trace(Args&&... args) { - std::string_view level = "TRC"; - log(level, std::forward(args)...); + log(level::trace, std::forward(args)...); } template inline void info(Args&&... args) { - std::string_view level = "INF"; - log(level, std::forward(args)...); + log(level::info, std::forward(args)...); } template inline void warn(Args&&... args) { - std::string_view level = "WRN"; - log(level, std::forward(args)...); + log(level::warn, std::forward(args)...); } template inline void error(Args&&... args) { - std::string_view level = "ERR"; - log(level, std::forward(args)...); + log(level::error, std::forward(args)...); } template - inline void log_with_custom_level(std::string_view level, Args&&... args) + inline void log_with_custom_level(level lv, Args&&... args) { - log(level, std::forward(args)...); + log(lv, std::forward(args)...); } void flush() { @@ -96,7 +117,7 @@ namespace asst } } - protected: + private: friend class SingletonHolder; Logger() @@ -105,7 +126,6 @@ namespace asst log_init_info(); } - private: void check_filesize_and_remove() const { constexpr uintmax_t MaxLogSize = 4ULL * 1024 * 1024; @@ -131,7 +151,7 @@ namespace asst } template - void log(std::string_view level, Args&&... args) + void log(level lv, Args&&... args) { std::unique_lock trace_lock(m_trace_mutex); @@ -143,10 +163,10 @@ namespace asst #else // ! _MSC_VER sprintf(buff, #endif // END _MSC_VER - "[%s][%s][Px%x][Tx%lx]", asst::utils::get_format_time().c_str(), level.data(), _getpid(), + "[%s][%s][Px%x][Tx%lx]", asst::utils::get_format_time().c_str(), lv.str.data(), _getpid(), ::GetCurrentThreadId()); #else // ! _WIN32 - sprintf(buff, "[%s][%s][Px%x][Tx%lx]", asst::utils::get_format_time().c_str(), level.data(), getpid(), + sprintf(buff, "[%s][%s][Px%x][Tx%lx]", asst::utils::get_format_time().c_str(), lv.str.data(), getpid(), (unsigned long)(std::hash {}(std::this_thread::get_id()))); #endif // END _WIN32 @@ -269,6 +289,12 @@ namespace asst inline const Logger::separator Logger::separator::newline("\n"); inline const Logger::separator Logger::separator::comma(","); + inline const Logger::level Logger::level::debug("DBG"); + inline const Logger::level Logger::level::trace("TRC"); + inline const Logger::level Logger::level::info("INF"); + inline const Logger::level Logger::level::warn("WRN"); + inline const Logger::level Logger::level::error("ERR"); + class LoggerAux { public: From c553dd9c06699adcfa6b069a7c137994e1de4b85 Mon Sep 17 00:00:00 2001 From: MistEO Date: Sat, 10 Sep 2022 21:56:29 +0800 Subject: [PATCH 8/8] fix: build error (cherry picked from commit 3ed940c49377e49e194a31c8714f8de6ca1d7aca) --- src/MeoAssistant/AsstCaller.cpp | 2 +- src/MeoAssistant/Logger.hpp | 67 +++++++++++++++------------------ 2 files changed, 32 insertions(+), 37 deletions(-) diff --git a/src/MeoAssistant/AsstCaller.cpp b/src/MeoAssistant/AsstCaller.cpp index ed9102b13d..4dc13eba20 100644 --- a/src/MeoAssistant/AsstCaller.cpp +++ b/src/MeoAssistant/AsstCaller.cpp @@ -196,5 +196,5 @@ void AsstLog(const char* level, const char* message) std::cerr << "Not inited" << std::endl; return; } - asst::Log.log_with_custom_level(level, message); + asst::Log.log(asst::Logger::level(level), message); } diff --git a/src/MeoAssistant/Logger.hpp b/src/MeoAssistant/Logger.hpp index 703a582460..34f995ee9b 100644 --- a/src/MeoAssistant/Logger.hpp +++ b/src/MeoAssistant/Logger.hpp @@ -105,10 +105,39 @@ namespace asst log(level::error, std::forward(args)...); } template - inline void log_with_custom_level(level lv, Args&&... args) + void log(level lv, Args&&... args) { - log(lv, std::forward(args)...); + std::unique_lock trace_lock(m_trace_mutex); + + constexpr int buff_len = 128; + char buff[buff_len] = { 0 }; +#ifdef _WIN32 +#ifdef _MSC_VER + sprintf_s(buff, buff_len, +#else // ! _MSC_VER + sprintf(buff, +#endif // END _MSC_VER + "[%s][%s][Px%x][Tx%lx]", asst::utils::get_format_time().c_str(), lv.str.data(), _getpid(), + ::GetCurrentThreadId()); +#else // ! _WIN32 + sprintf(buff, "[%s][%s][Px%x][Tx%lx]", asst::utils::get_format_time().c_str(), lv.str.data(), getpid(), + (unsigned long)(std::hash {}(std::this_thread::get_id()))); +#endif // END _WIN32 + + if (!m_ofs || !m_ofs.is_open()) { + m_ofs = std::ofstream(m_log_path, std::ios::out | std::ios::app); + } +#ifdef ASST_DEBUG + stream_put_line(m_ofs, buff, args...); +#else + stream_put_line(m_ofs, buff, std::forward(args)...); +#endif + +#ifdef ASST_DEBUG + stream_put_line(std::cout, buff, std::forward(args)...); +#endif } + void flush() { std::unique_lock trace_lock(m_trace_mutex); @@ -150,40 +179,6 @@ namespace asst trace("-----------------------------"); } - template - void log(level lv, Args&&... args) - { - std::unique_lock trace_lock(m_trace_mutex); - - constexpr int buff_len = 128; - char buff[buff_len] = { 0 }; -#ifdef _WIN32 -#ifdef _MSC_VER - sprintf_s(buff, buff_len, -#else // ! _MSC_VER - sprintf(buff, -#endif // END _MSC_VER - "[%s][%s][Px%x][Tx%lx]", asst::utils::get_format_time().c_str(), lv.str.data(), _getpid(), - ::GetCurrentThreadId()); -#else // ! _WIN32 - sprintf(buff, "[%s][%s][Px%x][Tx%lx]", asst::utils::get_format_time().c_str(), lv.str.data(), getpid(), - (unsigned long)(std::hash {}(std::this_thread::get_id()))); -#endif // END _WIN32 - - if (!m_ofs || !m_ofs.is_open()) { - m_ofs = std::ofstream(m_log_path, std::ios::out | std::ios::app); - } -#ifdef ASST_DEBUG - stream_put_line(m_ofs, buff, args...); -#else - stream_put_line(m_ofs, buff, std::forward(args)...); -#endif - -#ifdef ASST_DEBUG - stream_put_line(std::cout, buff, std::forward(args)...); -#endif - } - template struct has_stream_insertion_operator : std::false_type {};