mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-16 17:57:01 +08:00
perf: add logger::level
(cherry picked from commit ae317affff09d7c9c94920d6f5618706b070797c)
This commit is contained in:
@@ -23,7 +23,7 @@ namespace asst::http
|
||||
std::string_view m_body;
|
||||
std::unordered_map<std::string_view, std::string_view> 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<std::string_view> find_header(const std::string_view& key) const
|
||||
std::optional<std::string_view> find_header(std::string_view key) const
|
||||
{
|
||||
if (auto iter = m_headers.find(key); iter != m_headers.cend()) {
|
||||
return iter->second;
|
||||
|
||||
@@ -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>(args)...);
|
||||
log(level::debug, std::forward<Args>(args)...);
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
inline void trace(Args&&... args)
|
||||
{
|
||||
std::string_view level = "TRC";
|
||||
log(level, std::forward<Args>(args)...);
|
||||
log(level::trace, std::forward<Args>(args)...);
|
||||
}
|
||||
template <typename... Args>
|
||||
inline void info(Args&&... args)
|
||||
{
|
||||
std::string_view level = "INF";
|
||||
log(level, std::forward<Args>(args)...);
|
||||
log(level::info, std::forward<Args>(args)...);
|
||||
}
|
||||
template <typename... Args>
|
||||
inline void warn(Args&&... args)
|
||||
{
|
||||
std::string_view level = "WRN";
|
||||
log(level, std::forward<Args>(args)...);
|
||||
log(level::warn, std::forward<Args>(args)...);
|
||||
}
|
||||
template <typename... Args>
|
||||
inline void error(Args&&... args)
|
||||
{
|
||||
std::string_view level = "ERR";
|
||||
log(level, std::forward<Args>(args)...);
|
||||
log(level::error, std::forward<Args>(args)...);
|
||||
}
|
||||
template <typename... Args>
|
||||
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>(args)...);
|
||||
log(lv, std::forward<Args>(args)...);
|
||||
}
|
||||
void flush()
|
||||
{
|
||||
@@ -96,7 +117,7 @@ namespace asst
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
private:
|
||||
friend class SingletonHolder<Logger>;
|
||||
|
||||
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 <typename... Args>
|
||||
void log(std::string_view level, Args&&... args)
|
||||
void log(level lv, Args&&... args)
|
||||
{
|
||||
std::unique_lock<std::mutex> 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::thread::id> {}(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:
|
||||
|
||||
Reference in New Issue
Block a user