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: