From f10ba48c77b547e4767863a0d983d773ed8508ea Mon Sep 17 00:00:00 2001 From: beegee1 Date: Thu, 11 Apr 2013 15:58:01 +0000 Subject: [PATCH] - Fully integrated new logging API(ERM, Editor, missing parts) - Removed old logger --- AI/EmptyAI/CEmptyAI.cpp | 4 +- Editor/Editor.cpp | 7 +- Editor/Editor.h | 1 - Global.h | 15 ++--- client/CMT.cpp | 8 +-- lib/CConsoleHandler.cpp | 2 + lib/CConsoleHandler.h | 2 + lib/CGameState.cpp | 1 - lib/CLogger.cpp | 72 --------------------- lib/CLogger.h | 63 ------------------ lib/CMakeLists.txt | 3 +- lib/CObjectHandler.cpp | 2 - lib/Connection.cpp | 6 +- lib/Connection.h | 8 +-- lib/VCMI_Lib.cpp | 5 +- lib/VCMI_Lib.h | 2 +- lib/VCMI_lib.cbp | 2 - lib/VCMI_lib.vcxproj | 8 +-- lib/logging/CBasicLogConfigurator.cpp | 12 ++-- lib/logging/CBasicLogConfigurator.h | 2 +- lib/logging/{CNewLogger.cpp => CLogger.cpp} | 72 ++++++++++----------- lib/logging/{CNewLogger.h => CLogger.h} | 37 +++++------ server/CVCMIServer.cpp | 5 +- 23 files changed, 99 insertions(+), 240 deletions(-) delete mode 100644 lib/CLogger.cpp delete mode 100644 lib/CLogger.h rename lib/logging/{CNewLogger.cpp => CLogger.cpp} (81%) rename lib/logging/{CNewLogger.h => CLogger.h} (89%) diff --git a/AI/EmptyAI/CEmptyAI.cpp b/AI/EmptyAI/CEmptyAI.cpp index fe556ef92..629987559 100644 --- a/AI/EmptyAI/CEmptyAI.cpp +++ b/AI/EmptyAI/CEmptyAI.cpp @@ -6,7 +6,7 @@ void CEmptyAI::init(CCallback * CB) cb = CB; human=false; playerID = *cb->getMyColor(); - std::cout << "EmptyAI initialized." << std::endl; + logAi->infoStream() << "EmptyAI initialized."; } void CEmptyAI::yourTurn() { @@ -31,4 +31,4 @@ void CEmptyAI::showBlockingDialog(const std::string &text, const std::vectorselectionMade(0, queryID); -} \ No newline at end of file +} diff --git a/Editor/Editor.cpp b/Editor/Editor.cpp index be3d7244e..8987d01f8 100644 --- a/Editor/Editor.cpp +++ b/Editor/Editor.cpp @@ -6,14 +6,17 @@ #include "../lib/CGeneralTextHandler.h" #include "../lib/mapping/CMap.h" #include "../lib/mapping/CMapService.h" +#include "../lib/logging/CBasicLogConfigurator.h" Editor::Editor(QWidget *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; + CBasicLogConfigurator logConfig(VCMIDirs::get().localPath() + "/VCMI_Editor_log.txt", console); + logConfig.configureDefault(); - preinitDLL(console,logfile); + preinitDLL(console); loadDLLClasses(); VLC->generaltexth->readToVector("DATA/EDITOR", txtEditor); diff --git a/Editor/Editor.h b/Editor/Editor.h index 3f05d8b99..f87898b3f 100644 --- a/Editor/Editor.h +++ b/Editor/Editor.h @@ -22,7 +22,6 @@ private: std::vector txtEditor, txtEditorCmd; - std::ofstream * logfile; CConsoleHandler * console; Ui::EditorClass ui; diff --git a/Global.h b/Global.h index f863e2ab0..d58abad25 100644 --- a/Global.h +++ b/Global.h @@ -193,34 +193,34 @@ typedef boost::lock_guard TLockGuardRec; #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 #define HANDLE_EXCEPTION \ catch (const std::exception& e) { \ - tlog1 << e.what() << std::endl; \ + logGlobal->errorStream() << e.what(); \ throw; \ } \ catch (const std::exception * e) \ { \ - tlog1 << e->what()<< std::endl; \ + logGlobal->errorStream() << e->what(); \ throw; \ } \ catch (const std::string& e) { \ - tlog1 << e << std::endl; \ + logGlobal->errorStream() << e; \ throw; \ } #define HANDLE_EXCEPTIONC(COMMAND) \ catch (const std::exception& e) { \ COMMAND; \ - tlog1 << e.what() << std::endl; \ + logGlobal->errorStream() << e.what(); \ throw; \ } \ catch (const std::string &e) \ { \ COMMAND; \ - tlog1 << e << std::endl; \ + logGlobal->errorStream() << e; \ throw; \ } @@ -575,5 +575,4 @@ using vstd::make_unique; /* ---------------------------------------------------------------------------- */ /* VCMI headers */ /* ---------------------------------------------------------------------------- */ -#include "lib/CLogger.h" -#include "lib/logging/CNewLogger.h" +#include "lib/logging/CLogger.h" diff --git a/client/CMT.cpp b/client/CMT.cpp index 9614e4c71..5e53740cf 100644 --- a/client/CMT.cpp +++ b/client/CMT.cpp @@ -253,18 +253,17 @@ int main(int argc, char** argv) // Init old logging system and new (temporary) logging system CStopWatch total, pomtime; std::cout.flags(std::ios::unitbuf); - logfile = new std::ofstream((VCMIDirs::get().localPath() + "/VCMI_Client_log.txt").c_str()); console = new CConsoleHandler; *console->cb = boost::bind(&processCommand, _1); console->start(); atexit(dispose); - CBasicLogConfigurator logConfig(VCMIDirs::get().localPath() + "/VCMI_Client_log2.txt", console); + CBasicLogConfigurator logConfig(VCMIDirs::get().localPath() + "/VCMI_Client_log.txt", console); logConfig.configureDefault(); - logGlobal->infoStream() <<"Creating console and logfile: "<infoStream() <<"Creating console "<getTile(pos).extTileFlags |= 128; } } - //std::cout<<"\tRandomizing objects: "<debugStream() << "\tCreating player entries in gs"; diff --git a/lib/CLogger.cpp b/lib/CLogger.cpp deleted file mode 100644 index 317a0cce7..000000000 --- a/lib/CLogger.cpp +++ /dev/null @@ -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; -} \ No newline at end of file diff --git a/lib/CLogger.h b/lib/CLogger.h deleted file mode 100644 index c096a008b..000000000 --- a/lib/CLogger.h +++ /dev/null @@ -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 - 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(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 diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 48c64f81f..1f7e409bc 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -14,7 +14,7 @@ set(lib_SRCS filesystem/CFileInputStream.cpp filesystem/CCompressedStream.cpp logging/CBasicLogConfigurator.cpp - logging/CNewLogger.cpp + logging/CLogger.cpp mapping/CCampaignHandler.cpp mapping/CMap.cpp mapping/CMapEditManager.cpp @@ -39,7 +39,6 @@ set(lib_SRCS CGameState.cpp CGeneralTextHandler.cpp CHeroHandler.cpp - CLogger.cpp CModHandler.cpp CObjectHandler.cpp CObstacleInstance.cpp diff --git a/lib/CObjectHandler.cpp b/lib/CObjectHandler.cpp index c174b52da..a61eb15ef 100644 --- a/lib/CObjectHandler.cpp +++ b/lib/CObjectHandler.cpp @@ -270,7 +270,6 @@ PlayerColor CGObjectInstance::getOwner() const CGObjectInstance::CGObjectInstance() { pos = int3(-1,-1,-1); - //std::cout << "Tworze obiekt "<debugStream() << "CConnection"; 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"; 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"; if(!!sfile && *sfile) diff --git a/lib/Connection.h b/lib/Connection.h index 04ad656cb..9bdf96ef9 100644 --- a/lib/Connection.h +++ b/lib/Connection.h @@ -333,7 +333,7 @@ public: CSerializer(); ~CSerializer(); - virtual void reportState(CGLogger * out){}; + virtual void reportState(CLogger * out){}; template void registerVectoredType(const std::vector *Vector, const U T::*IdPtr) @@ -1211,7 +1211,7 @@ public: void openNextFile(const std::string &fname); //throws! void clear(); - void reportState(CGLogger * out); + void reportState(CLogger * out); void putMagicBytes(const std::string &text); }; @@ -1234,7 +1234,7 @@ public: void openNextFile(const std::string &fname, int minimalVersion); //throws! void clear(); - void reportState(CGLogger * out); + void reportState(CLogger * out); void checkMagicBytes(const std::string &text); }; @@ -1263,7 +1263,7 @@ class DLL_LINKAGE CConnection CConnection(void); void init(); - void reportState(CGLogger * out); + void reportState(CLogger * out); public: boost::mutex *rmx, *wmx; // read/write mutexes TSocket * socket; diff --git a/lib/VCMI_Lib.cpp b/lib/VCMI_Lib.cpp index 29fb80366..ff8cd20c6 100644 --- a/lib/VCMI_Lib.cpp +++ b/lib/VCMI_Lib.cpp @@ -26,14 +26,13 @@ #include "CStopWatch.h" #include "VCMIDirs.h" #include "filesystem/CResourceLoader.h" - +#include "CConsoleHandler.h" LibClasses * VLC = NULL; -DLL_LINKAGE void preinitDLL(CConsoleHandler *Console, std::ostream *Logfile) +DLL_LINKAGE void preinitDLL(CConsoleHandler *Console) { console = Console; - logfile = Logfile; VLC = new LibClasses; try { diff --git a/lib/VCMI_Lib.h b/lib/VCMI_Lib.h index a36cfb953..30c58e7f9 100644 --- a/lib/VCMI_Lib.h +++ b/lib/VCMI_Lib.h @@ -67,6 +67,6 @@ public: extern DLL_LINKAGE LibClasses * VLC; -DLL_LINKAGE void preinitDLL(CConsoleHandler *Console, std::ostream *Logfile); +DLL_LINKAGE void preinitDLL(CConsoleHandler *Console); DLL_LINKAGE void loadDLLClasses(); diff --git a/lib/VCMI_lib.cbp b/lib/VCMI_lib.cbp index 26704f163..4ebce19b5 100644 --- a/lib/VCMI_lib.cbp +++ b/lib/VCMI_lib.cbp @@ -97,8 +97,6 @@ - - diff --git a/lib/VCMI_lib.vcxproj b/lib/VCMI_lib.vcxproj index ec648397d..1512a3666 100644 --- a/lib/VCMI_lib.vcxproj +++ b/lib/VCMI_lib.vcxproj @@ -178,7 +178,6 @@ - @@ -204,7 +203,7 @@ - + @@ -242,7 +241,6 @@ - @@ -274,7 +272,7 @@ - + @@ -296,4 +294,4 @@ - \ No newline at end of file + diff --git a/lib/logging/CBasicLogConfigurator.cpp b/lib/logging/CBasicLogConfigurator.cpp index 3ba386042..6bed85862 100644 --- a/lib/logging/CBasicLogConfigurator.cpp +++ b/lib/logging/CBasicLogConfigurator.cpp @@ -10,8 +10,8 @@ CBasicLogConfigurator::CBasicLogConfigurator(const std::string & filePath, CCons void CBasicLogConfigurator::configureDefault() { - CGLogger::getGlobalLogger()->addTarget(make_unique(console)); - CGLogger::getGlobalLogger()->addTarget(make_unique(filePath, appendToLogFile)); + CLogger::getGlobalLogger()->addTarget(make_unique(console)); + CLogger::getGlobalLogger()->addTarget(make_unique(filePath, appendToLogFile)); appendToLogFile = true; } @@ -30,13 +30,13 @@ void CBasicLogConfigurator::configure() { // Get logger std::string name = loggerNode["domain"].String(); - CGLogger * logger = CGLogger::getLogger(CLoggerDomain(name)); + CLogger * logger = CLogger::getLogger(CLoggerDomain(name)); // Set log level logger->setLevel(getLogLevel(loggerNode["level"].String())); } } - CGLogger::getGlobalLogger()->clearTargets(); + CLogger::getGlobalLogger()->clearTargets(); // Add console target auto consoleTarget = make_unique(console); @@ -64,7 +64,7 @@ void CBasicLogConfigurator::configure() } consoleTarget->setColorMapping(colorMapping); } - CGLogger::getGlobalLogger()->addTarget(std::move(consoleTarget)); + CLogger::getGlobalLogger()->addTarget(std::move(consoleTarget)); // Add file target auto fileTarget = make_unique(filePath, appendToLogFile); @@ -74,7 +74,7 @@ void CBasicLogConfigurator::configure() const JsonNode & fileFormatNode = fileNode["format"]; if(!fileFormatNode.isNull()) fileTarget->setFormatter(CLogFormatter(fileFormatNode.String())); } - CGLogger::getGlobalLogger()->addTarget(std::move(fileTarget)); + CLogger::getGlobalLogger()->addTarget(std::move(fileTarget)); appendToLogFile = true; } catch(const std::exception & e) diff --git a/lib/logging/CBasicLogConfigurator.h b/lib/logging/CBasicLogConfigurator.h index 56af2234e..d92e0144e 100644 --- a/lib/logging/CBasicLogConfigurator.h +++ b/lib/logging/CBasicLogConfigurator.h @@ -11,7 +11,7 @@ #pragma once -#include "../CLogger.h" +#include "../CConsoleHandler.h" class CConsoleHandler; class JsonNode; diff --git a/lib/logging/CNewLogger.cpp b/lib/logging/CLogger.cpp similarity index 81% rename from lib/logging/CNewLogger.cpp rename to lib/logging/CLogger.cpp index 3c0ab082e..7cd56e156 100644 --- a/lib/logging/CNewLogger.cpp +++ b/lib/logging/CLogger.cpp @@ -1,5 +1,5 @@ #include "StdInc.h" -#include "CNewLogger.h" +#include "CLogger.h" const std::string CLoggerDomain::DOMAIN_GLOBAL = "global"; @@ -33,7 +33,7 @@ std::string CLoggerDomain::getName() const 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 _(smx); - CGLogger * logger = CLogManager::get().getLogger(domain); + CLogger * logger = CLogManager::get().getLogger(domain); if(logger) { return logger; } else { - logger = new CGLogger(domain); + logger = new CLogger(domain); if(domain.isGlobalDomain()) { 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)); } -CGLogger::CGLogger(const CLoggerDomain & domain) : domain(domain) +CLogger::CLogger(const CLoggerDomain & domain) : domain(domain) { 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); } -CLoggerStream CGLogger::traceStream() const +CLoggerStream CLogger::traceStream() const { 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); } -CLoggerStream CGLogger::debugStream() const +CLoggerStream CLogger::debugStream() const { 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); } -CLoggerStream CGLogger::infoStream() const +CLoggerStream CLogger::infoStream() const { 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); } -CLoggerStream CGLogger::warnStream() const +CLoggerStream CLogger::warnStream() const { 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); } -CLoggerStream CGLogger::errorStream() const +CLoggerStream CLogger::errorStream() const { 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) { @@ -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); return level; } -void CGLogger::setLevel(ELogLevel::ELogLevel level) +void CLogger::setLevel(ELogLevel::ELogLevel level) { TLockGuard _(mx); if(domain.isGlobalDomain() && level == ELogLevel::NOT_SET) return; this->level = level; } -const CLoggerDomain & CGLogger::getDomain() const +const CLoggerDomain & CLogger::getDomain() const { return domain; } -void CGLogger::addTarget(unique_ptr && target) +void CLogger::addTarget(unique_ptr && target) { TLockGuard _(mx); 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(); } @@ -191,10 +191,10 @@ ELogLevel::ELogLevel CGLogger::getEffectiveLevel() const return ELogLevel::INFO; } -void CGLogger::callTargets(const LogRecord & record) const +void CLogger::callTargets(const LogRecord & record) const { 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) { @@ -203,18 +203,18 @@ void CGLogger::callTargets(const LogRecord & record) const } } -void CGLogger::clearTargets() +void CLogger::clearTargets() { TLockGuard _(mx); targets.clear(); } -bool CGLogger::isDebugEnabled() const +bool CLogger::isDebugEnabled() const { return getEffectiveLevel() <= ELogLevel::DEBUG; } -bool CGLogger::isTraceEnabled() const +bool CLogger::isTraceEnabled() const { return getEffectiveLevel() <= ELogLevel::TRACE; } @@ -241,13 +241,13 @@ CLogManager::~CLogManager() } } -void CLogManager::addLogger(CGLogger * logger) +void CLogManager::addLogger(CLogger * logger) { TLockGuard _(mx); loggers[logger->getDomain().getName()] = logger; } -CGLogger * CLogManager::getLogger(const CLoggerDomain & domain) +CLogger * CLogManager::getLogger(const CLoggerDomain & domain) { TLockGuard _(mx); auto it = loggers.find(domain.getName()); diff --git a/lib/logging/CNewLogger.h b/lib/logging/CLogger.h similarity index 89% rename from lib/logging/CNewLogger.h rename to lib/logging/CLogger.h index aaf1ff29e..709282085 100644 --- a/lib/logging/CNewLogger.h +++ b/lib/logging/CLogger.h @@ -11,7 +11,9 @@ #pragma once -class CGLogger; +#include "../CConsoleHandler.h" + +class CLogger; struct LogRecord; class ILogTarget; @@ -50,7 +52,7 @@ private: class DLL_LINKAGE CLoggerStream { public: - CLoggerStream(const CGLogger & logger, ELogLevel::ELogLevel level); + CLoggerStream(const CLogger & logger, ELogLevel::ELogLevel level); ~CLoggerStream(); template @@ -62,14 +64,13 @@ public: } private: - const CGLogger & logger; + const CLogger & logger; ELogLevel::ELogLevel level; std::stringstream * sbuffer; }; -/// The class CGLogger 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 CGLogger +/// The class CLogger is used to log messages to certain targets of a specific domain/name. +class DLL_LINKAGE CLogger { public: inline ELogLevel::ELogLevel getLevel() const; @@ -77,8 +78,8 @@ public: const CLoggerDomain & getDomain() const; /// Logger access methods - static CGLogger * getLogger(const CLoggerDomain & domain); - static CGLogger * getGlobalLogger(); + static CLogger * getLogger(const CLoggerDomain & domain); + static CLogger * getGlobalLogger(); /// Log methods for various log levels void trace(const std::string & message) const; @@ -107,23 +108,23 @@ public: bool isTraceEnabled() const; private: - explicit CGLogger(const CLoggerDomain & domain); - CGLogger * getParent() const; + explicit CLogger(const CLoggerDomain & domain); + CLogger * getParent() const; inline ELogLevel::ELogLevel getEffectiveLevel() const; /// Returns the log level applied on this logger whether directly or indirectly. inline void callTargets(const LogRecord & record) const; CLoggerDomain domain; - CGLogger * parent; + CLogger * parent; ELogLevel::ELogLevel level; std::list > targets; mutable boost::mutex mx; static boost::recursive_mutex smx; }; -extern DLL_LINKAGE CGLogger * logGlobal; -extern DLL_LINKAGE CGLogger * logBonus; -extern DLL_LINKAGE CGLogger * logNetwork; -extern DLL_LINKAGE CGLogger * logAi; +extern DLL_LINKAGE CLogger * logGlobal; +extern DLL_LINKAGE CLogger * logBonus; +extern DLL_LINKAGE CLogger * logNetwork; +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 /// 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: static CLogManager & get(); - void addLogger(CGLogger * logger); - CGLogger * getLogger(const CLoggerDomain & domain); /// Returns a logger or nullptr if no one is registered for the given domain. + void addLogger(CLogger * logger); + CLogger * getLogger(const CLoggerDomain & domain); /// Returns a logger or nullptr if no one is registered for the given domain. private: CLogManager(); ~CLogManager(); - std::map loggers; + std::map loggers; mutable boost::mutex mx; static boost::recursive_mutex smx; }; diff --git a/server/CVCMIServer.cpp b/server/CVCMIServer.cpp index f8ac510fe..84b50c281 100644 --- a/server/CVCMIServer.cpp +++ b/server/CVCMIServer.cpp @@ -507,9 +507,8 @@ int _tmain(int argc, _TCHAR* argv[]) int main(int argc, char** argv) #endif { - logfile = new std::ofstream((VCMIDirs::get().localPath() + "/VCMI_Server_log.txt").c_str()); 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(); //boost::thread t(boost::bind(&CConsoleHandler::run,::console)); if(argc > 1) @@ -520,7 +519,7 @@ int main(int argc, char** argv) port = _ttoi(argv[1]); #endif } - preinitDLL(console,logfile); + preinitDLL(console); settings.init(); logConfig.configure();