diff --git a/WeChatFerry/com/log.cpp b/WeChatFerry/com/log.cpp deleted file mode 100644 index 6524dd0..0000000 --- a/WeChatFerry/com/log.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include - -#include "log.h" -#include "util.h" - -#define LOGGER_NAME "WCF" -#define LOGGER_FILE_NAME "/logs/wcf.txt" -#define LOGGER_MAX_SIZE 1024 * 1024 * 10 // 10M -#define LOGGER_MAX_FILES 10 // 10 files - -void InitLogger(std::string path) -{ - static std::shared_ptr logger = nullptr; - if (logger != nullptr) { - return; - } - // check and create logs folder - std::filesystem::path logDir = std::filesystem::path(path) / "logs"; - if (!std::filesystem::exists(logDir)) { - std::filesystem::create_directory(logDir); - } - auto filename = std::filesystem::path(path + LOGGER_FILE_NAME).make_preferred().string(); - try { - logger = spdlog::rotating_logger_mt(LOGGER_NAME, filename, LOGGER_MAX_SIZE, LOGGER_MAX_FILES); - } catch (const spdlog::spdlog_ex &ex) { - MessageBox(NULL, String2Wstring(ex.what()).c_str(), L"Init LOGGER ERROR", 0); - } - - spdlog::set_default_logger(logger); - logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] [%n] [%s::%#::%!] %v"); -#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG - spdlog::set_level(spdlog::level::debug); - logger->flush_on(spdlog::level::debug); -#else - logger->flush_on(spdlog::level::info); -#endif - LOG_DEBUG("InitLogger with debug level"); -} - -#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG - -#define BUF_SIZE (1024 * 1024) -static char buf[BUF_SIZE] = { 0 }; - -void log_buffer(uint8_t *buffer, size_t len) -{ - size_t j = sprintf_s(buf, BUF_SIZE, "BUF@%p[%zd]: ", buffer, len); - for (size_t i = 0; i < len; i++) { - j += sprintf_s(buf + j, BUF_SIZE, "%02X ", buffer[i]); - if (j > BUF_SIZE - 3) { - break; - } - } - LOG_DEBUG(buf); -} -#endif diff --git a/WeChatFerry/com/log.h b/WeChatFerry/com/log.h deleted file mode 100644 index dbc66d5..0000000 --- a/WeChatFerry/com/log.h +++ /dev/null @@ -1,24 +0,0 @@ -#pragma once - -#include - -#ifdef ENABLE_DEBUG_LOG -#include - -#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG -void log_buffer(uint8_t *buffer, size_t len); -#define LOG_BUFFER(buf, len) log_buffer((buf), (len)) -#else -#define LOG_BUFFER(...) (void)0 -#endif - -#include "spdlog/sinks/rotating_file_sink.h" -#include "spdlog/sinks/stdout_color_sinks.h" -#include "spdlog/spdlog.h" - -#define LOG_DEBUG(...) SPDLOG_DEBUG(__VA_ARGS__); -#define LOG_INFO(...) SPDLOG_INFO(__VA_ARGS__); -#define LOG_WARN(...) SPDLOG_WARN(__VA_ARGS__); -#define LOG_ERROR(...) SPDLOG_ERROR(__VA_ARGS__); - -void InitLogger(std::string path); diff --git a/WeChatFerry/com/log.hpp b/WeChatFerry/com/log.hpp new file mode 100644 index 0000000..4f30b27 --- /dev/null +++ b/WeChatFerry/com/log.hpp @@ -0,0 +1,87 @@ +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "util.h" + +#define LOG_DEBUG(...) SPDLOG_DEBUG(__VA_ARGS__) +#define LOG_INFO(...) SPDLOG_INFO(__VA_ARGS__) +#define LOG_WARN(...) SPDLOG_WARN(__VA_ARGS__) +#define LOG_ERROR(...) SPDLOG_ERROR(__VA_ARGS__) + +#ifdef ENABLE_DEBUG_LOG +#define LOG_BUFFER(buf, len) Log::log_buffer((buf), (len)) +#else +#define LOG_BUFFER(...) (void)0 +#endif + +namespace Log +{ +inline constexpr char DEFAULT_LOGGER_NAME[] = "WCF"; +inline constexpr char DEFAULT_LOGGER_FILE[] = "logs/wcf.txt"; +inline constexpr size_t DEFAULT_LOGGER_MAX_SIZE = 1024 * 1024 * 10; // 10MB +inline constexpr size_t DEFAULT_LOGGER_MAX_FILES = 10; + +inline void InitLogger(const std::string &path) +{ + static std::shared_ptr logger = nullptr; + + if (logger != nullptr) { + return; // 已初始化 + } + + std::filesystem::path filename = std::filesystem::path(path) / DEFAULT_LOGGER_FILE; + std::filesystem::path logDir = filename.parent_path(); + + if (!std::filesystem::exists(logDir)) { + std::filesystem::create_directories(logDir); + } + + try { + logger = spdlog::rotating_logger_mt(DEFAULT_LOGGER_NAME, filename.string(), DEFAULT_LOGGER_MAX_SIZE, + DEFAULT_LOGGER_MAX_FILES); + } catch (const spdlog::spdlog_ex &ex) { + MessageBox(NULL, String2Wstring(ex.what()).c_str(), L"Init LOGGER ERROR", MB_ICONERROR); + return; + } + + spdlog::set_default_logger(logger); + logger->set_pattern("[%Y-%m-%d %H:%M:%S.%e] [%l] [%n] [%s::%#::%!] %v"); + +#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG + spdlog::set_level(spdlog::level::debug); + logger->flush_on(spdlog::level::debug); +#else + spdlog::set_level(spdlog::level::info); + logger->flush_on(spdlog::level::info); +#endif + + SPDLOG_DEBUG("Logger initialized with default settings."); +} + +#ifdef ENABLE_DEBUG_LOG +inline void log_buffer(uint8_t *buffer, size_t len) +{ + constexpr size_t BUF_SIZE = 1024 * 1024; + std::ostringstream oss; + + oss << "BUF@" << static_cast(buffer) << "[" << len << "]: "; + for (size_t i = 0; i < len; ++i) { + oss << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast(buffer[i]) << " "; + if (oss.tellp() > BUF_SIZE - 3) { + break; // 防止缓冲区溢出 + } + } + + SPDLOG_DEBUG(oss.str()); +} +#endif + +} // namespace Log diff --git a/WeChatFerry/com/util.cpp b/WeChatFerry/com/util.cpp index 5e906e6..0134592 100644 --- a/WeChatFerry/com/util.cpp +++ b/WeChatFerry/com/util.cpp @@ -8,7 +8,7 @@ #include #include -#include "log.h" +#include "log.hpp" #include "util.h" #pragma comment(lib, "shlwapi") diff --git a/WeChatFerry/rpc/pb_util.cpp b/WeChatFerry/rpc/pb_util.cpp index fa13d4b..5f8c4bd 100644 --- a/WeChatFerry/rpc/pb_util.cpp +++ b/WeChatFerry/rpc/pb_util.cpp @@ -1,5 +1,5 @@ #include "pb_util.h" -#include "log.h" +#include "log.hpp" #include "pb_types.h" #include "wcf.pb.h" diff --git a/WeChatFerry/spy/Spy.vcxproj b/WeChatFerry/spy/Spy.vcxproj index f7ae057..6a66d2d 100644 --- a/WeChatFerry/spy/Spy.vcxproj +++ b/WeChatFerry/spy/Spy.vcxproj @@ -237,7 +237,7 @@ xcopy /y $(SolutionDir)DISCLAIMER.md $(SolutionDir)..\clients\python\wcferry - + @@ -262,7 +262,6 @@ xcopy /y $(SolutionDir)DISCLAIMER.md $(SolutionDir)..\clients\python\wcferry - diff --git a/WeChatFerry/spy/Spy.vcxproj.filters b/WeChatFerry/spy/Spy.vcxproj.filters index f2b9096..3bf11bb 100644 --- a/WeChatFerry/spy/Spy.vcxproj.filters +++ b/WeChatFerry/spy/Spy.vcxproj.filters @@ -81,10 +81,10 @@ 头文件 - + 头文件 - + 头文件 @@ -134,9 +134,6 @@ 源文件 - - 源文件 - 源文件 diff --git a/WeChatFerry/spy/chatroom_mgmt.cpp b/WeChatFerry/spy/chatroom_mgmt.cpp index 41d1da5..0950d45 100644 --- a/WeChatFerry/spy/chatroom_mgmt.cpp +++ b/WeChatFerry/spy/chatroom_mgmt.cpp @@ -3,7 +3,7 @@ #include #include "chatroom_mgmt.h" -#include "log.h" +#include "log.hpp" #include "util.h" using namespace std; diff --git a/WeChatFerry/spy/contact_mgmt.cpp b/WeChatFerry/spy/contact_mgmt.cpp index f81d1c1..84f916a 100644 --- a/WeChatFerry/spy/contact_mgmt.cpp +++ b/WeChatFerry/spy/contact_mgmt.cpp @@ -1,7 +1,7 @@ #pragma execution_character_set("utf-8") #include "contact_mgmt.h" -#include "log.h" +#include "log.hpp" #include "util.h" using namespace std; diff --git a/WeChatFerry/spy/exec_sql.cpp b/WeChatFerry/spy/exec_sql.cpp index c3f114f..834c856 100644 --- a/WeChatFerry/spy/exec_sql.cpp +++ b/WeChatFerry/spy/exec_sql.cpp @@ -1,7 +1,7 @@ #include #include "exec_sql.h" -#include "log.h" +#include "log.hpp" #include "sqlite3.h" #include "util.h" diff --git a/WeChatFerry/spy/funcs.cpp b/WeChatFerry/spy/funcs.cpp index 2d72f5a..c12f9b5 100644 --- a/WeChatFerry/spy/funcs.cpp +++ b/WeChatFerry/spy/funcs.cpp @@ -7,7 +7,7 @@ #include "codec.h" #include "exec_sql.h" #include "funcs.h" -#include "log.h" +#include "log.hpp" #include "spy_types.h" #include "util.h" diff --git a/WeChatFerry/spy/receive_msg.cpp b/WeChatFerry/spy/receive_msg.cpp index 997102a..e283ce0 100644 --- a/WeChatFerry/spy/receive_msg.cpp +++ b/WeChatFerry/spy/receive_msg.cpp @@ -6,7 +6,7 @@ #include #include -#include "log.h" +#include "log.hpp" #include "receive_msg.h" #include "user_info.h" #include "util.h" diff --git a/WeChatFerry/spy/rpc_server.cpp b/WeChatFerry/spy/rpc_server.cpp index d033730..1d09e30 100644 --- a/WeChatFerry/spy/rpc_server.cpp +++ b/WeChatFerry/spy/rpc_server.cpp @@ -22,7 +22,7 @@ #include "contact_mgmt.h" #include "exec_sql.h" #include "funcs.h" -#include "log.h" +#include "log.hpp" #include "pb_types.h" #include "pb_util.h" #include "receive_msg.h" diff --git a/WeChatFerry/spy/send_msg.cpp b/WeChatFerry/spy/send_msg.cpp index 2b9c6ba..dca37f7 100644 --- a/WeChatFerry/spy/send_msg.cpp +++ b/WeChatFerry/spy/send_msg.cpp @@ -4,7 +4,7 @@ #include #include "exec_sql.h" -#include "log.h" +#include "log.hpp" #include "send_msg.h" #include "spy_types.h" #include "util.h" diff --git a/WeChatFerry/spy/spy.cpp b/WeChatFerry/spy/spy.cpp index 576276c..e179aa0 100644 --- a/WeChatFerry/spy/spy.cpp +++ b/WeChatFerry/spy/spy.cpp @@ -1,6 +1,6 @@ #include -#include "log.h" +#include "log.hpp" #include "rpc_server.h" #include "spy.h" #include "util.h" @@ -21,7 +21,7 @@ void InitSpy(LPVOID args) wchar_t version[16] = { 0 }; PortPath_t *pp = (PortPath_t *)args; - InitLogger(pp->path); + Log::InitLogger(pp->path); g_WeChatWinDllAddr = (UINT64)GetModuleHandle(L"WeChatWin.dll"); // 获取wechatWin模块地址 if (g_WeChatWinDllAddr == 0) { LOG_ERROR("获取 wechatWin.dll 模块地址失败"); diff --git a/WeChatFerry/spy/user_info.cpp b/WeChatFerry/spy/user_info.cpp index f7f2b53..a273592 100644 --- a/WeChatFerry/spy/user_info.cpp +++ b/WeChatFerry/spy/user_info.cpp @@ -1,5 +1,5 @@ #include "user_info.h" -#include "log.h" +#include "log.hpp" #include "util.h" extern UINT64 g_WeChatWinDllAddr;