Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor logger #295

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion source/MaaUtils/Logger/Logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
86 changes: 27 additions & 59 deletions source/include/Utils/Logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,34 @@ class MAA_UTILS_API Logger
Logger& operator=(const Logger&) = delete;
Logger& operator=(Logger&&) = delete;

template <typename... args_t>
auto fatal(args_t&&... args)
auto fatal(const std::source_location& loc = std::source_location::current())
{
return stream(level::fatal, std::forward<args_t>(args)...);
return stream(level::fatal, loc);
}

template <typename... args_t>
auto error(args_t&&... args)
auto error(const std::source_location& loc = std::source_location::current())
{
return stream(level::error, std::forward<args_t>(args)...);
return stream(level::error, loc);
}

template <typename... args_t>
auto warn(args_t&&... args)
auto warn(const std::source_location& loc = std::source_location::current())
{
return stream(level::warn, std::forward<args_t>(args)...);
return stream(level::warn, loc);
}

template <typename... args_t>
auto info(args_t&&... args)
auto info(const std::source_location& loc = std::source_location::current())
{
return stream(level::info, std::forward<args_t>(args)...);
return stream(level::info, loc);
}

template <typename... args_t>
auto debug(args_t&&... args)
auto debug(const std::source_location& loc = std::source_location::current())
{
return stream(level::debug, std::forward<args_t>(args)...);
return stream(level::debug, loc);
}

template <typename... args_t>
auto trace(args_t&&... args)
auto trace(const std::source_location& loc = std::source_location::current())
{
return stream(level::trace, std::forward<args_t>(args)...);
return stream(level::trace, loc);
}

void start_logging(std::filesystem::path dir);
Expand Down Expand Up @@ -107,9 +101,8 @@ class MAA_UTILS_API Logger
class LogScopeEnterHelper
{
public:
template <typename... args_t>
explicit LogScopeEnterHelper(args_t&&... args)
: stream_(Logger::get_instance().debug(std::forward<args_t>(args)...))
explicit LogScopeEnterHelper(const std::source_location& loc = std::source_location::current())
: stream_(Logger::get_instance().debug(loc))
{
}

Expand All @@ -121,61 +114,36 @@ class LogScopeEnterHelper
LogStream stream_;
};

template <typename... args_t>
class LogScopeLeaveHelper
{
public:
explicit LogScopeLeaveHelper(args_t&&... args)
: args_(std::forward<args_t>(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<decltype(args)>(args)...);
},
std::move(args_))
<< "| leave," << duration_since(start_);
Logger::get_instance().trace(loc_) << "| leave," << duration_since(start_);
}

private:
std::tuple<args_t...> args_;
std::source_location loc_;
std::chrono::time_point<std::chrono::steady_clock> 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
Expand Down
24 changes: 18 additions & 6 deletions source/include/Utils/LoggerUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <iostream>
#include <mutex>
#include <ranges>
#include <source_location>
#include <sstream>
#include <thread>
#include <tuple>
Expand Down Expand Up @@ -186,8 +187,22 @@ class MAA_UTILS_API LogStream
}
}

template <typename... args_t>
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();
Expand All @@ -197,10 +212,7 @@ class MAA_UTILS_API LogStream
auto tid = static_cast<uint16_t>(std::hash<std::thread::id> {}(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_);
}

Expand Down
Loading