mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-16 17:57:01 +08:00
Merge pull request #1803 from MaaAssistantArknights/feat/logger_separator_v1
feat: logger separator - v1 version
This commit is contained in:
@@ -196,5 +196,5 @@ void AsstLog(const char* level, const char* message)
|
||||
std::cerr << "Not inited" << std::endl;
|
||||
return;
|
||||
}
|
||||
asst::Log.log_with_custom_level(level, message);
|
||||
asst::Log.log(asst::Logger::level(level), message);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -422,7 +422,7 @@ std::optional<std::string> asst::Controller::call_command(const std::string& cmd
|
||||
Log.info("Call `", cmd, "` ret", exit_ret, ", cost", duration, "ms , stdout size:", pipe_data.size(),
|
||||
", socket size:", sock_data.size());
|
||||
if (!pipe_data.empty() && pipe_data.size() < 4096) {
|
||||
Log.trace("output:\n", pipe_data);
|
||||
Log.trace("output:", Logger::separator::newline, pipe_data);
|
||||
}
|
||||
|
||||
if (!exit_ret) {
|
||||
|
||||
@@ -17,6 +17,52 @@ namespace asst
|
||||
{
|
||||
class Logger : public SingletonHolder<Logger>
|
||||
{
|
||||
public:
|
||||
struct separator
|
||||
{
|
||||
constexpr separator() = default;
|
||||
constexpr separator(const separator&) = default;
|
||||
constexpr separator(separator&&) noexcept = default;
|
||||
constexpr explicit separator(std::string_view s) noexcept : str(s) {}
|
||||
constexpr separator& operator=(const separator&) = default;
|
||||
constexpr separator& operator=(separator&&) noexcept = default;
|
||||
constexpr separator& operator=(std::string_view s) noexcept
|
||||
{
|
||||
str = s;
|
||||
return *this;
|
||||
}
|
||||
|
||||
static const separator none;
|
||||
static const separator space;
|
||||
static const separator tab;
|
||||
static const separator newline;
|
||||
static const separator comma;
|
||||
|
||||
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(); }
|
||||
|
||||
@@ -34,40 +80,64 @@ 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)
|
||||
void log(level lv, Args&&... args)
|
||||
{
|
||||
log(level, std::forward<Args>(args)...);
|
||||
std::unique_lock<std::mutex> trace_lock(m_trace_mutex);
|
||||
|
||||
constexpr int buff_len = 128;
|
||||
char buff[buff_len] = { 0 };
|
||||
#ifdef _WIN32
|
||||
#ifdef _MSC_VER
|
||||
sprintf_s(buff, buff_len,
|
||||
#else // ! _MSC_VER
|
||||
sprintf(buff,
|
||||
#endif // END _MSC_VER
|
||||
"[%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(), lv.str.data(), getpid(),
|
||||
(unsigned long)(std::hash<std::thread::id> {}(std::this_thread::get_id())));
|
||||
#endif // END _WIN32
|
||||
|
||||
if (!m_ofs || !m_ofs.is_open()) {
|
||||
m_ofs = std::ofstream(m_log_path, std::ios::out | std::ios::app);
|
||||
}
|
||||
#ifdef ASST_DEBUG
|
||||
stream_put_line(m_ofs, buff, args...);
|
||||
#else
|
||||
stream_put_line<false>(m_ofs, buff, std::forward<Args>(args)...);
|
||||
#endif
|
||||
|
||||
#ifdef ASST_DEBUG
|
||||
stream_put_line<true>(std::cout, buff, std::forward<Args>(args)...);
|
||||
#endif
|
||||
}
|
||||
|
||||
void flush()
|
||||
{
|
||||
std::unique_lock<std::mutex> trace_lock(m_trace_mutex);
|
||||
@@ -76,7 +146,7 @@ namespace asst
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
private:
|
||||
friend class SingletonHolder<Logger>;
|
||||
|
||||
Logger()
|
||||
@@ -85,7 +155,6 @@ namespace asst
|
||||
log_init_info();
|
||||
}
|
||||
|
||||
private:
|
||||
void check_filesize_and_remove() const
|
||||
{
|
||||
constexpr uintmax_t MaxLogSize = 4ULL * 1024 * 1024;
|
||||
@@ -110,40 +179,6 @@ namespace asst
|
||||
trace("-----------------------------");
|
||||
}
|
||||
|
||||
template <typename... Args>
|
||||
void log(std::string_view level, Args&&... args)
|
||||
{
|
||||
std::unique_lock<std::mutex> trace_lock(m_trace_mutex);
|
||||
|
||||
constexpr int buff_len = 128;
|
||||
char buff[buff_len] = { 0 };
|
||||
#ifdef _WIN32
|
||||
#ifdef _MSC_VER
|
||||
sprintf_s(buff, buff_len,
|
||||
#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(),
|
||||
::GetCurrentThreadId());
|
||||
#else // ! _WIN32
|
||||
sprintf(buff, "[%s][%s][Px%x][Tx%lx]", asst::utils::get_format_time().c_str(), level.data(), getpid(),
|
||||
(unsigned long)(std::hash<std::thread::id> {}(std::this_thread::get_id())));
|
||||
#endif // END _WIN32
|
||||
|
||||
if (!m_ofs || !m_ofs.is_open()) {
|
||||
m_ofs = std::ofstream(m_log_path, std::ios::out | std::ios::app);
|
||||
}
|
||||
#ifdef ASST_DEBUG
|
||||
stream_put_line(m_ofs, buff, args...);
|
||||
#else
|
||||
stream_put_line<false>(m_ofs, buff, std::forward<Args>(args)...);
|
||||
#endif
|
||||
|
||||
#ifdef ASST_DEBUG
|
||||
stream_put_line<true>(std::cout, buff, std::forward<Args>(args)...);
|
||||
#endif
|
||||
}
|
||||
|
||||
template <typename Stream, typename T, typename Enable = void>
|
||||
struct has_stream_insertion_operator : std::false_type
|
||||
{};
|
||||
@@ -181,7 +216,7 @@ namespace asst
|
||||
for (const auto& elem : std::forward<T>(v)) {
|
||||
s << comma;
|
||||
stream_put<ToAnsi>(s, elem);
|
||||
comma = ",";
|
||||
comma = ", ";
|
||||
}
|
||||
s << "]";
|
||||
return s;
|
||||
@@ -202,40 +237,37 @@ namespace asst
|
||||
template <bool ToAnsi, typename Stream>
|
||||
struct stream_put_line_impl<ToAnsi, Stream>
|
||||
{
|
||||
static constexpr Stream& apply(Stream& s)
|
||||
static constexpr Stream& apply(Stream& s, const separator&)
|
||||
{
|
||||
s << std::endl;
|
||||
return s;
|
||||
}
|
||||
};
|
||||
|
||||
template <bool ToAnsi, typename Stream, typename Only>
|
||||
struct stream_put_line_impl<ToAnsi, Stream, Only>
|
||||
{
|
||||
static constexpr Stream& apply(Stream& s, Only&& only)
|
||||
{
|
||||
stream_put<ToAnsi>(s, std::forward<Only>(only));
|
||||
s << std::endl;
|
||||
return s;
|
||||
}
|
||||
};
|
||||
|
||||
template <bool ToAnsi, typename Stream, typename First, typename... Rest>
|
||||
struct stream_put_line_impl<ToAnsi, Stream, First, Rest...>
|
||||
{
|
||||
static constexpr Stream& apply(Stream& s, First&& f, Rest&&... rs)
|
||||
static constexpr Stream& apply(Stream& s, [[maybe_unused]] const separator& sep, First&& f, Rest&&... rs)
|
||||
{
|
||||
stream_put<ToAnsi>(s, std::forward<First>(f));
|
||||
s << " ";
|
||||
stream_put_line_impl<ToAnsi, Stream, Rest...>::apply(s, std::forward<Rest>(rs)...);
|
||||
if constexpr (std::same_as<std::decay_t<First>, separator>) {
|
||||
stream_put_line_impl<ToAnsi, Stream, Rest...>::apply(s, std::forward<First>(f),
|
||||
std::forward<Rest>(rs)...);
|
||||
}
|
||||
else {
|
||||
s << sep.str;
|
||||
stream_put<ToAnsi>(s, std::forward<First>(f));
|
||||
stream_put_line_impl<ToAnsi, Stream, Rest...>::apply(s, sep, std::forward<Rest>(rs)...);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
};
|
||||
|
||||
template <bool ToAnsi = false, typename Stream, typename... Args>
|
||||
Stream& stream_put_line(Stream& s, Args&&... args)
|
||||
template <bool ToAnsi = false, typename Stream, typename First, typename... Args>
|
||||
Stream& stream_put_line(Stream& s, First&& a0, Args&&... args)
|
||||
{
|
||||
return stream_put_line_impl<ToAnsi, Stream, Args...>::apply(s, std::forward<Args>(args)...);
|
||||
stream_put<ToAnsi>(s, std::forward<First>(a0));
|
||||
stream_put_line_impl<ToAnsi, Stream, Args...>::apply(s, separator::space, std::forward<Args>(args)...);
|
||||
return s;
|
||||
}
|
||||
|
||||
inline static std::filesystem::path m_directory;
|
||||
@@ -246,6 +278,18 @@ namespace asst
|
||||
std::ofstream m_ofs;
|
||||
};
|
||||
|
||||
inline const Logger::separator Logger::separator::none;
|
||||
inline const Logger::separator Logger::separator::space(" ");
|
||||
inline const Logger::separator Logger::separator::tab("\t");
|
||||
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