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/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/Controller.cpp b/src/MeoAssistant/Controller.cpp index 408aa4b7d9..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:\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 26a52c6970..34f995ee9b 100644 --- a/src/MeoAssistant/Logger.hpp +++ b/src/MeoAssistant/Logger.hpp @@ -17,6 +17,52 @@ namespace asst { class Logger : public SingletonHolder { + public: + struct separator + { + constexpr separator() = default; + constexpr separator(const separator&) = default; + constexpr separator(separator&&) noexcept = default; + constexpr explicit separator(std::string_view s) noexcept : str(s) {} + constexpr separator& operator=(const separator&) = default; + constexpr separator& operator=(separator&&) noexcept = default; + constexpr separator& operator=(std::string_view s) noexcept + { + str = s; + return *this; + } + + static const separator none; + static const separator space; + static const separator tab; + static const separator newline; + static const separator comma; + + 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(); } @@ -34,40 +80,64 @@ 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) + void log(level lv, Args&&... args) { - log(level, 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); @@ -76,7 +146,7 @@ namespace asst } } - protected: + private: friend class SingletonHolder; Logger() @@ -85,7 +155,6 @@ namespace asst log_init_info(); } - private: void check_filesize_and_remove() const { constexpr uintmax_t MaxLogSize = 4ULL * 1024 * 1024; @@ -110,40 +179,6 @@ namespace asst trace("-----------------------------"); } - template - void log(std::string_view level, 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(), level.data(), _getpid(), - ::GetCurrentThreadId()); -#else // ! _WIN32 - sprintf(buff, "[%s][%s][Px%x][Tx%lx]", asst::utils::get_format_time().c_str(), level.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 {}; @@ -181,7 +216,7 @@ namespace asst for (const auto& elem : std::forward(v)) { s << comma; stream_put(s, elem); - comma = ","; + comma = ", "; } s << "]"; return s; @@ -202,40 +237,37 @@ namespace asst template struct stream_put_line_impl { - static constexpr Stream& apply(Stream& s) + static constexpr Stream& apply(Stream& s, const separator&) { s << std::endl; return s; } }; - template - struct stream_put_line_impl - { - static constexpr Stream& apply(Stream& s, Only&& only) - { - stream_put(s, std::forward(only)); - s << std::endl; - return s; - } - }; - template struct stream_put_line_impl { - static constexpr Stream& apply(Stream& s, First&& f, Rest&&... rs) + static constexpr Stream& apply(Stream& s, [[maybe_unused]] const separator& sep, First&& f, Rest&&... rs) { - stream_put(s, std::forward(f)); - s << " "; - stream_put_line_impl::apply(s, std::forward(rs)...); + if constexpr (std::same_as, separator>) { + stream_put_line_impl::apply(s, std::forward(f), + std::forward(rs)...); + } + else { + s << sep.str; + stream_put(s, std::forward(f)); + stream_put_line_impl::apply(s, sep, std::forward(rs)...); + } return s; } }; - template - Stream& stream_put_line(Stream& s, Args&&... args) + template + Stream& stream_put_line(Stream& s, First&& a0, Args&&... args) { - return stream_put_line_impl::apply(s, std::forward(args)...); + stream_put(s, std::forward(a0)); + stream_put_line_impl::apply(s, separator::space, std::forward(args)...); + return s; } inline static std::filesystem::path m_directory; @@ -246,6 +278,18 @@ 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(","); + + 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: