diff --git a/android/app/src/main/cpp/ScreenHandler.h b/android/app/src/main/cpp/ScreenHandler.h index 5d26dfa..a7b7c5f 100644 --- a/android/app/src/main/cpp/ScreenHandler.h +++ b/android/app/src/main/cpp/ScreenHandler.h @@ -2,6 +2,7 @@ // Android 屏幕处理器:管理截屏子连接,接收 Java 侧 MediaCodec 输出的 H.264 NALU, // 按协议封装后通过子连接发送给服务端。 #include "common/commands.h" +#include "common/logger.h" #include "client/IOCPClient.h" #include #include @@ -13,8 +14,10 @@ #include #include -#define LOGI_SH(...) __android_log_print(ANDROID_LOG_INFO, "YAMA_SCR", __VA_ARGS__) -#define LOGE_SH(...) __android_log_print(ANDROID_LOG_ERROR, "YAMA_SCR", __VA_ARGS__) +// 与 main.cpp 的 LOGI/LOGE 同理:进 logcat 的同时进 Logger 内存 ring(MCP get_client_log)。 +// 抓屏链路的问题(黑屏、码率)只能靠这些日志定位,必须能被主控取到。 +#define LOGI_SH(...) android_log_tag(ANDROID_LOG_INFO, "YAMA_SCR", __VA_ARGS__) +#define LOGE_SH(...) android_log_tag(ANDROID_LOG_ERROR, "YAMA_SCR", __VA_ARGS__) extern uint64_t g_myClientID; // 定义在 main.cpp,供子连接的 OnReceive 调用 diff --git a/android/app/src/main/cpp/main.cpp b/android/app/src/main/cpp/main.cpp index 100970d..92d54a4 100644 --- a/android/app/src/main/cpp/main.cpp +++ b/android/app/src/main/cpp/main.cpp @@ -27,10 +27,13 @@ #define XXH_INLINE_ALL #include "common/xxhash.h" #include "common/logger.h" +#include "common/ClientLogHandler.h" #define LOG_TAG "YAMA" -#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) -#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) +// LOGI/LOGE 走 android_log_tag(logger.h):既进 logcat,也进 Logger 的内存 ring, +// 后者是 MCP get_client_log 的数据源。若只写 logcat,主控会拿到一份空日志。 +#define LOGI(...) android_log_tag(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__) +#define LOGE(...) android_log_tag(ANDROID_LOG_ERROR, LOG_TAG, __VA_ARGS__) // 1 = 状态提示显示到电视屏幕;0 = 只写 logcat,不显示到屏幕 #define SCREEN_STATUS_ENABLED 0 @@ -474,6 +477,44 @@ static void ScreenSpyThread() LOGI("ScreenSpyThread exit"); } +// ------------------------------------------------------------------ 运行日志上报 +// 对应 Windows 的 CClientLogManager / Linux 的 ClientLogThread。 +// 服务端 COMMAND_QUERY_LOG → 开子连接 → 首包 TOKEN_REPORT_LOG + Logger 内存 ring 全量。 +// 必须走子连接:服务端 TOKEN_REPORT_LOG 分支取走首包后会 CancelIO,若回在主连接上, +// 被关掉的就是主连接本身(客户端当场掉线)。 +static void ClientLogThread() +{ + LOGI("ClientLogThread start → %s:%d", g_serverIp.c_str(), g_serverPort); + for (int attempt = 1; attempt <= 3 && S_CLIENT_NORMAL == g_bExit && g_running.load(); ++attempt) { + std::unique_ptr client; + std::unique_ptr handler; + try { + client = std::make_unique(g_bExit, true); + client->EnableSubConnAuth(true, g_myClientID); + if (!client->ConnectServer(g_serverIp.c_str(), g_serverPort)) { + LOGE("ClientLogThread: connect failed (attempt %d/3)", attempt); + Sleep(1000); + continue; + } + // ctor 里就把 ring 全量发出去了;服务端取走首包即关闭子连接 + handler = std::make_unique(client.get()); + client->setManagerCallBack(handler.get(), + IOCPManager::DataProcess, + IOCPManager::ReconnectProcess); + while (client->IsRunning() && client->IsConnected() && S_CLIENT_NORMAL == g_bExit) + Sleep(200); + client->setManagerCallBack(nullptr, nullptr, nullptr); + LOGI("ClientLogThread: sub-conn closed by server (attempt %d)", attempt); + break; // 服务端取走日志即主动断开,属正常结束 + } catch (const std::exception& e) { + LOGE("ClientLogThread exception (attempt %d): %s", attempt, e.what()); + if (client) client->setManagerCallBack(nullptr, nullptr, nullptr); + Sleep(1000); + } + } + LOGI("ClientLogThread exit"); +} + // ------------------------------------------------------------------ 在线升级 // 载荷布局:[COMMAND_UPDATE:1][APK 大小:8 LE][APK:size][xxh64:8 LE] // 校验通过后写到 filesDir/update.apk,交给 Kotlin 的 PackageInstaller 会话去装 @@ -705,6 +746,12 @@ int DataProcess(void* /*user*/, PBYTE szBuffer, ULONG ulLength) break; } + case COMMAND_QUERY_LOG: + // MCP get_client_log / 主控「运行日志」窗口:一次子连接取 ring 全量快照 + LOGI("cmd: COMMAND_QUERY_LOG → sub-conn"); + std::thread(ClientLogThread).detach(); + break; + case COMMAND_SHELL: LOGI("COMMAND_SHELL (not implemented)"); break; diff --git a/common/logger.cpp b/common/logger.cpp index 1f90926..7aeee23 100644 --- a/common/logger.cpp +++ b/common/logger.cpp @@ -1,17 +1,32 @@ #include "logger.h" #if defined(__ANDROID__) +#include +#include + static void (*androidLog)(const char*) = nullptr; void UseAndroidLog(void (*cb)(const char*)) { androidLog = cb; } -__attribute__((format(printf, 1, 2))) -void android_log(const char* fmt, ...) { - char buf[256]; +// 落点:logcat + Logger 内存 ring buffer(Android 不写文件,见 logger.h 的 Android +// 初始化分支)。ring 是 MCP get_client_log(TOKEN_REPORT_LOG)的数据源,所以平台 +// 日志都必须经过这里,否则 logcat 里看得到、MCP 却取不到。 +__attribute__((format(printf, 3, 4))) +void android_log(const char* file, int line, const char* fmt, ...) { + char buf[1024]; va_list ap; va_start(ap, fmt); vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); __android_log_print(ANDROID_LOG_DEBUG, "YAMA_NET", "%s", buf); + Logger::getInstance().log(file, line, "%s", buf); if (androidLog) androidLog(buf); } + +__attribute__((format(printf, 3, 4))) +void android_log_tag(int prio, const char* tag, const char* fmt, ...) { + char buf[1024]; + va_list ap; va_start(ap, fmt); vsnprintf(buf, sizeof(buf), fmt, ap); va_end(ap); + __android_log_print(prio, tag, "%s", buf); + Logger::getInstance().log(nullptr, 0, "%s", buf); +} #endif diff --git a/common/logger.h b/common/logger.h index 527cb66..ffc2e81 100644 --- a/common/logger.h +++ b/common/logger.h @@ -69,6 +69,14 @@ public: instance.enable = stringToBool(var); instance.log("logger.h", __LINE__, "GetEnvironmentVariable: %s=%s\n", name, var); #endif +#elif defined(__ANDROID__) + // Android:App 沙箱里没有 native 稳定可写的日志目录(可写的 filesDir 归 Java 侧 + // 管),故不建目录、不写文件;只保留内存 ring buffer,供 MCP 的 get_client_log + // 经 TOKEN_REPORT_LOG 取回(logger.cpp 的 android_log/android_log_tag 往这里灌)。 + char buf[16] = {}; + snprintf(buf, sizeof(buf), "%d", (int)getpid()); + instance.pid = buf; + instance.enable = false; #else // Linux char buf[16] = {}; snprintf(buf, sizeof(buf), "%d", (int)getpid()); @@ -391,8 +399,18 @@ inline const char* getFileName(const char* path) #undef Mprintf #endif void UseAndroidLog(void (*cb)(const char*)); -void android_log(const char* fmt, ...); -#define Mprintf android_log + +// Android 的 Mprintf:输出到 logcat,同时写入 Logger 的内存 ring buffer。 +// 带源文件/行号(与 Windows/Linux 的 Mprintf 对齐),MCP 的 get_client_log 才能从 +// ring 里取回可定位的日志。 +void android_log(const char* file, int line, const char* fmt, ...) + __attribute__((format(printf, 3, 4))); +#define Mprintf(format, ...) android_log(getFileName(__FILE__), __LINE__, format, ##__VA_ARGS__) + +// 平台侧宏(main.cpp 的 LOGI/LOGE、ScreenHandler.h 的 LOGI_SH/LOGE_SH)用:自定义 +// logcat 优先级与 tag,同样写入 ring,但不带源文件(这些宏只有 tag 没有行号)。 +void android_log_tag(int prio, const char* tag, const char* fmt, ...) + __attribute__((format(printf, 3, 4))); #else // Linux: 覆盖 commands.h 中的 printf 回退定义,改用 Logger 写文件 #ifdef Mprintf diff --git a/server/2015Remote/McpServer.cpp b/server/2015Remote/McpServer.cpp index 8274eae..51f362f 100644 --- a/server/2015Remote/McpServer.cpp +++ b/server/2015Remote/McpServer.cpp @@ -343,8 +343,8 @@ const char* kServerInstructions = u8"exec_command、terminal_open / terminal_exec / terminal_close" u8"(客户端有 PTY 实现,但服务端本轮未开放)、kill_process;" u8"文件传输、进程列表、日志可用。\n" - u8"apk(APK):只支持主机列表(list_online_hosts / list_all_hosts)/ 详情 / 搜索 / " - u8"审计日志 / 服务状态 / 会话管理,以及 " + u8"apk(APK):支持主机列表(list_online_hosts / list_all_hosts)/ 详情 / 搜索 / " + u8"审计日志 / 服务状态 / 会话管理、运行日志(get_client_log),以及 " u8"host_set_remark / host_set_watch_level 这两个只改服务端本地数据的工具。其余一律不支持" u8"(客户端未实现 SYSTEM 与 SHELL)。\n" u8"copy_files_c2c 是唯一有方向性的工具:源端 src_id 必须是 win,目标端 dst_id 可以是 " @@ -4173,9 +4173,10 @@ std::string BuildGetClientLog(const Json::Value& id, const Json::Value& args, CM // data[0]=token,其后为 Logger 内存 ring buffer 的日志文本(无 '\0' 终止)。 // 客户端在子连接建立时 m_sentIdx=0 返回全量,随后每 3s 推增量;MessageHandle 在 // 取走首条全量后立即 CancelIO 关子链接,增量不再到达,故此处即为完整快照。 - // 日志文本为客户端 ANSI(Windows 走 vsnprintf A 版),按 clientType 判定编码。 + // 日志文本为客户端 ANSI(Windows 走 vsnprintf A 版),按 clientType 判定编码: + // POSIX 客户端(LNX/MAC/APK)一律 UTF-8,Windows 是 GBK。 CString clientType = ctx->GetAdditionalData(RES_CLIENT_TYPE); - UINT cp = (clientType == "LNX" || clientType == "MAC") ? CP_UTF8 : 936; + UINT cp = (clientType == "LNX" || clientType == "MAC" || clientType == "APK") ? CP_UTF8 : 936; std::string log; if (data.size() > 1) { @@ -9935,7 +9936,8 @@ const ToolPlatform kToolPlatforms[] = { // ---- win+lnx+mac ---- SYSTEM / LIST_DRIVE / QUERY_LOG / V2 文件传输 { "list_processes", kPlatWin|kPlatLnx|kPlatMac }, { "list_files", kPlatWin|kPlatLnx|kPlatMac }, - { "get_client_log", kPlatWin|kPlatLnx|kPlatMac }, + // Android 客户端同样实现了 COMMAND_QUERY_LOG → TOKEN_REPORT_LOG 子连接上报 + { "get_client_log", kPlatWin|kPlatLnx|kPlatMac|kPlatApk }, { "download_file", kPlatWin|kPlatLnx|kPlatMac }, { "upload_file", kPlatWin|kPlatLnx|kPlatMac }, // ---- win+lnx ---- 远程控制全族 + 需 SCREEN_PREVIEW 的截屏 + macOS 未开放项