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
33 lines
1.2 KiB
C++
33 lines
1.2 KiB
C++
#include "logger.h"
|
||
|
||
#if defined(__ANDROID__)
|
||
#include <android/log.h>
|
||
#include <cstdarg>
|
||
|
||
static void (*androidLog)(const char*) = nullptr;
|
||
|
||
void UseAndroidLog(void (*cb)(const char*)) {
|
||
androidLog = cb;
|
||
}
|
||
|
||
// 落点: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
|