---
title: "Log"
description: "线程安全的日志系统，同时写入文件和内存缓存。"
---

---
title: Log
description: 线程安全的日志系统，同时写入文件和内存缓存。
---

`include/XBase/Log.h` · `XBase::Log`

```cpp
void Init(const char* filePath = nullptr);  // 默认 <游戏根目录>\XBase\debug.log
void InitForMod(const char* modName);      // 默认 <游戏根目录>\XBase\Mods\<模组名>\debug.log
void Shutdown();
bool IsInitialized();

void Write(Level level, const char* message);
void Write(Level level, const std::string& message);
void Info(const char* message);
void Warn(const char* message);
void Error(const char* message);
void Debug(const char* message);
void Info(const std::string& message);
void Warn(const std::string& message);
void Error(const std::string& message);
void Debug(const std::string& message);

std::vector<Entry> GetEntries();    // 最近 600 条
std::size_t GetTotalCount();
std::string GetText();              // 完整文本
std::string GetLogFilePath();
```

- `Level` 枚举: `Debug`, `Info`, `Warn`, `Error`
- `Entry` 结构: `Level level`, `std::string message`, `std::chrono::system_clock::time_point timestamp`
- 线程安全，写入 `[YYYY-MM-DD HH:MM:SS] [LEVEL] message` 格式日志。

## 路径约定

不再有 `logs` 子目录，也不按 asi 位置分家：

```cpp
XBase::Log::InitForMod("XMenu");   // <游戏根目录>\XBase\Mods\XMenu\debug.log
```

- `Log::Init()` 写 XBase 自身日志 `<游戏根目录>\XBase\debug.log`
- 宿主日志必须走 `InitForMod()`，模组名与 asi 文件名一致
- 查看日志用 `<游戏根目录>\XBase\XBase.exe`，它会列出 XBase 自身与 `Mods\` 下每个模组的配置与日志
