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
460 lines
15 KiB
C++
460 lines
15 KiB
C++
#pragma once
|
||
#ifdef _MSC_VER
|
||
#pragma warning(disable: 4996)
|
||
#endif
|
||
|
||
#ifdef _WIN32
|
||
#ifdef _WINDOWS
|
||
#include <afxwin.h>
|
||
#else
|
||
#include <windows.h>
|
||
#endif
|
||
#include "skCrypter.h"
|
||
#else
|
||
#include <unistd.h>
|
||
#include <sys/types.h>
|
||
#include <sys/stat.h>
|
||
#endif
|
||
|
||
#include <iostream>
|
||
#include <cstdio> // fputs/fflush/stderr:macOS 的 mirrorStderr 把每行日志同步写 stderr
|
||
#include <cstring> // strrchr:getFileName 不依赖调用方先包含 <string.h>
|
||
#include <fstream>
|
||
#include <mutex>
|
||
#include <queue>
|
||
#include <string>
|
||
#include <thread>
|
||
#include <condition_variable>
|
||
#include <atomic>
|
||
#include <chrono>
|
||
#include <sstream>
|
||
#include <cstdarg>
|
||
#include <iomanip>
|
||
#include <algorithm>
|
||
#include <deque>
|
||
|
||
|
||
inline bool stringToBool(const std::string& str)
|
||
{
|
||
std::string lower = str;
|
||
std::transform(lower.begin(), lower.end(), lower.begin(), ::tolower);
|
||
return (lower == "true" || lower == "1");
|
||
}
|
||
|
||
class Logger
|
||
{
|
||
public:
|
||
enum LogLevel {
|
||
InfoLevel, WarningLevel, ErrorLevel
|
||
};
|
||
|
||
// 单例模式
|
||
static Logger& getInstance()
|
||
{
|
||
static Logger instance;
|
||
if (instance.pid.empty()) {
|
||
#ifdef _WIN32
|
||
char buf[16] = {};
|
||
sprintf_s(buf, "%d", GetCurrentProcessId());
|
||
instance.pid = buf;
|
||
// SYSTEM | C:\Windows\Temp
|
||
// 普通用户 | C:\Users\用户名\AppData\Local\Temp
|
||
char logPath[MAX_PATH] = { 0 };
|
||
GetEnvironmentVariableA("TEMP", logPath, MAX_PATH);
|
||
instance.InitLogFile(logPath, instance.pid);
|
||
#ifdef _WINDOWS
|
||
instance.enable = true; // 主控日志默认打开
|
||
#else
|
||
char var[32] = {};
|
||
const char* name = skCrypt("ENABLE_LOG");
|
||
DWORD size = GetEnvironmentVariableA(name, var, sizeof(var));
|
||
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;
|
||
#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());
|
||
instance.pid = buf;
|
||
// ~/.config/ghost/ 目录写日志
|
||
std::string logDir;
|
||
const char* xdg = getenv("XDG_CONFIG_HOME");
|
||
if (xdg && xdg[0]) {
|
||
logDir = std::string(xdg) + "/ghost";
|
||
} else {
|
||
const char* home = getenv("HOME");
|
||
if (home && home[0]) {
|
||
logDir = std::string(home) + "/.config/ghost";
|
||
} else {
|
||
logDir = "/tmp";
|
||
}
|
||
}
|
||
mkdir(logDir.c_str(), 0755); // 确保日志目录存在
|
||
instance.InitLogFile(logDir, instance.pid);
|
||
// daemon 模式默认打开日志,可通过 ENABLE_LOG=0 关闭
|
||
const char* envLog = getenv("ENABLE_LOG");
|
||
if (envLog) {
|
||
instance.enable = stringToBool(envLog);
|
||
} else {
|
||
instance.enable = true;
|
||
}
|
||
#endif
|
||
}
|
||
return instance;
|
||
}
|
||
|
||
// 禁止拷贝和赋值
|
||
Logger(const Logger&) = delete;
|
||
Logger& operator=(const Logger&) = delete;
|
||
|
||
// 设置日志文件名
|
||
void setLogFile(const std::string& filename)
|
||
{
|
||
std::lock_guard<std::mutex> lock(fileMutex);
|
||
logFileName = filename;
|
||
}
|
||
|
||
// 启用日志
|
||
void usingLog(bool b = true)
|
||
{
|
||
enable = b;
|
||
}
|
||
|
||
// 刷新日志队列(同步等待所有日志写入完成)
|
||
void flush()
|
||
{
|
||
// 等待队列清空
|
||
while (true) {
|
||
{
|
||
std::lock_guard<std::mutex> lock(queueMutex);
|
||
if (logQueue.empty()) break;
|
||
}
|
||
cv.notify_one();
|
||
#ifdef _WIN32
|
||
Sleep(1);
|
||
#else
|
||
usleep(1000);
|
||
#endif
|
||
}
|
||
}
|
||
|
||
// 写日志,支持 printf 格式化
|
||
void log(const char* file, int line, const char* format, ...)
|
||
{
|
||
va_list args;
|
||
va_start(args, format);
|
||
|
||
std::string message = formatString(format, args);
|
||
|
||
va_end(args);
|
||
|
||
auto timestamp = getCurrentTimestamp();
|
||
std::string id = pid.empty() ? "" : "[" + pid + "]";
|
||
|
||
std::string logEntry = file && line ?
|
||
id + "[" + timestamp + "] [" + file + ":" + std::to_string(line) + "] " + message:
|
||
id + "[" + timestamp + "] " + message;
|
||
|
||
// 方案B:在源头统一保证每条日志以单个 '\n' 结尾,文件与内存 ring buffer
|
||
// 共用同一份文本;写文件时不再额外追加换行,内存导出直接拼接即可正确分行。
|
||
while (!logEntry.empty() && (logEntry.back() == '\n' || logEntry.back() == '\r'))
|
||
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);
|
||
m_memLog.push_back(logEntry);
|
||
m_totalCount++;
|
||
if (m_memLog.size() > kMemLogMax)
|
||
m_memLog.pop_front();
|
||
}
|
||
|
||
if (enable) {
|
||
if (running) {
|
||
std::lock_guard<std::mutex> lock(queueMutex);
|
||
logQueue.push(logEntry);
|
||
} else {
|
||
writeToFile(logEntry);
|
||
}
|
||
}
|
||
#ifdef _DEBUG
|
||
#ifndef _WINDOWS
|
||
printf("%s", logEntry.c_str());
|
||
#else
|
||
OutputDebugStringA(logEntry.c_str());
|
||
#endif
|
||
#endif
|
||
cv.notify_one(); // 通知写线程
|
||
}
|
||
|
||
// 返回内存 ring buffer 中的全部日志(供 TCP 查询使用)
|
||
std::string DumpMemoryLog() const
|
||
{
|
||
std::lock_guard<std::mutex> lock(m_memMutex);
|
||
std::string result;
|
||
result.reserve(m_memLog.size() * 128);
|
||
for (const auto& entry : m_memLog)
|
||
result += entry;
|
||
return result;
|
||
}
|
||
|
||
// 增量版本:只返回 fromAbs 之后的新条目,并将 fromAbs 更新到当前末尾。
|
||
// fromAbs 是调用方维护的绝对计数器(首次传 0 即得全量)。
|
||
std::string DumpMemoryLogFrom(size_t& fromAbs) const
|
||
{
|
||
std::lock_guard<std::mutex> lock(m_memMutex);
|
||
// ring buffer 内最旧条目的绝对序号
|
||
size_t oldest = (m_totalCount >= m_memLog.size()) ? m_totalCount - m_memLog.size() : 0;
|
||
size_t startIdx = (fromAbs <= oldest) ? 0 : (fromAbs - oldest);
|
||
std::string result;
|
||
for (size_t i = startIdx; i < m_memLog.size(); ++i)
|
||
result += m_memLog[i];
|
||
fromAbs = m_totalCount;
|
||
return result;
|
||
}
|
||
|
||
// 停止日志系统
|
||
void stop()
|
||
{
|
||
if (!running) return;
|
||
{
|
||
std::lock_guard<std::mutex> lock(queueMutex);
|
||
running = false; // 设置运行状态
|
||
}
|
||
cv.notify_one();
|
||
if (workerThread.joinable()) {
|
||
try {
|
||
workerThread.join();
|
||
} catch (const std::system_error& e) {
|
||
printf("Join failed: %s [%d]\n", e.what(), e.code().value());
|
||
}
|
||
}
|
||
#ifdef _WIN32
|
||
for (int i = 0; threadRun && i++ < 1000; Sleep(1));
|
||
#else
|
||
for (int i = 0; threadRun && i++ < 1000; usleep(1000));
|
||
#endif
|
||
}
|
||
|
||
private:
|
||
// 日志按月份起名
|
||
void InitLogFile(const std::string & dir, const std::string& pid)
|
||
{
|
||
time_t currentTime = time(nullptr);
|
||
tm* localTime = localtime(¤tTime);
|
||
char timeString[32];
|
||
strftime(timeString, sizeof(timeString), "%Y-%m", localTime);
|
||
char fileName[100];
|
||
#ifdef _WINDOWS
|
||
sprintf_s(fileName, "\\YAMA_%s_%s.txt", timeString, pid.c_str());
|
||
#elif defined(_WIN32)
|
||
sprintf_s(fileName, "\\log_%s_%s.txt", timeString, pid.c_str());
|
||
#else
|
||
snprintf(fileName, sizeof(fileName), "/log_%s_%s.txt", timeString, pid.c_str());
|
||
#endif
|
||
logFileName = dir + fileName;
|
||
}
|
||
std::string logFileName; // 日志文件名
|
||
bool enable; // 是否启用
|
||
bool mirrorStderr = false; // macOS:每行同时镜像到 stderr(/tmp/ghost.log)
|
||
bool threadRun; // 日志线程状态
|
||
std::queue<std::string> logQueue; // 日志队列
|
||
std::mutex queueMutex; // 队列互斥锁
|
||
std::condition_variable cv; // 条件变量
|
||
std::atomic<bool> running; // 是否运行
|
||
std::thread workerThread; // 后台线程
|
||
std::mutex fileMutex; // 文件写入锁
|
||
std::string pid; // 进程ID
|
||
|
||
static constexpr size_t kMemLogMax = 1000;
|
||
std::deque<std::string> m_memLog;
|
||
size_t m_totalCount = 0;
|
||
mutable std::mutex m_memMutex;
|
||
|
||
Logger() : enable(false), threadRun(false), running(true), workerThread(&Logger::processLogs, this) {}
|
||
|
||
~Logger()
|
||
{
|
||
stop();
|
||
}
|
||
|
||
// 后台线程处理日志
|
||
// 退出语义:stop() 设 running=false 后,本线程必须把队列里**已入队**的日志
|
||
// 全部刷盘再退出。否则进程死亡前最后几条 Mprintf(包括退出原因)会丢失。
|
||
void processLogs()
|
||
{
|
||
threadRun = true;
|
||
while (true) {
|
||
std::unique_lock<std::mutex> lock(queueMutex);
|
||
cv.wait(lock, [this]() {
|
||
return !running || !logQueue.empty();
|
||
});
|
||
|
||
// drain:不带 running 判断,确保 stop() 时残留条目也写完
|
||
while (!logQueue.empty()) {
|
||
std::string logEntry = logQueue.front();
|
||
logQueue.pop();
|
||
lock.unlock();
|
||
|
||
// 写入日志文件
|
||
writeToFile(logEntry);
|
||
|
||
lock.lock();
|
||
}
|
||
|
||
// 队列已空再决定要不要退出
|
||
if (!running) break;
|
||
}
|
||
threadRun = false;
|
||
}
|
||
|
||
// 写入文件
|
||
void writeToFile(const std::string& logEntry)
|
||
{
|
||
std::lock_guard<std::mutex> lock(fileMutex);
|
||
std::ofstream logFile(logFileName, std::ios::app);
|
||
if (logFile.is_open()) {
|
||
logFile << logEntry; // logEntry 已在 log() 中以单个 '\n' 结尾
|
||
}
|
||
}
|
||
|
||
// 获取当前时间戳
|
||
std::string getCurrentTimestamp()
|
||
{
|
||
auto now = std::chrono::system_clock::now();
|
||
auto in_time_t = std::chrono::system_clock::to_time_t(now);
|
||
|
||
std::tm tm;
|
||
#ifdef _WIN32
|
||
localtime_s(&tm, &in_time_t); // Windows 安全版本
|
||
#else
|
||
localtime_r(&in_time_t, &tm); // POSIX 安全版本
|
||
#endif
|
||
|
||
std::stringstream ss;
|
||
ss << std::put_time(&tm, "%Y-%m-%d %H:%M:%S");
|
||
return ss.str();
|
||
}
|
||
|
||
// 将日志级别转换为字符串
|
||
std::string logLevelToString(LogLevel level)
|
||
{
|
||
switch (level) {
|
||
case InfoLevel:
|
||
return "INFO";
|
||
case WarningLevel:
|
||
return "WARNING";
|
||
case ErrorLevel:
|
||
return "ERROR";
|
||
default:
|
||
return "UNKNOWN";
|
||
}
|
||
}
|
||
|
||
// 格式化字符串
|
||
std::string formatString(const char* format, va_list args)
|
||
{
|
||
char buffer[1024];
|
||
vsnprintf(buffer, sizeof(buffer), format, args);
|
||
return std::string(buffer);
|
||
}
|
||
};
|
||
|
||
inline const char* getFileName(const char* path)
|
||
{
|
||
const char* fileName = strrchr(path, '\\');
|
||
if (!fileName) {
|
||
fileName = strrchr(path, '/');
|
||
}
|
||
return fileName ? fileName + 1 : path;
|
||
}
|
||
|
||
#ifdef _WINDOWS
|
||
#ifdef _DEBUG
|
||
#define Mprintf(format, ...) TRACE(format, __VA_ARGS__)
|
||
#else
|
||
#define Mprintf(format, ...) Logger::getInstance().log(getFileName(__FILE__), __LINE__, format, __VA_ARGS__)
|
||
#endif
|
||
#elif defined(_WIN32)
|
||
#define Mprintf(format, ...) Logger::getInstance().log(getFileName((__FILE__)), __LINE__, (format), __VA_ARGS__)
|
||
#elif defined(__APPLE__)
|
||
// macOS:与 Linux 一样写 Logger —— 内存 ring 供服务端「运行日志」菜单取回
|
||
// (COMMAND_QUERY_LOG → TOKEN_REPORT_LOG),Logger 初始化时置 mirrorStderr,
|
||
// 同一行再落到 stderr,也就是 /tmp/ghost.log。不要改回 NSLog/fprintf:那样
|
||
// ring 永远为空,菜单打开后没有任何内容。
|
||
#ifdef Mprintf
|
||
#undef Mprintf
|
||
#endif
|
||
#define Mprintf(format, ...) Logger::getInstance().log(getFileName(__FILE__), __LINE__, format, ##__VA_ARGS__)
|
||
#elif defined(__ANDROID__)
|
||
#include <android/log.h>
|
||
#include <cstdarg>
|
||
#ifdef Mprintf
|
||
#undef Mprintf
|
||
#endif
|
||
void UseAndroidLog(void (*cb)(const char*));
|
||
|
||
// 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
|
||
#undef Mprintf
|
||
#endif
|
||
#define Mprintf(format, ...) Logger::getInstance().log(getFileName(__FILE__), __LINE__, format, ##__VA_ARGS__)
|
||
#endif
|
||
|
||
inline void Log(const char* message)
|
||
{
|
||
return Logger::getInstance().log(NULL, 0, "%s", message);
|
||
}
|
||
|
||
inline void Logf(const char* file, int line, const char* format, ...)
|
||
{
|
||
va_list args;
|
||
va_start(args, format);
|
||
char message[1024];
|
||
vsnprintf(message, sizeof(message), format, args);
|
||
va_end(args);
|
||
return Logger::getInstance().log(getFileName(file), line, "%s", message);
|
||
}
|