diff --git a/source/MaaUtils/Logger/Logger.cpp b/source/MaaUtils/Logger/Logger.cpp index c004e1c23..aa4a57103 100644 --- a/source/MaaUtils/Logger/Logger.cpp +++ b/source/MaaUtils/Logger/Logger.cpp @@ -211,7 +211,7 @@ void Logger::log_proc_info() LogStream Logger::internal_dbg() { - return debug("Logger"); + return stream(level::debug, "Logger"); } std::string StringConverter::operator()(const std::filesystem::path& path) const diff --git a/source/include/Utils/Logger.h b/source/include/Utils/Logger.h index 1902e58f7..32378d624 100644 --- a/source/include/Utils/Logger.h +++ b/source/include/Utils/Logger.h @@ -25,40 +25,34 @@ class MAA_UTILS_API Logger Logger& operator=(const Logger&) = delete; Logger& operator=(Logger&&) = delete; - template - auto fatal(args_t&&... args) + auto fatal(const std::source_location& loc = std::source_location::current()) { - return stream(level::fatal, std::forward(args)...); + return stream(level::fatal, loc); } - template - auto error(args_t&&... args) + auto error(const std::source_location& loc = std::source_location::current()) { - return stream(level::error, std::forward(args)...); + return stream(level::error, loc); } - template - auto warn(args_t&&... args) + auto warn(const std::source_location& loc = std::source_location::current()) { - return stream(level::warn, std::forward(args)...); + return stream(level::warn, loc); } - template - auto info(args_t&&... args) + auto info(const std::source_location& loc = std::source_location::current()) { - return stream(level::info, std::forward(args)...); + return stream(level::info, loc); } - template - auto debug(args_t&&... args) + auto debug(const std::source_location& loc = std::source_location::current()) { - return stream(level::debug, std::forward(args)...); + return stream(level::debug, loc); } - template - auto trace(args_t&&... args) + auto trace(const std::source_location& loc = std::source_location::current()) { - return stream(level::trace, std::forward(args)...); + return stream(level::trace, loc); } void start_logging(std::filesystem::path dir); @@ -107,9 +101,8 @@ class MAA_UTILS_API Logger class LogScopeEnterHelper { public: - template - explicit LogScopeEnterHelper(args_t&&... args) - : stream_(Logger::get_instance().debug(std::forward(args)...)) + explicit LogScopeEnterHelper(const std::source_location& loc = std::source_location::current()) + : stream_(Logger::get_instance().debug(loc)) { } @@ -121,61 +114,36 @@ class LogScopeEnterHelper LogStream stream_; }; -template class LogScopeLeaveHelper { public: - explicit LogScopeLeaveHelper(args_t&&... args) - : args_(std::forward(args)...) + explicit LogScopeLeaveHelper(std::source_location&& loc = std::source_location::current()) + : loc_(loc) { } ~LogScopeLeaveHelper() { - std::apply( - [](auto&&... args) { - return Logger::get_instance().trace(std::forward(args)...); - }, - std::move(args_)) - << "| leave," << duration_since(start_); + Logger::get_instance().trace(loc_) << "| leave," << duration_since(start_); } private: - std::tuple args_; + std::source_location loc_; std::chrono::time_point start_ = std::chrono::steady_clock::now(); }; -inline constexpr std::string_view pertty_file(std::string_view file) -{ - size_t pos = file.find_last_of(std::filesystem::path::preferred_separator); - return file.substr(pos + 1, file.size()); -} - MAA_LOG_NS_END -#define STRINGIZE(x) STRINGIZE2(x) -#define STRINGIZE2(x) #x -#define LINE_STRING STRINGIZE(__LINE__) +#define LogFatal MAA_LOG_NS::Logger::get_instance().fatal() +#define LogError MAA_LOG_NS::Logger::get_instance().error() +#define LogWarn MAA_LOG_NS::Logger::get_instance().warn() +#define LogInfo MAA_LOG_NS::Logger::get_instance().info() +#define LogDebug MAA_LOG_NS::Logger::get_instance().debug() +#define LogTrace MAA_LOG_NS::Logger::get_instance().trace() -#define MAA_FILE MAA_LOG_NS::pertty_file(__FILE__) -#define MAA_LINE std::string_view("L" LINE_STRING) -#ifdef _MSC_VER -#define MAA_FUNCTION std::string_view(__FUNCTION__) -#else -#define MAA_FUNCTION std::string_view(__PRETTY_FUNCTION__) -#endif -#define LOG_ARGS MAA_FILE, MAA_LINE, MAA_FUNCTION - -#define LogFatal MAA_LOG_NS::Logger::get_instance().fatal(LOG_ARGS) -#define LogError MAA_LOG_NS::Logger::get_instance().error(LOG_ARGS) -#define LogWarn MAA_LOG_NS::Logger::get_instance().warn(LOG_ARGS) -#define LogInfo MAA_LOG_NS::Logger::get_instance().info(LOG_ARGS) -#define LogDebug MAA_LOG_NS::Logger::get_instance().debug(LOG_ARGS) -#define LogTrace MAA_LOG_NS::Logger::get_instance().trace(LOG_ARGS) - -#define LogFunc \ - MAA_LOG_NS::LogScopeLeaveHelper ScopeHelperVarName(LOG_ARGS); \ - MAA_LOG_NS::LogScopeEnterHelper(LOG_ARGS)() +#define LogFunc \ + MAA_LOG_NS::LogScopeLeaveHelper ScopeHelperVarName; \ + MAA_LOG_NS::LogScopeEnterHelper()() #define VAR_RAW(x) "[" << #x << "=" << (x) << "] " #define VAR(x) MAA_LOG_NS::separator::none << VAR_RAW(x) << MAA_LOG_NS::separator::space diff --git a/source/include/Utils/LoggerUtils.h b/source/include/Utils/LoggerUtils.h index 771f98801..899369ff3 100644 --- a/source/include/Utils/LoggerUtils.h +++ b/source/include/Utils/LoggerUtils.h @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -186,8 +187,22 @@ class MAA_UTILS_API LogStream } } - template - void stream_props(args_t&&... args) + static constexpr std::string_view pretty_file(std::string_view file) + { + size_t pos = file.find_last_of(std::filesystem::path::preferred_separator); + return file.substr(pos + 1, file.size()); + } + + void stream_props(const std::source_location& loc) + { + stream_props(std::format( + "[{}][L{}][{}]", + LogStream::pretty_file(loc.file_name()), + loc.line(), + loc.function_name())); + } + + void stream_props(std::string_view prefix) { #ifdef _WIN32 int pid = _getpid(); @@ -197,10 +212,7 @@ class MAA_UTILS_API LogStream auto tid = static_cast(std::hash {}(std::this_thread::get_id())); std::string props = - std::format("[{}][{}][Px{}][Tx{}]", format_now(), level_str(), pid, tid); - for (auto&& arg : { args... }) { - props += std::format("[{}]", arg); - } + std::format("[{}][{}][Px{}][Tx{}]{}", format_now(), level_str(), pid, tid, prefix); stream(props, sep_); }