载入中...
搜索中...
未找到
CrashLog.cpp
浏览该文件的文档.
1#include "common/CrashLog.h"
2
3#include <chrono>
4#include <cstdlib>
5#include <ctime>
6#include <fstream>
7#include <mutex>
8#include <string>
9
10namespace eve {
11
12namespace {
13
14std::mutex g_logMutex;
15std::ofstream g_logFile;
16std::string g_logPath;
17bool g_open = false;
18bool g_explicitInit = false;
19
20std::string nowStamp() {
21 using clock = std::chrono::system_clock;
22 const auto t = clock::to_time_t(clock::now());
23 std::tm tm{};
24#if defined(_WIN32)
25 localtime_s(&tm, &t);
26#else
27 localtime_r(&t, &tm);
28#endif
29 char buf[40];
30 std::strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tm);
31 return buf;
32}
33
34std::string resolvePath(const std::string& logDir) {
35 std::string dir = logDir;
36 if (dir.empty()) {
37 const char* env = std::getenv("EVE_LOG_DIR");
38 if (env) dir = env;
39 }
40 if (dir.empty()) return "eve.log";
41 const char last = dir.back();
42 return (last == '/' || last == '\\') ? dir + "eve.log" : dir + "/eve.log";
43}
44
45void openLocked(const std::string& path, bool explicitInit) {
46 g_logFile.open(path, std::ios::app);
47 if (!g_logFile) return;
48 g_open = true;
49 g_explicitInit = explicitInit;
50 g_logPath = path;
51 g_logFile << "==== " << nowStamp() << " " << kSessionStartMarker << " ====\n";
52 g_logFile.flush();
53}
54
55// Open on first use with the default/env path. Never overrides an explicit
56// initSystemLogging(dir) that failed to open.
57void ensureOpenLocked() {
58 if (g_open || g_explicitInit) return;
59 openLocked(resolvePath({}), /*explicitInit=*/false);
60}
61
62} // namespace
63
64void initSystemLogging(const std::string& logDir) {
65 std::lock_guard<std::mutex> lock(g_logMutex);
66 if (g_open) return;
67 openLocked(resolvePath(logDir), /*explicitInit=*/true);
68}
69
70void recordLogEvent(const std::string& level, const std::string& message) {
71 std::lock_guard<std::mutex> lock(g_logMutex);
72 ensureOpenLocked();
73 if (!g_open || !g_logFile) return;
74 g_logFile << "[" << nowStamp() << "] " << level << " | " << message << "\n";
75 g_logFile.flush();
76}
77
78void recordCrashEvent(const std::string& report) {
79 std::unique_lock<std::mutex> lock(g_logMutex, std::try_to_lock);
80 if (!lock.owns_lock()) return;
81 ensureOpenLocked();
82 if (!g_open || !g_logFile) return;
83 g_logFile << "[" << nowStamp() << "] crash | ";
84 g_logFile.write(report.data(), static_cast<std::streamsize>(report.size()));
85 if (report.empty() || report.back() != '\n') g_logFile << "\n";
86 g_logFile.flush();
87}
88
89std::string crashLogPath() {
90 std::lock_guard<std::mutex> lock(g_logMutex);
91 return g_logPath;
92}
93
95 std::lock_guard<std::mutex> lock(g_logMutex);
96 ensureOpenLocked();
97 if (!g_open || !g_logFile) return;
98 g_logFile << "==== " << nowStamp() << " " << kSessionStartMarker << " ====\n";
99 g_logFile.flush();
100}
101
102void recordSessionEnd(int exitCode) {
103 recordLogEvent("info", std::string(kSessionEndMarker) + " " + std::to_string(exitCode) + ")");
104}
105
106} // namespace eve
Persistent, thread-safe record of engine crash and problem events.
std::string message
int level
std::string path
Definition PlayHost.cpp:110
float t
V3 dir
Definition TreeMesh.cpp:150
Build metadata (engine git commit, build time, third-party version).
Definition Build.cpp:16
void recordSessionEnd(int exitCode)
Appends the session-end marker carrying the process exit code.
Definition CrashLog.cpp:102
constexpr const char * kSessionStartMarker
Marker text that opens a session block in eve.log.
Definition CrashLog.h:39
constexpr const char * kSessionEndMarker
Marker text that closes a session block (followed by the exit code). @ownership Borrowed pointer to a...
Definition CrashLog.h:46
void recordSessionStart()
Appends a session-start marker.
Definition CrashLog.cpp:94
void initSystemLogging(const std::string &logDir)
Opens (append) the crash/error log file and records a session marker.
Definition CrashLog.cpp:64
void recordCrashEvent(const std::string &report)
Records a crash report, safe to call from a crash/exception handler.
Definition CrashLog.cpp:78
std::string crashLogPath()
Path of the open crash/error log file.
Definition CrashLog.cpp:89
void recordLogEvent(const std::string &level, const std::string &message)
Appends a timestamped event line to the crash/error log.
Definition CrashLog.cpp:70