diff --git a/apps/bulletobjecttool/main.cpp b/apps/bulletobjecttool/main.cpp index 4dbdb56350..190eb3364d 100644 --- a/apps/bulletobjecttool/main.cpp +++ b/apps/bulletobjecttool/main.cpp @@ -121,14 +121,14 @@ namespace if (variables.find("help") != variables.end()) { - getRawStdout() << desc << std::endl; + Debug::getRawStdout() << desc << std::endl; return 0; } Files::ConfigurationManager config; config.readConfiguration(variables, desc); - setupLogging(config.getLogPath(), applicationName); + Debug::setupLogging(config.getLogPath(), applicationName); const std::string encoding(variables["encoding"].as()); Log(Debug::Info) << ToUTF8::encodingUsingMessage(encoding); @@ -202,5 +202,5 @@ namespace int main(int argc, char* argv[]) { - return wrapApplication(runBulletObjectTool, argc, argv, applicationName); + return Debug::wrapApplication(runBulletObjectTool, argc, argv, applicationName); } diff --git a/apps/launcher/main.cpp b/apps/launcher/main.cpp index 0fdf6c304a..2ea152305f 100644 --- a/apps/launcher/main.cpp +++ b/apps/launcher/main.cpp @@ -30,7 +30,7 @@ int runLauncher(int argc, char* argv[]) configurationManager.addCommonOptions(description); configurationManager.readConfiguration(variables, description, true); - setupLogging(configurationManager.getLogPath(), "Launcher"); + Debug::setupLogging(configurationManager.getLogPath(), "Launcher"); try { @@ -66,5 +66,5 @@ int runLauncher(int argc, char* argv[]) int main(int argc, char* argv[]) { - return wrapApplication(runLauncher, argc, argv, "Launcher"); + return Debug::wrapApplication(runLauncher, argc, argv, "Launcher"); } diff --git a/apps/navmeshtool/main.cpp b/apps/navmeshtool/main.cpp index d75a1af5e2..ed1ab57274 100644 --- a/apps/navmeshtool/main.cpp +++ b/apps/navmeshtool/main.cpp @@ -140,14 +140,14 @@ namespace NavMeshTool if (variables.find("help") != variables.end()) { - getRawStdout() << desc << std::endl; + Debug::getRawStdout() << desc << std::endl; return 0; } Files::ConfigurationManager config; config.readConfiguration(variables, desc); - setupLogging(config.getLogPath(), applicationName); + Debug::setupLogging(config.getLogPath(), applicationName); const std::string encoding(variables["encoding"].as()); Log(Debug::Info) << ToUTF8::encodingUsingMessage(encoding); @@ -260,5 +260,5 @@ namespace NavMeshTool int main(int argc, char* argv[]) { - return wrapApplication(NavMeshTool::runNavMeshTool, argc, argv, NavMeshTool::applicationName); + return Debug::wrapApplication(NavMeshTool::runNavMeshTool, argc, argv, NavMeshTool::applicationName); } diff --git a/apps/navmeshtool/navmesh.cpp b/apps/navmeshtool/navmesh.cpp index fe03376557..6a4a708ef9 100644 --- a/apps/navmeshtool/navmesh.cpp +++ b/apps/navmeshtool/navmesh.cpp @@ -59,7 +59,7 @@ namespace NavMeshTool void serializeToStderr(const T& value) { const std::vector data = serialize(value); - getLockedRawStderr()->write( + Debug::getLockedRawStderr()->write( reinterpret_cast(data.data()), static_cast(data.size())); } diff --git a/apps/navmeshtool/worldspacedata.cpp b/apps/navmeshtool/worldspacedata.cpp index 5c8468d7c6..3a4ff63329 100644 --- a/apps/navmeshtool/worldspacedata.cpp +++ b/apps/navmeshtool/worldspacedata.cpp @@ -219,7 +219,8 @@ namespace NavMeshTool void serializeToStderr(const T& value) { const std::vector data = serialize(value); - getRawStderr().write(reinterpret_cast(data.data()), static_cast(data.size())); + Debug::getRawStderr().write( + reinterpret_cast(data.data()), static_cast(data.size())); } std::string makeAddObjectErrorMessage( diff --git a/apps/opencs/editor.cpp b/apps/opencs/editor.cpp index 42fac11bf1..24957b0015 100644 --- a/apps/opencs/editor.cpp +++ b/apps/opencs/editor.cpp @@ -139,7 +139,7 @@ boost::program_options::variables_map CS::Editor::readConfiguration() mCfgMgr.readConfiguration(variables, desc, false); Settings::Manager::load(mCfgMgr, true); - setupLogging(mCfgMgr.getLogPath(), "OpenMW-CS"); + Debug::setupLogging(mCfgMgr.getLogPath(), "OpenMW-CS"); return variables; } diff --git a/apps/opencs/main.cpp b/apps/opencs/main.cpp index fbdcb4e689..18caa3181c 100644 --- a/apps/opencs/main.cpp +++ b/apps/opencs/main.cpp @@ -78,5 +78,5 @@ int runApplication(int argc, char* argv[]) int main(int argc, char* argv[]) { - return wrapApplication(&runApplication, argc, argv, "OpenMW-CS"); + return Debug::wrapApplication(&runApplication, argc, argv, "OpenMW-CS"); } diff --git a/apps/openmw/main.cpp b/apps/openmw/main.cpp index d8095a645b..4fa4e4f39f 100644 --- a/apps/openmw/main.cpp +++ b/apps/openmw/main.cpp @@ -53,19 +53,19 @@ bool parseOptions(int argc, char** argv, OMW::Engine& engine, Files::Configurati if (variables.count("help")) { - getRawStdout() << desc << std::endl; + Debug::getRawStdout() << desc << std::endl; return false; } if (variables.count("version")) { - getRawStdout() << Version::getOpenmwVersionDescription() << std::endl; + Debug::getRawStdout() << Version::getOpenmwVersionDescription() << std::endl; return false; } cfgMgr.readConfiguration(variables, desc); - setupLogging(cfgMgr.getLogPath(), "OpenMW"); + Debug::setupLogging(cfgMgr.getLogPath(), "OpenMW"); Log(Debug::Info) << Version::getOpenmwVersionDescription(); Settings::Manager::load(cfgMgr); @@ -245,7 +245,7 @@ extern "C" int SDL_main(int argc, char** argv) int main(int argc, char** argv) #endif { - return wrapApplication(&runApplication, argc, argv, "OpenMW"); + return Debug::wrapApplication(&runApplication, argc, argv, "OpenMW"); } // 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 06c749e032..0b0547996f 100644 --- a/components/debug/debugging.cpp +++ b/components/debug/debugging.cpp @@ -352,130 +352,130 @@ namespace Debug } #endif -} - -static std::unique_ptr rawStdout = nullptr; -static std::unique_ptr rawStderr = nullptr; -static std::unique_ptr rawStderrMutex = nullptr; -static std::ofstream logfile; + static std::unique_ptr rawStdout = nullptr; + static std::unique_ptr rawStderr = nullptr; + static std::unique_ptr rawStderrMutex = nullptr; + static std::ofstream logfile; #if defined(_WIN32) && defined(_DEBUG) -static boost::iostreams::stream_buffer sb; + static boost::iostreams::stream_buffer sb; #else -static boost::iostreams::stream_buffer> standardOut; -static boost::iostreams::stream_buffer> standardErr; -static boost::iostreams::stream_buffer> bufferedOut; -static boost::iostreams::stream_buffer> bufferedErr; + static boost::iostreams::stream_buffer> standardOut; + static boost::iostreams::stream_buffer> standardErr; + static boost::iostreams::stream_buffer> bufferedOut; + static boost::iostreams::stream_buffer> bufferedErr; #endif -std::ostream& getRawStdout() -{ - return rawStdout ? *rawStdout : std::cout; -} + std::ostream& getRawStdout() + { + return rawStdout ? *rawStdout : std::cout; + } -std::ostream& getRawStderr() -{ - return rawStderr ? *rawStderr : std::cerr; -} + std::ostream& getRawStderr() + { + return rawStderr ? *rawStderr : std::cerr; + } -Misc::Locked getLockedRawStderr() -{ - return Misc::Locked(*rawStderrMutex, getRawStderr()); -} + Misc::Locked getLockedRawStderr() + { + return Misc::Locked(*rawStderrMutex, getRawStderr()); + } -void setupLogging(const std::filesystem::path& logDir, std::string_view appName) -{ + void setupLogging(const std::filesystem::path& logDir, std::string_view appName) + { #if !(defined(_WIN32) && defined(_DEBUG)) - const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log"; - logfile.open(logDir / logName, std::ios::out); + const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log"; + logfile.open(logDir / logName, std::ios::out); - Debug::Identity log(logfile); + Identity log(logfile); - for (const Debug::Record& v : Debug::globalBuffer) - log.write(v.mValue.data(), v.mValue.size(), v.mLevel); + for (const Record& v : globalBuffer) + log.write(v.mValue.data(), v.mValue.size(), v.mLevel); - Debug::globalBuffer.clear(); + globalBuffer.clear(); - standardOut.open(Debug::Tee(log, Debug::Coloured(*rawStdout))); - standardErr.open(Debug::Tee(log, Debug::Coloured(*rawStderr))); + standardOut.open(Tee(log, Coloured(*rawStdout))); + standardErr.open(Tee(log, Coloured(*rawStderr))); - std::cout.rdbuf(&standardOut); - std::cerr.rdbuf(&standardErr); + std::cout.rdbuf(&standardOut); + std::cerr.rdbuf(&standardErr); #endif #ifdef _WIN32 - if (Crash::CrashCatcher::instance()) - { - Crash::CrashCatcher::instance()->updateDumpPath(logDir); - } + if (Crash::CrashCatcher::instance()) + { + Crash::CrashCatcher::instance()->updateDumpPath(logDir); + } #endif -} + } -int wrapApplication(int (*innerApplication)(int argc, char* argv[]), int argc, char* argv[], std::string_view appName) -{ + int wrapApplication( + int (*innerApplication)(int argc, char* argv[]), int argc, char* argv[], std::string_view appName) + { #if defined _WIN32 - (void)Debug::attachParentConsole(); + (void)attachParentConsole(); #endif - rawStdout = std::make_unique(std::cout.rdbuf()); - rawStderr = std::make_unique(std::cerr.rdbuf()); - rawStderrMutex = std::make_unique(); + rawStdout = std::make_unique(std::cout.rdbuf()); + rawStderr = std::make_unique(std::cerr.rdbuf()); + rawStderrMutex = std::make_unique(); #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); + // Redirect cout and cerr to VS debug output when running in debug mode + sb.open(DebugOutput()); + std::cout.rdbuf(&sb); + std::cerr.rdbuf(&sb); #else - bufferedOut.open(Debug::Tee(Debug::Buffer(Debug::globalBuffer), Debug::Coloured(*rawStdout))); - bufferedErr.open(Debug::Tee(Debug::Buffer(Debug::globalBuffer), Debug::Coloured(*rawStderr))); + bufferedOut.open(Tee(Buffer(globalBuffer), Coloured(*rawStdout))); + bufferedErr.open(Tee(Buffer(globalBuffer), Coloured(*rawStderr))); - std::cout.rdbuf(&bufferedOut); - std::cerr.rdbuf(&bufferedErr); + std::cout.rdbuf(&bufferedOut); + std::cerr.rdbuf(&bufferedErr); #endif - int ret = 0; - try - { - if (const auto env = std::getenv("OPENMW_DISABLE_CRASH_CATCHER"); - env == nullptr || Misc::StringUtils::toNumeric(env, 0) == 0) + int ret = 0; + try { -#if defined(_WIN32) - const std::string crashDumpName = Misc::StringUtils::lowerCase(appName) + "-crash.dmp"; - const std::string freezeDumpName = Misc::StringUtils::lowerCase(appName) + "-freeze.dmp"; - std::filesystem::path dumpDirectory = std::filesystem::temp_directory_path(); - PWSTR userProfile = nullptr; - if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Profile, 0, nullptr, &userProfile))) + if (const auto env = std::getenv("OPENMW_DISABLE_CRASH_CATCHER"); + env == nullptr || Misc::StringUtils::toNumeric(env, 0) == 0) { - dumpDirectory = userProfile; - } - CoTaskMemFree(userProfile); - Crash::CrashCatcher crashy(argc, argv, dumpDirectory, crashDumpName, freezeDumpName); +#if defined(_WIN32) + const std::string crashDumpName = Misc::StringUtils::lowerCase(appName) + "-crash.dmp"; + const std::string freezeDumpName = Misc::StringUtils::lowerCase(appName) + "-freeze.dmp"; + std::filesystem::path dumpDirectory = std::filesystem::temp_directory_path(); + PWSTR userProfile = nullptr; + if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_Profile, 0, nullptr, &userProfile))) + { + dumpDirectory = userProfile; + } + CoTaskMemFree(userProfile); + Crash::CrashCatcher crashy(argc, argv, dumpDirectory, crashDumpName, freezeDumpName); #else - const std::string crashLogName = Misc::StringUtils::lowerCase(appName) + "-crash.log"; - // install the crash handler as soon as possible. - crashCatcherInstall(argc, argv, std::filesystem::temp_directory_path() / crashLogName); + const std::string crashLogName = Misc::StringUtils::lowerCase(appName) + "-crash.log"; + // install the crash handler as soon as possible. + crashCatcherInstall(argc, argv, std::filesystem::temp_directory_path() / crashLogName); #endif - ret = innerApplication(argc, argv); + ret = innerApplication(argc, argv); + } + else + ret = innerApplication(argc, argv); } - else - ret = innerApplication(argc, argv); - } - catch (const std::exception& e) - { + catch (const std::exception& e) + { #if (defined(__APPLE__) || defined(__linux) || defined(__unix) || defined(__posix)) - if (!isatty(fileno(stdin))) + if (!isatty(fileno(stdin))) #endif - SDL_ShowSimpleMessageBox(0, (std::string(appName) + ": Fatal error").c_str(), e.what(), nullptr); + SDL_ShowSimpleMessageBox(0, (std::string(appName) + ": Fatal error").c_str(), e.what(), nullptr); - Log(Debug::Error) << "Fatal error: " << e.what(); + Log(Debug::Error) << "Fatal error: " << e.what(); - ret = 1; - } + ret = 1; + } - // Restore cout and cerr - std::cout.rdbuf(rawStdout->rdbuf()); - std::cerr.rdbuf(rawStderr->rdbuf()); - Debug::CurrentDebugLevel = Debug::NoLevel; + // Restore cout and cerr + std::cout.rdbuf(rawStdout->rdbuf()); + std::cerr.rdbuf(rawStderr->rdbuf()); + CurrentDebugLevel = NoLevel; - return ret; + return ret; + } } diff --git a/components/debug/debugging.hpp b/components/debug/debugging.hpp index 9e375de3d7..7efbffa657 100644 --- a/components/debug/debugging.hpp +++ b/components/debug/debugging.hpp @@ -26,18 +26,19 @@ namespace Debug using LogListener = std::function; void setLogListener(LogListener); -} -// Can be used to print messages without timestamps -std::ostream& getRawStdout(); + // Can be used to print messages without timestamps + std::ostream& getRawStdout(); -std::ostream& getRawStderr(); + std::ostream& getRawStderr(); -Misc::Locked getLockedRawStderr(); + Misc::Locked getLockedRawStderr(); -// Redirect cout and cerr to the log file -void setupLogging(const std::filesystem::path& logDir, std::string_view appName); + // Redirect cout and cerr to the log file + void setupLogging(const std::filesystem::path& logDir, std::string_view appName); -int wrapApplication(int (*innerApplication)(int argc, char* argv[]), int argc, char* argv[], std::string_view appName); + int wrapApplication( + int (*innerApplication)(int argc, char* argv[]), int argc, char* argv[], std::string_view appName); +} #endif