From c557970ef438dd19d15ddc7cd1c9778a7448b1f2 Mon Sep 17 00:00:00 2001 From: MistEO Date: Fri, 9 Sep 2022 23:06:21 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=9D=E7=89=88=20Logger::separator?= 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";