feat: Implement logging to a file

This commit is contained in:
Vsevolod Kremianskii 2020-12-11 15:35:51 +07:00
parent 176beb7e0c
commit c24955f857
5 changed files with 40 additions and 10 deletions

View file

@ -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)

View file

@ -18,11 +18,18 @@
#include "log.h"
#include <iostream>
#include <memory>
#include <boost/filesystem.hpp>
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<fs::ofstream> 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<fs::ofstream>(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

View file

@ -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

View file

@ -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) {

View file

@ -68,7 +68,8 @@ void Program::initOptions() {
("soundvol", po::value<int>()->default_value(kDefaultSoundVolume), "sound volume in percents")
("movievol", po::value<int>()->default_value(kDefaultMovieVolume), "movie volume in percents")
("port", po::value<int>()->default_value(kDefaultMultiplayerPort), "multiplayer port number")
("debug", po::value<int>()->default_value(0), "debug log level (0-3)");
("debug", po::value<int>()->default_value(0), "debug log level (0-3)")
("logfile", po::value<bool>()->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<int>();
setDebugLogLevel(vars["debug"].as<int>());
setLogToFile(vars["logfile"].as<bool>());
if (vars.count("serve") > 0) {
_multiplayerMode = MultiplayerMode::Server;