1
0
mirror of https://github.com/vcmi/vcmi.git synced 2024-12-24 22:14:36 +02:00

- Fully integrated new logging API(ERM, Editor, missing parts) - Removed old logger

This commit is contained in:
beegee1 2013-04-11 15:58:01 +00:00
parent 928dc23003
commit f10ba48c77
23 changed files with 99 additions and 240 deletions

View File

@ -6,7 +6,7 @@ void CEmptyAI::init(CCallback * CB)
cb = CB; cb = CB;
human=false; human=false;
playerID = *cb->getMyColor(); playerID = *cb->getMyColor();
std::cout << "EmptyAI initialized." << std::endl; logAi->infoStream() << "EmptyAI initialized.";
} }
void CEmptyAI::yourTurn() void CEmptyAI::yourTurn()
{ {
@ -31,4 +31,4 @@ void CEmptyAI::showBlockingDialog(const std::string &text, const std::vector<Com
void CEmptyAI::showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, int queryID) void CEmptyAI::showGarrisonDialog(const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits, int queryID)
{ {
cb->selectionMade(0, queryID); cb->selectionMade(0, queryID);
} }

View File

@ -6,14 +6,17 @@
#include "../lib/CGeneralTextHandler.h" #include "../lib/CGeneralTextHandler.h"
#include "../lib/mapping/CMap.h" #include "../lib/mapping/CMap.h"
#include "../lib/mapping/CMapService.h" #include "../lib/mapping/CMapService.h"
#include "../lib/logging/CBasicLogConfigurator.h"
Editor::Editor(QWidget *parent) Editor::Editor(QWidget *parent)
: QMainWindow(parent) : QMainWindow(parent)
{ {
logfile = new std::ofstream((VCMIDirs::get().localPath() + "/VCMI_Editor_log.txt").c_str()); // Setup default logging(enough for now)
console = new CConsoleHandler; console = new CConsoleHandler;
CBasicLogConfigurator logConfig(VCMIDirs::get().localPath() + "/VCMI_Editor_log.txt", console);
logConfig.configureDefault();
preinitDLL(console,logfile); preinitDLL(console);
loadDLLClasses(); loadDLLClasses();
VLC->generaltexth->readToVector("DATA/EDITOR", txtEditor); VLC->generaltexth->readToVector("DATA/EDITOR", txtEditor);

View File

@ -22,7 +22,6 @@ private:
std::vector<std::string> txtEditor, txtEditorCmd; std::vector<std::string> txtEditor, txtEditorCmd;
std::ofstream * logfile;
CConsoleHandler * console; CConsoleHandler * console;
Ui::EditorClass ui; Ui::EditorClass ui;

View File

@ -193,34 +193,34 @@ typedef boost::lock_guard<boost::recursive_mutex> TLockGuardRec;
#define THROW_FORMAT(message, formatting_elems) throw std::runtime_error(boost::str(boost::format(message) % formatting_elems)) #define THROW_FORMAT(message, formatting_elems) throw std::runtime_error(boost::str(boost::format(message) % formatting_elems))
#define ASSERT_IF_CALLED_WITH_PLAYER if(!player) {tlog1 << __FUNCTION__ << "\n"; assert(0);} #define ASSERT_IF_CALLED_WITH_PLAYER if(!player) {logGlobal->errorStream() << BOOST_CURRENT_FUNCTION; assert(0);}
//XXX pls dont - 'debug macros' are usually more trouble than it's worth //XXX pls dont - 'debug macros' are usually more trouble than it's worth
#define HANDLE_EXCEPTION \ #define HANDLE_EXCEPTION \
catch (const std::exception& e) { \ catch (const std::exception& e) { \
tlog1 << e.what() << std::endl; \ logGlobal->errorStream() << e.what(); \
throw; \ throw; \
} \ } \
catch (const std::exception * e) \ catch (const std::exception * e) \
{ \ { \
tlog1 << e->what()<< std::endl; \ logGlobal->errorStream() << e->what(); \
throw; \ throw; \
} \ } \
catch (const std::string& e) { \ catch (const std::string& e) { \
tlog1 << e << std::endl; \ logGlobal->errorStream() << e; \
throw; \ throw; \
} }
#define HANDLE_EXCEPTIONC(COMMAND) \ #define HANDLE_EXCEPTIONC(COMMAND) \
catch (const std::exception& e) { \ catch (const std::exception& e) { \
COMMAND; \ COMMAND; \
tlog1 << e.what() << std::endl; \ logGlobal->errorStream() << e.what(); \
throw; \ throw; \
} \ } \
catch (const std::string &e) \ catch (const std::string &e) \
{ \ { \
COMMAND; \ COMMAND; \
tlog1 << e << std::endl; \ logGlobal->errorStream() << e; \
throw; \ throw; \
} }
@ -575,5 +575,4 @@ using vstd::make_unique;
/* ---------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------- */
/* VCMI headers */ /* VCMI headers */
/* ---------------------------------------------------------------------------- */ /* ---------------------------------------------------------------------------- */
#include "lib/CLogger.h" #include "lib/logging/CLogger.h"
#include "lib/logging/CNewLogger.h"

View File

@ -253,18 +253,17 @@ int main(int argc, char** argv)
// Init old logging system and new (temporary) logging system // Init old logging system and new (temporary) logging system
CStopWatch total, pomtime; CStopWatch total, pomtime;
std::cout.flags(std::ios::unitbuf); std::cout.flags(std::ios::unitbuf);
logfile = new std::ofstream((VCMIDirs::get().localPath() + "/VCMI_Client_log.txt").c_str());
console = new CConsoleHandler; console = new CConsoleHandler;
*console->cb = boost::bind(&processCommand, _1); *console->cb = boost::bind(&processCommand, _1);
console->start(); console->start();
atexit(dispose); atexit(dispose);
CBasicLogConfigurator logConfig(VCMIDirs::get().localPath() + "/VCMI_Client_log2.txt", console); CBasicLogConfigurator logConfig(VCMIDirs::get().localPath() + "/VCMI_Client_log.txt", console);
logConfig.configureDefault(); logConfig.configureDefault();
logGlobal->infoStream() <<"Creating console and logfile: "<<pomtime.getDiff(); logGlobal->infoStream() <<"Creating console "<<pomtime.getDiff();
// Init filesystem and settings // Init filesystem and settings
preinitDLL(::console, logfile); preinitDLL(::console);
settings.init(); settings.init();
// Initialize logging based on settings // Initialize logging based on settings
@ -655,7 +654,6 @@ void dispose()
{ {
if (console) if (console)
delete console; delete console;
delete logfile;
} }
//used only once during initialization //used only once during initialization

View File

@ -15,6 +15,8 @@
boost::mutex CConsoleHandler::smx; boost::mutex CConsoleHandler::smx;
DLL_LINKAGE CConsoleHandler * console = nullptr;
#ifndef _WIN32 #ifndef _WIN32
typedef std::string TColor; typedef std::string TColor;
#define CONSOLE_GREEN "\x1b[1;32m" #define CONSOLE_GREEN "\x1b[1;32m"

View File

@ -91,3 +91,5 @@ private:
boost::thread * thread; boost::thread * thread;
}; };
extern DLL_LINKAGE CConsoleHandler * console;

View File

@ -1025,7 +1025,6 @@ void CGameState::init(StartInfo * si)
map->getTile(pos).extTileFlags |= 128; map->getTile(pos).extTileFlags |= 128;
} }
} }
//std::cout<<"\tRandomizing objects: "<<th.getDif()<<std::endl;
/*********creating players entries in gs****************************************/ /*********creating players entries in gs****************************************/
logGlobal->debugStream() << "\tCreating player entries in gs"; logGlobal->debugStream() << "\tCreating player entries in gs";

View File

@ -1,72 +0,0 @@
#include "StdInc.h"
#include "CLogger.h"
/*
* CLogger.cpp, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
// Console, file definitions
DLL_LINKAGE CConsoleHandler *console = NULL;
DLL_LINKAGE std::ostream *logfile = NULL;
// CLogger definitions
DLL_LINKAGE CLogger tlog0(0);
DLL_LINKAGE CLogger tlog1(1);
DLL_LINKAGE CLogger tlog2(2);
DLL_LINKAGE CLogger tlog3(3);
DLL_LINKAGE CLogger tlog4(4);
DLL_LINKAGE CLogger tlog5(5);
DLL_LINKAGE CLogger tlog6(-2);
// Logging level settings
const int CLogger::CONSOLE_LOGGING_LEVEL = 5;
const int CLogger::FILE_LOGGING_LEVEL = 6;
CLogger::CLogger(const int Lvl) : lvl(Lvl)
{
#ifdef ANDROID
androidloglevel = ANDROID_LOG_INFO;
switch(lvl) {
case 0: androidloglevel = ANDROID_LOG_INFO; break;
case 1: androidloglevel = ANDROID_LOG_FATAL; break;
case 2: androidloglevel = ANDROID_LOG_ERROR; break;
case 3: androidloglevel = ANDROID_LOG_WARN; break;
case 4: androidloglevel = ANDROID_LOG_INFO; break;
case 5: androidloglevel = ANDROID_LOG_DEBUG; break;
case 6: case -2: androidloglevel = ANDROID_LOG_VERBOSE; break;
}
#endif
}
#ifdef ANDROID
void CLogger::outputAndroid()
{
int pos = buf.str().find("\n");
while( pos >= 0 )
{
__android_log_print(androidloglevel, "VCMI", "%s", buf.str().substr(0, pos).c_str() );
buf.str( buf.str().substr(pos+1) );
pos = buf.str().find("\n");
}
}
#endif
CLogger& CLogger::operator<<(std::ostream& (*fun)(std::ostream&))
{
#ifdef ANDROID
buf << fun;
outputAndroid();
#else
if(lvl < CLogger::CONSOLE_LOGGING_LEVEL)
std::cout << fun;
if((lvl < CLogger::FILE_LOGGING_LEVEL) && logfile)
*logfile << fun;
#endif
return *this;
}

View File

@ -1,63 +0,0 @@
#pragma once
#include "CConsoleHandler.h"
/*
* CLogger.h, part of VCMI engine
*
* Authors: listed in file AUTHORS in main folder
*
* License: GNU General Public License v2.0 or later
* Full text of license available in license.txt file, in main folder
*
*/
extern DLL_LINKAGE std::ostream *logfile;
extern DLL_LINKAGE CConsoleHandler *console;
// CLogger, prints log info to console and saves in file
class DLL_LINKAGE CLogger
{
const int lvl;
#ifdef ANDROID
std::ostringstream buf;
int androidloglevel;
void outputAndroid();
#endif
public:
static const int CONSOLE_LOGGING_LEVEL;
static const int FILE_LOGGING_LEVEL;
CLogger& operator<<(std::ostream& (*fun)(std::ostream&));
template<typename T>
CLogger & operator<<(const T & data)
{
#ifdef ANDROID
buf << data;
outputAndroid();
#else
if(lvl < CLogger::CONSOLE_LOGGING_LEVEL)
{
if(console)
console->print(data, false, static_cast<EConsoleTextColor::EConsoleTextColor>(lvl));
else
std::cout << data << std::flush;
}
if((lvl < CLogger::FILE_LOGGING_LEVEL) && logfile)
*logfile << data << std::flush;
#endif
return *this;
}
CLogger(const int Lvl);
};
extern DLL_LINKAGE CLogger tlog0; //green - standard progress info
extern DLL_LINKAGE CLogger tlog1; //red - big errors
extern DLL_LINKAGE CLogger tlog2; //magenta - major warnings
extern DLL_LINKAGE CLogger tlog3; //yellow - minor warnings
extern DLL_LINKAGE CLogger tlog4; //white - detailed log info
extern DLL_LINKAGE CLogger tlog5; //gray - minor log info
extern DLL_LINKAGE CLogger tlog6; //teal - AI info

View File

@ -14,7 +14,7 @@ set(lib_SRCS
filesystem/CFileInputStream.cpp filesystem/CFileInputStream.cpp
filesystem/CCompressedStream.cpp filesystem/CCompressedStream.cpp
logging/CBasicLogConfigurator.cpp logging/CBasicLogConfigurator.cpp
logging/CNewLogger.cpp logging/CLogger.cpp
mapping/CCampaignHandler.cpp mapping/CCampaignHandler.cpp
mapping/CMap.cpp mapping/CMap.cpp
mapping/CMapEditManager.cpp mapping/CMapEditManager.cpp
@ -39,7 +39,6 @@ set(lib_SRCS
CGameState.cpp CGameState.cpp
CGeneralTextHandler.cpp CGeneralTextHandler.cpp
CHeroHandler.cpp CHeroHandler.cpp
CLogger.cpp
CModHandler.cpp CModHandler.cpp
CObjectHandler.cpp CObjectHandler.cpp
CObstacleInstance.cpp CObstacleInstance.cpp

View File

@ -270,7 +270,6 @@ PlayerColor CGObjectInstance::getOwner() const
CGObjectInstance::CGObjectInstance() CGObjectInstance::CGObjectInstance()
{ {
pos = int3(-1,-1,-1); pos = int3(-1,-1,-1);
//std::cout << "Tworze obiekt "<<this<<std::endl;
//state = new CLuaObjectScript(); //state = new CLuaObjectScript();
ID = Obj::NO_OBJ; ID = Obj::NO_OBJ;
subID = -1; subID = -1;
@ -280,7 +279,6 @@ CGObjectInstance::CGObjectInstance()
} }
CGObjectInstance::~CGObjectInstance() CGObjectInstance::~CGObjectInstance()
{ {
//std::cout << "Usuwam obiekt "<<this<<std::endl;
//if (state) //if (state)
// delete state; // delete state;
//state=NULL; //state=NULL;

View File

@ -208,7 +208,7 @@ bool CConnection::isOpen() const
return socket && connected; return socket && connected;
} }
void CConnection::reportState(CGLogger * out) void CConnection::reportState(CLogger * out)
{ {
out->debugStream() << "CConnection"; out->debugStream() << "CConnection";
if(socket && socket->is_open()) if(socket && socket->is_open())
@ -315,7 +315,7 @@ void CSaveFile::openNextFile(const std::string &fname)
} }
} }
void CSaveFile::reportState(CGLogger * out) void CSaveFile::reportState(CLogger * out)
{ {
out->debugStream() << "CSaveFile"; out->debugStream() << "CSaveFile";
if(sfile.get() && *sfile) if(sfile.get() && *sfile)
@ -399,7 +399,7 @@ void CLoadFile::openNextFile(const std::string &fname, int minimalVersion)
} }
} }
void CLoadFile::reportState(CGLogger * out) void CLoadFile::reportState(CLogger * out)
{ {
out->debugStream() << "CLoadFile"; out->debugStream() << "CLoadFile";
if(!!sfile && *sfile) if(!!sfile && *sfile)

View File

@ -333,7 +333,7 @@ public:
CSerializer(); CSerializer();
~CSerializer(); ~CSerializer();
virtual void reportState(CGLogger * out){}; virtual void reportState(CLogger * out){};
template <typename T, typename U> template <typename T, typename U>
void registerVectoredType(const std::vector<T*> *Vector, const U T::*IdPtr) void registerVectoredType(const std::vector<T*> *Vector, const U T::*IdPtr)
@ -1211,7 +1211,7 @@ public:
void openNextFile(const std::string &fname); //throws! void openNextFile(const std::string &fname); //throws!
void clear(); void clear();
void reportState(CGLogger * out); void reportState(CLogger * out);
void putMagicBytes(const std::string &text); void putMagicBytes(const std::string &text);
}; };
@ -1234,7 +1234,7 @@ public:
void openNextFile(const std::string &fname, int minimalVersion); //throws! void openNextFile(const std::string &fname, int minimalVersion); //throws!
void clear(); void clear();
void reportState(CGLogger * out); void reportState(CLogger * out);
void checkMagicBytes(const std::string &text); void checkMagicBytes(const std::string &text);
}; };
@ -1263,7 +1263,7 @@ class DLL_LINKAGE CConnection
CConnection(void); CConnection(void);
void init(); void init();
void reportState(CGLogger * out); void reportState(CLogger * out);
public: public:
boost::mutex *rmx, *wmx; // read/write mutexes boost::mutex *rmx, *wmx; // read/write mutexes
TSocket * socket; TSocket * socket;

View File

@ -26,14 +26,13 @@
#include "CStopWatch.h" #include "CStopWatch.h"
#include "VCMIDirs.h" #include "VCMIDirs.h"
#include "filesystem/CResourceLoader.h" #include "filesystem/CResourceLoader.h"
#include "CConsoleHandler.h"
LibClasses * VLC = NULL; LibClasses * VLC = NULL;
DLL_LINKAGE void preinitDLL(CConsoleHandler *Console, std::ostream *Logfile) DLL_LINKAGE void preinitDLL(CConsoleHandler *Console)
{ {
console = Console; console = Console;
logfile = Logfile;
VLC = new LibClasses; VLC = new LibClasses;
try try
{ {

View File

@ -67,6 +67,6 @@ public:
extern DLL_LINKAGE LibClasses * VLC; extern DLL_LINKAGE LibClasses * VLC;
DLL_LINKAGE void preinitDLL(CConsoleHandler *Console, std::ostream *Logfile); DLL_LINKAGE void preinitDLL(CConsoleHandler *Console);
DLL_LINKAGE void loadDLLClasses(); DLL_LINKAGE void loadDLLClasses();

View File

@ -97,8 +97,6 @@
<Unit filename="CGeneralTextHandler.h" /> <Unit filename="CGeneralTextHandler.h" />
<Unit filename="CHeroHandler.cpp" /> <Unit filename="CHeroHandler.cpp" />
<Unit filename="CHeroHandler.h" /> <Unit filename="CHeroHandler.h" />
<Unit filename="CLogger.cpp" />
<Unit filename="CLogger.h" />
<Unit filename="CModHandler.cpp" /> <Unit filename="CModHandler.cpp" />
<Unit filename="CModHandler.h" /> <Unit filename="CModHandler.h" />
<Unit filename="CObjectHandler.cpp" /> <Unit filename="CObjectHandler.cpp" />

View File

@ -178,7 +178,6 @@
<ClCompile Include="CGameState.cpp" /> <ClCompile Include="CGameState.cpp" />
<ClCompile Include="CGeneralTextHandler.cpp" /> <ClCompile Include="CGeneralTextHandler.cpp" />
<ClCompile Include="CHeroHandler.cpp" /> <ClCompile Include="CHeroHandler.cpp" />
<ClCompile Include="CLogger.cpp" />
<ClCompile Include="CModHandler.cpp" /> <ClCompile Include="CModHandler.cpp" />
<ClCompile Include="CObjectHandler.cpp" /> <ClCompile Include="CObjectHandler.cpp" />
<ClCompile Include="CObstacleInstance.cpp" /> <ClCompile Include="CObstacleInstance.cpp" />
@ -204,7 +203,7 @@
<ClCompile Include="RegisterTypes.cpp" /> <ClCompile Include="RegisterTypes.cpp" />
<ClCompile Include="rmg\CMapGenOptions.cpp" /> <ClCompile Include="rmg\CMapGenOptions.cpp" />
<ClCompile Include="rmg\CMapGenerator.cpp" /> <ClCompile Include="rmg\CMapGenerator.cpp" />
<ClCompile Include="logging\CNewLogger.cpp" /> <ClCompile Include="logging\CLogger.cpp" />
<ClCompile Include="logging\CBasicLogConfigurator.cpp" /> <ClCompile Include="logging\CBasicLogConfigurator.cpp" />
<ClCompile Include="HeroBonus.cpp" /> <ClCompile Include="HeroBonus.cpp" />
<ClCompile Include="CBattleCallback.cpp" /> <ClCompile Include="CBattleCallback.cpp" />
@ -242,7 +241,6 @@
<ClInclude Include="CGameState.h" /> <ClInclude Include="CGameState.h" />
<ClInclude Include="CGeneralTextHandler.h" /> <ClInclude Include="CGeneralTextHandler.h" />
<ClInclude Include="CHeroHandler.h" /> <ClInclude Include="CHeroHandler.h" />
<ClInclude Include="CLogger.h" />
<ClInclude Include="CModHandler.h" /> <ClInclude Include="CModHandler.h" />
<ClInclude Include="CObjectHandler.h" /> <ClInclude Include="CObjectHandler.h" />
<ClInclude Include="CObstacleInstance.h" /> <ClInclude Include="CObstacleInstance.h" />
@ -274,7 +272,7 @@
<ClInclude Include="mapping\MapFormatH3M.h" /> <ClInclude Include="mapping\MapFormatH3M.h" />
<ClInclude Include="rmg\CMapGenOptions.h" /> <ClInclude Include="rmg\CMapGenOptions.h" />
<ClInclude Include="rmg\CMapGenerator.h" /> <ClInclude Include="rmg\CMapGenerator.h" />
<ClInclude Include="logging\CNewLogger.h" /> <ClInclude Include="logging\CLogger.h" />
<ClInclude Include="logging\CBasicLogConfigurator.h" /> <ClInclude Include="logging\CBasicLogConfigurator.h" />
<ClInclude Include="GameConstants.h" /> <ClInclude Include="GameConstants.h" />
<ClInclude Include="HeroBonus.h" /> <ClInclude Include="HeroBonus.h" />
@ -296,4 +294,4 @@
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>
</Project> </Project>

View File

@ -10,8 +10,8 @@ CBasicLogConfigurator::CBasicLogConfigurator(const std::string & filePath, CCons
void CBasicLogConfigurator::configureDefault() void CBasicLogConfigurator::configureDefault()
{ {
CGLogger::getGlobalLogger()->addTarget(make_unique<CLogConsoleTarget>(console)); CLogger::getGlobalLogger()->addTarget(make_unique<CLogConsoleTarget>(console));
CGLogger::getGlobalLogger()->addTarget(make_unique<CLogFileTarget>(filePath, appendToLogFile)); CLogger::getGlobalLogger()->addTarget(make_unique<CLogFileTarget>(filePath, appendToLogFile));
appendToLogFile = true; appendToLogFile = true;
} }
@ -30,13 +30,13 @@ void CBasicLogConfigurator::configure()
{ {
// Get logger // Get logger
std::string name = loggerNode["domain"].String(); std::string name = loggerNode["domain"].String();
CGLogger * logger = CGLogger::getLogger(CLoggerDomain(name)); CLogger * logger = CLogger::getLogger(CLoggerDomain(name));
// Set log level // Set log level
logger->setLevel(getLogLevel(loggerNode["level"].String())); logger->setLevel(getLogLevel(loggerNode["level"].String()));
} }
} }
CGLogger::getGlobalLogger()->clearTargets(); CLogger::getGlobalLogger()->clearTargets();
// Add console target // Add console target
auto consoleTarget = make_unique<CLogConsoleTarget>(console); auto consoleTarget = make_unique<CLogConsoleTarget>(console);
@ -64,7 +64,7 @@ void CBasicLogConfigurator::configure()
} }
consoleTarget->setColorMapping(colorMapping); consoleTarget->setColorMapping(colorMapping);
} }
CGLogger::getGlobalLogger()->addTarget(std::move(consoleTarget)); CLogger::getGlobalLogger()->addTarget(std::move(consoleTarget));
// Add file target // Add file target
auto fileTarget = make_unique<CLogFileTarget>(filePath, appendToLogFile); auto fileTarget = make_unique<CLogFileTarget>(filePath, appendToLogFile);
@ -74,7 +74,7 @@ void CBasicLogConfigurator::configure()
const JsonNode & fileFormatNode = fileNode["format"]; const JsonNode & fileFormatNode = fileNode["format"];
if(!fileFormatNode.isNull()) fileTarget->setFormatter(CLogFormatter(fileFormatNode.String())); if(!fileFormatNode.isNull()) fileTarget->setFormatter(CLogFormatter(fileFormatNode.String()));
} }
CGLogger::getGlobalLogger()->addTarget(std::move(fileTarget)); CLogger::getGlobalLogger()->addTarget(std::move(fileTarget));
appendToLogFile = true; appendToLogFile = true;
} }
catch(const std::exception & e) catch(const std::exception & e)

View File

@ -11,7 +11,7 @@
#pragma once #pragma once
#include "../CLogger.h" #include "../CConsoleHandler.h"
class CConsoleHandler; class CConsoleHandler;
class JsonNode; class JsonNode;

View File

@ -1,5 +1,5 @@
#include "StdInc.h" #include "StdInc.h"
#include "CNewLogger.h" #include "CLogger.h"
const std::string CLoggerDomain::DOMAIN_GLOBAL = "global"; const std::string CLoggerDomain::DOMAIN_GLOBAL = "global";
@ -33,7 +33,7 @@ std::string CLoggerDomain::getName() const
return name; return name;
} }
CLoggerStream::CLoggerStream(const CGLogger & logger, ELogLevel::ELogLevel level) : logger(logger), level(level), sbuffer(nullptr) CLoggerStream::CLoggerStream(const CLogger & logger, ELogLevel::ELogLevel level) : logger(logger), level(level), sbuffer(nullptr)
{ {
} }
@ -48,28 +48,28 @@ CLoggerStream::~CLoggerStream()
} }
} }
boost::recursive_mutex CGLogger::smx; boost::recursive_mutex CLogger::smx;
CGLogger * logGlobal = CGLogger::getGlobalLogger(); DLL_LINKAGE CLogger * logGlobal = CLogger::getGlobalLogger();
CGLogger * logBonus = CGLogger::getLogger(CLoggerDomain("bonus")); DLL_LINKAGE CLogger * logBonus = CLogger::getLogger(CLoggerDomain("bonus"));
CGLogger * logNetwork = CGLogger::getLogger(CLoggerDomain("network")); DLL_LINKAGE CLogger * logNetwork = CLogger::getLogger(CLoggerDomain("network"));
CGLogger * logAi = CGLogger::getLogger(CLoggerDomain("ai")); DLL_LINKAGE CLogger * logAi = CLogger::getLogger(CLoggerDomain("ai"));
CGLogger * CGLogger::getLogger(const CLoggerDomain & domain) CLogger * CLogger::getLogger(const CLoggerDomain & domain)
{ {
boost::lock_guard<boost::recursive_mutex> _(smx); boost::lock_guard<boost::recursive_mutex> _(smx);
CGLogger * logger = CLogManager::get().getLogger(domain); CLogger * logger = CLogManager::get().getLogger(domain);
if(logger) if(logger)
{ {
return logger; return logger;
} }
else else
{ {
logger = new CGLogger(domain); logger = new CLogger(domain);
if(domain.isGlobalDomain()) if(domain.isGlobalDomain())
{ {
logger->setLevel(ELogLevel::INFO); logger->setLevel(ELogLevel::INFO);
@ -79,12 +79,12 @@ CGLogger * CGLogger::getLogger(const CLoggerDomain & domain)
} }
} }
CGLogger * CGLogger::getGlobalLogger() CLogger * CLogger::getGlobalLogger()
{ {
return getLogger(CLoggerDomain(CLoggerDomain::DOMAIN_GLOBAL)); return getLogger(CLoggerDomain(CLoggerDomain::DOMAIN_GLOBAL));
} }
CGLogger::CGLogger(const CLoggerDomain & domain) : domain(domain) CLogger::CLogger(const CLoggerDomain & domain) : domain(domain)
{ {
if(domain.isGlobalDomain()) if(domain.isGlobalDomain())
{ {
@ -98,57 +98,57 @@ CGLogger::CGLogger(const CLoggerDomain & domain) : domain(domain)
} }
} }
void CGLogger::trace(const std::string & message) const void CLogger::trace(const std::string & message) const
{ {
log(ELogLevel::TRACE, message); log(ELogLevel::TRACE, message);
} }
CLoggerStream CGLogger::traceStream() const CLoggerStream CLogger::traceStream() const
{ {
return CLoggerStream(*this, ELogLevel::TRACE); return CLoggerStream(*this, ELogLevel::TRACE);
} }
void CGLogger::debug(const std::string & message) const void CLogger::debug(const std::string & message) const
{ {
log(ELogLevel::DEBUG, message); log(ELogLevel::DEBUG, message);
} }
CLoggerStream CGLogger::debugStream() const CLoggerStream CLogger::debugStream() const
{ {
return CLoggerStream(*this, ELogLevel::DEBUG); return CLoggerStream(*this, ELogLevel::DEBUG);
} }
void CGLogger::info(const std::string & message) const void CLogger::info(const std::string & message) const
{ {
log(ELogLevel::INFO, message); log(ELogLevel::INFO, message);
} }
CLoggerStream CGLogger::infoStream() const CLoggerStream CLogger::infoStream() const
{ {
return CLoggerStream(*this, ELogLevel::INFO); return CLoggerStream(*this, ELogLevel::INFO);
} }
void CGLogger::warn(const std::string & message) const void CLogger::warn(const std::string & message) const
{ {
log(ELogLevel::WARN, message); log(ELogLevel::WARN, message);
} }
CLoggerStream CGLogger::warnStream() const CLoggerStream CLogger::warnStream() const
{ {
return CLoggerStream(*this, ELogLevel::WARN); return CLoggerStream(*this, ELogLevel::WARN);
} }
void CGLogger::error(const std::string & message) const void CLogger::error(const std::string & message) const
{ {
log(ELogLevel::ERROR, message); log(ELogLevel::ERROR, message);
} }
CLoggerStream CGLogger::errorStream() const CLoggerStream CLogger::errorStream() const
{ {
return CLoggerStream(*this, ELogLevel::ERROR); return CLoggerStream(*this, ELogLevel::ERROR);
} }
void CGLogger::log(ELogLevel::ELogLevel level, const std::string & message) const void CLogger::log(ELogLevel::ELogLevel level, const std::string & message) const
{ {
if(getEffectiveLevel() <= level) if(getEffectiveLevel() <= level)
{ {
@ -156,33 +156,33 @@ void CGLogger::log(ELogLevel::ELogLevel level, const std::string & message) cons
} }
} }
ELogLevel::ELogLevel CGLogger::getLevel() const ELogLevel::ELogLevel CLogger::getLevel() const
{ {
TLockGuard _(mx); TLockGuard _(mx);
return level; return level;
} }
void CGLogger::setLevel(ELogLevel::ELogLevel level) void CLogger::setLevel(ELogLevel::ELogLevel level)
{ {
TLockGuard _(mx); TLockGuard _(mx);
if(domain.isGlobalDomain() && level == ELogLevel::NOT_SET) return; if(domain.isGlobalDomain() && level == ELogLevel::NOT_SET) return;
this->level = level; this->level = level;
} }
const CLoggerDomain & CGLogger::getDomain() const const CLoggerDomain & CLogger::getDomain() const
{ {
return domain; return domain;
} }
void CGLogger::addTarget(unique_ptr<ILogTarget> && target) void CLogger::addTarget(unique_ptr<ILogTarget> && target)
{ {
TLockGuard _(mx); TLockGuard _(mx);
targets.push_back(std::move(target)); targets.push_back(std::move(target));
} }
ELogLevel::ELogLevel CGLogger::getEffectiveLevel() const ELogLevel::ELogLevel CLogger::getEffectiveLevel() const
{ {
for(const CGLogger * logger = this; logger != nullptr; logger = logger->parent) for(const CLogger * logger = this; logger != nullptr; logger = logger->parent)
{ {
if(logger->getLevel() != ELogLevel::NOT_SET) return logger->getLevel(); if(logger->getLevel() != ELogLevel::NOT_SET) return logger->getLevel();
} }
@ -191,10 +191,10 @@ ELogLevel::ELogLevel CGLogger::getEffectiveLevel() const
return ELogLevel::INFO; return ELogLevel::INFO;
} }
void CGLogger::callTargets(const LogRecord & record) const void CLogger::callTargets(const LogRecord & record) const
{ {
TLockGuard _(mx); TLockGuard _(mx);
for(const CGLogger * logger = this; logger != nullptr; logger = logger->parent) for(const CLogger * logger = this; logger != nullptr; logger = logger->parent)
{ {
BOOST_FOREACH(auto & target, logger->targets) BOOST_FOREACH(auto & target, logger->targets)
{ {
@ -203,18 +203,18 @@ void CGLogger::callTargets(const LogRecord & record) const
} }
} }
void CGLogger::clearTargets() void CLogger::clearTargets()
{ {
TLockGuard _(mx); TLockGuard _(mx);
targets.clear(); targets.clear();
} }
bool CGLogger::isDebugEnabled() const bool CLogger::isDebugEnabled() const
{ {
return getEffectiveLevel() <= ELogLevel::DEBUG; return getEffectiveLevel() <= ELogLevel::DEBUG;
} }
bool CGLogger::isTraceEnabled() const bool CLogger::isTraceEnabled() const
{ {
return getEffectiveLevel() <= ELogLevel::TRACE; return getEffectiveLevel() <= ELogLevel::TRACE;
} }
@ -241,13 +241,13 @@ CLogManager::~CLogManager()
} }
} }
void CLogManager::addLogger(CGLogger * logger) void CLogManager::addLogger(CLogger * logger)
{ {
TLockGuard _(mx); TLockGuard _(mx);
loggers[logger->getDomain().getName()] = logger; loggers[logger->getDomain().getName()] = logger;
} }
CGLogger * CLogManager::getLogger(const CLoggerDomain & domain) CLogger * CLogManager::getLogger(const CLoggerDomain & domain)
{ {
TLockGuard _(mx); TLockGuard _(mx);
auto it = loggers.find(domain.getName()); auto it = loggers.find(domain.getName());

View File

@ -11,7 +11,9 @@
#pragma once #pragma once
class CGLogger; #include "../CConsoleHandler.h"
class CLogger;
struct LogRecord; struct LogRecord;
class ILogTarget; class ILogTarget;
@ -50,7 +52,7 @@ private:
class DLL_LINKAGE CLoggerStream class DLL_LINKAGE CLoggerStream
{ {
public: public:
CLoggerStream(const CGLogger & logger, ELogLevel::ELogLevel level); CLoggerStream(const CLogger & logger, ELogLevel::ELogLevel level);
~CLoggerStream(); ~CLoggerStream();
template<typename T> template<typename T>
@ -62,14 +64,13 @@ public:
} }
private: private:
const CGLogger & logger; const CLogger & logger;
ELogLevel::ELogLevel level; ELogLevel::ELogLevel level;
std::stringstream * sbuffer; std::stringstream * sbuffer;
}; };
/// The class CGLogger is used to log messages to certain targets of a specific domain/name. /// The class CLogger is used to log messages to certain targets of a specific domain/name.
/// Temporary name is CGLogger, should be renamed to CLogger after refactoring. class DLL_LINKAGE CLogger
class DLL_LINKAGE CGLogger
{ {
public: public:
inline ELogLevel::ELogLevel getLevel() const; inline ELogLevel::ELogLevel getLevel() const;
@ -77,8 +78,8 @@ public:
const CLoggerDomain & getDomain() const; const CLoggerDomain & getDomain() const;
/// Logger access methods /// Logger access methods
static CGLogger * getLogger(const CLoggerDomain & domain); static CLogger * getLogger(const CLoggerDomain & domain);
static CGLogger * getGlobalLogger(); static CLogger * getGlobalLogger();
/// Log methods for various log levels /// Log methods for various log levels
void trace(const std::string & message) const; void trace(const std::string & message) const;
@ -107,23 +108,23 @@ public:
bool isTraceEnabled() const; bool isTraceEnabled() const;
private: private:
explicit CGLogger(const CLoggerDomain & domain); explicit CLogger(const CLoggerDomain & domain);
CGLogger * getParent() const; CLogger * getParent() const;
inline ELogLevel::ELogLevel getEffectiveLevel() const; /// Returns the log level applied on this logger whether directly or indirectly. inline ELogLevel::ELogLevel getEffectiveLevel() const; /// Returns the log level applied on this logger whether directly or indirectly.
inline void callTargets(const LogRecord & record) const; inline void callTargets(const LogRecord & record) const;
CLoggerDomain domain; CLoggerDomain domain;
CGLogger * parent; CLogger * parent;
ELogLevel::ELogLevel level; ELogLevel::ELogLevel level;
std::list<unique_ptr<ILogTarget> > targets; std::list<unique_ptr<ILogTarget> > targets;
mutable boost::mutex mx; mutable boost::mutex mx;
static boost::recursive_mutex smx; static boost::recursive_mutex smx;
}; };
extern DLL_LINKAGE CGLogger * logGlobal; extern DLL_LINKAGE CLogger * logGlobal;
extern DLL_LINKAGE CGLogger * logBonus; extern DLL_LINKAGE CLogger * logBonus;
extern DLL_LINKAGE CGLogger * logNetwork; extern DLL_LINKAGE CLogger * logNetwork;
extern DLL_LINKAGE CGLogger * logAi; extern DLL_LINKAGE CLogger * logAi;
/// Macros for tracing the control flow of the application conveniently. If the TRACE_BEGIN macro is used it should be the first statement in the function, whereas /// Macros for tracing the control flow of the application conveniently. If the TRACE_BEGIN macro is used it should be the first statement in the function, whereas
/// the TRACE_END should be last one before a return statement. Logging traces via this macro have almost no impact when the trace is disabled. /// the TRACE_END should be last one before a return statement. Logging traces via this macro have almost no impact when the trace is disabled.
@ -142,14 +143,14 @@ class DLL_LINKAGE CLogManager : public boost::noncopyable
public: public:
static CLogManager & get(); static CLogManager & get();
void addLogger(CGLogger * logger); void addLogger(CLogger * logger);
CGLogger * getLogger(const CLoggerDomain & domain); /// Returns a logger or nullptr if no one is registered for the given domain. CLogger * getLogger(const CLoggerDomain & domain); /// Returns a logger or nullptr if no one is registered for the given domain.
private: private:
CLogManager(); CLogManager();
~CLogManager(); ~CLogManager();
std::map<std::string, CGLogger *> loggers; std::map<std::string, CLogger *> loggers;
mutable boost::mutex mx; mutable boost::mutex mx;
static boost::recursive_mutex smx; static boost::recursive_mutex smx;
}; };

View File

@ -507,9 +507,8 @@ int _tmain(int argc, _TCHAR* argv[])
int main(int argc, char** argv) int main(int argc, char** argv)
#endif #endif
{ {
logfile = new std::ofstream((VCMIDirs::get().localPath() + "/VCMI_Server_log.txt").c_str());
console = new CConsoleHandler; console = new CConsoleHandler;
CBasicLogConfigurator logConfig(VCMIDirs::get().localPath() + "/VCMI_Server_log2.txt", console); CBasicLogConfigurator logConfig(VCMIDirs::get().localPath() + "/VCMI_Server_log.txt", console);
logConfig.configureDefault(); logConfig.configureDefault();
//boost::thread t(boost::bind(&CConsoleHandler::run,::console)); //boost::thread t(boost::bind(&CConsoleHandler::run,::console));
if(argc > 1) if(argc > 1)
@ -520,7 +519,7 @@ int main(int argc, char** argv)
port = _ttoi(argv[1]); port = _ttoi(argv[1]);
#endif #endif
} }
preinitDLL(console,logfile); preinitDLL(console);
settings.init(); settings.init();
logConfig.configure(); logConfig.configure();