Refactoring

This commit is contained in:
Changhua 2025-01-27 12:11:41 +08:00
parent a273d6f11a
commit efc4688873
16 changed files with 102 additions and 99 deletions

View File

@ -1,56 +0,0 @@
#include <filesystem>
#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<spdlog::logger> 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

View File

@ -1,24 +0,0 @@
#pragma once
#include <string>
#ifdef ENABLE_DEBUG_LOG
#include <stdint.h>
#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);

87
WeChatFerry/com/log.hpp Normal file
View File

@ -0,0 +1,87 @@
#pragma once
#include <filesystem>
#include <iomanip>
#include <memory>
#include <spdlog/sinks/rotating_file_sink.h>
#include <spdlog/sinks/stdout_color_sinks.h>
#include <spdlog/spdlog.h>
#include <sstream>
#include <string>
#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<spdlog::logger> 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<void *>(buffer) << "[" << len << "]: ";
for (size_t i = 0; i < len; ++i) {
oss << std::hex << std::uppercase << std::setw(2) << std::setfill('0') << static_cast<int>(buffer[i]) << " ";
if (oss.tellp() > BUF_SIZE - 3) {
break; // 防止缓冲区溢出
}
}
SPDLOG_DEBUG(oss.str());
}
#endif
} // namespace Log

View File

@ -8,7 +8,7 @@
#include <vector>
#include <wchar.h>
#include "log.h"
#include "log.hpp"
#include "util.h"
#pragma comment(lib, "shlwapi")

View File

@ -1,5 +1,5 @@
#include "pb_util.h"
#include "log.h"
#include "log.hpp"
#include "pb_types.h"
#include "wcf.pb.h"

View File

@ -237,7 +237,7 @@ xcopy /y $(SolutionDir)DISCLAIMER.md $(SolutionDir)..\clients\python\wcferry</Co
</PostBuildEvent>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="..\com\log.h" />
<ClInclude Include="..\com\log.hpp" />
<ClInclude Include="..\com\util.h" />
<ClInclude Include="..\rpc\nanopb\pb.h" />
<ClInclude Include="..\rpc\nanopb\pb_common.h" />
@ -262,7 +262,6 @@ xcopy /y $(SolutionDir)DISCLAIMER.md $(SolutionDir)..\clients\python\wcferry</Co
<ClInclude Include="user_info.h" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="..\com\log.cpp" />
<ClCompile Include="..\com\util.cpp" />
<ClCompile Include="..\rpc\nanopb\pb_common.c" />
<ClCompile Include="..\rpc\nanopb\pb_decode.c" />

View File

@ -81,10 +81,10 @@
<ClInclude Include="..\smc\codec.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="..\com\log.h">
<ClInclude Include="..\com\util.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="..\com\util.h">
<ClInclude Include="..\com\log.hpp">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
@ -134,9 +134,6 @@
<ClCompile Include="funcs.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="..\com\log.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="..\com\util.cpp">
<Filter>源文件</Filter>
</ClCompile>

View File

@ -3,7 +3,7 @@
#include <vector>
#include "chatroom_mgmt.h"
#include "log.h"
#include "log.hpp"
#include "util.h"
using namespace std;

View File

@ -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;

View File

@ -1,7 +1,7 @@
#include <iterator>
#include "exec_sql.h"
#include "log.h"
#include "log.hpp"
#include "sqlite3.h"
#include "util.h"

View File

@ -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"

View File

@ -6,7 +6,7 @@
#include <mutex>
#include <queue>
#include "log.h"
#include "log.hpp"
#include "receive_msg.h"
#include "user_info.h"
#include "util.h"

View File

@ -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"

View File

@ -4,7 +4,7 @@
#include <vector>
#include "exec_sql.h"
#include "log.h"
#include "log.hpp"
#include "send_msg.h"
#include "spy_types.h"
#include "util.h"

View File

@ -1,6 +1,6 @@
#include <filesystem>
#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 模块地址失败");

View File

@ -1,5 +1,5 @@
#include "user_info.h"
#include "log.h"
#include "log.hpp"
#include "util.h"
extern UINT64 g_WeChatWinDllAddr;