diff --git a/bee/crash/handler_linux.cpp b/bee/crash/handler_linux.cpp index 1dcc41b1..992f890e 100644 --- a/bee/crash/handler_linux.cpp +++ b/bee/crash/handler_linux.cpp @@ -133,7 +133,11 @@ namespace bee::crash { handler::handler(const char* dump_path) noexcept { handler* expected = nullptr; if (handler_.compare_exchange_strong(expected, this)) { - snprintf(dump_path_, sizeof(dump_path_) / sizeof(dump_path_[0]), "%s/crash_%s.log", dump_path, nanoid().c_str()); + if (dump_path[0] == '-' && dump_path[1] == '\0') { + dump_path_[0] = '\0'; + } else { + snprintf(dump_path_, sizeof(dump_path_) / sizeof(dump_path_[0]), "%s/crash_%s.log", dump_path, nanoid().c_str()); + } memset(&context, 0, sizeof(context)); install_altstack(); install_handlers(); @@ -251,6 +255,10 @@ namespace bee::crash { bool handler::write_dump() noexcept { auto str = get_stacktrace(&context); + if (dump_path_[0] == L'\0') { + printf(L"\n\nCrash log: \n%s\n", str.c_str()); + return true; + } do { writefile file(dump_path_); if (!file) { diff --git a/bee/crash/handler_win.cpp b/bee/crash/handler_win.cpp index 376216f1..41086857 100644 --- a/bee/crash/handler_win.cpp +++ b/bee/crash/handler_win.cpp @@ -39,6 +39,16 @@ namespace bee::crash { } } } + template + void print(const std::basic_string& str) noexcept { + if (handle) { + if constexpr (std::is_same_v, char>) { + WriteConsoleA(handle, str.data(), (DWORD)str.size(), NULL, NULL); + } else if constexpr (std::is_same_v, wchar_t>) { + WriteConsoleW(handle, str.data(), (DWORD)str.size(), NULL, NULL); + } + } + } HANDLE handle; }; @@ -51,7 +61,11 @@ namespace bee::crash { handler::handler(const char* dump_path) noexcept { handler* expected = nullptr; if (handler_.compare_exchange_strong(expected, this)) { - _snwprintf_s(dump_path_, sizeof(dump_path_) / sizeof(dump_path_[0]), _TRUNCATE, L"%s/crash_%s.log", wtf8::u2w(dump_path).c_str(), wnanoid().c_str()); + if (dump_path[0] == '-' && dump_path[1] == '\0') { + dump_path_[0] = L'\0'; + } else { + _snwprintf_s(dump_path_, sizeof(dump_path_) / sizeof(dump_path_[0]), _TRUNCATE, L"%s/crash_%s.log", wtf8::u2w(dump_path).c_str(), wnanoid().c_str()); + } DWORD thread_id; thread_ = CreateThread(NULL, kHandlerThreadInitialStackSize, thread_func, this, 0, &thread_id); assert(thread_ != NULL); @@ -206,6 +220,13 @@ namespace bee::crash { bool handler::write_dump(DWORD thread_id, HANDLE thread_handle, EXCEPTION_POINTERS* exinfo) noexcept { auto str = get_stacktrace(exinfo->ContextRecord); + if (dump_path_[0] == L'\0') { + console c; + c.print(L"\n\nCrash log: \n"); + c.print(str); + c.print(L"\n"); + return true; + } do { writefile file(dump_path_); if (!file) { diff --git a/bootstrap/main.cpp b/bootstrap/main.cpp index 082ae615..8aedd8aa 100644 --- a/bootstrap/main.cpp +++ b/bootstrap/main.cpp @@ -213,9 +213,6 @@ static int loadfile(lua_State *L, const fs::path &filename, const char *chunknam return status; } -#include -static bee::crash::handler handler("."); - static int handle_script(lua_State *L) { auto progdir = getprogdir(L); int status = loadfile(L, progdir / "main.lua", "=(bootstrap.lua)"); diff --git a/test/test.lua b/test/test.lua index 4bed6a29..fcea458a 100644 --- a/test/test.lua +++ b/test/test.lua @@ -27,6 +27,9 @@ if not lt.options.list then print("DEBUG: ", platform.DEBUG) end +local crash = require "bee.crash" +local _ = crash.create_handler "-" + require "test_skip" --require "test_lua" require "test_serialization"