From c24955f8576abcbc14e2192eae0638d67b672e62 Mon Sep 17 00:00:00 2001 From: Vsevolod Kremianskii Date: Fri, 11 Dec 2020 15:35:51 +0700 Subject: [PATCH] feat: Implement logging to a file --- CMakeLists.txt | 2 +- src/common/log.cpp | 40 +++++++++++++++++++++++++++++++------- src/common/log.h | 1 + src/game/object/module.cpp | 3 ++- src/program.cpp | 4 +++- 5 files changed, 40 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index d9ee438c..741fce1c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -644,7 +644,7 @@ if(BUILD_TESTS) foreach(TEST_FILE ${TEST_FILES}) get_filename_component(TEST_NAME "${TEST_FILE}" NAME_WE) add_executable(test_${TEST_NAME} ${TEST_FILE}) - target_link_libraries(test_${TEST_NAME} PRIVATE libgame libscript libcommon) + target_link_libraries(test_${TEST_NAME} PRIVATE libgame libscript libcommon ${Boost_FILESYSTEM_LIBRARY}) if(WIN32) target_link_libraries(test_${TEST_NAME} PRIVATE SDL2::SDL2) diff --git a/src/common/log.cpp b/src/common/log.cpp index 5f021ea3..d3794166 100644 --- a/src/common/log.cpp +++ b/src/common/log.cpp @@ -18,11 +18,18 @@ #include "log.h" #include +#include + +#include + +namespace fs = boost::filesystem; using namespace std; namespace reone { +const char kLogFilename[] = "reone.log"; + enum class LogLevel { Error, Warn, @@ -31,6 +38,9 @@ enum class LogLevel { }; static uint32_t g_debugLevel = 0; +static bool g_logToFile = false; + +static std::unique_ptr g_logFile; inline static const char *describeLogLevel(LogLevel level) { switch (level) { @@ -52,33 +62,45 @@ static void log(ostream &out, LogLevel level, const string &s) { out << msg << endl; } +static void log(LogLevel level, const string &s) { + if (g_logToFile && !g_logFile) { + fs::path path(fs::current_path()); + path.append(kLogFilename); + + g_logFile = make_unique(path); + } + + auto &out = g_logToFile ? *g_logFile : cout; + log(out, level, s); +} + void error(const string &s) { - log(cerr, LogLevel::Error, s); + log(LogLevel::Error, s); } void error(const boost::format &s) { - log(cerr, LogLevel::Error, str(s)); + log(LogLevel::Error, str(s)); } void warn(const string &s) { - log(cout, LogLevel::Warn, s); + log(LogLevel::Warn, s); } void warn(const boost::format &s) { - log(cout, LogLevel::Warn, str(s)); + log(LogLevel::Warn, str(s)); } void info(const string &s) { - log(cout, LogLevel::Info, s); + log(LogLevel::Info, s); } void info(const boost::format &s) { - log(cout, LogLevel::Info, str(s)); + log(LogLevel::Info, str(s)); } void debug(const string &s, uint32_t level) { if (level <= getDebugLogLevel()) { - log(cout, LogLevel::Debug, s); + log(LogLevel::Debug, s); } } @@ -94,4 +116,8 @@ void setDebugLogLevel(uint32_t level) { g_debugLevel = level; } +void setLogToFile(bool logToFile) { + g_logToFile = logToFile; +} + } // namespace reone diff --git a/src/common/log.h b/src/common/log.h index ebd8c715..90026564 100644 --- a/src/common/log.h +++ b/src/common/log.h @@ -36,5 +36,6 @@ void debug(const boost::format &s, uint32_t level = 1); uint32_t getDebugLogLevel(); void setDebugLogLevel(uint32_t level); +void setLogToFile(bool logToFile); } // namespace reone diff --git a/src/game/object/module.cpp b/src/game/object/module.cpp index 32675bc2..32a4dcb2 100644 --- a/src/game/object/module.cpp +++ b/src/game/object/module.cpp @@ -49,10 +49,11 @@ void Module::load(const string &name, const GffStruct &ifo) { loadInfo(ifo); loadArea(ifo); - loadPlayer(); _area->initCameras(_info.entryPosition, _info.entryHeading); _area->runSpawnScripts(); + + loadPlayer(); } void Module::loadInfo(const GffStruct &ifo) { diff --git a/src/program.cpp b/src/program.cpp index 81658dcc..59b78caf 100644 --- a/src/program.cpp +++ b/src/program.cpp @@ -68,7 +68,8 @@ void Program::initOptions() { ("soundvol", po::value()->default_value(kDefaultSoundVolume), "sound volume in percents") ("movievol", po::value()->default_value(kDefaultMovieVolume), "movie volume in percents") ("port", po::value()->default_value(kDefaultMultiplayerPort), "multiplayer port number") - ("debug", po::value()->default_value(0), "debug log level (0-3)"); + ("debug", po::value()->default_value(0), "debug log level (0-3)") + ("logfile", po::value()->default_value(false), "log to file"); _cmdLineOpts.add(_commonOpts).add_options() ("help", "print this message") @@ -102,6 +103,7 @@ void Program::loadOptions() { _gameOpts.network.port = vars["port"].as(); setDebugLogLevel(vars["debug"].as()); + setLogToFile(vars["logfile"].as()); if (vars.count("serve") > 0) { _multiplayerMode = MultiplayerMode::Server;