From 524efc4e1891dd3af425a09c7819912bce04f6d6 Mon Sep 17 00:00:00 2001 From: elsid Date: Tue, 30 May 2023 09:24:51 +0200 Subject: [PATCH] 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. --- apps/bulletobjecttool/main.cpp | 9 +++++---- apps/navmeshtool/main.cpp | 11 +++++++---- apps/opencs/editor.cpp | 2 +- apps/opencs/editor.hpp | 2 ++ apps/opencs/main.cpp | 2 +- apps/openmw/main.cpp | 9 +++++++-- components/debug/debugging.cpp | 23 ++++++----------------- components/debug/debugging.hpp | 6 ++++-- 8 files changed, 33 insertions(+), 31 deletions(-) diff --git a/apps/bulletobjecttool/main.cpp b/apps/bulletobjecttool/main.cpp index aad8c5e082..055b3d39eb 100644 --- a/apps/bulletobjecttool/main.cpp +++ b/apps/bulletobjecttool/main.cpp @@ -34,6 +34,8 @@ namespace using StringsVector = std::vector; + 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()); 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); } diff --git a/apps/navmeshtool/main.cpp b/apps/navmeshtool/main.cpp index 13e05cb200..b207da5889 100644 --- a/apps/navmeshtool/main.cpp +++ b/apps/navmeshtool/main.cpp @@ -48,6 +48,8 @@ namespace NavMeshTool using StringsVector = std::vector; + 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()); Log(Debug::Info) << ToUTF8::encodingUsingMessage(encoding); @@ -181,6 +182,8 @@ namespace NavMeshTool const std::uint64_t maxDbFileSize = static_cast(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); } diff --git a/apps/opencs/editor.cpp b/apps/opencs/editor.cpp index b70c9f9ca8..4a57eebbef 100644 --- a/apps/opencs/editor.cpp +++ b/apps/opencs/editor.cpp @@ -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; } diff --git a/apps/opencs/editor.hpp b/apps/opencs/editor.hpp index b99c2d91e8..b70e79c586 100644 --- a/apps/opencs/editor.hpp +++ b/apps/opencs/editor.hpp @@ -36,6 +36,8 @@ namespace CSMDoc namespace CS { + inline constexpr std::string_view applicationName = "OpenMW-CS"; + class Editor : public QObject { Q_OBJECT diff --git a/apps/opencs/main.cpp b/apps/opencs/main.cpp index e46a46a34e..b23f3c4f95 100644 --- a/apps/opencs/main.cpp +++ b/apps/opencs/main.cpp @@ -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); } diff --git a/apps/openmw/main.cpp b/apps/openmw/main.cpp index a826b11b89..d4217e3878 100644 --- a/apps/openmw/main.cpp +++ b/apps/openmw/main.cpp @@ -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().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. diff --git a/components/debug/debugging.cpp b/components/debug/debugging.cpp index 5225c59e85..933b3d2335 100644 --- a/components/debug/debugging.cpp +++ b/components/debug/debugging.cpp @@ -267,7 +267,7 @@ Misc::Locked 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(); diff --git a/components/debug/debugging.hpp b/components/debug/debugging.hpp index 8ac4bcd8ef..51e1ca0a3a 100644 --- a/components/debug/debugging.hpp +++ b/components/debug/debugging.hpp @@ -1,6 +1,8 @@ #ifndef DEBUG_DEBUGGING_H #define DEBUG_DEBUGGING_H +#include + #include #include @@ -34,9 +36,9 @@ std::ostream& getRawStderr(); Misc::Locked 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