summaryrefslogtreecommitdiffstats
path: root/source/cMCLogger.cpp
diff options
context:
space:
mode:
authorfaketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6>2011-10-03 20:41:19 +0200
committerfaketruth <faketruth@0a769ca7-a7f5-676a-18bf-c427514a06d6>2011-10-03 20:41:19 +0200
commit386d58b5862d8b76925c6523721594887606e82a (patch)
treeef073e7a843f4b75a4008d4b7383f7cdf08ceee5 /source/cMCLogger.cpp
parentVisual Studio 2010 solution and project files (diff)
downloadcuberite-386d58b5862d8b76925c6523721594887606e82a.tar
cuberite-386d58b5862d8b76925c6523721594887606e82a.tar.gz
cuberite-386d58b5862d8b76925c6523721594887606e82a.tar.bz2
cuberite-386d58b5862d8b76925c6523721594887606e82a.tar.lz
cuberite-386d58b5862d8b76925c6523721594887606e82a.tar.xz
cuberite-386d58b5862d8b76925c6523721594887606e82a.tar.zst
cuberite-386d58b5862d8b76925c6523721594887606e82a.zip
Diffstat (limited to '')
-rw-r--r--source/cMCLogger.cpp155
1 files changed, 155 insertions, 0 deletions
diff --git a/source/cMCLogger.cpp b/source/cMCLogger.cpp
new file mode 100644
index 000000000..0c6d25df3
--- /dev/null
+++ b/source/cMCLogger.cpp
@@ -0,0 +1,155 @@
+#include "cMCLogger.h"
+#include "cLog.h"
+#include "cCriticalSection.h"
+
+#include <stdio.h>
+#include <cstdarg>
+#include <time.h>
+
+#ifndef _WIN32
+#define sprintf_s(buffer, buffer_size, stringbuffer, ...) (sprintf(buffer, stringbuffer, __VA_ARGS__))
+#else
+#include <Windows.h>
+#endif
+
+cMCLogger* cMCLogger::s_MCLogger = 0;
+
+cMCLogger* cMCLogger::GetInstance()
+{
+ return s_MCLogger;
+}
+
+cMCLogger::cMCLogger()
+{
+ m_CriticalSection = new cCriticalSection();
+ char c_Buffer[128];
+ sprintf_s(c_Buffer, 128, "LOG_%d.txt", (int)time(0) );
+ m_Log = new cLog(c_Buffer);
+ m_Log->Log("--- Started Log ---");
+
+ s_MCLogger = this;
+}
+
+cMCLogger::cMCLogger( char* a_File )
+{
+ m_CriticalSection = new cCriticalSection();
+ m_Log = new cLog( a_File );
+}
+
+cMCLogger::~cMCLogger()
+{
+ m_Log->Log("--- Stopped Log ---");
+ delete m_Log;
+ delete m_CriticalSection;
+ if( this == s_MCLogger )
+ s_MCLogger = 0;
+}
+
+void cMCLogger::LogSimple(const char* a_Text, int a_LogType /* = 0 */ )
+{
+ switch( a_LogType )
+ {
+ case 0:
+ Log(a_Text, 0);
+ break;
+ case 1:
+ Info(a_Text, 0);
+ break;
+ case 2:
+ Warn(a_Text, 0);
+ break;
+ case 3:
+ Error(a_Text, 0);
+ break;
+ default:
+ Log(a_Text, 0);
+ break;
+ }
+}
+
+void cMCLogger::Log(const char* a_Format, va_list a_ArgList)
+{
+ m_CriticalSection->Lock();
+ SetColor( 0x7 ); // 0x7 is default grey color
+ m_Log->Log( a_Format, a_ArgList );
+ m_CriticalSection->Unlock();
+}
+
+void cMCLogger::Info(const char* a_Format, va_list a_ArgList)
+{
+ m_CriticalSection->Lock();
+// for( int i = 0; i < 16; i++)
+// {
+// for( int j = 0; j < 16; j++ )
+// {
+// SetConsoleTextAttribute( hConsole, i | (j<<4) );
+// printf("0x%x", (i|j<<4));
+// }
+// printf("\n");
+// }
+
+ SetColor( 0xe ); // 0xe is yellow
+ m_Log->Log( a_Format, a_ArgList );
+ m_CriticalSection->Unlock();
+}
+
+void cMCLogger::Warn(const char* a_Format, va_list a_ArgList)
+{
+ m_CriticalSection->Lock();
+ SetColor( 0xc ); // 0xc is red
+ m_Log->Log( a_Format, a_ArgList );
+ m_CriticalSection->Unlock();
+}
+
+void cMCLogger::Error(const char* a_Format, va_list a_ArgList)
+{
+ m_CriticalSection->Lock();
+ SetColor( 0xc0 ); // 0xc0 is red bg and black text
+ m_Log->Log( a_Format, a_ArgList );
+ m_CriticalSection->Unlock();
+}
+
+void cMCLogger::SetColor( unsigned char a_Color )
+{
+#ifdef _WIN32
+ HANDLE hConsole = GetStdHandle( STD_OUTPUT_HANDLE );
+ SetConsoleTextAttribute( hConsole, a_Color );
+#else
+ (void)a_Color;
+#endif
+}
+
+
+//////////////////////////////////////////////////////////////////////////
+// Global functions
+void LOG(const char* a_Format, ...)
+{
+ va_list argList;
+ va_start(argList, a_Format);
+ cMCLogger::GetInstance()->Log( a_Format, argList );
+ va_end(argList);
+}
+
+void LOGINFO(const char* a_Format, ...)
+{
+ va_list argList;
+ va_start(argList, a_Format);
+ cMCLogger::GetInstance()->Info( a_Format, argList );
+ va_end(argList);
+}
+
+void LOGWARN(const char* a_Format, ...)
+{
+ va_list argList;
+ va_start(argList, a_Format);
+ cMCLogger::GetInstance()->Warn( a_Format, argList );
+ va_end(argList);
+}
+
+void LOGERROR(const char* a_Format, ...)
+{
+ va_list argList;
+ va_start(argList, a_Format);
+ cMCLogger::GetInstance()->Error( a_Format, argList );
+ va_end(argList);
+}