1
0
Fork 0
mirror of https://github.com/OpenMW/openmw.git synced 2025-01-16 17:29:55 +00:00

Setup logging with properly initialized configuration manager

Default constructed instance may provide different paths from initialized one.
This commit is contained in:
elsid 2023-01-30 09:17:56 +01:00
parent 9c8a110efb
commit c44bc5ade5
No known key found for this signature in database
GPG key ID: 4DE04C198CBA7625
7 changed files with 26 additions and 29 deletions

View file

@ -48,6 +48,8 @@ namespace
using StringsVector = std::vector<std::string>; using StringsVector = std::vector<std::string>;
constexpr std::string_view applicationName = "BulletObjectTool";
bpo::options_description makeOptionsDescription() bpo::options_description makeOptionsDescription()
{ {
using Fallback::FallbackMap; using Fallback::FallbackMap;
@ -177,8 +179,9 @@ namespace
VFS::registerArchives(&vfs, fileCollections, archives, true); VFS::registerArchives(&vfs, fileCollections, archives, true);
Settings::Manager settings; Settings::Manager::load(config);
settings.load(config);
setupLogging(config.getLogPath(), applicationName);
ESM::ReadersCache readers; ESM::ReadersCache readers;
EsmLoader::Query query; EsmLoader::Query query;
@ -221,5 +224,5 @@ namespace
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
return wrapApplication(runBulletObjectTool, argc, argv, "BulletObjectTool"); return wrapApplication(runBulletObjectTool, argc, argv, applicationName);
} }

View file

@ -13,6 +13,7 @@
#include <boost/program_options/options_description.hpp> #include <boost/program_options/options_description.hpp>
#include <boost/program_options/variables_map.hpp> #include <boost/program_options/variables_map.hpp>
#include <components/debug/debugging.hpp>
#include <components/files/conversion.hpp> #include <components/files/conversion.hpp>
#include <components/files/qtconfigpath.hpp> #include <components/files/qtconfigpath.hpp>
#include <components/files/qtconversion.hpp> #include <components/files/qtconversion.hpp>
@ -424,6 +425,7 @@ bool Launcher::MainDialog::setupGraphicsSettings()
mCfgMgr.addCommonOptions(desc); mCfgMgr.addCommonOptions(desc);
mCfgMgr.readConfiguration(variables, desc, true); mCfgMgr.readConfiguration(variables, desc, true);
Settings::Manager::load(mCfgMgr); Settings::Manager::load(mCfgMgr);
setupLogging(mCfgMgr.getLogPath(), "Launcher");
return true; return true;
} }
catch (std::exception& e) catch (std::exception& e)

View file

@ -57,6 +57,8 @@ namespace NavMeshTool
using StringsVector = std::vector<std::string>; using StringsVector = std::vector<std::string>;
constexpr std::string_view applicationName = "NavMeshTool";
bpo::options_description makeOptionsDescription() bpo::options_description makeOptionsDescription()
{ {
using Fallback::FallbackMap; using Fallback::FallbackMap;
@ -197,8 +199,9 @@ namespace NavMeshTool
VFS::registerArchives(&vfs, fileCollections, archives, true); VFS::registerArchives(&vfs, fileCollections, archives, true);
Settings::Manager settings; Settings::Manager::load(config);
settings.load(config);
setupLogging(config.getLogPath(), applicationName);
const auto agentCollisionShape = DetourNavigator::toCollisionShapeType( const auto agentCollisionShape = DetourNavigator::toCollisionShapeType(
Settings::Manager::getInt("actor collision shape type", "Game")); Settings::Manager::getInt("actor collision shape type", "Game"));
@ -263,5 +266,5 @@ namespace NavMeshTool
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
return wrapApplication(NavMeshTool::runNavMeshTool, argc, argv, "NavMeshTool"); return wrapApplication(NavMeshTool::runNavMeshTool, argc, argv, NavMeshTool::applicationName);
} }

View file

@ -85,5 +85,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", false); return wrapApplication(&runApplication, argc, argv, "OpenMW-CS");
} }

View file

@ -256,7 +256,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", false); return wrapApplication(&runApplication, argc, argv, "OpenMW");
} }
// 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

@ -2,13 +2,16 @@
#include <chrono> #include <chrono>
#include <fstream> #include <fstream>
#include <iostream>
#include <map>
#include <memory> #include <memory>
#include <boost/iostreams/stream.hpp> #include <boost/iostreams/stream.hpp>
#include <components/crashcatcher/crashcatcher.hpp> #include <components/crashcatcher/crashcatcher.hpp>
#include <components/files/configurationmanager.hpp>
#include <components/files/conversion.hpp> #include <components/files/conversion.hpp>
#include <components/misc/strings/lower.hpp>
#ifdef _WIN32 #ifdef _WIN32
#include <components/crashcatcher/windows_crashcatcher.hpp> #include <components/crashcatcher/windows_crashcatcher.hpp>
#include <components/files/conversion.hpp> #include <components/files/conversion.hpp>
@ -280,7 +283,7 @@ Misc::Locked<std::ostream&> getLockedRawStderr()
} }
// Redirect cout and cerr to the log file // Redirect cout and cerr to the log file
void setupLogging(const std::filesystem::path& logDir, const std::string& appName, std::ios_base::openmode mode) void setupLogging(const std::filesystem::path& logDir, std::string_view appName, std::ios_base::openmode mode)
{ {
#if defined(_WIN32) && defined(_DEBUG) #if defined(_WIN32) && defined(_DEBUG)
// Redirect cout and cerr to VS debug output when running in debug mode // Redirect cout and cerr to VS debug output when running in debug mode
@ -299,8 +302,7 @@ void setupLogging(const std::filesystem::path& logDir, const std::string& appNam
#endif #endif
} }
int wrapApplication(int (*innerApplication)(int argc, char* argv[]), int argc, char* argv[], const std::string& appName, int wrapApplication(int (*innerApplication)(int argc, char* argv[]), int argc, char* argv[], std::string_view appName)
bool autoSetupLogging)
{ {
#if defined _WIN32 #if defined _WIN32
(void)Debug::attachParentConsole(); (void)Debug::attachParentConsole();
@ -312,19 +314,6 @@ int wrapApplication(int (*innerApplication)(int argc, char* argv[]), int argc, c
int ret = 0; int ret = 0;
try try
{ {
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(), appName, mode);
}
if (const auto env = std::getenv("OPENMW_DISABLE_CRASH_CATCHER"); env == nullptr || std::atol(env) == 0) if (const auto env = std::getenv("OPENMW_DISABLE_CRASH_CATCHER"); env == nullptr || std::atol(env) == 0)
{ {
#if defined(_WIN32) #if defined(_WIN32)
@ -347,7 +336,7 @@ int wrapApplication(int (*innerApplication)(int argc, char* argv[]), int argc, c
#if (defined(__APPLE__) || defined(__linux) || defined(__unix) || defined(__posix)) #if (defined(__APPLE__) || defined(__linux) || defined(__unix) || defined(__posix))
if (!isatty(fileno(stdin))) if (!isatty(fileno(stdin)))
#endif #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(); Log(Debug::Error) << "Error: " << e.what();

View file

@ -3,6 +3,7 @@
#include <filesystem> #include <filesystem>
#include <functional> #include <functional>
#include <string_view>
#include <components/misc/guarded.hpp> #include <components/misc/guarded.hpp>
@ -35,9 +36,8 @@ std::ostream& getRawStderr();
Misc::Locked<std::ostream&> getLockedRawStderr(); Misc::Locked<std::ostream&> getLockedRawStderr();
void setupLogging( void setupLogging(
const std::filesystem::path& logDir, const std::string& appName, std::ios_base::openmode mode = std::ios::out); const std::filesystem::path& logDir, std::string_view appName, std::ios_base::openmode mode = std::ios::out);
int wrapApplication(int (*innerApplication)(int argc, char* argv[]), int argc, char* argv[], const std::string& appName, int wrapApplication(int (*innerApplication)(int argc, char* argv[]), int argc, char* argv[], std::string_view appName);
bool autoSetupLogging = true);
#endif #endif