1
0

First Implementatation of new Loggin framework

This commit is contained in:
Tycho 2014-08-10 19:34:11 +01:00
parent eae42d91f9
commit 806d0936dc
15 changed files with 51 additions and 398 deletions

View File

@ -1,3 +1,5 @@
macro (add_flags_lnk FLAGS)
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${FLAGS}")
set(CMAKE_EXE_LINKER_FLAGS_DEBUG "${CMAKE_EXE_LINKER_FLAGS_DEBUG} ${FLAGS}")

View File

@ -1,6 +1,6 @@
#pragma once
#include "../MCLogger.h"
#include "LogDispacher.h"
#include <time.h>
// tolua_begin

View File

@ -168,7 +168,7 @@ static AString GetLogMessage(lua_State * tolua_S)
static int tolua_LOG(lua_State * tolua_S)
{
// If the param is a cCompositeChat, read the log level from it:
cMCLogger::eLogLevel LogLevel = cMCLogger::llRegular;
Logger::eLogLevel LogLevel = Logger::llRegular;
tolua_Error err;
if (tolua_isusertype(tolua_S, 1, "cCompositeChat", false, &err))
{
@ -176,7 +176,7 @@ static int tolua_LOG(lua_State * tolua_S)
}
// Log the message:
cMCLogger::GetInstance()->LogSimple(GetLogMessage(tolua_S).c_str(), LogLevel);
Logger::GetInstance().LogSimple(GetLogMessage(tolua_S).c_str(), LogLevel);
return 0;
}
@ -186,7 +186,7 @@ static int tolua_LOG(lua_State * tolua_S)
static int tolua_LOGINFO(lua_State * tolua_S)
{
cMCLogger::GetInstance()->LogSimple(GetLogMessage(tolua_S).c_str(), cMCLogger::llInfo);
Logger::GetInstance().LogSimple(GetLogMessage(tolua_S).c_str(), Logger::llInfo);
return 0;
}
@ -196,7 +196,7 @@ static int tolua_LOGINFO(lua_State * tolua_S)
static int tolua_LOGWARN(lua_State * tolua_S)
{
cMCLogger::GetInstance()->LogSimple(GetLogMessage(tolua_S).c_str(), cMCLogger::llWarning);
Logger::GetInstance().LogSimple(GetLogMessage(tolua_S).c_str(), Logger::llWarning);
return 0;
}
@ -206,7 +206,7 @@ static int tolua_LOGWARN(lua_State * tolua_S)
static int tolua_LOGERROR(lua_State * tolua_S)
{
cMCLogger::GetInstance()->LogSimple(GetLogMessage(tolua_S).c_str(), cMCLogger::llError);
Logger::GetInstance().LogSimple(GetLogMessage(tolua_S).c_str(), Logger::llError);
return 0;
}

View File

@ -1,6 +1,7 @@
cmake_minimum_required (VERSION 2.8.2)
project (MCServer)
include_directories (SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/../lib/")
include_directories (SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/../lib/jsoncpp/include")
include_directories (SYSTEM "${CMAKE_CURRENT_SOURCE_DIR}/../lib/polarssl/include")
@ -41,8 +42,9 @@ SET (SRCS
LightingThread.cpp
LineBlockTracer.cpp
LinearInterpolation.cpp
Listeners.cpp
Log.cpp
MCLogger.cpp
LogDispacher.cpp
Map.cpp
MapManager.cpp
MobCensus.cpp
@ -108,7 +110,7 @@ SET (HDRS
LinearInterpolation.h
LinearUpscale.h
Log.h
MCLogger.h
LogDispacher.h
Map.h
MapManager.h
Matrix4.h

View File

@ -353,23 +353,23 @@ AString cCompositeChat::ExtractText(void) const
cMCLogger::eLogLevel cCompositeChat::MessageTypeToLogLevel(eMessageType a_MessageType)
Logger::eLogLevel cCompositeChat::MessageTypeToLogLevel(eMessageType a_MessageType)
{
switch (a_MessageType)
{
case mtCustom: return cMCLogger::llRegular;
case mtFailure: return cMCLogger::llWarning;
case mtInformation: return cMCLogger::llInfo;
case mtSuccess: return cMCLogger::llRegular;
case mtWarning: return cMCLogger::llWarning;
case mtFatal: return cMCLogger::llError;
case mtDeath: return cMCLogger::llRegular;
case mtPrivateMessage: return cMCLogger::llRegular;
case mtJoin: return cMCLogger::llRegular;
case mtLeave: return cMCLogger::llRegular;
case mtCustom: return Logger::llRegular;
case mtFailure: return Logger::llWarning;
case mtInformation: return Logger::llInfo;
case mtSuccess: return Logger::llRegular;
case mtWarning: return Logger::llWarning;
case mtFatal: return Logger::llError;
case mtDeath: return Logger::llRegular;
case mtPrivateMessage: return Logger::llRegular;
case mtJoin: return Logger::llRegular;
case mtLeave: return Logger::llRegular;
}
ASSERT(!"Unhandled MessageType");
return cMCLogger::llError;
return Logger::llError;
}

View File

@ -196,7 +196,7 @@ public:
/** Converts the MessageType to a LogLevel value.
Used by the logging bindings when logging a cCompositeChat object. */
static cMCLogger::eLogLevel MessageTypeToLogLevel(eMessageType a_MessageType);
static Logger::eLogLevel MessageTypeToLogLevel(eMessageType a_MessageType);
protected:
/** All the parts that */

View File

@ -249,7 +249,7 @@ template class SizeChecker<UInt16, 2>;
#include "OSSupport/Event.h"
#include "OSSupport/Thread.h"
#include "OSSupport/File.h"
#include "MCLogger.h"
#include "LogDispacher.h"
#else
// Logging functions
void inline LOGERROR(const char* a_Format, ...) FORMATSTRING(1, 2);

View File

@ -1,267 +0,0 @@
#include "Globals.h" // NOTE: MSVC stupidness requires this to be the same across all modules
#include <time.h>
#include "Log.h"
cMCLogger * cMCLogger::s_MCLogger = NULL;
#ifdef _WIN32
#include <io.h> // Needed for _isatty(), not available on Linux
HANDLE g_Console = GetStdHandle(STD_OUTPUT_HANDLE);
WORD g_DefaultConsoleAttrib = 0x07;
#elif defined (__linux) && !defined(ANDROID_NDK)
#include <unistd.h> // Needed for isatty() on Linux
#endif
cMCLogger * cMCLogger::GetInstance(void)
{
return s_MCLogger;
}
cMCLogger::cMCLogger(void):
m_ShouldColorOutput(false)
{
AString FileName;
Printf(FileName, "LOG_%d.txt", (int)time(NULL));
InitLog(FileName);
}
cMCLogger::cMCLogger(const AString & a_FileName)
{
InitLog(a_FileName);
}
cMCLogger::~cMCLogger()
{
m_Log->Log("--- Stopped Log ---\n");
delete m_Log;
m_Log = NULL;
if (this == s_MCLogger)
{
s_MCLogger = NULL;
}
}
void cMCLogger::InitLog(const AString & a_FileName)
{
m_Log = new cLog(a_FileName);
m_Log->Log("--- Started Log ---\n");
s_MCLogger = this;
#ifdef _WIN32
// See whether we are writing to a console the default console attrib:
m_ShouldColorOutput = (_isatty(_fileno(stdin)) != 0);
if (m_ShouldColorOutput)
{
CONSOLE_SCREEN_BUFFER_INFO sbi;
GetConsoleScreenBufferInfo(g_Console, &sbi);
g_DefaultConsoleAttrib = sbi.wAttributes;
}
#elif defined (__linux) && !defined(ANDROID_NDK)
m_ShouldColorOutput = isatty(fileno(stdout));
// TODO: Check if the terminal supports colors, somehow?
#endif
}
void cMCLogger::LogSimple(const char * a_Text, eLogLevel a_LogLevel)
{
switch (a_LogLevel)
{
case llRegular:
{
LOG("%s", a_Text);
break;
}
case llInfo:
{
LOGINFO("%s", a_Text);
break;
}
case llWarning:
{
LOGWARN("%s", a_Text);
break;
}
case llError:
{
LOGERROR("%s", a_Text);
break;
}
}
}
void cMCLogger::Log(const char * a_Format, va_list a_ArgList)
{
cCSLock Lock(m_CriticalSection);
SetColor(csRegular);
m_Log->Log(a_Format, a_ArgList);
ResetColor();
puts("");
}
void cMCLogger::Info(const char * a_Format, va_list a_ArgList)
{
cCSLock Lock(m_CriticalSection);
SetColor(csInfo);
m_Log->Log(a_Format, a_ArgList);
ResetColor();
puts("");
}
void cMCLogger::Warn(const char * a_Format, va_list a_ArgList)
{
cCSLock Lock(m_CriticalSection);
SetColor(csWarning);
m_Log->Log(a_Format, a_ArgList);
ResetColor();
puts("");
}
void cMCLogger::Error(const char * a_Format, va_list a_ArgList)
{
cCSLock Lock(m_CriticalSection);
SetColor(csError);
m_Log->Log(a_Format, a_ArgList);
ResetColor();
puts("");
}
void cMCLogger::SetColor(eColorScheme a_Scheme)
{
if (!m_ShouldColorOutput)
{
return;
}
#ifdef _WIN32
WORD Attrib = 0x07; // by default, gray on black
switch (a_Scheme)
{
case csRegular: Attrib = 0x07; break; // Gray on black
case csInfo: Attrib = 0x0e; break; // Yellow on black
case csWarning: Attrib = 0x0c; break; // Read on black
case csError: Attrib = 0xc0; break; // Black on red
default: ASSERT(!"Unhandled color scheme");
}
SetConsoleTextAttribute(g_Console, Attrib);
#elif defined(__linux) && !defined(ANDROID_NDK)
switch (a_Scheme)
{
case csRegular: printf("\x1b[0m"); break; // Whatever the console default is
case csInfo: printf("\x1b[33;1m"); break; // Yellow on black
case csWarning: printf("\x1b[31;1m"); break; // Red on black
case csError: printf("\x1b[1;33;41;1m"); break; // Yellow on red
default: ASSERT(!"Unhandled color scheme");
}
#endif
}
void cMCLogger::ResetColor(void)
{
if (!m_ShouldColorOutput)
{
return;
}
#ifdef _WIN32
SetConsoleTextAttribute(g_Console, g_DefaultConsoleAttrib);
#elif defined(__linux) && !defined(ANDROID_NDK)
printf("\x1b[0m");
#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);
}

View File

@ -1,95 +0,0 @@
#pragma once
class cLog;
class cMCLogger
{
public:
enum eLogLevel
{
llRegular,
llInfo,
llWarning,
llError,
};
// tolua_end
/** Creates a logger with the default filename, "logs/LOG_<timestamp>.log" */
cMCLogger(void);
/** Creates a logger with the specified filename inside "logs" folder */
cMCLogger(const AString & a_FileName);
~cMCLogger();
void Log (const char * a_Format, va_list a_ArgList) FORMATSTRING(2, 0);
void Info (const char * a_Format, va_list a_ArgList) FORMATSTRING(2, 0);
void Warn (const char * a_Format, va_list a_ArgList) FORMATSTRING(2, 0);
void Error(const char * a_Format, va_list a_ArgList) FORMATSTRING(2, 0);
/** Logs the simple text message at the specified log level. */
void LogSimple(const char * a_Text, eLogLevel a_LogLevel = llRegular);
static cMCLogger * GetInstance();
private:
enum eColorScheme
{
csRegular,
csInfo,
csWarning,
csError,
} ;
cCriticalSection m_CriticalSection;
cLog * m_Log;
static cMCLogger * s_MCLogger;
bool m_ShouldColorOutput;
/// Sets the specified color scheme in the terminal (TODO: if coloring available)
void SetColor(eColorScheme a_Scheme);
/// Resets the color back to whatever is the default in the terminal
void ResetColor(void);
/// Common initialization for all constructors, creates a logfile with the specified name and assigns s_MCLogger to this
void InitLog(const AString & a_FileName);
};
extern void LOG(const char* a_Format, ...) FORMATSTRING(1, 2);
extern void LOGINFO(const char* a_Format, ...) FORMATSTRING(1, 2);
extern void LOGWARN(const char* a_Format, ...) FORMATSTRING(1, 2);
extern void LOGERROR(const char* a_Format, ...) FORMATSTRING(1, 2);
// In debug builds, translate LOGD to LOG, otherwise leave it out altogether:
#ifdef _DEBUG
#define LOGD LOG
#else
#define LOGD(...)
#endif // _DEBUG
#define LOGWARNING LOGWARN

View File

@ -70,6 +70,7 @@ bool cFile::Open(const AString & iFileName, eMode iMode)
case fmRead: Mode = "rb"; break;
case fmWrite: Mode = "wb"; break;
case fmReadWrite: Mode = "rb+"; break;
case fmAppend: Mode = "a+"; break;
}
if (Mode == NULL)
{
@ -255,7 +256,7 @@ int cFile::ReadRestOfFile(AString & a_Contents)
return -1;
}
int DataSize = GetSize() - Tell();
size_t DataSize = GetSize() - Tell();
// HACK: This depends on the internal knowledge that AString's data() function returns the internal buffer directly
a_Contents.assign((size_t)DataSize, '\0');
@ -459,7 +460,7 @@ int cFile::Printf(const char * a_Fmt, ...)
va_start(args, a_Fmt);
AppendVPrintf(buf, a_Fmt, args);
va_end(args);
return Write(buf.c_str(), (int)buf.length());
return Write(buf.c_str(), buf.length());
}

View File

@ -62,7 +62,8 @@ public:
{
fmRead, // Read-only. If the file doesn't exist, object will not be valid
fmWrite, // Write-only. If the file already exists, it will be overwritten
fmReadWrite // Read/write. If the file already exists, it will be left intact; writing will overwrite the data from the beginning
fmReadWrite, // Read/write. If the file already exists, it will be left intact; writing will overwrite the data from the beginning
fmAppend // Write-only. If the file already exists cursor will be moved to the end of the file
} ;
/** Simple constructor - creates an unopened file object, use Open() to open / create a real file */

View File

@ -18,6 +18,7 @@
#include "CommandOutput.h"
#include "DeadlockDetect.h"
#include "OSSupport/Timer.h"
#include "Listeners.h"
#include "inifile/iniFile.h"
@ -51,7 +52,6 @@ cRoot::cRoot(void) :
m_FurnaceRecipe(NULL),
m_WebAdmin(NULL),
m_PluginManager(NULL),
m_Log(NULL),
m_bStop(false),
m_bRestart(false)
{
@ -105,10 +105,15 @@ void cRoot::Start(void)
HMENU hmenu = GetSystemMenu(hwnd, FALSE);
EnableMenuItem(hmenu, SC_CLOSE, MF_GRAYED); // Disable close button when starting up; it causes problems with our CTRL-CLOSE handling
#endif
Logger::cLoggerListener * consoleLogListener = Logger::MakeConsoleListener();
Logger::cLoggerListener * fileLogListener = new Logger::cFileListener();
Logger::GetInstance().AttachListener(consoleLogListener);
Logger::GetInstance().AttachListener(fileLogListener);
LOG("--- Started Log ---\n");
cDeadlockDetect dd;
delete m_Log;
m_Log = new cMCLogger();
m_bStop = false;
while (!m_bStop)
@ -249,8 +254,13 @@ void cRoot::Start(void)
delete m_Server; m_Server = NULL;
LOG("Shutdown successful!");
}
delete m_Log; m_Log = NULL;
LOG("--- Stopped Log ---");
Logger::GetInstance().DetachListener(consoleLogListener);
delete consoleLogListener;
Logger::GetInstance().DetachListener(fileLogListener);
delete fileLogListener;
}
@ -274,15 +284,15 @@ void cRoot::LoadWorlds(cIniFile & IniFile)
m_WorldsByName[ DefaultWorldName ] = m_pDefaultWorld;
// Then load the other worlds
unsigned int KeyNum = IniFile.FindKey("Worlds");
unsigned int NumWorlds = IniFile.GetNumValues(KeyNum);
int KeyNum = IniFile.FindKey("Worlds");
int NumWorlds = IniFile.GetNumValues(KeyNum);
if (NumWorlds <= 0)
{
return;
}
bool FoundAdditionalWorlds = false;
for (unsigned int i = 0; i < NumWorlds; i++)
for (int i = 0; i < NumWorlds; i++)
{
AString ValueName = IniFile.GetValueName(KeyNum, i);
if (ValueName.compare("World") != 0)

View File

@ -196,8 +196,6 @@ private:
cMojangAPI m_MojangAPI;
cHTTPServer m_HTTPServer;
cMCLogger * m_Log;
bool m_bStop;
bool m_bRestart;

View File

@ -12,7 +12,6 @@
typedef std::string AString;
typedef std::vector<AString> AStringVector;
typedef std::list<AString> AStringList;

View File

@ -273,6 +273,8 @@ int main( int argc, char **argv)
}
} // for i - argv[]
Logger::InitiateMultithreading();
#if !defined(ANDROID_NDK)
try
#endif