Feat(client): report macOS runtime logs to the controller
macOS already handled COMMAND_QUERY_LOG and opened the sub-connection that sends TOKEN_REPORT_LOG, but the snapshot was always empty: macOS Mprintf went to NSLog/fprintf(stderr) and never through Logger, so the ring it dumps stayed empty and both the 运行日志 dialog and MCP get_client_log got nothing. macOS now writes Logger like Linux does, with enable=false so no second log file appears under ~/.config/ghost, and Logger mirrors each line to stderr, which main.mm already redirects to /tmp/ghost.log in daemon and App bundle mode. Prefix and timestamp match on both sides, so dialog and file can be compared line for line. main.mm logged its own lifecycle through NSLog, which never reaches the ring, so the lines people open the dialog for were missing; those calls now use Mprintf. The ObjC helpers (ScreenHandler/H264Encoder/InputHandler/Permissions) keep NSLog and stay file-only. POSIX reports one snapshot per connection, so reopening 运行日志 takes a fresh one; Windows keeps pushing every 3s. Verified with a WSL probe built with -D__APPLE__ (ring, incremental dump, stderr mirror), a MinGW probe for the _WIN32 branch, and a green Linux client rebuild. Not built or run on a real Mac. Co-Authored-By: deepseek-flash
This commit is contained in:
1 parent
b743de6747
commit
a25e89b220
4 files changed
+102
-38
No files matched your search
+30
-11
@@ -17,7 +17,8 @@
|
||||
#endif
|
||||
|
||||
#include <iostream>
|
||||
#include <cstdio> // fprintf:macOS 非 ObjC 翻译单元的 Mprintf 走 stderr
|
||||
#include <cstdio> // fputs/fflush/stderr:macOS 的 mirrorStderr 把每行日志同步写 stderr
|
||||
#include <cstring> // strrchr:getFileName 不依赖调用方先包含 <string.h>
|
||||
#include <fstream>
|
||||
#include <mutex>
|
||||
#include <queue>
|
||||
@@ -78,6 +79,20 @@ public:
|
||||
snprintf(buf, sizeof(buf), "%d", (int)getpid());
|
||||
instance.pid = buf;
|
||||
instance.enable = false;
|
||||
#elif defined(__APPLE__)
|
||||
// macOS:只保留内存 ring + stderr,不另建日志文件。
|
||||
// - ring 是服务端「运行日志」菜单的数据源:COMMAND_QUERY_LOG →
|
||||
// TOKEN_REPORT_LOG,见 common/ClientLogHandler.h;
|
||||
// - stderr 已被 macos/main.mm 重定向到 /tmp/ghost.log(daemon 与 App
|
||||
// bundle 两种启动方式都做),那是现场唯一日志文件,再往
|
||||
// ~/.config/ghost 写一份只会让两边对不上。
|
||||
// 此前 macOS 的 Mprintf 是 NSLog/fprintf,从不经过 Logger,ring 永远是
|
||||
// 空的 —— 服务端点「运行日志」打开后一个字都没有,就是这个原因。
|
||||
char buf[16] = {};
|
||||
snprintf(buf, sizeof(buf), "%d", (int)getpid());
|
||||
instance.pid = buf;
|
||||
instance.enable = false;
|
||||
instance.mirrorStderr = true;
|
||||
#else // Linux
|
||||
char buf[16] = {};
|
||||
snprintf(buf, sizeof(buf), "%d", (int)getpid());
|
||||
@@ -167,6 +182,14 @@ public:
|
||||
logEntry.pop_back();
|
||||
logEntry += '\n';
|
||||
|
||||
// macOS:把同一行镜像到 stderr,也就是 /tmp/ghost.log。ring 只留最近
|
||||
// kMemLogMax 条,而现场排查往往要往前翻,两边保持完全相同的文本(含前缀和
|
||||
// 时间戳),对照时不必换算。
|
||||
if (mirrorStderr) {
|
||||
fputs(logEntry.c_str(), stderr);
|
||||
fflush(stderr);
|
||||
}
|
||||
|
||||
// Always record to in-memory ring buffer regardless of enable flag.
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(m_memMutex);
|
||||
@@ -263,6 +286,7 @@ private:
|
||||
}
|
||||
std::string logFileName; // 日志文件名
|
||||
bool enable; // 是否启用
|
||||
bool mirrorStderr = false; // macOS:每行同时镜像到 stderr(/tmp/ghost.log)
|
||||
bool threadRun; // 日志线程状态
|
||||
std::queue<std::string> logQueue; // 日志队列
|
||||
std::mutex queueMutex; // 队列互斥锁
|
||||
@@ -384,19 +408,14 @@ inline const char* getFileName(const char* path)
|
||||
#elif defined(_WIN32)
|
||||
#define Mprintf(format, ...) Logger::getInstance().log(getFileName((__FILE__)), __LINE__, (format), __VA_ARGS__)
|
||||
#elif defined(__APPLE__)
|
||||
// macOS: 使用 NSLog 输出到系统日志(可通过 Console.app 查看)
|
||||
// macOS:与 Linux 一样写 Logger —— 内存 ring 供服务端「运行日志」菜单取回
|
||||
// (COMMAND_QUERY_LOG → TOKEN_REPORT_LOG),Logger 初始化时置 mirrorStderr,
|
||||
// 同一行再落到 stderr,也就是 /tmp/ghost.log。不要改回 NSLog/fprintf:那样
|
||||
// ring 永远为空,菜单打开后没有任何内容。
|
||||
#ifdef Mprintf
|
||||
#undef Mprintf
|
||||
#endif
|
||||
#ifdef __OBJC__
|
||||
#define Mprintf(format, ...) NSLog(@"%@", [NSString stringWithFormat:@(format), ##__VA_ARGS__])
|
||||
#else
|
||||
// 非 ObjC 翻译单元(IOCPClient/FileManager/TlsChannel 等)没有 NSLog,只能写流。
|
||||
// 必须写 stderr:macos/main.mm 把 stderr 重定向到 /tmp/ghost.log(daemon 与 App bundle
|
||||
// 两种启动方式都做,App bundle 还设了 CFLOG_FORCE_STDERR 让 NSLog 也落到同一文件);
|
||||
// stdout 在 App bundle / login item 启动时没人接收,写过去的日志会直接丢掉。
|
||||
#define Mprintf(format, ...) fprintf(stderr, format, ##__VA_ARGS__)
|
||||
#endif
|
||||
#define Mprintf(format, ...) Logger::getInstance().log(getFileName(__FILE__), __LINE__, format, ##__VA_ARGS__)
|
||||
#elif defined(__ANDROID__)
|
||||
#include <android/log.h>
|
||||
#include <cstdarg>
|
||||
|
||||
Reference in new issue
Block a user