1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-30 19:15:41 +00:00

Redirect log only after parsing configuration

This commit is contained in:
Petr Mikheev 2022-01-13 00:20:16 +01:00
parent bb6b031afd
commit dd5ba5c57b
5 changed files with 54 additions and 41 deletions

View file

@ -5,6 +5,7 @@
#include <QLocalSocket> #include <QLocalSocket>
#include <QMessageBox> #include <QMessageBox>
#include <components/debug/debugging.hpp>
#include <components/debug/debuglog.hpp> #include <components/debug/debuglog.hpp>
#include <components/fallback/validate.hpp> #include <components/fallback/validate.hpp>
#include <components/misc/rng.hpp> #include <components/misc/rng.hpp>
@ -105,6 +106,7 @@ std::pair<Files::PathContainer, std::vector<std::string> > CS::Editor::readConfi
boost::program_options::notify(variables); boost::program_options::notify(variables);
mCfgMgr.readConfiguration(variables, desc, false); mCfgMgr.readConfiguration(variables, desc, false);
setupLogging(mCfgMgr.getLogPath().string(), "OpenMW-CS");
Fallback::Map::init(variables["fallback"].as<FallbackMap>().mMap); Fallback::Map::init(variables["fallback"].as<FallbackMap>().mMap);

View file

@ -79,5 +79,5 @@ int runApplication(int argc, char *argv[])
int main(int argc, char *argv[]) int main(int argc, char *argv[])
{ {
return wrapApplication(&runApplication, argc, argv, "OpenMW-CS"); return wrapApplication(&runApplication, argc, argv, "OpenMW-CS", false);
} }

View file

@ -65,6 +65,8 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
cfgMgr.readConfiguration(variables, desc); cfgMgr.readConfiguration(variables, desc);
Files::mergeComposingVariables(variables, composingVariables, desc); Files::mergeComposingVariables(variables, composingVariables, desc);
setupLogging(cfgMgr.getLogPath().string(), "OpenMW");
Version::Version v = Version::getOpenmwVersion(variables["resources"].as<Files::MaybeQuotedPath>().string()); Version::Version v = Version::getOpenmwVersion(variables["resources"].as<Files::MaybeQuotedPath>().string());
Log(Debug::Info) << v.describe(); Log(Debug::Info) << v.describe();
@ -230,7 +232,7 @@ extern "C" int SDL_main(int argc, char**argv)
int main(int argc, char**argv) int main(int argc, char**argv)
#endif #endif
{ {
return wrapApplication(&runApplication, argc, argv, "OpenMW"); return wrapApplication(&runApplication, argc, argv, "OpenMW", false);
} }
// Platform specific for Windows when there is no console built into the executable. // Platform specific for Windows when there is no console built into the executable.

View file

@ -137,61 +137,65 @@ namespace Debug
} }
static std::unique_ptr<std::ostream> rawStdout = nullptr; static std::unique_ptr<std::ostream> rawStdout = nullptr;
static std::unique_ptr<std::ostream> rawStderr = nullptr;
static boost::filesystem::ofstream logfile;
#if defined(_WIN32) && defined(_DEBUG)
static boost::iostreams::stream_buffer<Debug::DebugOutput> sb;
#else
static boost::iostreams::stream_buffer<Debug::Tee> coutsb;
static boost::iostreams::stream_buffer<Debug::Tee> cerrsb;
#endif
std::ostream& getRawStdout() std::ostream& getRawStdout()
{ {
return rawStdout ? *rawStdout : std::cout; return rawStdout ? *rawStdout : std::cout;
} }
int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, char *argv[], const std::string& appName) // Redirect cout and cerr to the log file
void setupLogging(const std::string& logDir, const std::string& appName, std::ios_base::openmode mode)
{
#if defined(_WIN32) && defined(_DEBUG)
// Redirect cout and cerr to VS debug output when running in debug mode
sb.open(Debug::DebugOutput());
std::cout.rdbuf(&sb);
std::cerr.rdbuf(&sb);
#else
const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log";
logfile.open(boost::filesystem::path(logDir) / logName, mode);
coutsb.open(Debug::Tee(logfile, *rawStdout));
cerrsb.open(Debug::Tee(logfile, *rawStderr));
std::cout.rdbuf(&coutsb);
std::cerr.rdbuf(&cerrsb);
#endif
}
int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, char *argv[],
const std::string& appName, bool autoSetupLogging)
{ {
#if defined _WIN32 #if defined _WIN32
(void)Debug::attachParentConsole(); (void)Debug::attachParentConsole();
#endif #endif
rawStdout = std::make_unique<std::ostream>(std::cout.rdbuf()); rawStdout = std::make_unique<std::ostream>(std::cout.rdbuf());
rawStderr = std::make_unique<std::ostream>(std::cerr.rdbuf());
// Some objects used to redirect cout and cerr
// Scope must be here, so this still works inside the catch block for logging exceptions
std::streambuf* cout_rdbuf = std::cout.rdbuf ();
std::streambuf* cerr_rdbuf = std::cerr.rdbuf ();
#if defined(_WIN32) && defined(_DEBUG)
boost::iostreams::stream_buffer<Debug::DebugOutput> sb;
#else
boost::iostreams::stream_buffer<Debug::Tee> coutsb;
boost::iostreams::stream_buffer<Debug::Tee> cerrsb;
std::ostream oldcout(cout_rdbuf);
std::ostream oldcerr(cerr_rdbuf);
#endif
const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log";
boost::filesystem::ofstream logfile;
int ret = 0; int ret = 0;
try try
{ {
Files::ConfigurationManager cfgMgr; Files::ConfigurationManager cfgMgr;
#if defined(_WIN32) && defined(_DEBUG) if (autoSetupLogging)
// Redirect cout and cerr to VS debug output when running in debug mode {
sb.open(Debug::DebugOutput()); std::ios_base::openmode mode = std::ios::out;
std::cout.rdbuf (&sb);
std::cerr.rdbuf (&sb);
#else
// Redirect cout and cerr to the log file
// If we are collecting a stack trace, append to existing log file
std::ios_base::openmode mode = std::ios::out;
if(argc == 2 && strcmp(argv[1], crash_switch) == 0)
mode |= std::ios::app;
logfile.open (boost::filesystem::path(cfgMgr.getLogPath() / logName), mode); // If we are collecting a stack trace, append to existing log file
if (argc == 2 && strcmp(argv[1], crash_switch) == 0)
mode |= std::ios::app;
coutsb.open (Debug::Tee(logfile, oldcout)); setupLogging(cfgMgr.getLogPath().string(), appName, mode);
cerrsb.open (Debug::Tee(logfile, oldcerr)); }
std::cout.rdbuf (&coutsb);
std::cerr.rdbuf (&cerrsb);
#endif
#if defined(_WIN32) #if defined(_WIN32)
const std::string crashLogName = Misc::StringUtils::lowerCase(appName) + "-crash.dmp"; const std::string crashLogName = Misc::StringUtils::lowerCase(appName) + "-crash.dmp";
@ -217,8 +221,8 @@ int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, c
} }
// Restore cout and cerr // Restore cout and cerr
std::cout.rdbuf(cout_rdbuf); std::cout.rdbuf(rawStdout->rdbuf());
std::cerr.rdbuf(cerr_rdbuf); std::cerr.rdbuf(rawStderr->rdbuf());
Debug::CurrentDebugLevel = Debug::NoLevel; Debug::CurrentDebugLevel = Debug::NoLevel;
return ret; return ret;

View file

@ -133,11 +133,16 @@ namespace Debug
std::map<Level, int> mColors; std::map<Level, int> mColors;
}; };
#endif #endif
} }
// Can be used to print messages without timestamps // Can be used to print messages without timestamps
std::ostream& getRawStdout(); std::ostream& getRawStdout();
int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, char *argv[], const std::string& appName); void setupLogging(const std::string& logDir, const std::string& appName, std::ios_base::openmode = std::ios::out);
int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, char *argv[],
const std::string& appName, bool autoSetupLogging = true);
#endif #endif