From a6c0fdb71a01ac454d2a25e93d83080c2d8090a9 Mon Sep 17 00:00:00 2001 From: zzyyyl Date: Sun, 11 Sep 2022 03:28:43 +0800 Subject: [PATCH] chore: rename --- src/MeoAssistant/Logger.hpp | 98 ++++++++++++------------------------- 1 file changed, 32 insertions(+), 66 deletions(-) diff --git a/src/MeoAssistant/Logger.hpp b/src/MeoAssistant/Logger.hpp index 8f88a0ce12..3a15367a39 100644 --- a/src/MeoAssistant/Logger.hpp +++ b/src/MeoAssistant/Logger.hpp @@ -67,27 +67,27 @@ namespace asst {}; static constexpr _endl endl {}; - class FlagLogger + class LogStream { public: - FlagLogger(std::mutex& mtx, std::ofstream& ofs) : trace_lock(mtx), m_ofs(ofs) {} + LogStream(std::mutex& mtx, std::ofstream& ofs) : m_trace_lock(mtx), m_ofs(ofs) {} template - FlagLogger(std::mutex& mtx, std::ofstream& ofs, Args&&... buff) : trace_lock(mtx), m_ofs(ofs) + LogStream(std::mutex& mtx, std::ofstream& ofs, Args&&... buff) : m_trace_lock(mtx), m_ofs(ofs) { (*this << ... << std::forward(buff)); } template - FlagLogger& operator<<(T&& arg) + LogStream& operator<<(T&& arg) { - if constexpr (std::same_as>) { + if constexpr (std::same_as>) { m_sep = std::forward(arg); } - else if constexpr (std::same_as<_endl, std::remove_cvref_t>) { + else if constexpr (std::same_as<_endl, std::decay_t>) { m_ofs << std::endl; } else { - if (!is_first) { + if (!m_is_first) { #ifdef ASST_DEBUG stream_put(m_ofs, m_sep.str); #else @@ -95,7 +95,7 @@ namespace asst #endif } else { - is_first = false; + m_is_first = false; } #ifdef ASST_DEBUG stream_put(m_ofs, std::forward(arg)); @@ -120,7 +120,7 @@ namespace asst template static Stream& stream_put(Stream& s, T&& v) { - if constexpr (std::same_as>) { + if constexpr (std::same_as>) { if constexpr (ToAnsi) s << utils::utf8_to_ansi(utils::path_to_utf8_string(std::forward(v))); else @@ -160,9 +160,9 @@ namespace asst } } - bool is_first = true; + bool m_is_first = true; separator m_sep { " " }; - std::unique_lock trace_lock; + std::unique_lock m_trace_lock; std::ofstream& m_ofs; }; @@ -208,10 +208,26 @@ namespace asst log(level::error, std::forward(args)...); } template - void log(level lv, Args&&... args) + inline void log(level lv, Args&&... args) { - std::unique_lock trace_lock(m_trace_mutex); + log(lv, std::forward(args)...); + } + void flush() + { + std::unique_lock m_trace_lock(m_trace_mutex); + if (m_ofs.is_open()) { + m_ofs.close(); + } + } + + template + LogStream operator<<(T&& arg) + { + level lv = level::trace; + if constexpr (std::same_as>) { + lv = std::forward(arg); + } constexpr int buff_len = 128; char buff[buff_len] = { 0 }; #ifdef _WIN32 @@ -230,56 +246,12 @@ namespace asst if (!m_ofs || !m_ofs.is_open()) { m_ofs = std::ofstream(m_log_path, std::ios::out | std::ios::app); } -#ifdef ASST_DEBUG -#ifdef _WIN32 - constexpr bool ansi = true; -#else - constexpr bool ansi = false; -#endif - stream_put_line(std::cout, buff, args...); -#endif - stream_put_line(m_ofs, buff, std::forward(args)...); - } - void flush() - { - std::unique_lock trace_lock(m_trace_mutex); - if (m_ofs.is_open()) { - m_ofs.close(); - } - } - - template - FlagLogger operator<<(T&& arg) - { - std::string_view level_str = "TRC"; - if constexpr (std::same_as>) { - level_str = arg.str; - } - 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(), level_str.data(), _getpid(), - ::GetCurrentThreadId()); -#else // ! _WIN32 - sprintf(buff, "[%s][%s][Px%x][Tx%lx]", asst::utils::get_format_time().c_str(), level_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); - } - - if constexpr (std::same_as>) { - return FlagLogger(m_trace_mutex, m_ofs, buff); + if constexpr (std::same_as>) { + return LogStream(m_trace_mutex, m_ofs, buff); } else { - return FlagLogger(m_trace_mutex, m_ofs, buff, arg); + return LogStream(m_trace_mutex, m_ofs, buff, arg); } } @@ -316,12 +288,6 @@ namespace asst trace("-----------------------------"); } - template - void log(std::string_view level, Args&&... args) - { - ((*this << Logger::level(level)) << ... << std::forward(args)) << endl; - } - inline static std::filesystem::path m_directory; std::filesystem::path m_log_path = m_directory / "asst.log";