diff --git a/rpc/pb_util.cpp b/rpc/pb_util.cpp index 89fcde5..1c77945 100644 --- a/rpc/pb_util.cpp +++ b/rpc/pb_util.cpp @@ -3,21 +3,6 @@ #include "pb_types.h" #include "wcf.pb.h" -#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, "Encoded message[%ld]: ", 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_INFO(buf); -} - bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void *const *arg) { const char *str = (const char *)*arg; @@ -83,7 +68,7 @@ bool encode_contacts(pb_ostream_t *stream, const pb_field_t *field, void *const vector *v = (vector *)*arg; RpcContact message = RpcContact_init_default; - LOG_INFO("encode_contacts[{}]", v->size()); + LOG_DEBUG("encode_contacts[{}]", v->size()); for (auto it = v->begin(); it != v->end(); it++) { message.wxid.funcs.encode = &encode_string; message.wxid.arg = (void *)(*it).wxid.c_str(); diff --git a/rpc/pb_util.h b/rpc/pb_util.h index 61a1abb..3c238a4 100644 --- a/rpc/pb_util.h +++ b/rpc/pb_util.h @@ -3,7 +3,6 @@ #include #include -void log_buffer(uint8_t *buffer, size_t len); bool encode_string(pb_ostream_t *stream, const pb_field_t *field, void *const *arg); bool decode_string(pb_istream_t *stream, const pb_field_t *field, void **arg); bool encode_types(pb_ostream_t *stream, const pb_field_t *field, void *const *arg); diff --git a/spy/accept_new_friend.cpp b/spy/accept_new_friend.cpp index a2031a5..4783089 100644 --- a/spy/accept_new_friend.cpp +++ b/spy/accept_new_friend.cpp @@ -34,7 +34,7 @@ int AcceptNewFriend(std::string v3, std::string v4) param.statusEnd2 = (DWORD)&status[8]; NewFriendParam_t *pParam = ¶m; - LOG_INFO("v3: {}\nv4: {}", v3, v4); + LOG_DEBUG("v3: {}\nv4: {}", v3, v4); const wchar_t *wsV3 = String2Wstring(v3).c_str(); const wchar_t *wsV4 = String2Wstring(v4).c_str(); diff --git a/spy/log.cpp b/spy/log.cpp index 3ea1d13..b48283c 100644 --- a/spy/log.cpp +++ b/spy/log.cpp @@ -7,9 +7,6 @@ void InitLogger() { -#if SPDLOG_ACTIVE_LEVEL <= SPDLOG_LEVEL_DEBUG - spdlog::set_level(spdlog::level::debug); -#endif static std::shared_ptr gLogger = nullptr; if (gLogger != nullptr) { return; @@ -20,7 +17,29 @@ void InitLogger() spdlog::set_default_logger(gLogger); gLogger->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); + gLogger->flush_on(spdlog::level::debug); +#else gLogger->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, "Encoded message[%ld]: ", 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/spy/log.h b/spy/log.h index c3e04ca..c6a2926 100644 --- a/spy/log.h +++ b/spy/log.h @@ -1,7 +1,13 @@ #pragma once #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" diff --git a/spy/rpc_server.cpp b/spy/rpc_server.cpp index c0e9546..6ddc235 100644 --- a/spy/rpc_server.cpp +++ b/spy/rpc_server.cpp @@ -229,7 +229,7 @@ static void PushMessage() return; } - LOG_INFO("Server listening on {}", url); + LOG_DEBUG("Server listening on {}", url); if ((rv = nng_setopt_ms(msg_sock, NNG_OPT_SENDTIMEO, 2000)) != 0) { LOG_ERROR("nng_setopt_ms: {}", rv); return; @@ -241,7 +241,7 @@ static void PushMessage() if (rv == WAIT_TIMEOUT) { continue; } else if (rv != WAIT_OBJECT_0) { - LOG_INFO("WaitForSingleObject ERRIR[{}]: {}", rv, GetLastError()); + LOG_ERROR("WaitForSingleObject ERROR[{}]: {}", rv, GetLastError()); continue; } @@ -383,65 +383,65 @@ static bool dispatcher(uint8_t *in, size_t in_len, uint8_t *out, size_t *out_len return false; } - LOG_INFO("Func: {}", (uint8_t)req.func); + LOG_DEBUG("Func: {}", (uint8_t)req.func); switch (req.func) { case Functions_FUNC_IS_LOGIN: { - LOG_INFO("[Functions_FUNC_IS_LOGIN]"); + LOG_DEBUG("[Functions_FUNC_IS_LOGIN]"); ret = func_is_login(out, out_len); break; } case Functions_FUNC_GET_SELF_WXID: { - LOG_INFO("[Functions_FUNC_GET_SELF_WXID]"); + LOG_DEBUG("[Functions_FUNC_GET_SELF_WXID]"); ret = func_get_self_wxid(out, out_len); break; } case Functions_FUNC_GET_MSG_TYPES: { - LOG_INFO("[Functions_FUNC_GET_MSG_TYPES]"); + LOG_DEBUG("[Functions_FUNC_GET_MSG_TYPES]"); ret = func_get_msg_types(out, out_len); break; } case Functions_FUNC_GET_CONTACTS: { - LOG_INFO("[Functions_FUNC_GET_CONTACTS]"); + LOG_DEBUG("[Functions_FUNC_GET_CONTACTS]"); ret = func_get_contacts(out, out_len); break; } case Functions_FUNC_GET_DB_NAMES: { - LOG_INFO("[Functions_FUNC_GET_DB_NAMES]"); + LOG_DEBUG("[Functions_FUNC_GET_DB_NAMES]"); ret = func_get_db_names(out, out_len); break; } case Functions_FUNC_GET_DB_TABLES: { - LOG_INFO("[Functions_FUNC_GET_DB_TABLES]"); + LOG_DEBUG("[Functions_FUNC_GET_DB_TABLES]"); ret = func_get_db_tables(req.msg.str, out, out_len); break; } case Functions_FUNC_SEND_TXT: { - LOG_INFO("[Functions_FUNC_SEND_TXT]"); + LOG_DEBUG("[Functions_FUNC_SEND_TXT]"); ret = func_send_txt(req.msg.txt, out, out_len); break; } case Functions_FUNC_SEND_IMG: { - LOG_INFO("[Functions_FUNC_SEND_IMG]"); + LOG_DEBUG("[Functions_FUNC_SEND_IMG]"); ret = func_send_img(req.msg.file.path, req.msg.file.receiver, out, out_len); break; } case Functions_FUNC_ENABLE_RECV_TXT: { - LOG_INFO("[Functions_FUNC_ENABLE_RECV_TXT]"); + LOG_DEBUG("[Functions_FUNC_ENABLE_RECV_TXT]"); ret = func_enable_recv_txt(out, out_len); break; } case Functions_FUNC_DISABLE_RECV_TXT: { - LOG_INFO("[Functions_FUNC_DISABLE_RECV_TXT]"); + LOG_DEBUG("[Functions_FUNC_DISABLE_RECV_TXT]"); ret = func_disable_recv_txt(out, out_len); break; } case Functions_FUNC_EXEC_DB_QUERY: { - LOG_INFO("[Functions_FUNC_EXEC_DB_QUERY]"); + LOG_DEBUG("[Functions_FUNC_EXEC_DB_QUERY]"); ret = func_exec_db_query(req.msg.query.db, req.msg.query.sql, out, out_len); break; } case Functions_FUNC_ACCEPT_FRIEND: { - LOG_INFO("[Functions_FUNC_ACCEPT_FRIEND]"); + LOG_DEBUG("[Functions_FUNC_ACCEPT_FRIEND]"); ret = func_accept_friend(req.msg.v.v3, req.msg.v.v4, out, out_len); break; } @@ -483,10 +483,10 @@ static int RunServer() break; } - log_buffer(in, in_len); + LOG_BUFFER(in, in_len); if (dispatcher(in, in_len, gBuffer, &out_len)) { - LOG_INFO("Send data length {}", out_len); - // log_buffer(gBuffer, out_len); + LOG_DEBUG("Send data length {}", out_len); + // LOG_BUFFER(gBuffer, out_len); rv = nng_send(sock, gBuffer, out_len, 0); if (rv != 0) { LOG_ERROR("nng_send: {}", rv); @@ -500,7 +500,7 @@ static int RunServer() } nng_free(in, in_len); } - LOG_INFO("Leave RunServer"); + LOG_DEBUG("Leave RunServer"); return rv; }