Feature: Report client run logs from the Android client

Before this change the Android client's run log went to logcat only:
Logger's init tried to create a log directory, enable was true, and the
in-memory ring that MCP's get_client_log reads was never fed, so the
tool returned an empty log for APK hosts.

Logger now has an Android branch that skips the directory and file writer
and keeps only the ring, which is always written regardless of enable.
Mprintf carries the source file and line as on Windows/Linux, and both
android_log (Mprintf) and a new android_log_tag (platform LOGI/LOGE,
LOGI_SH/LOGE_SH) print to logcat and append to the ring.

A new ClientLogThread opens a sub-connection on COMMAND_QUERY_LOG and
lets ClientLogHandler send TOKEN_REPORT_LOG plus the full ring as the
first packet. It has to be a sub-connection: the server takes the first
packet and calls CancelIO, which would drop the main connection. The
server side allows get_client_log on kPlatApk and decodes the payload as
UTF-8 like the other POSIX clients.

Verified on the emulator against a local debug server: get_client_log
returned 21 UTF-8 lines including [file:line] Mprintf output, LOGI
lines, and the ClientLogThread handshake.

Co-Authored-By: deepseek-flash
This commit is contained in:
yuanyuanxianganddeepseek-flash committed 2026-11-11 12:57:12 +01:00
1 parent 51747dc0ea
commit d365a4f948
5 files changed
+99 -14

No files matched your search

+49 -2
View File
@@ -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<IOCPClient> client;
std::unique_ptr<ClientLogHandler> handler;
try {
client = std::make_unique<IOCPClient>(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<ClientLogHandler>(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;