Explorar el Código

Log now fills a stringstream and can send data to RPCServer

Daniele Bartolini hace 12 años
padre
commit
4068110718
Se han modificado 2 ficheros con 29 adiciones y 28 borrados
  1. 26 25
      engine/core/Log.cpp
  2. 3 3
      engine/core/Log.h

+ 26 - 25
engine/core/Log.cpp

@@ -25,13 +25,18 @@ OTHER DEALINGS IN THE SOFTWARE.
 */
 
 #include "Log.h"
-#include "OS.h"
+#include "LinearAllocator.h"
+#include "StringStream.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;
-int32_t Log::m_indent_count = 0;
 
 //-----------------------------------------------------------------------------
 LogLevel Log::threshold()
@@ -55,21 +60,18 @@ void Log::log_message(LogLevel level, const char* message, ::va_list arg)
 
 	switch (level)
 	{
-		case LL_DEBUG:
-			os::log_info(message, arg);
-			break;
-		case LL_ERROR:
-			os::log_error(message, arg);
-			break;
-		case LL_WARN:
-			os::log_warning(message, arg);
-			break;
-		case LL_INFO:
-			os::log_info(message, arg);
-			break;
-		default:
-			break;
+		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;
 }
 
 //-----------------------------------------------------------------------------
@@ -109,19 +111,18 @@ void Log::i(const char* message, ...)
 }
 
 //-----------------------------------------------------------------------------
-void Log::indent_in()
+void Log::flush(RPCServer* server)
 {
-	m_indent_count += 1;
-}
+	os::printf(g_stream.c_str());
+	::fflush(stdout);
 
-//-----------------------------------------------------------------------------
-void Log::indent_out()
-{
-	if (m_indent_count > 0)
+	if (server != NULL)
 	{
-		m_indent_count -= 1;
+		server->send_message_to_all(g_stream.c_str());
 	}
+
+	g_stream.clear();
+	g_allocator.clear();
 }
 
 } // namespace crown
-

+ 3 - 3
engine/core/Log.h

@@ -42,6 +42,8 @@ enum LogLevel
 	LL_DEBUG	= 3
 };
 
+class RPCServer;
+
 /// Used to log messages.
 class Log
 {
@@ -61,13 +63,11 @@ public:
 	static void			w(const char* message, ...);
 	static void			i(const char* message, ...);
 
-	static void			indent_in();
-	static void			indent_out();
+	static void			flush(RPCServer* server);
 
 private:
 
 	static LogLevel		m_threshold;
-	static int32_t		m_indent_count;
 };
 
 } // namespace crown