From c454f1bdad953cdc84af1f4b3242a081146ad090 Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Fri, 3 Aug 2018 08:58:50 +0400 Subject: [PATCH 1/5] Use log file for editor (feature #4012) --- CHANGELOG.md | 1 + apps/opencs/main.cpp | 59 ++++++++++++++++++++++++++++++++--- apps/openmw/main.cpp | 51 +++++------------------------- components/misc/debugging.hpp | 47 ++++++++++++++++++++++++++++ 4 files changed, 109 insertions(+), 49 deletions(-) create mode 100644 components/misc/debugging.hpp diff --git a/CHANGELOG.md b/CHANGELOG.md index 1923e5237..63bd55184 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,6 +79,7 @@ Feature #3276: Editor: Search- Show number of (remaining) search results and indicate a search without any results Feature #3641: Editor: Limit FPS in 3d preview window Feature #3703: Ranged sneak attack criticals + Feature #4012: OpenMW-CS: Create log file like with OpenMW Feature #4222: 360° screenshots Feature #4256: Implement ToggleBorders (TB) console command Feature #4324: Add CFBundleIdentifier in Info.plist to allow for macOS function key shortcuts diff --git a/apps/opencs/main.cpp b/apps/opencs/main.cpp index d599f1f96..7b138169b 100644 --- a/apps/opencs/main.cpp +++ b/apps/opencs/main.cpp @@ -8,10 +8,13 @@ #include #include -#include "model/doc/messages.hpp" +#include +#include "model/doc/messages.hpp" #include "model/world/universalid.hpp" +#include + #ifdef Q_OS_MAC #include #endif @@ -47,8 +50,43 @@ int main(int argc, char *argv[]) setenv("OSG_GL_TEXTURE_STORAGE", "OFF", 0); #endif + // 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 (); + +#if !(defined(_WIN32) && defined(_DEBUG)) + boost::iostreams::stream_buffer coutsb; + boost::iostreams::stream_buffer cerrsb; +#endif + + std::ostream oldcout(cout_rdbuf); + std::ostream oldcerr(cerr_rdbuf); + + 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 sb; + sb.open(Misc::DebugOutput()); + std::cout.rdbuf (&sb); + std::cerr.rdbuf (&sb); +#else + // Redirect cout and cerr to openmw.log + logfile.open (boost::filesystem::path(cfgMgr.getLogPath() / "/openmw-cs.log")); + + coutsb.open (Misc::Tee(logfile, oldcout)); + cerrsb.open (Misc::Tee(logfile, oldcerr)); + + std::cout.rdbuf (&coutsb); + std::cerr.rdbuf (&cerrsb); +#endif + // To allow background thread drawing in OSG QApplication::setAttribute(Qt::AA_X11InitThreads, true); @@ -74,12 +112,23 @@ int main(int argc, char *argv[]) editor.connectToIPCServer(); return 0; } - return editor.run(); + ret = editor.run(); } catch (std::exception& e) { - std::cerr << "ERROR: " << e.what() << std::endl; - return 0; +#if (defined(__APPLE__) || defined(__linux) || defined(__unix) || defined(__posix)) + if (!isatty(fileno(stdin))) +#endif + SDL_ShowSimpleMessageBox(0, "OpenMW-CS: Fatal error", e.what(), NULL); + + std::cerr << "\nERROR: " << e.what() << std::endl; + + ret = 1; } + // Restore cout and cerr + std::cout.rdbuf(cout_rdbuf); + std::cerr.rdbuf(cerr_rdbuf); + + return ret; } diff --git a/apps/openmw/main.cpp b/apps/openmw/main.cpp index d13a73127..ed7672694 100644 --- a/apps/openmw/main.cpp +++ b/apps/openmw/main.cpp @@ -5,6 +5,7 @@ #include #include #include +#include #include #include "engine.hpp" @@ -241,44 +242,6 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat return true; } -#if defined(_WIN32) && defined(_DEBUG) - -class DebugOutput : public boost::iostreams::sink -{ -public: - std::streamsize write(const char *str, std::streamsize size) - { - // Make a copy for null termination - std::string tmp (str, static_cast(size)); - // Write string to Visual Studio Debug output - OutputDebugString (tmp.c_str ()); - return size; - } -}; -#else -class Tee : public boost::iostreams::sink -{ -public: - Tee(std::ostream &stream, std::ostream &stream2) - : out(stream), out2(stream2) - { - } - - std::streamsize write(const char *str, std::streamsize size) - { - out.write (str, size); - out.flush(); - out2.write (str, size); - out2.flush(); - return size; - } - -private: - std::ostream &out; - std::ostream &out2; -}; -#endif - #ifdef ANDROID extern "C" int SDL_main(int argc, char**argv) #else @@ -295,8 +258,8 @@ int main(int argc, char**argv) std::streambuf* cerr_rdbuf = std::cerr.rdbuf (); #if !(defined(_WIN32) && defined(_DEBUG)) - boost::iostreams::stream_buffer coutsb; - boost::iostreams::stream_buffer cerrsb; + boost::iostreams::stream_buffer coutsb; + boost::iostreams::stream_buffer cerrsb; #endif std::ostream oldcout(cout_rdbuf); @@ -313,16 +276,16 @@ int main(int argc, char**argv) #if defined(_WIN32) && defined(_DEBUG) // Redirect cout and cerr to VS debug output when running in debug mode - boost::iostreams::stream_buffer sb; - sb.open(DebugOutput()); + boost::iostreams::stream_buffer sb; + sb.open(Misc::DebugOutput()); std::cout.rdbuf (&sb); std::cerr.rdbuf (&sb); #else // Redirect cout and cerr to openmw.log logfile.open (boost::filesystem::path(cfgMgr.getLogPath() / "/openmw.log")); - coutsb.open (Tee(logfile, oldcout)); - cerrsb.open (Tee(logfile, oldcerr)); + coutsb.open (Misc::Tee(logfile, oldcout)); + cerrsb.open (Misc::Tee(logfile, oldcerr)); std::cout.rdbuf (&coutsb); std::cerr.rdbuf (&cerrsb); diff --git a/components/misc/debugging.hpp b/components/misc/debugging.hpp new file mode 100644 index 000000000..b367f0912 --- /dev/null +++ b/components/misc/debugging.hpp @@ -0,0 +1,47 @@ +#ifndef MISC_DEBUGGING_H +#define MISC_DEBUGGING_H + +#include + +namespace Misc +{ + #if defined(_WIN32) && defined(_DEBUG) + + class DebugOutput : public boost::iostreams::sink + { + public: + std::streamsize write(const char *str, std::streamsize size) + { + // Make a copy for null termination + std::string tmp (str, static_cast(size)); + // Write string to Visual Studio Debug output + OutputDebugString (tmp.c_str ()); + return size; + } + }; + #else + class Tee : public boost::iostreams::sink + { + public: + Tee(std::ostream &stream, std::ostream &stream2) + : out(stream), out2(stream2) + { + } + + std::streamsize write(const char *str, std::streamsize size) + { + out.write (str, size); + out.flush(); + out2.write (str, size); + out2.flush(); + return size; + } + + private: + std::ostream &out; + std::ostream &out2; + }; + #endif +} + +#endif From ac987979998cf6af73acf18e48bd1ccfe1d846a3 Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Fri, 3 Aug 2018 15:05:13 +0400 Subject: [PATCH 2/5] Add missing file --- components/CMakeLists.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/CMakeLists.txt b/components/CMakeLists.txt index b6f390b06..e2e6b97bb 100644 --- a/components/CMakeLists.txt +++ b/components/CMakeLists.txt @@ -85,7 +85,7 @@ add_component_dir (esmterrain ) add_component_dir (misc - utf8stream stringops resourcehelpers rng messageformatparser + utf8stream stringops resourcehelpers rng debugging messageformatparser ) IF(NOT WIN32 AND NOT APPLE) From c2a175c2e01af1e3896b2a7714a8dc43a2de773d Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Fri, 3 Aug 2018 15:51:17 +0400 Subject: [PATCH 3/5] Move crash catcher wrapper to separate file --- apps/opencs/main.cpp | 100 +++++++++------------------------- apps/openmw/main.cpp | 88 ++++++------------------------ components/misc/debugging.hpp | 70 +++++++++++++++++++++++- 3 files changed, 109 insertions(+), 149 deletions(-) diff --git a/apps/opencs/main.cpp b/apps/opencs/main.cpp index 7b138169b..ebc686c23 100644 --- a/apps/opencs/main.cpp +++ b/apps/opencs/main.cpp @@ -13,8 +13,6 @@ #include "model/doc/messages.hpp" #include "model/world/universalid.hpp" -#include - #ifdef Q_OS_MAC #include #endif @@ -44,91 +42,43 @@ class Application : public QApplication Application (int& argc, char *argv[]) : QApplication (argc, argv) {} }; -int main(int argc, char *argv[]) +int runApplication(int argc, char *argv[]) { - #ifdef Q_OS_MAC - setenv("OSG_GL_TEXTURE_STORAGE", "OFF", 0); - #endif - - // 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 (); - -#if !(defined(_WIN32) && defined(_DEBUG)) - boost::iostreams::stream_buffer coutsb; - boost::iostreams::stream_buffer cerrsb; -#endif - - std::ostream oldcout(cout_rdbuf); - std::ostream oldcerr(cerr_rdbuf); - - 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 sb; - sb.open(Misc::DebugOutput()); - std::cout.rdbuf (&sb); - std::cerr.rdbuf (&sb); -#else - // Redirect cout and cerr to openmw.log - logfile.open (boost::filesystem::path(cfgMgr.getLogPath() / "/openmw-cs.log")); - - coutsb.open (Misc::Tee(logfile, oldcout)); - cerrsb.open (Misc::Tee(logfile, oldcerr)); - - std::cout.rdbuf (&coutsb); - std::cerr.rdbuf (&cerrsb); +#ifdef Q_OS_MAC + setenv("OSG_GL_TEXTURE_STORAGE", "OFF", 0); #endif - // To allow background thread drawing in OSG - QApplication::setAttribute(Qt::AA_X11InitThreads, true); + // To allow background thread drawing in OSG + QApplication::setAttribute(Qt::AA_X11InitThreads, true); - Q_INIT_RESOURCE (resources); + Q_INIT_RESOURCE (resources); - qRegisterMetaType ("std::string"); - qRegisterMetaType ("CSMWorld::UniversalId"); - qRegisterMetaType ("CSMDoc::Message"); + qRegisterMetaType ("std::string"); + qRegisterMetaType ("CSMWorld::UniversalId"); + qRegisterMetaType ("CSMDoc::Message"); - Application application (argc, argv); + Application application (argc, argv); - #ifdef Q_OS_MAC - QDir dir(QCoreApplication::applicationDirPath()); - QDir::setCurrent(dir.absolutePath()); - #endif +#ifdef Q_OS_MAC + QDir dir(QCoreApplication::applicationDirPath()); + QDir::setCurrent(dir.absolutePath()); +#endif - application.setWindowIcon (QIcon (":./openmw-cs.png")); + application.setWindowIcon (QIcon (":./openmw-cs.png")); - CS::Editor editor(argc, argv); + CS::Editor editor(argc, argv); - if(!editor.makeIPCServer()) - { - editor.connectToIPCServer(); - return 0; - } - ret = editor.run(); - } - catch (std::exception& e) + if(!editor.makeIPCServer()) { -#if (defined(__APPLE__) || defined(__linux) || defined(__unix) || defined(__posix)) - if (!isatty(fileno(stdin))) -#endif - SDL_ShowSimpleMessageBox(0, "OpenMW-CS: Fatal error", e.what(), NULL); - - std::cerr << "\nERROR: " << e.what() << std::endl; - - ret = 1; + editor.connectToIPCServer(); + return 0; } - // Restore cout and cerr - std::cout.rdbuf(cout_rdbuf); - std::cerr.rdbuf(cerr_rdbuf); + return editor.run(); +} + - return ret; +int main(int argc, char *argv[]) +{ + return wrapApplication(&runApplication, argc, argv, "/openmw-cs.log"); } diff --git a/apps/openmw/main.cpp b/apps/openmw/main.cpp index ed7672694..2b4fa9147 100644 --- a/apps/openmw/main.cpp +++ b/apps/openmw/main.cpp @@ -7,7 +7,6 @@ #include #include -#include #include "engine.hpp" #include @@ -242,86 +241,33 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat return true; } -#ifdef ANDROID -extern "C" int SDL_main(int argc, char**argv) -#else -int main(int argc, char**argv) -#endif +int runApplication(int argc, char *argv[]) { -#if defined(__APPLE__) +#ifdef __APPLE__ + boost::filesystem::path binary_path = boost::filesystem::system_complete(boost::filesystem::path(argv[0])); + boost::filesystem::current_path(binary_path.parent_path()); setenv("OSG_GL_TEXTURE_STORAGE", "OFF", 0); #endif - // 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 (); - -#if !(defined(_WIN32) && defined(_DEBUG)) - boost::iostreams::stream_buffer coutsb; - boost::iostreams::stream_buffer cerrsb; -#endif - - std::ostream oldcout(cout_rdbuf); - std::ostream oldcerr(cerr_rdbuf); - - boost::filesystem::ofstream logfile; - + Files::ConfigurationManager cfgMgr; std::unique_ptr engine; + engine.reset(new OMW::Engine(cfgMgr)); - 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 sb; - sb.open(Misc::DebugOutput()); - std::cout.rdbuf (&sb); - std::cerr.rdbuf (&sb); -#else - // Redirect cout and cerr to openmw.log - logfile.open (boost::filesystem::path(cfgMgr.getLogPath() / "/openmw.log")); - - coutsb.open (Misc::Tee(logfile, oldcout)); - cerrsb.open (Misc::Tee(logfile, oldcerr)); - - std::cout.rdbuf (&coutsb); - std::cerr.rdbuf (&cerrsb); -#endif - - crashCatcherInstall(argc, argv, (cfgMgr.getLogPath() / "crash.log").string()); - -#ifdef __APPLE__ - boost::filesystem::path binary_path = boost::filesystem::system_complete(boost::filesystem::path(argv[0])); - boost::filesystem::current_path(binary_path.parent_path()); -#endif - - engine.reset(new OMW::Engine(cfgMgr)); - - if (parseOptions(argc, argv, *engine, cfgMgr)) - { - engine->go(); - } - } - catch (std::exception &e) + if (parseOptions(argc, argv, *engine, cfgMgr)) { -#if (defined(__APPLE__) || defined(__linux) || defined(__unix) || defined(__posix)) - if (!isatty(fileno(stdin))) -#endif - SDL_ShowSimpleMessageBox(0, "OpenMW: Fatal error", e.what(), NULL); - - std::cerr << "\nERROR: " << e.what() << std::endl; - - ret = 1; + engine->go(); } - // Restore cout and cerr - std::cout.rdbuf(cout_rdbuf); - std::cerr.rdbuf(cerr_rdbuf); + return 0; +} - return ret; +#ifdef ANDROID +extern "C" int SDL_main(int argc, char**argv) +#else +int main(int argc, char**argv) +#endif +{ + return wrapApplication(&runApplication, argc, argv, "/openmw.log"); } // Platform specific for Windows when there is no console built into the executable. diff --git a/components/misc/debugging.hpp b/components/misc/debugging.hpp index b367f0912..af13d0cc6 100644 --- a/components/misc/debugging.hpp +++ b/components/misc/debugging.hpp @@ -3,9 +3,11 @@ #include +#include + namespace Misc { - #if defined(_WIN32) && defined(_DEBUG) +#if defined(_WIN32) && defined(_DEBUG) class DebugOutput : public boost::iostreams::sink { @@ -19,7 +21,7 @@ namespace Misc return size; } }; - #else +#else class Tee : public boost::iostreams::sink { public: @@ -41,7 +43,69 @@ namespace Misc std::ostream &out; std::ostream &out2; }; - #endif +#endif +} + +int wrapApplication(int (*innerApplication)(int argc, char *argv[]), int argc, char *argv[], const std::string& logName) +{ + // 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 (); + +#if !(defined(_WIN32) && defined(_DEBUG)) + boost::iostreams::stream_buffer coutsb; + boost::iostreams::stream_buffer cerrsb; +#endif + + std::ostream oldcout(cout_rdbuf); + std::ostream oldcerr(cerr_rdbuf); + + 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 sb; + sb.open(Misc::DebugOutput()); + std::cout.rdbuf (&sb); + std::cerr.rdbuf (&sb); +#else + // Redirect cout and cerr to the log file + boost::filesystem::ofstream logfile; + logfile.open (boost::filesystem::path(cfgMgr.getLogPath() / logName)); + + std::ostream oldcout(cout_rdbuf); + std::ostream oldcerr(cerr_rdbuf); + + coutsb.open (Misc::Tee(logfile, oldcout)); + cerrsb.open (Misc::Tee(logfile, oldcerr)); + + std::cout.rdbuf (&coutsb); + std::cerr.rdbuf (&cerrsb); +#endif + ret = innerApplication(argc, argv); + } + catch (std::exception& e) + { +#if (defined(__APPLE__) || defined(__linux) || defined(__unix) || defined(__posix)) + if (!isatty(fileno(stdin))) +#endif + SDL_ShowSimpleMessageBox(0, "OpenMW: Fatal error", e.what(), NULL); + + std::cerr << "\nERROR: " << e.what() << std::endl; + + ret = 1; + } + + // Restore cout and cerr + std::cout.rdbuf(cout_rdbuf); + std::cerr.rdbuf(cerr_rdbuf); + + return ret; } #endif From e2519226aa73bfa524aae5d2eac9db845ea17977 Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Fri, 3 Aug 2018 17:04:07 +0400 Subject: [PATCH 4/5] Move boost include --- apps/openmw/main.cpp | 2 -- components/misc/debugging.hpp | 1 + 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/apps/openmw/main.cpp b/apps/openmw/main.cpp index 2b4fa9147..d9fff6952 100644 --- a/apps/openmw/main.cpp +++ b/apps/openmw/main.cpp @@ -9,8 +9,6 @@ #include "engine.hpp" -#include - #if defined(_WIN32) // For OutputDebugString #ifndef WIN32_LEAN_AND_MEAN diff --git a/components/misc/debugging.hpp b/components/misc/debugging.hpp index af13d0cc6..a983a88df 100644 --- a/components/misc/debugging.hpp +++ b/components/misc/debugging.hpp @@ -1,6 +1,7 @@ #ifndef MISC_DEBUGGING_H #define MISC_DEBUGGING_H +#include #include #include From dd6cb85783b81e0f66872217e72cf7243d622f24 Mon Sep 17 00:00:00 2001 From: Andrei Kortunov Date: Fri, 3 Aug 2018 19:31:10 +0400 Subject: [PATCH 5/5] Remove redundant changelog entry --- CHANGELOG.md | 1 - 1 file changed, 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 63bd55184..1923e5237 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -79,7 +79,6 @@ Feature #3276: Editor: Search- Show number of (remaining) search results and indicate a search without any results Feature #3641: Editor: Limit FPS in 3d preview window Feature #3703: Ranged sneak attack criticals - Feature #4012: OpenMW-CS: Create log file like with OpenMW Feature #4222: 360° screenshots Feature #4256: Implement ToggleBorders (TB) console command Feature #4324: Add CFBundleIdentifier in Info.plist to allow for macOS function key shortcuts