Use log file for editor (feature #4012)

remotes/1728160796594174844/tmp_0.7.0-alpha
Andrei Kortunov 7 years ago
parent 1c13256456
commit c454f1bdad

@ -79,6 +79,7 @@
Feature #3276: Editor: Search- Show number of (remaining) search results and indicate a search without any results 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 #3641: Editor: Limit FPS in 3d preview window
Feature #3703: Ranged sneak attack criticals Feature #3703: Ranged sneak attack criticals
Feature #4012: OpenMW-CS: Create log file like with OpenMW
Feature #4222: 360° screenshots Feature #4222: 360° screenshots
Feature #4256: Implement ToggleBorders (TB) console command Feature #4256: Implement ToggleBorders (TB) console command
Feature #4324: Add CFBundleIdentifier in Info.plist to allow for macOS function key shortcuts Feature #4324: Add CFBundleIdentifier in Info.plist to allow for macOS function key shortcuts

@ -8,10 +8,13 @@
#include <QIcon> #include <QIcon>
#include <QMetaType> #include <QMetaType>
#include "model/doc/messages.hpp" #include <components/misc/debugging.hpp>
#include "model/doc/messages.hpp"
#include "model/world/universalid.hpp" #include "model/world/universalid.hpp"
#include <SDL_messagebox.h>
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
#include <QDir> #include <QDir>
#endif #endif
@ -47,8 +50,43 @@ int main(int argc, char *argv[])
setenv("OSG_GL_TEXTURE_STORAGE", "OFF", 0); setenv("OSG_GL_TEXTURE_STORAGE", "OFF", 0);
#endif #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<Misc::Tee> coutsb;
boost::iostreams::stream_buffer<Misc::Tee> cerrsb;
#endif
std::ostream oldcout(cout_rdbuf);
std::ostream oldcerr(cerr_rdbuf);
boost::filesystem::ofstream logfile;
int ret = 0;
try 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<Misc::DebugOutput> 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 // To allow background thread drawing in OSG
QApplication::setAttribute(Qt::AA_X11InitThreads, true); QApplication::setAttribute(Qt::AA_X11InitThreads, true);
@ -74,12 +112,23 @@ int main(int argc, char *argv[])
editor.connectToIPCServer(); editor.connectToIPCServer();
return 0; return 0;
} }
return editor.run(); ret = editor.run();
} }
catch (std::exception& e) catch (std::exception& e)
{ {
std::cerr << "ERROR: " << e.what() << std::endl; #if (defined(__APPLE__) || defined(__linux) || defined(__unix) || defined(__posix))
return 0; 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;
} }

@ -5,6 +5,7 @@
#include <components/files/configurationmanager.hpp> #include <components/files/configurationmanager.hpp>
#include <components/files/escape.hpp> #include <components/files/escape.hpp>
#include <components/fallback/validate.hpp> #include <components/fallback/validate.hpp>
#include <components/misc/debugging.hpp>
#include <SDL_messagebox.h> #include <SDL_messagebox.h>
#include "engine.hpp" #include "engine.hpp"
@ -241,44 +242,6 @@ bool parseOptions (int argc, char** argv, OMW::Engine& engine, Files::Configurat
return true; 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<unsigned int>(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 #ifdef ANDROID
extern "C" int SDL_main(int argc, char**argv) extern "C" int SDL_main(int argc, char**argv)
#else #else
@ -295,8 +258,8 @@ int main(int argc, char**argv)
std::streambuf* cerr_rdbuf = std::cerr.rdbuf (); std::streambuf* cerr_rdbuf = std::cerr.rdbuf ();
#if !(defined(_WIN32) && defined(_DEBUG)) #if !(defined(_WIN32) && defined(_DEBUG))
boost::iostreams::stream_buffer<Tee> coutsb; boost::iostreams::stream_buffer<Misc::Tee> coutsb;
boost::iostreams::stream_buffer<Tee> cerrsb; boost::iostreams::stream_buffer<Misc::Tee> cerrsb;
#endif #endif
std::ostream oldcout(cout_rdbuf); std::ostream oldcout(cout_rdbuf);
@ -313,16 +276,16 @@ int main(int argc, char**argv)
#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
boost::iostreams::stream_buffer<DebugOutput> sb; boost::iostreams::stream_buffer<Misc::DebugOutput> sb;
sb.open(DebugOutput()); sb.open(Misc::DebugOutput());
std::cout.rdbuf (&sb); std::cout.rdbuf (&sb);
std::cerr.rdbuf (&sb); std::cerr.rdbuf (&sb);
#else #else
// Redirect cout and cerr to openmw.log // Redirect cout and cerr to openmw.log
logfile.open (boost::filesystem::path(cfgMgr.getLogPath() / "/openmw.log")); logfile.open (boost::filesystem::path(cfgMgr.getLogPath() / "/openmw.log"));
coutsb.open (Tee(logfile, oldcout)); coutsb.open (Misc::Tee(logfile, oldcout));
cerrsb.open (Tee(logfile, oldcerr)); cerrsb.open (Misc::Tee(logfile, oldcerr));
std::cout.rdbuf (&coutsb); std::cout.rdbuf (&coutsb);
std::cerr.rdbuf (&cerrsb); std::cerr.rdbuf (&cerrsb);

@ -0,0 +1,47 @@
#ifndef MISC_DEBUGGING_H
#define MISC_DEBUGGING_H
#include <boost/iostreams/stream.hpp>
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<unsigned int>(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
Loading…
Cancel
Save