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 "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

View 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;

View 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!");
}

View 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:

View 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" << "-";

View File

@ -21,10 +21,10 @@
#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" << "-";
}

View 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());
}
}
@ -2183,6 +2183,19 @@ bool x264_set_thread_execution_state(const bool systemRequired)
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)
*/
@ -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
}

View 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 \
#define THROW(MESSAGE, ...) do \
{ \
throw std::runtime_error((MESSAGE)); \
} \
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); \
throw X264Exception((MESSAGE), __VA_ARGS__); \
} \
while(0)

View 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())

View 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;

View 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)

View 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)

View 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)

View 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!");
}
}

View File

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