Skip to content

Commit

Permalink
允许输出日志到控制台
Browse files Browse the repository at this point in the history
  • Loading branch information
actboy168 committed Jul 10, 2024
1 parent 4f7dfb4 commit a194fc2
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 5 deletions.
10 changes: 9 additions & 1 deletion bee/crash/handler_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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) {
Expand Down
23 changes: 22 additions & 1 deletion bee/crash/handler_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,16 @@ namespace bee::crash {
}
}
}
template <class char_t>
void print(const std::basic_string<char_t>& str) noexcept {
if (handle) {
if constexpr (std::is_same_v<std::remove_cv_t<char_t>, char>) {
WriteConsoleA(handle, str.data(), (DWORD)str.size(), NULL, NULL);
} else if constexpr (std::is_same_v<std::remove_cv_t<char_t>, wchar_t>) {
WriteConsoleW(handle, str.data(), (DWORD)str.size(), NULL, NULL);
}
}
}
HANDLE handle;
};

Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down
3 changes: 0 additions & 3 deletions bootstrap/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,6 @@ static int loadfile(lua_State *L, const fs::path &filename, const char *chunknam
return status;
}

#include <bee/crash/handler.h>
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)");
Expand Down
3 changes: 3 additions & 0 deletions test/test.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a194fc2

Please sign in to comment.