Skip to content

Commit c7fb075

Browse files
committed
Use low-level Windows-safe startup trace writer
1 parent 52b64c6 commit c7fb075

File tree

2 files changed

+34
-43
lines changed

2 files changed

+34
-43
lines changed

src/gui/main_gui.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,6 +377,7 @@ int main(int argc, char* argv[]) {
377377

378378
// Create application with parsed options
379379
writeStartupLog("Constructing App");
380+
ultra::gui::startupTrace("main_gui", "before-app-construction");
380381
ultra::gui::App app(opts);
381382
writeStartupLog("App initialized");
382383

src/gui/startup_trace.hpp

Lines changed: 33 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,50 @@
11
#pragma once
22

3-
#include <chrono>
3+
#include <cstdint>
44
#include <cstdio>
55
#include <cstdlib>
6-
#include <cstring>
7-
#include <filesystem>
8-
#include <mutex>
9-
#include <string>
6+
7+
#ifdef _WIN32
8+
#include <windows.h>
9+
#endif
1010

1111
namespace ultra {
1212
namespace gui {
1313

1414
inline void startupTrace(const char* component, const char* phase) {
1515
#ifdef _WIN32
16-
static std::mutex trace_mutex;
17-
std::lock_guard<std::mutex> lock(trace_mutex);
18-
19-
namespace fs = std::filesystem;
20-
21-
std::string line;
22-
{
23-
using namespace std::chrono;
24-
auto now = system_clock::now();
25-
auto ms = duration_cast<milliseconds>(now.time_since_epoch()).count();
26-
char buf[512];
27-
std::snprintf(
28-
buf, sizeof(buf), "[%lld][STARTUP][%s] %s\n",
29-
static_cast<long long>(ms),
30-
component ? component : "<unknown>",
31-
phase ? phase : "<unknown>"
32-
);
33-
line = buf;
16+
static char g_trace_path[MAX_PATH] = {0};
17+
static bool g_path_initialized = false;
18+
19+
if (!g_path_initialized) {
20+
const char* env_path = std::getenv("ULTRA_STARTUP_LOG");
21+
if (env_path && env_path[0] != '\0') {
22+
std::snprintf(g_trace_path, sizeof(g_trace_path), "%s", env_path);
23+
} else {
24+
char temp_path[MAX_PATH] = {0};
25+
DWORD n = GetTempPathA(static_cast<DWORD>(sizeof(temp_path)), temp_path);
26+
if (n > 0 && n < sizeof(temp_path)) {
27+
char dir_path[MAX_PATH] = {0};
28+
std::snprintf(dir_path, sizeof(dir_path), "%sProjectUltra", temp_path);
29+
CreateDirectoryA(dir_path, nullptr);
30+
std::snprintf(g_trace_path, sizeof(g_trace_path), "%s\\startup.log", dir_path);
31+
} else {
32+
std::snprintf(g_trace_path, sizeof(g_trace_path), "startup.log");
33+
}
34+
}
35+
g_path_initialized = true;
3436
}
3537

36-
std::string env_log;
37-
if (const char* p = std::getenv("ULTRA_STARTUP_LOG")) {
38-
env_log = p;
38+
if (g_trace_path[0] == '\0') {
39+
return;
3940
}
4041

41-
fs::path log_path;
42-
if (!env_log.empty()) {
43-
log_path = fs::path(env_log);
44-
} else if (const char* temp = std::getenv("TEMP")) {
45-
log_path = fs::path(temp) / "ProjectUltra" / "startup.log";
46-
} else {
47-
log_path = fs::path("startup.log");
48-
}
49-
50-
std::error_code ec;
51-
if (!log_path.parent_path().empty()) {
52-
fs::create_directories(log_path.parent_path(), ec);
53-
}
54-
55-
if (FILE* f = std::fopen(log_path.string().c_str(), "a")) {
56-
std::fwrite(line.data(), 1, line.size(), f);
42+
if (FILE* f = std::fopen(g_trace_path, "a")) {
43+
unsigned long long t = static_cast<unsigned long long>(GetTickCount64());
44+
std::fprintf(f, "[%llu][STARTUP][%s] %s\n",
45+
t,
46+
component ? component : "<unknown>",
47+
phase ? phase : "<unknown>");
5748
std::fflush(f);
5849
std::fclose(f);
5950
}
@@ -65,4 +56,3 @@ inline void startupTrace(const char* component, const char* phase) {
6556

6657
} // namespace gui
6758
} // namespace ultra
68-

0 commit comments

Comments
 (0)