From 42b75be796e8481394a8dd380c2500f76287919f Mon Sep 17 00:00:00 2001 From: OEOTYAN Date: Fri, 10 Jan 2025 02:27:00 +0800 Subject: [PATCH] refactor: refactoring logger --- src/ll/api/io/Logger.cpp | 14 ++--- src/ll/api/io/Logger.h | 51 ++++++++++--------- src/mc/server/commands/CommandParameterData.h | 34 ++++++------- 3 files changed, 47 insertions(+), 52 deletions(-) diff --git a/src/ll/api/io/Logger.cpp b/src/ll/api/io/Logger.cpp index b7a2b16ac5..59a3dac055 100644 --- a/src/ll/api/io/Logger.cpp +++ b/src/ll/api/io/Logger.cpp @@ -51,21 +51,13 @@ Logger::Logger(PrivateTag, std::string_view title) : impl(std::make_unique impl->sinks.push_back(std::make_shared()); } -std::string const& Logger::getTitle() { return impl->title; } +std::string const& Logger::getTitle() const noexcept { return impl->title; } -LogLevel Logger::getLevel() { return impl->level; } +LogLevel Logger::getLevel() const noexcept { return impl->level; } -void Logger::printView(LogLevel level, std::string_view msg) const noexcept { - if (level > impl->level) { - return; - } - printStr(level, std::string{msg}); -} +bool Logger::shouldLog(LogLevel level) const noexcept { return level <= impl->level; } void Logger::printStr(LogLevel level, std::string&& msg) const noexcept try { - if (level > impl->level) { - return; - } impl->pool.execute([logger = shared_from_this(), msg = LogMessage{std::move(msg), impl->title, level, sys_utils::getLocalTime()}] { try { diff --git a/src/ll/api/io/Logger.h b/src/ll/api/io/Logger.h index 301457f868..91a8ad67d4 100644 --- a/src/ll/api/io/Logger.h +++ b/src/ll/api/io/Logger.h @@ -24,7 +24,6 @@ namespace ll::io { class LoggerRegistry; class Logger : public std::enable_shared_from_this { LLAPI void printStr(LogLevel, std::string&&) const noexcept; - LLAPI void printView(LogLevel, std::string_view) const noexcept; friend LoggerRegistry; @@ -39,81 +38,85 @@ class Logger : public std::enable_shared_from_this { public: template void log(LogLevel level, fmt::format_string fmt, Args&&... args) const { - printStr(level, fmt::vformat(fmt.get(), fmt::make_format_args(args...))); + if (shouldLog(level)) printStr(level, fmt::vformat(fmt.get(), fmt::make_format_args(args...))); + } + void log(LogLevel level, std::string&& msg) const { + if (shouldLog(level)) printStr(level, std::move(msg)); } - void log(LogLevel level, std::string&& msg) const { printStr(level, std::move(msg)); } template void log(LogLevel level, S const& msg) const { - printView(level, msg); + if (shouldLog(level)) printStr(level, std::string{msg}); } template void fatal(fmt::format_string fmt, Args&&... args) const { - printStr(LogLevel::Fatal, fmt::vformat(fmt.get(), fmt::make_format_args(args...))); + log(LogLevel::Fatal, fmt, std::forward(args)...); } - void fatal(std::string&& msg) const { printStr(LogLevel::Fatal, std::move(msg)); } + void fatal(std::string&& msg) const { log(LogLevel::Fatal, std::move(msg)); } template void fatal(S const& msg) const { - printView(LogLevel::Fatal, msg); + log(LogLevel::Fatal, msg); } template void error(fmt::format_string fmt, Args&&... args) const { - printStr(LogLevel::Error, fmt::vformat(fmt.get(), fmt::make_format_args(args...))); + log(LogLevel::Error, fmt, std::forward(args)...); } - void error(std::string&& msg) const { printStr(LogLevel::Error, std::move(msg)); } + void error(std::string&& msg) const { log(LogLevel::Error, std::move(msg)); } template void error(S const& msg) const { - printView(LogLevel::Error, msg); + log(LogLevel::Error, msg); } template void warn(fmt::format_string fmt, Args&&... args) const { - printStr(LogLevel::Warn, fmt::vformat(fmt.get(), fmt::make_format_args(args...))); + log(LogLevel::Warn, fmt, std::forward(args)...); } - void warn(std::string&& msg) const { printStr(LogLevel::Warn, std::move(msg)); } + void warn(std::string&& msg) const { log(LogLevel::Warn, std::move(msg)); } template void warn(S const& msg) const { - printView(LogLevel::Warn, msg); + log(LogLevel::Warn, msg); } template void info(fmt::format_string fmt, Args&&... args) const { - printStr(LogLevel::Info, fmt::vformat(fmt.get(), fmt::make_format_args(args...))); + log(LogLevel::Info, fmt, std::forward(args)...); } - void info(std::string&& msg) const { printStr(LogLevel::Info, std::move(msg)); } + void info(std::string&& msg) const { log(LogLevel::Info, std::move(msg)); } template void info(S const& msg) const { - printView(LogLevel::Info, msg); + log(LogLevel::Info, msg); } template void debug(fmt::format_string fmt, Args&&... args) const { - printStr(LogLevel::Debug, fmt::vformat(fmt.get(), fmt::make_format_args(args...))); + log(LogLevel::Debug, fmt, std::forward(args)...); } - void debug(std::string&& msg) const { printStr(LogLevel::Debug, std::move(msg)); } + void debug(std::string&& msg) const { log(LogLevel::Debug, std::move(msg)); } template void debug(S const& msg) const { - printView(LogLevel::Debug, msg); + log(LogLevel::Debug, msg); } template void trace(fmt::format_string fmt, Args&&... args) const { - printStr(LogLevel::Trace, fmt::vformat(fmt.get(), fmt::make_format_args(args...))); + log(LogLevel::Trace, fmt, std::forward(args)...); } - void trace(std::string&& msg) const { printStr(LogLevel::Trace, std::move(msg)); } + void trace(std::string&& msg) const { log(LogLevel::Trace, std::move(msg)); } template void trace(S const& msg) const { - printView(LogLevel::Trace, msg); + log(LogLevel::Trace, msg); } LLAPI ~Logger(); explicit Logger(PrivateTag, std::string_view); - LLNDAPI std::string const& getTitle(); + LLNDAPI std::string const& getTitle() const noexcept; + + LLNDAPI LogLevel getLevel() const noexcept; - LLNDAPI LogLevel getLevel(); + LLNDAPI bool shouldLog(LogLevel level) const noexcept; LLAPI void setLevel(LogLevel level); diff --git a/src/mc/server/commands/CommandParameterData.h b/src/mc/server/commands/CommandParameterData.h index b4a96934e9..3f01970249 100644 --- a/src/mc/server/commands/CommandParameterData.h +++ b/src/mc/server/commands/CommandParameterData.h @@ -21,23 +21,6 @@ class CommandParameterData { bool (::CommandRegistry::*)(void*, ::CommandRegistry::ParseToken const&, ::CommandOrigin const&, int, ::std::string&, ::std::vector<::std::string>&) const; -public: - // member variables - // NOLINTBEGIN - ::Bedrock::typeid_t<::CommandRegistry> mTypeIndex; - ParseFunction mParse; - ::std::string mName; - char const* mEnumNameOrPostfix; - CommandRegistry::Symbol mEnumOrPostfixSymbol; - char const* mChainedSubcommand; - CommandRegistry::Symbol mChainedSubcommandSymbol; - ::CommandParameterDataType mParamType; - int mOffset; - int mSetOffset; - bool mIsOptional; - ::CommandParameterOption mOptions; - // NOLINTEND - public: CommandParameterData() = default; @@ -59,6 +42,23 @@ class CommandParameterData { CommandParameterData(CommandParameterData const&) = default; CommandParameterData& operator=(CommandParameterData const&) = default; +public: + // member variables + // NOLINTBEGIN + ::Bedrock::typeid_t<::CommandRegistry> mTypeIndex; + ParseFunction mParse; + ::std::string mName; + char const* mEnumNameOrPostfix; + CommandRegistry::Symbol mEnumOrPostfixSymbol; + char const* mChainedSubcommand; + CommandRegistry::Symbol mChainedSubcommandSymbol; + ::CommandParameterDataType mParamType; + int mOffset; + int mSetOffset; + bool mIsOptional; + ::CommandParameterOption mOptions; + // NOLINTEND + public: // member functions // NOLINTBEGIN