OSDN Git Service

Improved exception handling.
authorlordmulder <mulder2@gmx.de>
Wed, 16 Apr 2014 12:57:32 +0000 (14:57 +0200)
committerlordmulder <mulder2@gmx.de>
Wed, 16 Apr 2014 12:57:32 +0000 (14:57 +0200)
15 files changed:
src/binaries.cpp
src/checksum.cpp
src/encoder_abstract.cpp
src/encoder_abstract.h
src/encoder_x264.cpp
src/encoder_x265.cpp
src/global.cpp
src/global.h
src/ipc.cpp
src/main.cpp
src/taskbar7.cpp
src/version.h
src/win_addJob.cpp
src/win_main.cpp
src/win_updater.cpp

index 4837e0b..5e2d7f0 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "binaries.h"
 
+#include "global.h"
 #include "model_sysinfo.h"
 #include "model_preferences.h"
 #include "model_options.h"
@@ -67,7 +68,7 @@ QString ENC_BINARY(const SysinfoModel *sysinfo, const OptionsModel::EncType &enc
        //Sanity check
        if(baseName.isEmpty() || arch.isEmpty() || variant.isEmpty())
        {
-               throw "Failed to determine the encoder binarty path!";
+               THROW("Failed to determine the encoder binarty path!");
        }
 
        //Return path
index c160155..4f78434 100644 (file)
@@ -33,6 +33,8 @@
 */
 
 #include "checksum.h"
+
+#include "global.h"
 #include "3rd_party/blake2.h"
 
 #include <malloc.h>
@@ -49,7 +51,7 @@ class QBlake2ChecksumContext
        {
                if(!(state = (blake2b_state*) _aligned_malloc(sizeof(blake2b_state), 64)))
                {
-                       throw std::runtime_error("Aligend malloc has failed!");
+                       THROW("Aligend malloc has failed!");
                }
                memset(state, 0, sizeof(blake2b_state));
        }
@@ -89,14 +91,14 @@ void QBlake2Checksum::update(const QByteArray &data)
 {
        if(m_finalized)
        {
-               throw std::runtime_error("BLAKE2 was already finalized!");
+               THROW("BLAKE2 was already finalized!");
        }
 
        if(data.size() > 0)
        {
                if(blake2b_update(m_context->state, (const uint8_t*) data.constData(), data.size()) != 0)
                {
-                       throw std::runtime_error("BLAKE2 internal error!");
+                       THROW("BLAKE2 internal error!");
                }
        }
 }
@@ -129,7 +131,7 @@ QByteArray QBlake2Checksum::finalize(const bool bAsHex)
        {
                if(blake2b_final(m_context->state, (uint8_t*) m_hash.data(), m_hash.size()) != 0)
                {
-                       throw std::runtime_error("BLAKE2 internal error!");
+                       THROW("BLAKE2 internal error!");
                }
 
                m_finalized = true;
index 24d70cb..4ffafbf 100644 (file)
@@ -326,3 +326,8 @@ QString AbstractEncoder::sizeToString(qint64 size)
 
        return tr("N/A");
 }
+
+const AbstractEncoderInfo& AbstractEncoder::getEncoderInfo(void)
+{
+       THROW("[getEncoderInfo] This function must be overwritten in sub-classes!");
+}
index d3638a9..881a33c 100644 (file)
@@ -42,6 +42,7 @@ public:
        virtual ~AbstractEncoder(void);
 
        virtual bool runEncodingPass(AbstractSource* pipedSource, const QString outputFile, const unsigned int &frames, const int &pass = 0, const QString &passLogFile = QString());
+       
        static const AbstractEncoderInfo& getEncoderInfo(void);
 
 protected:
index 9fa2719..a54abe3 100644 (file)
@@ -21,6 +21,7 @@
 
 #include "encoder_x264.h"
 
+#include "global.h"
 #include "model_options.h"
 #include "model_status.h"
 #include "binaries.h"
@@ -144,7 +145,7 @@ X264Encoder::X264Encoder(JobObject *jobObject, const OptionsModel *options, cons
 {
        if(options->encType() != OptionsModel::EncType_X264)
        {
-               throw "Invalid encoder type!";
+               THROW("Invalid encoder type!");
        }
 }
 
@@ -250,8 +251,7 @@ void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, co
                cmdLine << "--bitrate" << QString::number(m_options->bitrate());
                break;
        default:
-               throw "Bad rate-control mode !!!";
-               break;
+               THROW("Bad rate-control mode !!!");
        }
        
        if((pass == 1) || (pass == 2))
@@ -296,7 +296,7 @@ void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, co
        
        if(usePipe)
        {
-               if(frames < 1) throw "Frames not set!";
+               if(frames < 1) THROW("Frames not set!");
                cmdLine << "--frames" << QString::number(frames);
                cmdLine << "--demuxer" << "y4m";
                cmdLine << "--stdin" << "y4m" << "-";
index 42c551a..a1275bf 100644 (file)
 
 #include "encoder_x265.h"
 
+#include "global.h"
 #include "model_options.h"
 #include "model_status.h"
 #include "binaries.h"
-#include "binaries.h"
 
 #include <QStringList>
 #include <QDir>
@@ -132,7 +132,7 @@ X265Encoder::X265Encoder(JobObject *jobObject, const OptionsModel *options, cons
 {
        if(options->encType() != OptionsModel::EncType_X265)
        {
-               throw "Invalid encoder type!";
+               THROW("Invalid encoder type!");
        }
 }
 
@@ -221,7 +221,7 @@ void X265Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, co
                cmdLine << "--bitrate" << QString::number(m_options->bitrate());
                break;
        default:
-               throw "Bad rate-control mode !!!";
+               THROW("Bad rate-control mode !!!");
                break;
        }
        
@@ -267,7 +267,7 @@ void X265Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, co
        
        if(usePipe)
        {
-               if(frames < 1) throw "Frames not set!";
+               if(frames < 1) THROW("Frames not set!");
                cmdLine << "--frames" << QString::number(frames);
                cmdLine << "--y4m" << "-";
        }
index c94ffbb..fff821a 100644 (file)
@@ -650,7 +650,7 @@ void x264_message_handler(QtMsgType type, const char *msg)
        if((type == QtCriticalMsg) || (type == QtFatalMsg))
        {
                lock.unlock();
-               x264_fatal_exit(L"The application has encountered a critical error and will exit now!", QWCHAR(QString::fromUtf8(msg)));
+               x264_fatal_exit(L"The application has encountered a critical error and will exit now!", QString::fromUtf8(msg).toLatin1().constData());
        }
 }
 
@@ -2184,6 +2184,19 @@ bool x264_set_thread_execution_state(const bool systemRequired)
 }
 
 /*
+ * Exception class
+ */
+X264Exception::X264Exception(const char *message, ...)
+:
+       runtime_error(message)
+{
+       va_list args;
+       va_start(args, message);
+       vsnprintf_s(m_message, MAX_MSGLEN, _TRUNCATE, message, args);
+       va_end(args);
+}
+
+/*
  * Check for debugger (detect routine)
  */
 static __forceinline bool x264_check_for_debugger(void)
@@ -2241,7 +2254,7 @@ static HANDLE x264_debug_thread_init()
  * Fatal application exit
  */
 #pragma intrinsic(_InterlockedExchange)
-void x264_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessage)
+void x264_fatal_exit(const wchar_t* exitMessage, const char* errorBoxMessage)
 {
        static volatile long bFatalFlag = 0L;
 
@@ -2255,7 +2268,7 @@ void x264_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessage)
        
                if(errorBoxMessage)
                {
-                       MessageBoxW(NULL, errorBoxMessage, L"Simple x264 Launcher - GURU MEDITATION", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL);
+                       MessageBoxA(NULL, errorBoxMessage, "Simple x264 Launcher - GURU MEDITATION", MB_ICONERROR | MB_TOPMOST | MB_TASKMODAL);
                }
 
                FatalAppExit(0, exitMessage);
@@ -2332,7 +2345,7 @@ size_t x264_dbg_private_bytes(void)
        GetProcessMemoryInfo(GetCurrentProcess(), (PPROCESS_MEMORY_COUNTERS) &memoryCounters, sizeof(PROCESS_MEMORY_COUNTERS_EX));
        return memoryCounters.PrivateUsage;
 #else
-       throw "Cannot call this function in a non-debug build!";
+       THROW("Cannot call this function in a non-debug build!");
 #endif //X264_DEBUG
 }
 
index 94a3fba..8510741 100644 (file)
@@ -23,6 +23,7 @@
 
 #define _CRT_RAND_S
 #include <cstdlib>
+#include <stdexcept>
 
 //Forward declarations
 class QString;
@@ -113,6 +114,17 @@ extern const x264_os_version_t x264_winver_win70;
 extern const x264_os_version_t x264_winver_win80;
 extern const x264_os_version_t x264_winver_win81;
 
+//Exception class
+class X264Exception : public std::runtime_error
+{
+public:
+       X264Exception(const char *message, ...);
+       virtual const char* what() const { return m_message; }
+private:
+       static const size_t MAX_MSGLEN = 256;
+       char m_message[MAX_MSGLEN];
+};
+
 ///////////////////////////////////////////////////////////////////////////////
 // GLOBAL FUNCTIONS
 ///////////////////////////////////////////////////////////////////////////////
@@ -130,7 +142,7 @@ const QString &x264_data_path(void);
 size_t x264_dbg_private_bytes(void);
 x264_cpu_t x264_detect_cpu_features(const int argc, char **argv);
 bool x264_enable_close_button(const QWidget *win, const bool bEnable);
-void x264_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessage = NULL);
+void x264_fatal_exit(const wchar_t* exitMessage, const char* errorBoxMessage = NULL);
 void x264_finalization(void);
 void x264_init_console(const int argc, char **argv);
 void x264_init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir = true);
@@ -193,16 +205,9 @@ const char *x264_version_time(void);
 #endif
 
 //Helper macro for throwing exceptions
-#define THROW(MESSAGE) do \
-{ \
-       throw std::runtime_error((MESSAGE)); \
-} \
-while(0)
-#define THROW_FMT(FORMAT, ...) do \
+#define THROW(MESSAGE, ...) do \
 { \
-       char _error_msg[512]; \
-       _snprintf_s(_error_msg, 512, _TRUNCATE, (FORMAT), __VA_ARGS__); \
-       throw std::runtime_error(_error_msg); \
+       throw X264Exception((MESSAGE), __VA_ARGS__); \
 } \
 while(0)
 
index 2321f38..eb81b18 100644 (file)
@@ -259,7 +259,7 @@ bool IPCCore::pushCommand(const int &command, const QStringList *args, const uns
 {
        if(m_initialized < 0)
        {
-               throw std::runtime_error("IPC not initialized!");
+               THROW("IPC not initialized!");
        }
 
        if(!m_semaphoreWr->acquire())
@@ -320,7 +320,7 @@ bool IPCCore::popCommand(int &command, QStringList &args, unsigned int &flags)
 
        if(m_initialized < 0)
        {
-               throw std::runtime_error("IPC not initialized!");
+               THROW("IPC not initialized!");
        }
 
        if(!m_semaphoreRd->acquire())
index 0cc2fa3..b55357e 100644 (file)
@@ -187,6 +187,15 @@ void handleMultipleInstances(const QStringList &args, IPC *ipc)
 LONG WINAPI x264_exception_handler(__in struct _EXCEPTION_POINTERS *ExceptionInfo);
 void x264_invalid_param_handler(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, uintptr_t);
 
+#define PRINT_ERROR(MESSAGE, ...) do \
+{ \
+       fflush(stdout); \
+       fflush(stderr); \
+       fprintf(stderr, (MESSAGE), __VA_ARGS__); \
+       fflush(stderr); \
+} \
+while(0)
+
 static int _main(int argc, char* argv[])
 {
        if(X264_DEBUG)
@@ -206,25 +215,29 @@ static int _main(int argc, char* argv[])
                        iResult = x264_main(argc, argv);
                        x264_finalization();
                }
+               catch(const X264Exception &e)
+               {
+                       PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error message: %s\n", e.what());
+                       x264_fatal_exit(L"An internal error was detected, application will exit!", e.what());
+               }
+               catch(const std::exception &e)
+               {
+                       PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error message: %s\n", e.what());
+                       x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!", e.what());
+               }
                catch(char *error)
                {
-                       fflush(stdout);
-                       fflush(stderr);
-                       fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error message: %s\n", error);
+                       PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error message: %s\n", error);
                        x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
                }
                catch(int error)
                {
-                       fflush(stdout);
-                       fflush(stderr);
-                       fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error code: 0x%X\n", error);
+                       PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error code: 0x%X\n", error);
                        x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
                }
                catch(...)
                {
-                       fflush(stdout);
-                       fflush(stderr);
-                       fprintf(stderr, "\nGURU MEDITATION !!!\n");
+                       PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown C++ exception!\n");
                        x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
                }
                return iResult;
index 8f3d5f9..809266b 100644 (file)
@@ -30,7 +30,7 @@ static ITaskbarList3 *s_ptbl = NULL;
 
 WinSevenTaskbar::WinSevenTaskbar(void)
 {
-       throw "Cannot create instance of this class!";
+       THROW("Cannot create instance of this class!");
 }
 
 WinSevenTaskbar::~WinSevenTaskbar(void)
index b274a20..2d66b3c 100644 (file)
@@ -26,7 +26,7 @@
 #define VER_X264_MAJOR 2
 #define VER_X264_MINOR 3
 #define VER_X264_PATCH 5
-#define VER_X264_BUILD 816
+#define VER_X264_BUILD 818
 
 #define VER_X264_PORTABLE_EDITION (0)
 
index 3d475e3..b009879 100644 (file)
@@ -442,6 +442,8 @@ void AddJobDialog::encoderIndexChanged(int index)
        ui->labelProfile->setEnabled(!noProf);
        ui->cbxProfile->setEnabled(!noProf);
        if(noProf) ui->cbxProfile->setCurrentIndex(0);
+
+       variantIndexChanged(ui->cbxEncoderVariant->currentIndex());
 }
 
 void AddJobDialog::variantIndexChanged(int index)
@@ -451,12 +453,14 @@ void AddJobDialog::variantIndexChanged(int index)
        ui->labelProfile->setEnabled(!noProf);
        ui->cbxProfile->setEnabled(!noProf);
        if(noProf) ui->cbxProfile->setCurrentIndex(0);
+
+       modeIndexChanged(ui->cbxRateControlMode->currentIndex());
 }
 
 void AddJobDialog::modeIndexChanged(int index)
 {
-       ui->spinQuantizer->setEnabled(index == 0 || index == 1);
-       ui->spinBitrate->setEnabled(index == 2 || index == 3);
+       ui->spinQuantizer->setEnabled(index == OptionsModel::RCMode_CRF || index == OptionsModel::RCMode_CQ);
+       ui->spinBitrate  ->setEnabled(index == OptionsModel::RCMode_ABR || index == OptionsModel::RCMode_2Pass);
 }
 
 void AddJobDialog::accept(void)
index af03323..4a767f8 100644 (file)
@@ -1105,7 +1105,7 @@ void MainWindow::handleCommand(const int &command, const QStringList &args, cons
                }
                break;
        default:
-               throw std::exception("Unknown command received!");
+               THROW("Unknown command received!");
        }
 }
 
index f6e0af4..b965873 100644 (file)
@@ -304,7 +304,7 @@ void UpdaterDialog::threadStatusChanged(int status)
                UPDATE_ICON(3, "play");
                break;
        default:
-               throw "Unknown status code!";
+               THROW("Unknown status code!");
        }
 }