Skip to content

Commit

Permalink
core: do not use StringStream
Browse files Browse the repository at this point in the history
  • Loading branch information
dbartolini committed Jan 18, 2025
1 parent cdc645a commit 1c9651d
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 46 deletions.
8 changes: 4 additions & 4 deletions src/core/error/callstack.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@

#pragma once

#include "core/strings/string_stream.h"
#include "core/strings/types.h"
#include "core/types.h"
#include "device/log.h"

namespace crown
{
namespace error
{
/// Fills @a ss with the current call stack.
void callstack(StringStream &ss);
/// Logs the current call stack.
void callstack(log_internal::System system, LogSeverity::Enum severity = LogSeverity::LOG_INFO);

} // namespace error

Expand Down
9 changes: 4 additions & 5 deletions src/core/error/callstack_emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@
#include "core/platform.h"

#if CROWN_PLATFORM_EMSCRIPTEN
#include "core/error/callstack.h"
#include "core/memory/allocator.h"
#include "core/memory/globals.h"
#include "core/strings/string_stream.inl"
#include <emscripten/emscripten.h>

namespace crown
{
namespace error
{
void callstack(StringStream &ss)
void callstack(log_internal::System system, LogSeverity::Enum severity)
{
int size = emscripten_get_callstack(EM_LOG_C_STACK | EM_LOG_JS_STACK, NULL, 0);

char *data = (char *)default_allocator().allocate(size);
emscripten_get_callstack(EM_LOG_C_STACK | EM_LOG_JS_STACK, data, size);

ss << data;

log_internal::logx(severity, system, data);
default_allocator().deallocate(data);
}

Expand Down
18 changes: 7 additions & 11 deletions src/core/error/callstack_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
#include "core/platform.h"

#if CROWN_PLATFORM_LINUX && (CROWN_COMPILER_GCC || CROWN_COMPILER_CLANG)
#include "core/error/callstack.h"
#include "core/process.h"
#include "core/strings/string.inl"
#include "core/strings/string_stream.inl"
#include <cxxabi.h>
#include <execinfo.h>
#include <stb_sprintf.h>
#include <stdlib.h> // free
#include <string.h> // strchr
#include <unistd.h> // getpid
#include <stb_sprintf.h>

namespace crown
{
Expand Down Expand Up @@ -46,7 +46,7 @@ namespace error
return "<addr2line missing>";
}

void callstack(StringStream &ss)
void callstack(log_internal::System system, LogSeverity::Enum severity)
{
void *array[64];
int size = backtrace(array, countof(array));
Expand All @@ -61,8 +61,6 @@ namespace error
char *addr_begin = strchr(msg, '[');
char *addr_end = strchr(msg, ']');

char buf[512];

// Attempt to demangle the symbol
if (mangled_name && offset_begin && offset_end && mangled_name < offset_begin) {
*mangled_name++ = '\0';
Expand All @@ -76,9 +74,9 @@ namespace error
char line[256];
memset(line, 0, sizeof(line));

stbsp_snprintf(buf
, sizeof(buf)
, " [%2d] %s: (%s)+%s in %s\n"
log_internal::logx(severity
, system
, "[%2d] %s: (%s)+%s in %s"
, i
, msg
, (demangle_ok == 0 ? real_name : mangled_name)
Expand All @@ -88,10 +86,8 @@ namespace error

free(real_name);
} else {
stbsp_snprintf(buf, sizeof(buf), " [%2d] %s\n", i, msg);
log_internal::logx(severity, system, "[%2d] %s", i, msg);
}

ss << buf;
}
free(messages);
}
Expand Down
6 changes: 3 additions & 3 deletions src/core/error/callstack_noop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
#include "core/platform.h"

#if CROWN_PLATFORM_ANDROID
#include "core/strings/string_stream.inl"
#include "core/error/callstack.h"

namespace crown
{
namespace error
{
void callstack(StringStream &ss)
void callstack(log_internal::System system, LogSeverity::Enum severity)
{
ss << "Not supported";
log_internal::logx(severity, system, "Not supported");
}

} // namespace error
Expand Down
21 changes: 8 additions & 13 deletions src/core/error/callstack_windows.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "core/platform.h"

#if CROWN_PLATFORM_WINDOWS
#include "core/strings/string_stream.inl"
#include "core/error/callstack.h"
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#endif
Expand All @@ -16,13 +16,12 @@
#include <dbghelp.h>
#pragma warning(pop)
#include <new>
#include <stb_sprintf.h>

namespace crown
{
namespace error
{
void callstack(StringStream &ss)
void callstack(log_internal::System system, LogSeverity::Enum severity)
{
SymInitialize(GetCurrentProcess(), NULL, TRUE);
SymSetOptions(SYMOPT_LOAD_LINES | SYMOPT_UNDNAME);
Expand Down Expand Up @@ -85,27 +84,23 @@ namespace error
);
res = res && SymFromAddr(GetCurrentProcess(), stack.AddrPC.Offset, 0, sym);

char str[512];

if (res == TRUE) {
stbsp_snprintf(str
, sizeof(str)
, " [%2i] %s in %s:%d\n"
log_internal::logx(severity
, system
, "[%2i] %s in %s:%d"
, num
, sym->Name
, line.FileName
, line.LineNumber
);
} else {
stbsp_snprintf(str
, sizeof(str)
, " [%2i] 0x%p\n"
log_internal::logx(severity
, system
, "[%2i] 0x%p"
, num
, stack.AddrPC.Offset
);
}

ss << str;
}

SymCleanup(GetCurrentProcess());
Expand Down
11 changes: 3 additions & 8 deletions src/core/error/error.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,9 @@ namespace error
{
char buf[1024];
stbsp_vsnprintf(buf, sizeof(buf), format, args);

TempAllocator4096 ta;
StringStream ss(ta);
ss << buf;
ss << "Stacktrace:\n";
callstack(ss);

loge(ERROR, string_stream::c_str(ss));
loge(ERROR, buf);
loge(ERROR, "Stacktrace:");
callstack(ERROR, LogSeverity::LOG_ERROR);
exit(EXIT_FAILURE);
}

Expand Down
4 changes: 2 additions & 2 deletions src/device/main_linux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1056,12 +1056,12 @@ int main(int argc, char **argv)
case SIGSEGV:
case SIGSYS:
// FIXME: only use signal-safe functions.
error::abort("Signal %d\n", signum);
error::abort("Signal %d", signum);
break;

default:
// FIXME: only use signal-safe functions.
error::abort("Unhandled signal %d\n", signum);
error::abort("Unhandled signal %d", signum);
break;
}
};
Expand Down

0 comments on commit 1c9651d

Please sign in to comment.