From 5e172ed831d797ab14d31689d6e9dcd872d663fb Mon Sep 17 00:00:00 2001
From: Andrei Kortunov <andrei.kortunov@yandex.ru>
Date: Tue, 14 Aug 2018 10:30:27 +0400
Subject: [PATCH 1/2] Move cerr initialization out of 'try' block

---
 components/debug/debugging.cpp | 14 +++++++++-----
 1 file changed, 9 insertions(+), 5 deletions(-)

diff --git a/components/debug/debugging.cpp b/components/debug/debugging.cpp
index bec97207a..470df47a7 100644
--- a/components/debug/debugging.cpp
+++ b/components/debug/debugging.cpp
@@ -55,6 +55,14 @@ int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, c
     std::streambuf* cout_rdbuf = std::cout.rdbuf ();
     std::streambuf* cerr_rdbuf = std::cerr.rdbuf ();
 
+#if !(defined(_WIN32) && defined(_DEBUG))
+    boost::iostreams::stream_buffer<Debug::Tee> coutsb;
+    boost::iostreams::stream_buffer<Debug::Tee> cerrsb;
+#endif
+
+    const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log";
+    boost::filesystem::ofstream logfile;
+
     int ret = 0;
     try
     {
@@ -67,12 +75,8 @@ int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, c
         std::cerr.rdbuf (&sb);
 #else
         // Redirect cout and cerr to the log file
-        const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log";
-        boost::filesystem::ofstream logfile;
         logfile.open (boost::filesystem::path(cfgMgr.getLogPath() / logName));
 
-        boost::iostreams::stream_buffer<Debug::Tee> coutsb;
-        boost::iostreams::stream_buffer<Debug::Tee> cerrsb;
         std::ostream oldcout(cout_rdbuf);
         std::ostream oldcerr(cerr_rdbuf);
         coutsb.open (Debug::Tee(logfile, oldcout));
@@ -90,7 +94,7 @@ int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, c
 #endif
             SDL_ShowSimpleMessageBox(0, (appName + ": Fatal error").c_str(), e.what(), NULL);
 
-        Log(Debug::Error) << "ERROR: " << e.what();
+        Log(Debug::Error) << "Error: " << e.what();
 
         ret = 1;
     }

From 9a5b0162931222900a20d455361c3007f9af53c4 Mon Sep 17 00:00:00 2001
From: Andrei Kortunov <andrei.kortunov@yandex.ru>
Date: Tue, 14 Aug 2018 11:17:05 +0400
Subject: [PATCH 2/2] Move crashcatcher initialization to components

---
 apps/opencs/editor.cpp         | 7 -------
 apps/openmw/main.cpp           | 1 -
 components/debug/debugging.cpp | 9 +++++++++
 3 files changed, 9 insertions(+), 8 deletions(-)

diff --git a/apps/opencs/editor.cpp b/apps/opencs/editor.cpp
index 6ca10e0f6..73208b926 100644
--- a/apps/opencs/editor.cpp
+++ b/apps/opencs/editor.cpp
@@ -5,9 +5,6 @@
 #include <QLocalSocket>
 #include <QMessageBox>
 
-
-#include <components/crashcatcher/crashcatcher.hpp>
-
 #include <components/fallback/validate.hpp>
 
 #include <components/nifosg/nifloader.hpp>
@@ -27,10 +24,6 @@ CS::Editor::Editor (int argc, char **argv)
   mLock(), mMerge (mDocumentManager),
   mIpcServerName ("org.openmw.OpenCS"), mServer(NULL), mClientSocket(NULL)
 {    
-    // install the crash handler as soon as possible. note that the log path
-    // does not depend on config being read.
-    crashCatcherInstall(argc, argv, (mCfgMgr.getLogPath() / "openmw-cs-crash.log").string());
-
     std::pair<Files::PathContainer, std::vector<std::string> > config = readConfig();
 
     setupDataFiles (config.first);
diff --git a/apps/openmw/main.cpp b/apps/openmw/main.cpp
index bbe1267b1..f9cf58544 100644
--- a/apps/openmw/main.cpp
+++ b/apps/openmw/main.cpp
@@ -1,7 +1,6 @@
 #include <iostream>
 
 #include <components/version/version.hpp>
-#include <components/crashcatcher/crashcatcher.hpp>
 #include <components/files/configurationmanager.hpp>
 #include <components/files/escape.hpp>
 #include <components/fallback/validate.hpp>
diff --git a/components/debug/debugging.cpp b/components/debug/debugging.cpp
index 470df47a7..a4c59c221 100644
--- a/components/debug/debugging.cpp
+++ b/components/debug/debugging.cpp
@@ -1,5 +1,7 @@
 #include "debugging.hpp"
 
+#include <components/crashcatcher/crashcatcher.hpp>
+
 namespace Debug
 {
     std::streamsize DebugOutputBase::write(const char *str, std::streamsize size)
@@ -61,12 +63,14 @@ int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, c
 #endif
 
     const std::string logName = Misc::StringUtils::lowerCase(appName) + ".log";
+    const std::string crashLogName = Misc::StringUtils::lowerCase(appName) + "-crash.log";
     boost::filesystem::ofstream logfile;
 
     int ret = 0;
     try
     {
         Files::ConfigurationManager cfgMgr;
+
 #if defined(_WIN32) && defined(_DEBUG)
         // Redirect cout and cerr to VS debug output when running in debug mode
         boost::iostreams::stream_buffer<Debug::DebugOutput> sb;
@@ -85,6 +89,11 @@ int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, c
         std::cout.rdbuf (&coutsb);
         std::cerr.rdbuf (&cerrsb);
 #endif
+
+        // 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());
+
         ret = innerApplication(argc, argv);
     }
     catch (std::exception& e)