From 54d8606b78ae0db14c4db865905ef493fd0300b7 Mon Sep 17 00:00:00 2001 From: scrawl Date: Fri, 13 Jun 2014 22:03:52 +0200 Subject: [PATCH] Fix logging fatal exceptions to cerr, and log them to openmw.log --- apps/openmw/main.cpp | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/apps/openmw/main.cpp b/apps/openmw/main.cpp index 503ccaf25..b082ff908 100644 --- a/apps/openmw/main.cpp +++ b/apps/openmw/main.cpp @@ -294,9 +294,19 @@ private: int main(int argc, char**argv) { + // Some objects used to redirect cout and cerr + // Scope must be here, so this still works inside the catch block for logging exceptions std::streambuf* cout_rdbuf = std::cout.rdbuf (); std::streambuf* cerr_rdbuf = std::cerr.rdbuf (); + boost::iostreams::stream_buffer coutsb; + boost::iostreams::stream_buffer cerrsb; + + std::ostream oldcout(cout_rdbuf); + std::ostream oldcerr(cerr_rdbuf); + + boost::filesystem::ofstream logfile; + int ret = 0; try { @@ -310,20 +320,16 @@ int main(int argc, char**argv) std::cerr.rdbuf (&sb); #else // Redirect cout and cerr to openmw.log - boost::filesystem::ofstream logfile (boost::filesystem::path( - cfgMgr.getLogPath() / "/openmw.log")); + logfile.open (boost::filesystem::path(cfgMgr.getLogPath() / "/openmw.log")); - boost::iostreams::stream_buffer coutsb; - std::ostream oldcout(cout_rdbuf); coutsb.open (Tee(logfile, oldcout)); - std::cout.rdbuf (&coutsb); - - boost::iostreams::stream_buffer cerrsb; - std::ostream oldcerr(cerr_rdbuf); cerrsb.open (Tee(logfile, oldcerr)); + + std::cout.rdbuf (&coutsb); std::cerr.rdbuf (&cerrsb); #endif + #if OGRE_PLATFORM == OGRE_PLATFORM_LINUX || OGRE_PLATFORM == OGRE_PLATFORM_APPLE // Unix crash catcher if ((argc == 2 && strcmp(argv[1], "--cc-handle-crash") == 0) || !is_debugger_attached())