1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-31 21:45:35 +00:00

Fix logging and config for portable setup

Setup logging after setting up paths but before settings loading to get proper
log records earlier.

Read configuration by navmeshtool and bulletobjecttool the same way editor and
engine do to properly handle --replace config and --config arguments.

Remove mode and autoSetupLogging arguments from setupLogging since they are no
longer used.

Use temp path to write crash logs because default paths might not be available
for portable setup.
This commit is contained in:
elsid 2023-05-30 09:24:51 +02:00
parent 8b71a31cec
commit 524efc4e18
No known key found for this signature in database
GPG key ID: 4DE04C198CBA7625
8 changed files with 33 additions and 31 deletions

View file

@ -34,6 +34,8 @@ namespace
using StringsVector = std::vector<std::string>;
constexpr std::string_view applicationName = "BulletObjectTool";
bpo::options_description makeOptionsDescription()
{
using Fallback::FallbackMap;
@ -126,10 +128,9 @@ namespace
}
Files::ConfigurationManager config;
bpo::variables_map composingVariables = Files::separateComposingVariables(variables, desc);
config.readConfiguration(variables, desc);
Files::mergeComposingVariables(variables, composingVariables, desc);
setupLogging(config.getLogPath().string(), applicationName);
const std::string encoding(variables["encoding"].as<std::string>());
Log(Debug::Info) << ToUTF8::encodingUsingMessage(encoding);
@ -199,5 +200,5 @@ namespace
int main(int argc, char *argv[])
{
return wrapApplication(runBulletObjectTool, argc, argv, "BulletObjectTool");
return wrapApplication(runBulletObjectTool, argc, argv, applicationName);
}

View file

@ -48,6 +48,8 @@ namespace NavMeshTool
using StringsVector = std::vector<std::string>;
constexpr std::string_view applicationName = "NavMeshTool";
bpo::options_description makeOptionsDescription()
{
using Fallback::FallbackMap;
@ -124,10 +126,9 @@ namespace NavMeshTool
}
Files::ConfigurationManager config;
bpo::variables_map composingVariables = Files::separateComposingVariables(variables, desc);
config.readConfiguration(variables, desc);
Files::mergeComposingVariables(variables, composingVariables, desc);
setupLogging(config.getLogPath().string(), applicationName);
const std::string encoding(variables["encoding"].as<std::string>());
Log(Debug::Info) << ToUTF8::encodingUsingMessage(encoding);
@ -181,6 +182,8 @@ namespace NavMeshTool
const std::uint64_t maxDbFileSize = static_cast<std::uint64_t>(Settings::Manager::getInt64("max navmeshdb file size", "Navigator"));
const std::string dbPath = (config.getUserDataPath() / "navmesh.db").string();
Log(Debug::Info) << "Using navmeshdb at " << dbPath;
DetourNavigator::NavMeshDb db(dbPath, maxDbFileSize);
ESM::ReadersCache readers;
@ -231,5 +234,5 @@ namespace NavMeshTool
int main(int argc, char *argv[])
{
return wrapApplication(NavMeshTool::runNavMeshTool, argc, argv, "NavMeshTool");
return wrapApplication(NavMeshTool::runNavMeshTool, argc, argv, NavMeshTool::applicationName);
}

View file

@ -111,7 +111,7 @@ boost::program_options::variables_map CS::Editor::readConfiguration()
mCfgMgr.readConfiguration(variables, desc, false);
Settings::Manager::load(mCfgMgr, true);
setupLogging(mCfgMgr.getLogPath().string(), "OpenMW-CS");
setupLogging(mCfgMgr.getLogPath().string(), applicationName);
return variables;
}

View file

@ -36,6 +36,8 @@ namespace CSMDoc
namespace CS
{
inline constexpr std::string_view applicationName = "OpenMW-CS";
class Editor : public QObject
{
Q_OBJECT

View file

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

View file

@ -28,6 +28,11 @@ extern "C" __declspec(dllexport) DWORD AmdPowerXpressRequestHighPerformance = 0x
using namespace Fallback;
namespace
{
constexpr std::string_view applicationName = "OpenMW";
}
/**
* \brief Parses application command line and calls \ref Cfg::ConfigurationManager
* to parse configuration files.
@ -67,7 +72,7 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
cfgMgr.readConfiguration(variables, desc);
Settings::Manager::load(cfgMgr);
setupLogging(cfgMgr.getLogPath().string(), "OpenMW");
setupLogging(cfgMgr.getLogPath().string(), applicationName);
MWGui::DebugWindow::startLogRecording();
Version::Version v = Version::getOpenmwVersion(variables["resources"].as<Files::MaybeQuotedPath>().string());
@ -235,7 +240,7 @@ extern "C" int SDL_main(int argc, char**argv)
int main(int argc, char**argv)
#endif
{
return wrapApplication(&runApplication, argc, argv, "OpenMW", false);
return wrapApplication(&runApplication, argc, argv, applicationName);
}
// Platform specific for Windows when there is no console built into the executable.

View file

@ -267,7 +267,7 @@ Misc::Locked<std::ostream&> getLockedRawStderr()
}
// Redirect cout and cerr to the log file
void setupLogging(const std::string& logDir, const std::string& appName, std::ios_base::openmode mode)
void setupLogging(const std::string& logDir, std::string_view appName)
{
#if defined(_WIN32) && defined(_DEBUG)
// Redirect cout and cerr to VS debug output when running in debug mode
@ -276,7 +276,7 @@ void setupLogging(const std::string& logDir, const std::string& appName, std::io
std::cerr.rdbuf(&sb);
#else
const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log";
logfile.open(boost::filesystem::path(logDir) / logName, mode);
logfile.open(boost::filesystem::path(logDir) / logName, std::ios::out);
coutsb.open(Debug::Tee(logfile, *rawStdout));
cerrsb.open(Debug::Tee(logfile, *rawStderr));
@ -287,7 +287,7 @@ void setupLogging(const std::string& logDir, const std::string& appName, std::io
}
int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, char *argv[],
const std::string& appName, bool autoSetupLogging)
std::string_view appName)
{
#if defined _WIN32
(void)Debug::attachParentConsole();
@ -301,27 +301,16 @@ int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, c
{
Files::ConfigurationManager cfgMgr;
if (autoSetupLogging)
{
std::ios_base::openmode mode = std::ios::out;
// 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;
setupLogging(cfgMgr.getLogPath().string(), appName, mode);
}
if (const auto env = std::getenv("OPENMW_DISABLE_CRASH_CATCHER"); env == nullptr || std::atol(env) == 0)
{
#if defined(_WIN32)
const std::string crashLogName = Misc::StringUtils::lowerCase(appName) + "-crash.dmp";
Crash::CrashCatcher crashy(argc, argv, (cfgMgr.getLogPath() / crashLogName).make_preferred().string());
Crash::CrashCatcher crashy(argc, argv, (boost::filesystem::temp_directory_path() / crashLogName).make_preferred().string());
#else
const std::string crashLogName = Misc::StringUtils::lowerCase(appName) + "-crash.log";
// install the crash handler as soon as possible. note that the log path
// does not depend on config being read.
crashCatcherInstall(argc, argv, (cfgMgr.getLogPath() / crashLogName).string());
crashCatcherInstall(argc, argv, (boost::filesystem::temp_directory_path() / crashLogName).string());
#endif
ret = innerApplication(argc, argv);
}
@ -333,7 +322,7 @@ int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, c
#if (defined(__APPLE__) || defined(__linux) || defined(__unix) || defined(__posix))
if (!isatty(fileno(stdin)))
#endif
SDL_ShowSimpleMessageBox(0, (appName + ": Fatal error").c_str(), e.what(), nullptr);
SDL_ShowSimpleMessageBox(0, (std::string(appName) + ": Fatal error").c_str(), e.what(), nullptr);
Log(Debug::Error) << "Error: " << e.what();

View file

@ -1,6 +1,8 @@
#ifndef DEBUG_DEBUGGING_H
#define DEBUG_DEBUGGING_H
#include <string_view>
#include <boost/filesystem/fstream.hpp>
#include <boost/iostreams/stream.hpp>
@ -34,9 +36,9 @@ std::ostream& getRawStderr();
Misc::Locked<std::ostream&> getLockedRawStderr();
void setupLogging(const std::string& logDir, const std::string& appName, std::ios_base::openmode = std::ios::out);
void setupLogging(const std::string& logDir, std::string_view appName);
int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, char *argv[],
const std::string& appName, bool autoSetupLogging = true);
std::string_view appName);
#endif