Procházet zdrojové kódy

Remove flush() from Log, instead, directly log to stdout and to remote console if available

Daniele Bartolini před 12 roky
rodič
revize
63b6830638

+ 8 - 10
engine/Device.cpp

@@ -166,8 +166,8 @@ void Device::init()
 	m_sound_renderer->init();
 	Log::d("SoundRenderer created.");
 
-	Log::i("Crown Engine initialized.");
-	Log::i("Initializing Game...");
+	Log::d("Crown Engine initialized.");
+	Log::d("Initializing Game...");
 
 	m_is_init = true;
 	start();
@@ -205,7 +205,7 @@ void Device::shutdown()
 		CE_DELETE(m_allocator, m_sound_renderer);
 	}
 
-	Log::i("Releasing LuaEnvironment...");
+	Log::d("Releasing LuaEnvironment...");
 	if (m_lua_environment)
 	{
 		m_lua_environment->shutdown();
@@ -213,25 +213,25 @@ void Device::shutdown()
 		CE_DELETE(m_allocator, m_lua_environment);
 	}
 
-	Log::i("Releasing Input Devices...");
+	Log::d("Releasing Input Devices...");
 	CE_DELETE(m_allocator, m_touch);
 	CE_DELETE(m_allocator, m_mouse);
 	CE_DELETE(m_allocator, m_keyboard);
 
-	Log::i("Releasing DebugRenderer...");
+	Log::d("Releasing DebugRenderer...");
 	if (m_debug_renderer)
 	{
 		CE_DELETE(m_allocator, m_debug_renderer);
 	}
 
-	Log::i("Releasing Renderer...");
+	Log::d("Releasing Renderer...");
 	if (m_renderer)
 	{
 		m_renderer->shutdown();
 		CE_DELETE(m_allocator, m_renderer);
 	}
 
-	Log::i("Releasing ResourceManager...");
+	Log::d("Releasing ResourceManager...");
 	if (m_resource_manager)
 	{
 		CE_DELETE(m_allocator, m_resource_manager);
@@ -242,14 +242,13 @@ void Device::shutdown()
 		Bundle::destroy(m_allocator, m_resource_bundle);
 	}
 
-	Log::i("Releasing Filesystem...");
+	Log::d("Releasing Filesystem...");
 	if (m_filesystem)
 	{
 		CE_DELETE(m_allocator, m_filesystem);
 	}
 
 	#if defined(CROWN_DEBUG) || defined(CROWN_DEVELOPMENT)
-		Log::flush();
 		m_rpc->execute_callbacks();
 		m_rpc->shutdown();
 		CE_DELETE(m_allocator, m_rpc);
@@ -416,7 +415,6 @@ void Device::frame()
 		m_sound_renderer->frame();
 	}
 
-	Log::flush();
 	m_rpc->execute_callbacks();
 
 	m_frame_count++;

+ 0 - 1
engine/compilers/BundleCompiler.cpp

@@ -106,7 +106,6 @@ bool BundleCompiler::compile(const char* bundle_dir, const char* source_dir, con
 		}
 
 		Log::i("%s <= %s", out_name, filename);
-		Log::flush();
 
 		bool result = false;
 		if (resource_type_hash == MESH_TYPE)

+ 1 - 2
engine/core/Assert.h

@@ -35,8 +35,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 	#define CE_ASSERT(condition, ...) do { if (!(condition)) {\
 		crown::Log::e("Assertion failed: %s", #condition);\
 		crown::Log::e("\t" __VA_ARGS__);\
-		crown::Log::e("\n\tIn %s:%d\n\n", __FILE__, __LINE__);\
-		crown::Log::flush(); abort();} } while (0)
+		crown::Log::e("\n\tIn %s:%d\n\n", __FILE__, __LINE__); abort();} } while (0)
 #else
 	#define CE_ASSERT(...) ((void)0)
 #endif

+ 17 - 40
engine/core/Log.cpp

@@ -25,54 +25,46 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "Log.h"
-#include "LinearAllocator.h"
-#include "StringStream.h"
 #include "Device.h"
 #include "RPCServer.h"
 
 namespace crown
 {
 
-static char g_buffer[16 * 1024];
-static LinearAllocator g_allocator(g_buffer, 16 * 1024);
-static StringStream g_stream(g_allocator);
-
-LogLevel Log::m_threshold = LL_DEBUG;
+LogSeverity::Enum Log::m_threshold = LogSeverity::DEBUG;
 
 //-----------------------------------------------------------------------------
-LogLevel Log::threshold()
+LogSeverity::Enum Log::threshold()
 {
 	return m_threshold;
 }
 
 //-----------------------------------------------------------------------------
-void Log::set_threshold(LogLevel threshold)
+void Log::set_threshold(LogSeverity::Enum threshold)
 {
 	m_threshold = threshold;
 }
 
 //-----------------------------------------------------------------------------
-void Log::log_message(LogLevel level, const char* message, ::va_list arg)
+void Log::log_message(LogSeverity::Enum severity, const char* message, ::va_list arg)
 {
-	if (level > m_threshold)
+	if (severity > m_threshold)
 	{
 		return;
 	}
 
-	switch (level)
-	{
-		case LL_DEBUG: g_stream << "D: "; break;
-		case LL_ERROR: g_stream << "E: "; break;
-		case LL_WARN: g_stream << "W: "; break;
-		case LL_INFO: g_stream << "I: "; break;
-		default: break;
-	}
-
 	char buf[1024];
 	int len = vsnprintf(buf, 1024 - 2, message, arg);
 	buf[len] = '\n';
 	buf[len + 1] = '\0';
-	g_stream << buf;
+
+	os::printf(buf);
+	::fflush(stdout);
+
+	if (device()->rpc() != NULL && string::strlen(buf) > 0)
+	{
+		device()->rpc()->log_to_all(buf, severity);
+	}
 }
 
 //-----------------------------------------------------------------------------
@@ -80,7 +72,7 @@ void Log::d(const char* message, ...)
 {
 	va_list args;
 	va_start (args, message);
-	log_message(LL_DEBUG, message, args);
+	log_message(LogSeverity::DEBUG, message, args);
 	va_end (args);
 }
 
@@ -89,7 +81,7 @@ void Log::e(const char* message, ...)
 {
 	va_list args;
 	va_start (args, message);
-	log_message(LL_ERROR, message, args);
+	log_message(LogSeverity::ERROR, message, args);
 	va_end (args);
 }
 
@@ -98,7 +90,7 @@ void Log::w(const char* message, ...)
 {
 	va_list args;
 	va_start (args, message);
-	log_message(LL_WARN, message, args);
+	log_message(LogSeverity::WARN, message, args);
 	va_end (args);
 }
 
@@ -107,23 +99,8 @@ void Log::i(const char* message, ...)
 {
 	va_list args;
 	va_start (args, message);
-	log_message(LL_INFO, message, args);
+	log_message(LogSeverity::INFO, message, args);
 	va_end (args);
 }
 
-//-----------------------------------------------------------------------------
-void Log::flush()
-{
-	os::printf(g_stream.c_str());
-	::fflush(stdout);
-
-	if (device()->rpc() != NULL)
-	{
-		device()->rpc()->send_message_to_all(g_stream.c_str());
-	}
-
-	g_stream.clear();
-	g_allocator.clear();
-}
-
 } // namespace crown

+ 12 - 11
engine/core/Log.h

@@ -34,12 +34,15 @@ namespace crown
 {
 
 /// Enumerates log levels.
-enum LogLevel
+struct LogSeverity
 {
-	LL_INFO		= 0,
-	LL_WARN		= 1,
-	LL_ERROR	= 2,
-	LL_DEBUG	= 3
+	enum Enum
+	{
+		INFO	= 0,
+		WARN	= 1,
+		ERROR	= 2,
+		DEBUG	= 3
+	};	
 };
 
 class RPCServer;
@@ -51,23 +54,21 @@ class CE_EXPORT Log
 public:
 
 	/// Returns the threshold used to filter out log messages.
-	static LogLevel		threshold();
+	static LogSeverity::Enum threshold();
 
 	/// Sets the thresold used to filter out log messages
-	static void			set_threshold(LogLevel threshold);
+	static void			set_threshold(LogSeverity::Enum threshold);
 
-	static void			log_message(LogLevel level, const char* message, ::va_list arg);
+	static void			log_message(LogSeverity::Enum severity, const char* message, ::va_list arg);
 
 	static void			d(const char* message, ...);
 	static void			e(const char* message, ...);
 	static void			w(const char* message, ...);
 	static void			i(const char* message, ...);
 
-	static void			flush();
-
 private:
 
-	static LogLevel		m_threshold;
+	static LogSeverity::Enum m_threshold;
 };
 
 } // namespace crown

+ 26 - 0
engine/rpc/RPCServer.cpp

@@ -28,6 +28,8 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Log.h"
 #include "JSONParser.h"
 #include "RPCHandler.h"
+#include "TempAllocator.h"
+#include "StringStream.h"
 
 namespace crown
 {
@@ -124,6 +126,30 @@ void RPCServer::update()
 	}
 }
 
+//-----------------------------------------------------------------------------
+void RPCServer::log_to_all(const char* message, LogSeverity::Enum severity)
+{
+	TempAllocator2048 alloc;
+	StringStream json(alloc);
+
+	static const char* severity_to_text[] = { "info", "warning", "error", "debug" };
+
+	json << "{\"type\":\"message\",";
+	json << "\"severity\":\"" << severity_to_text[severity] << "\",";
+
+	char buf[1024];
+	string::strncpy(buf, message, 1024);
+	for (uint32_t i = 0; i < string::strlen(message); i++)
+	{
+		if (buf[i] == '"')
+			buf[i] = '\'';
+	}
+
+	json << "\"message\":\"" << buf << "\"}";
+
+	send_message_to_all(json.c_str());
+}
+
 //-----------------------------------------------------------------------------
 void RPCServer::send_message_to(ClientId client, const char* message)
 {

+ 3 - 0
engine/rpc/RPCServer.h

@@ -31,6 +31,7 @@ OTHER DEALINGS IN THE SOFTWARE.
 #include "Queue.h"
 #include "IdTable.h"
 #include "LinearAllocator.h"
+#include "Log.h"
 
 namespace crown
 {
@@ -60,6 +61,8 @@ public:
 
 	void				update();
 
+	void				log_to_all(const char* message, LogSeverity::Enum severity);
+
 	void				send_message_to(ClientId client, const char* message);
 	void				send_message_to_all(const char* message);