Improved exception handling.

This commit is contained in:
LoRd_MuldeR 2014-04-16 14:57:32 +02:00
parent e67d61e8f6
commit 23dacbaddf
15 changed files with 88 additions and 44 deletions

View File

@ -21,6 +21,7 @@
#include "binaries.h" #include "binaries.h"
#include "global.h"
#include "model_sysinfo.h" #include "model_sysinfo.h"
#include "model_preferences.h" #include "model_preferences.h"
#include "model_options.h" #include "model_options.h"
@ -67,7 +68,7 @@ QString ENC_BINARY(const SysinfoModel *sysinfo, const OptionsModel::EncType &enc
//Sanity check //Sanity check
if(baseName.isEmpty() || arch.isEmpty() || variant.isEmpty()) 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 //Return path

View File

@ -33,6 +33,8 @@
*/ */
#include "checksum.h" #include "checksum.h"
#include "global.h"
#include "3rd_party/blake2.h" #include "3rd_party/blake2.h"
#include <malloc.h> #include <malloc.h>
@ -49,7 +51,7 @@ class QBlake2ChecksumContext
{ {
if(!(state = (blake2b_state*) _aligned_malloc(sizeof(blake2b_state), 64))) 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)); memset(state, 0, sizeof(blake2b_state));
} }
@ -89,14 +91,14 @@ void QBlake2Checksum::update(const QByteArray &data)
{ {
if(m_finalized) if(m_finalized)
{ {
throw std::runtime_error("BLAKE2 was already finalized!"); THROW("BLAKE2 was already finalized!");
} }
if(data.size() > 0) if(data.size() > 0)
{ {
if(blake2b_update(m_context->state, (const uint8_t*) data.constData(), 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) 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; m_finalized = true;

View File

@ -326,3 +326,8 @@ QString AbstractEncoder::sizeToString(qint64 size)
return tr("N/A"); return tr("N/A");
} }
const AbstractEncoderInfo& AbstractEncoder::getEncoderInfo(void)
{
THROW("[getEncoderInfo] This function must be overwritten in sub-classes!");
}

View File

@ -42,6 +42,7 @@ public:
virtual ~AbstractEncoder(void); virtual ~AbstractEncoder(void);
virtual bool runEncodingPass(AbstractSource* pipedSource, const QString outputFile, const unsigned int &frames, const int &pass = 0, const QString &passLogFile = QString()); 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); static const AbstractEncoderInfo& getEncoderInfo(void);
protected: protected:

View File

@ -21,6 +21,7 @@
#include "encoder_x264.h" #include "encoder_x264.h"
#include "global.h"
#include "model_options.h" #include "model_options.h"
#include "model_status.h" #include "model_status.h"
#include "binaries.h" #include "binaries.h"
@ -144,7 +145,7 @@ X264Encoder::X264Encoder(JobObject *jobObject, const OptionsModel *options, cons
{ {
if(options->encType() != OptionsModel::EncType_X264) 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()); cmdLine << "--bitrate" << QString::number(m_options->bitrate());
break; break;
default: default:
throw "Bad rate-control mode !!!"; THROW("Bad rate-control mode !!!");
break;
} }
if((pass == 1) || (pass == 2)) if((pass == 1) || (pass == 2))
@ -296,7 +296,7 @@ void X264Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, co
if(usePipe) if(usePipe)
{ {
if(frames < 1) throw "Frames not set!"; if(frames < 1) THROW("Frames not set!");
cmdLine << "--frames" << QString::number(frames); cmdLine << "--frames" << QString::number(frames);
cmdLine << "--demuxer" << "y4m"; cmdLine << "--demuxer" << "y4m";
cmdLine << "--stdin" << "y4m" << "-"; cmdLine << "--stdin" << "y4m" << "-";

View File

@ -21,10 +21,10 @@
#include "encoder_x265.h" #include "encoder_x265.h"
#include "global.h"
#include "model_options.h" #include "model_options.h"
#include "model_status.h" #include "model_status.h"
#include "binaries.h" #include "binaries.h"
#include "binaries.h"
#include <QStringList> #include <QStringList>
#include <QDir> #include <QDir>
@ -132,7 +132,7 @@ X265Encoder::X265Encoder(JobObject *jobObject, const OptionsModel *options, cons
{ {
if(options->encType() != OptionsModel::EncType_X265) 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()); cmdLine << "--bitrate" << QString::number(m_options->bitrate());
break; break;
default: default:
throw "Bad rate-control mode !!!"; THROW("Bad rate-control mode !!!");
break; break;
} }
@ -267,7 +267,7 @@ void X265Encoder::buildCommandLine(QStringList &cmdLine, const bool &usePipe, co
if(usePipe) if(usePipe)
{ {
if(frames < 1) throw "Frames not set!"; if(frames < 1) THROW("Frames not set!");
cmdLine << "--frames" << QString::number(frames); cmdLine << "--frames" << QString::number(frames);
cmdLine << "--y4m" << "-"; cmdLine << "--y4m" << "-";
} }

View File

@ -650,7 +650,7 @@ void x264_message_handler(QtMsgType type, const char *msg)
if((type == QtCriticalMsg) || (type == QtFatalMsg)) if((type == QtCriticalMsg) || (type == QtFatalMsg))
{ {
lock.unlock(); 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());
} }
} }
@ -2183,6 +2183,19 @@ bool x264_set_thread_execution_state(const bool systemRequired)
return (state != NULL); return (state != NULL);
} }
/*
* 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) * Check for debugger (detect routine)
*/ */
@ -2241,7 +2254,7 @@ static HANDLE x264_debug_thread_init()
* Fatal application exit * Fatal application exit
*/ */
#pragma intrinsic(_InterlockedExchange) #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; static volatile long bFatalFlag = 0L;
@ -2255,7 +2268,7 @@ void x264_fatal_exit(const wchar_t* exitMessage, const wchar_t* errorBoxMessage)
if(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); 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)); GetProcessMemoryInfo(GetCurrentProcess(), (PPROCESS_MEMORY_COUNTERS) &memoryCounters, sizeof(PROCESS_MEMORY_COUNTERS_EX));
return memoryCounters.PrivateUsage; return memoryCounters.PrivateUsage;
#else #else
throw "Cannot call this function in a non-debug build!"; THROW("Cannot call this function in a non-debug build!");
#endif //X264_DEBUG #endif //X264_DEBUG
} }

View File

@ -23,6 +23,7 @@
#define _CRT_RAND_S #define _CRT_RAND_S
#include <cstdlib> #include <cstdlib>
#include <stdexcept>
//Forward declarations //Forward declarations
class QString; 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_win80;
extern const x264_os_version_t x264_winver_win81; 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 // GLOBAL FUNCTIONS
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -130,7 +142,7 @@ const QString &x264_data_path(void);
size_t x264_dbg_private_bytes(void); size_t x264_dbg_private_bytes(void);
x264_cpu_t x264_detect_cpu_features(const int argc, char **argv); x264_cpu_t x264_detect_cpu_features(const int argc, char **argv);
bool x264_enable_close_button(const QWidget *win, const bool bEnable); 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_finalization(void);
void x264_init_console(const int argc, char **argv); void x264_init_console(const int argc, char **argv);
void x264_init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir = true); void x264_init_process(QProcess &process, const QString &wokringDir, const bool bReplaceTempDir = true);
@ -193,16 +205,9 @@ const char *x264_version_time(void);
#endif #endif
//Helper macro for throwing exceptions //Helper macro for throwing exceptions
#define THROW(MESSAGE) do \ #define THROW(MESSAGE, ...) do \
{ \ { \
throw std::runtime_error((MESSAGE)); \ throw X264Exception((MESSAGE), __VA_ARGS__); \
} \
while(0)
#define THROW_FMT(FORMAT, ...) do \
{ \
char _error_msg[512]; \
_snprintf_s(_error_msg, 512, _TRUNCATE, (FORMAT), __VA_ARGS__); \
throw std::runtime_error(_error_msg); \
} \ } \
while(0) while(0)

View File

@ -259,7 +259,7 @@ bool IPCCore::pushCommand(const int &command, const QStringList *args, const uns
{ {
if(m_initialized < 0) if(m_initialized < 0)
{ {
throw std::runtime_error("IPC not initialized!"); THROW("IPC not initialized!");
} }
if(!m_semaphoreWr->acquire()) if(!m_semaphoreWr->acquire())
@ -320,7 +320,7 @@ bool IPCCore::popCommand(int &command, QStringList &args, unsigned int &flags)
if(m_initialized < 0) if(m_initialized < 0)
{ {
throw std::runtime_error("IPC not initialized!"); THROW("IPC not initialized!");
} }
if(!m_semaphoreRd->acquire()) if(!m_semaphoreRd->acquire())

View File

@ -187,6 +187,15 @@ void handleMultipleInstances(const QStringList &args, IPC *ipc)
LONG WINAPI x264_exception_handler(__in struct _EXCEPTION_POINTERS *ExceptionInfo); 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); 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[]) static int _main(int argc, char* argv[])
{ {
if(X264_DEBUG) if(X264_DEBUG)
@ -206,25 +215,29 @@ static int _main(int argc, char* argv[])
iResult = x264_main(argc, argv); iResult = x264_main(argc, argv);
x264_finalization(); 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) catch(char *error)
{ {
fflush(stdout); PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error message: %s\n", error);
fflush(stderr);
fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error message: %s\n", error);
x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!"); x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
} }
catch(int error) catch(int error)
{ {
fflush(stdout); PRINT_ERROR("\nGURU MEDITATION !!!\n\nException error code: 0x%X\n", error);
fflush(stderr);
fprintf(stderr, "\nGURU MEDITATION !!!\n\nException error code: 0x%X\n", error);
x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!"); x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
} }
catch(...) catch(...)
{ {
fflush(stdout); PRINT_ERROR("\nGURU MEDITATION !!!\n\nUnknown C++ exception!\n");
fflush(stderr);
fprintf(stderr, "\nGURU MEDITATION !!!\n");
x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!"); x264_fatal_exit(L"Unhandeled C++ exception error, application will exit!");
} }
return iResult; return iResult;

View File

@ -30,7 +30,7 @@ static ITaskbarList3 *s_ptbl = NULL;
WinSevenTaskbar::WinSevenTaskbar(void) WinSevenTaskbar::WinSevenTaskbar(void)
{ {
throw "Cannot create instance of this class!"; THROW("Cannot create instance of this class!");
} }
WinSevenTaskbar::~WinSevenTaskbar(void) WinSevenTaskbar::~WinSevenTaskbar(void)

View File

@ -26,7 +26,7 @@
#define VER_X264_MAJOR 2 #define VER_X264_MAJOR 2
#define VER_X264_MINOR 3 #define VER_X264_MINOR 3
#define VER_X264_PATCH 5 #define VER_X264_PATCH 5
#define VER_X264_BUILD 816 #define VER_X264_BUILD 818
#define VER_X264_PORTABLE_EDITION (0) #define VER_X264_PORTABLE_EDITION (0)

View File

@ -442,6 +442,8 @@ void AddJobDialog::encoderIndexChanged(int index)
ui->labelProfile->setEnabled(!noProf); ui->labelProfile->setEnabled(!noProf);
ui->cbxProfile->setEnabled(!noProf); ui->cbxProfile->setEnabled(!noProf);
if(noProf) ui->cbxProfile->setCurrentIndex(0); if(noProf) ui->cbxProfile->setCurrentIndex(0);
variantIndexChanged(ui->cbxEncoderVariant->currentIndex());
} }
void AddJobDialog::variantIndexChanged(int index) void AddJobDialog::variantIndexChanged(int index)
@ -451,12 +453,14 @@ void AddJobDialog::variantIndexChanged(int index)
ui->labelProfile->setEnabled(!noProf); ui->labelProfile->setEnabled(!noProf);
ui->cbxProfile->setEnabled(!noProf); ui->cbxProfile->setEnabled(!noProf);
if(noProf) ui->cbxProfile->setCurrentIndex(0); if(noProf) ui->cbxProfile->setCurrentIndex(0);
modeIndexChanged(ui->cbxRateControlMode->currentIndex());
} }
void AddJobDialog::modeIndexChanged(int index) void AddJobDialog::modeIndexChanged(int index)
{ {
ui->spinQuantizer->setEnabled(index == 0 || index == 1); ui->spinQuantizer->setEnabled(index == OptionsModel::RCMode_CRF || index == OptionsModel::RCMode_CQ);
ui->spinBitrate->setEnabled(index == 2 || index == 3); ui->spinBitrate ->setEnabled(index == OptionsModel::RCMode_ABR || index == OptionsModel::RCMode_2Pass);
} }
void AddJobDialog::accept(void) void AddJobDialog::accept(void)

View File

@ -1105,7 +1105,7 @@ void MainWindow::handleCommand(const int &command, const QStringList &args, cons
} }
break; break;
default: default:
throw std::exception("Unknown command received!"); THROW("Unknown command received!");
} }
} }

View File

@ -304,7 +304,7 @@ void UpdaterDialog::threadStatusChanged(int status)
UPDATE_ICON(3, "play"); UPDATE_ICON(3, "play");
break; break;
default: default:
throw "Unknown status code!"; THROW("Unknown status code!");
} }
} }