Parcourir la source

Merge branch 'master' of github.com:taylor001/crown

Daniele Bartolini il y a 10 ans
Parent
commit
710fbfac77
5 fichiers modifiés avec 67 ajouts et 49 suppressions
  1. 0 4
      README.md
  2. 0 34
      src/console_server.cpp
  3. 0 3
      src/console_server.h
  4. 35 7
      src/core/error/stacktrace_linux.cpp
  5. 32 1
      src/core/log.cpp

+ 0 - 4
README.md

@@ -99,10 +99,6 @@ To run a sample on 64-bit linux debug mode:
 
 
 Engine initialization and shutdown.
 Engine initialization and shutdown.
 
 
-##Tools
-
-A number of tools can be found at https://github.com/taylor001/crown-tools
-
 Contact
 Contact
 -------
 -------
 
 

+ 0 - 34
src/console_server.cpp

@@ -44,40 +44,6 @@ void ConsoleServer::shutdown()
 	_server.close();
 	_server.close();
 }
 }
 
 
-namespace console_server_internal
-{
-	StringStream& sanitize(StringStream& ss, const char* msg)
-	{
-		using namespace string_stream;
-		const char* ch = msg;
-		for (; *ch; ch++)
-		{
-			if (*ch == '"')
-				ss << "\\";
-			ss << *ch;
-		}
-
-		return ss;
-	}
-}
-
-void ConsoleServer::log(const char* msg, LogSeverity::Enum severity)
-{
-	using namespace string_stream;
-	using namespace console_server_internal;
-	static const char* stt[] = { "info", "warning", "error", "debug" };
-
-	// Build json message
-	TempAllocator2048 alloc;
-	StringStream json(alloc);
-
-	json << "{\"type\":\"message\",";
-	json << "\"severity\":\"" << stt[severity] << "\",";
-	json << "\"message\":\""; sanitize(json, msg) << "\"}";
-
-	send(c_str(json));
-}
-
 void ConsoleServer::send(TCPSocket client, const char* json)
 void ConsoleServer::send(TCPSocket client, const char* json)
 {
 {
 	uint32_t len = strlen(json);
 	uint32_t len = strlen(json);

+ 0 - 3
src/console_server.h

@@ -7,7 +7,6 @@
 
 
 #include "container_types.h"
 #include "container_types.h"
 #include "socket.h"
 #include "socket.h"
-#include "log.h"
 
 
 namespace crown
 namespace crown
 {
 {
@@ -21,8 +20,6 @@ public:
 	ConsoleServer(uint16_t port, bool wait);
 	ConsoleServer(uint16_t port, bool wait);
 	void shutdown();
 	void shutdown();
 
 
-	void log(const char* msg, LogSeverity::Enum severity = LogSeverity::INFO);
-
 	/// Collects requests from clients and processes them all.
 	/// Collects requests from clients and processes them all.
 	void update();
 	void update();
 
 

+ 35 - 7
src/core/error/stacktrace_linux.cpp

@@ -13,15 +13,30 @@
 #include <cxxabi.h>
 #include <cxxabi.h>
 #include <execinfo.h>
 #include <execinfo.h>
 #include <string.h> // strchr
 #include <string.h> // strchr
+#include <unistd.h> // getpid
 
 
 namespace crown
 namespace crown
 {
 {
 
 
+const char* addr2line(const char* addr, char* line, int len)
+{
+	char buf[256];
+	snprintf(buf, sizeof(buf), "addr2line -e /proc/%u/exe %s", getpid(), addr);
+	FILE* f = popen(buf, "r");
+	if (f)
+	{
+		fgets(line, len, f);
+		line[strlen(line) - 1] = '\0';
+		pclose(f);
+		return line;
+	}
+	return "<addr2line missing>";
+}
+
 void print_callstack()
 void print_callstack()
 {
 {
 	void* array[64];
 	void* array[64];
 	int size = backtrace(array, CE_COUNTOF(array));
 	int size = backtrace(array, CE_COUNTOF(array));
-
 	char** messages = backtrace_symbols(array, size);
 	char** messages = backtrace_symbols(array, size);
 
 
 	// skip first stack frame (points here)
 	// skip first stack frame (points here)
@@ -30,25 +45,38 @@ void print_callstack()
 		char* msg = messages[i];
 		char* msg = messages[i];
 		char* mangled_name = strchr(msg, '(');
 		char* mangled_name = strchr(msg, '(');
 		char* offset_begin = strchr(msg, '+');
 		char* offset_begin = strchr(msg, '+');
-		char* offset_end = strchr(msg, ')');
+		char* offset_end   = strchr(msg, ')');
+		char* addr_begin   = strchr(msg, '[');
+		char* addr_end     = strchr(msg, ']');
 
 
 		// if the line could be processed, attempt to demangle the symbol
 		// if the line could be processed, attempt to demangle the symbol
 		if (mangled_name && offset_begin && offset_end && mangled_name < offset_begin)
 		if (mangled_name && offset_begin && offset_end && mangled_name < offset_begin)
 		{
 		{
 			*mangled_name++ = '\0';
 			*mangled_name++ = '\0';
 			*offset_begin++ = '\0';
 			*offset_begin++ = '\0';
-			*offset_end++ = '\0';
+			*offset_end++   = '\0';
+			*addr_begin++   = '\0';
+			*addr_end++     = '\0';
+
+			int demangle_ok;
+			char* real_name = abi::__cxa_demangle(mangled_name, 0, 0, &demangle_ok);
+			char line[256];
+			memset(line, 0, sizeof(line));
 
 
-			int status;
-			char* real_name = abi::__cxa_demangle(mangled_name, 0, 0, &status);
+			printf("\t[%d] %s: (%s)+%s in %s\n"
+				, i
+				, msg
+				, (demangle_ok == 0 ? real_name : mangled_name)
+				, offset_begin
+				, addr2line(addr_begin, line, sizeof(line))
+				);
 
 
-			printf("\t[%d] %s: (%s)+%s %s\n", i, messages[i], (status == 0 ? real_name : mangled_name), offset_begin, offset_end);
 			free(real_name);
 			free(real_name);
 		}
 		}
 		// otherwise, print the whole line
 		// otherwise, print the whole line
 		else
 		else
 		{
 		{
-			printf("\t[%d] %s\n", i, messages[i]);
+			printf("\t[%d] %s\n", i, msg);
 		}
 		}
 	}
 	}
 	free(messages);
 	free(messages);

+ 32 - 1
src/core/log.cpp

@@ -7,6 +7,7 @@
 #include "console_server.h"
 #include "console_server.h"
 #include "string_utils.h"
 #include "string_utils.h"
 #include "os.h"
 #include "os.h"
+#include "string_stream.h"
 
 
 #if CROWN_DEBUG
 #if CROWN_DEBUG
 
 
@@ -14,6 +15,36 @@ namespace crown
 {
 {
 namespace log_internal
 namespace log_internal
 {
 {
+	static StringStream& sanitize(StringStream& ss, const char* msg)
+	{
+		using namespace string_stream;
+		const char* ch = msg;
+		for (; *ch; ch++)
+		{
+			if (*ch == '"')
+				ss << "\\";
+			ss << *ch;
+		}
+
+		return ss;
+	}
+
+	static void console_log(const char* msg, LogSeverity::Enum severity)
+	{
+		using namespace string_stream;
+		static const char* stt[] = { "info", "warning", "error", "debug" };
+
+		// Build json message
+		TempAllocator2048 alloc;
+		StringStream json(alloc);
+
+		json << "{\"type\":\"message\",";
+		json << "\"severity\":\"" << stt[severity] << "\",";
+		json << "\"message\":\""; sanitize(json, msg) << "\"}";
+
+		console_server_globals::console().send(c_str(json));
+	}
+
 	void logx(LogSeverity::Enum sev, const char* msg, va_list args)
 	void logx(LogSeverity::Enum sev, const char* msg, va_list args)
 	{
 	{
 		char buf[2048];
 		char buf[2048];
@@ -22,7 +53,7 @@ namespace log_internal
 			len = sizeof(buf) - 1;
 			len = sizeof(buf) - 1;
 
 
 		buf[len] = '\0';
 		buf[len] = '\0';
-		console_server_globals::console().log(buf, sev);
+		console_log(buf, sev);
 		os::log(buf);
 		os::log(buf);
 	}
 	}