Przeglądaj źródła

core: do not use StringStream

Daniele Bartolini 1 rok temu
rodzic
commit
2e70a8c119

+ 4 - 4
src/core/error/callstack.h

@@ -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
 

+ 4 - 5
src/core/error/callstack_emscripten.cpp

@@ -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);
 	}
 

+ 7 - 11
src/core/error/callstack_linux.cpp

@@ -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
 {
@@ -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));
@@ -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';
@@ -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)
@@ -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);
 	}

+ 3 - 3
src/core/error/callstack_noop.cpp

@@ -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

+ 8 - 13
src/core/error/callstack_windows.cpp

@@ -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
@@ -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);
@@ -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());

+ 3 - 8
src/core/error/error.cpp

@@ -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);
 	}
 

+ 2 - 2
src/device/main_linux.cpp

@@ -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;
 			}
 		};