Move "debugging" function into Debug namespace

pull/3236/head
elsid 7 months ago
parent ea84d1ce0c
commit 1bdc01273e
No known key found for this signature in database
GPG Key ID: 4DE04C198CBA7625

@ -121,14 +121,14 @@ namespace
if (variables.find("help") != variables.end()) if (variables.find("help") != variables.end())
{ {
getRawStdout() << desc << std::endl; Debug::getRawStdout() << desc << std::endl;
return 0; return 0;
} }
Files::ConfigurationManager config; Files::ConfigurationManager config;
config.readConfiguration(variables, desc); config.readConfiguration(variables, desc);
setupLogging(config.getLogPath(), applicationName); Debug::setupLogging(config.getLogPath(), applicationName);
const std::string encoding(variables["encoding"].as<std::string>()); const std::string encoding(variables["encoding"].as<std::string>());
Log(Debug::Info) << ToUTF8::encodingUsingMessage(encoding); Log(Debug::Info) << ToUTF8::encodingUsingMessage(encoding);
@ -202,5 +202,5 @@ namespace
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
return wrapApplication(runBulletObjectTool, argc, argv, applicationName); return Debug::wrapApplication(runBulletObjectTool, argc, argv, applicationName);
} }

@ -30,7 +30,7 @@ int runLauncher(int argc, char* argv[])
configurationManager.addCommonOptions(description); configurationManager.addCommonOptions(description);
configurationManager.readConfiguration(variables, description, true); configurationManager.readConfiguration(variables, description, true);
setupLogging(configurationManager.getLogPath(), "Launcher"); Debug::setupLogging(configurationManager.getLogPath(), "Launcher");
try try
{ {
@ -66,5 +66,5 @@ int runLauncher(int argc, char* argv[])
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
return wrapApplication(runLauncher, argc, argv, "Launcher"); return Debug::wrapApplication(runLauncher, argc, argv, "Launcher");
} }

@ -140,14 +140,14 @@ namespace NavMeshTool
if (variables.find("help") != variables.end()) if (variables.find("help") != variables.end())
{ {
getRawStdout() << desc << std::endl; Debug::getRawStdout() << desc << std::endl;
return 0; return 0;
} }
Files::ConfigurationManager config; Files::ConfigurationManager config;
config.readConfiguration(variables, desc); config.readConfiguration(variables, desc);
setupLogging(config.getLogPath(), applicationName); Debug::setupLogging(config.getLogPath(), applicationName);
const std::string encoding(variables["encoding"].as<std::string>()); const std::string encoding(variables["encoding"].as<std::string>());
Log(Debug::Info) << ToUTF8::encodingUsingMessage(encoding); Log(Debug::Info) << ToUTF8::encodingUsingMessage(encoding);
@ -260,5 +260,5 @@ namespace NavMeshTool
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
return wrapApplication(NavMeshTool::runNavMeshTool, argc, argv, NavMeshTool::applicationName); return Debug::wrapApplication(NavMeshTool::runNavMeshTool, argc, argv, NavMeshTool::applicationName);
} }

@ -59,7 +59,7 @@ namespace NavMeshTool
void serializeToStderr(const T& value) void serializeToStderr(const T& value)
{ {
const std::vector<std::byte> data = serialize(value); const std::vector<std::byte> data = serialize(value);
getLockedRawStderr()->write( Debug::getLockedRawStderr()->write(
reinterpret_cast<const char*>(data.data()), static_cast<std::streamsize>(data.size())); reinterpret_cast<const char*>(data.data()), static_cast<std::streamsize>(data.size()));
} }

@ -219,7 +219,8 @@ namespace NavMeshTool
void serializeToStderr(const T& value) void serializeToStderr(const T& value)
{ {
const std::vector<std::byte> data = serialize(value); const std::vector<std::byte> data = serialize(value);
getRawStderr().write(reinterpret_cast<const char*>(data.data()), static_cast<std::streamsize>(data.size())); Debug::getRawStderr().write(
reinterpret_cast<const char*>(data.data()), static_cast<std::streamsize>(data.size()));
} }
std::string makeAddObjectErrorMessage( std::string makeAddObjectErrorMessage(

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

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

@ -53,19 +53,19 @@ bool parseOptions(int argc, char** argv, OMW::Engine& engine, Files::Configurati
if (variables.count("help")) if (variables.count("help"))
{ {
getRawStdout() << desc << std::endl; Debug::getRawStdout() << desc << std::endl;
return false; return false;
} }
if (variables.count("version")) if (variables.count("version"))
{ {
getRawStdout() << Version::getOpenmwVersionDescription() << std::endl; Debug::getRawStdout() << Version::getOpenmwVersionDescription() << std::endl;
return false; return false;
} }
cfgMgr.readConfiguration(variables, desc); cfgMgr.readConfiguration(variables, desc);
setupLogging(cfgMgr.getLogPath(), "OpenMW"); Debug::setupLogging(cfgMgr.getLogPath(), "OpenMW");
Log(Debug::Info) << Version::getOpenmwVersionDescription(); Log(Debug::Info) << Version::getOpenmwVersionDescription();
Settings::Manager::load(cfgMgr); Settings::Manager::load(cfgMgr);
@ -245,7 +245,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 Debug::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.

@ -352,20 +352,18 @@ namespace Debug
} }
#endif #endif
}
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 std::unique_ptr<std::ostream> rawStderr = nullptr;
static std::unique_ptr<std::mutex> rawStderrMutex = nullptr; static std::unique_ptr<std::mutex> rawStderrMutex = nullptr;
static std::ofstream logfile; static std::ofstream logfile;
#if defined(_WIN32) && defined(_DEBUG) #if defined(_WIN32) && defined(_DEBUG)
static boost::iostreams::stream_buffer<Debug::DebugOutput> sb; static boost::iostreams::stream_buffer<DebugOutput> sb;
#else #else
static boost::iostreams::stream_buffer<Debug::Tee<Debug::Identity, Debug::Coloured>> standardOut; static boost::iostreams::stream_buffer<Tee<Identity, Coloured>> standardOut;
static boost::iostreams::stream_buffer<Debug::Tee<Debug::Identity, Debug::Coloured>> standardErr; static boost::iostreams::stream_buffer<Tee<Identity, Coloured>> standardErr;
static boost::iostreams::stream_buffer<Debug::Tee<Debug::Buffer, Debug::Coloured>> bufferedOut; static boost::iostreams::stream_buffer<Tee<Buffer, Coloured>> bufferedOut;
static boost::iostreams::stream_buffer<Debug::Tee<Debug::Buffer, Debug::Coloured>> bufferedErr; static boost::iostreams::stream_buffer<Tee<Buffer, Coloured>> bufferedErr;
#endif #endif
std::ostream& getRawStdout() std::ostream& getRawStdout()
@ -389,15 +387,15 @@ void setupLogging(const std::filesystem::path& logDir, std::string_view appName)
const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log"; const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log";
logfile.open(logDir / logName, std::ios::out); logfile.open(logDir / logName, std::ios::out);
Debug::Identity log(logfile); Identity log(logfile);
for (const Debug::Record& v : Debug::globalBuffer) for (const Record& v : globalBuffer)
log.write(v.mValue.data(), v.mValue.size(), v.mLevel); log.write(v.mValue.data(), v.mValue.size(), v.mLevel);
Debug::globalBuffer.clear(); globalBuffer.clear();
standardOut.open(Debug::Tee(log, Debug::Coloured(*rawStdout))); standardOut.open(Tee(log, Coloured(*rawStdout)));
standardErr.open(Debug::Tee(log, Debug::Coloured(*rawStderr))); standardErr.open(Tee(log, Coloured(*rawStderr)));
std::cout.rdbuf(&standardOut); std::cout.rdbuf(&standardOut);
std::cerr.rdbuf(&standardErr); std::cerr.rdbuf(&standardErr);
@ -411,10 +409,11 @@ void setupLogging(const std::filesystem::path& logDir, std::string_view appName)
#endif #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 #if defined _WIN32
(void)Debug::attachParentConsole(); (void)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()); rawStderr = std::make_unique<std::ostream>(std::cerr.rdbuf());
@ -422,12 +421,12 @@ int wrapApplication(int (*innerApplication)(int argc, char* argv[]), int argc, c
#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
sb.open(Debug::DebugOutput()); sb.open(DebugOutput());
std::cout.rdbuf(&sb); std::cout.rdbuf(&sb);
std::cerr.rdbuf(&sb); std::cerr.rdbuf(&sb);
#else #else
bufferedOut.open(Debug::Tee(Debug::Buffer(Debug::globalBuffer), Debug::Coloured(*rawStdout))); bufferedOut.open(Tee(Buffer(globalBuffer), Coloured(*rawStdout)));
bufferedErr.open(Debug::Tee(Debug::Buffer(Debug::globalBuffer), Debug::Coloured(*rawStderr))); bufferedErr.open(Tee(Buffer(globalBuffer), Coloured(*rawStderr)));
std::cout.rdbuf(&bufferedOut); std::cout.rdbuf(&bufferedOut);
std::cerr.rdbuf(&bufferedErr); std::cerr.rdbuf(&bufferedErr);
@ -475,7 +474,8 @@ int wrapApplication(int (*innerApplication)(int argc, char* argv[]), int argc, c
// Restore cout and cerr // Restore cout and cerr
std::cout.rdbuf(rawStdout->rdbuf()); std::cout.rdbuf(rawStdout->rdbuf());
std::cerr.rdbuf(rawStderr->rdbuf()); std::cerr.rdbuf(rawStderr->rdbuf());
Debug::CurrentDebugLevel = Debug::NoLevel; CurrentDebugLevel = NoLevel;
return ret; return ret;
} }
}

@ -26,7 +26,6 @@ namespace Debug
using LogListener = std::function<void(Debug::Level, std::string_view prefix, std::string_view msg)>; using LogListener = std::function<void(Debug::Level, std::string_view prefix, std::string_view msg)>;
void setLogListener(LogListener); void setLogListener(LogListener);
}
// Can be used to print messages without timestamps // Can be used to print messages without timestamps
std::ostream& getRawStdout(); std::ostream& getRawStdout();
@ -38,6 +37,8 @@ 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, std::string_view appName); 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 #endif

Loading…
Cancel
Save