diff options
Diffstat (limited to 'main.cpp')
| -rw-r--r-- | main.cpp | 91 |
1 files changed, 61 insertions, 30 deletions
@@ -6,6 +6,8 @@ #include <locale.h> #include <fftw3.h> +#include <QSharedMemory> +#include <QTemporaryFile> #include <QDateTime> #include <QApplication> #include <QRegularExpression> @@ -17,26 +19,25 @@ #include <QStandardPaths> #include <QStringList> #include <QLockFile> -#include <QStack> #include <QSplashScreen> - -#if QT_VERSION >= 0x050200 #include <QCommandLineParser> #include <QCommandLineOption> -#endif +#include <QSqlDatabase> +#include <QSqlQuery> +#include <QSqlError> #include "revision_utils.hpp" #include "MetaDataRegistry.hpp" #include "SettingsGroup.hpp" #include "TraceFile.hpp" #include "MultiSettings.hpp" -#include "mainwindow.h" +#include "widgets/mainwindow.h" #include "commons.h" #include "lib/init_random_seed.h" #include "Radio.hpp" -#include "FrequencyList.hpp" -#include "SplashScreen.hpp" -#include "MessageBox.hpp" // last to avoid nasty MS macro definitions +#include "models/FrequencyList.hpp" +#include "widgets/SplashScreen.hpp" +#include "widgets/MessageBox.hpp" // last to avoid nasty MS macro definitions extern "C" { // Fortran procedures we need @@ -55,37 +56,43 @@ namespace } } seeding; - class MessageTimestamper + // We can't use the GUI after QApplication::exit() is called so + // uncaught exceptions can get lost on Windows systems where there + // is no console terminal, so here we override + // QApplication::notify() and wrap the base class call with a try + // block to catch and display exceptions in a message box. + class ExceptionCatchingApplication final + : public QApplication { public: - MessageTimestamper () + explicit ExceptionCatchingApplication (int& argc, char * * argv) + : QApplication {argc, argv} { - prior_handlers_.push (qInstallMessageHandler (message_handler)); } - ~MessageTimestamper () - { - if (prior_handlers_.size ()) qInstallMessageHandler (prior_handlers_.pop ()); - } - - private: - static void message_handler (QtMsgType type, QMessageLogContext const& context, QString const& msg) + bool notify (QObject * receiver, QEvent * e) override { - QtMessageHandler handler {prior_handlers_.top ()}; - if (handler) + try { - handler (type, context, - QDateTime::currentDateTimeUtc ().toString ("yy-MM-ddTHH:mm:ss.zzzZ: ") + msg); + return QApplication::notify (receiver, e); + } + catch (std::exception const& e) + { + MessageBox::critical_message (nullptr, translate ("main", "Fatal error"), e.what ()); + throw; + } + catch (...) + { + MessageBox::critical_message (nullptr, translate ("main", "Unexpected fatal error")); + throw; } } - static QStack<QtMessageHandler> prior_handlers_; }; - QStack<QtMessageHandler> MessageTimestamper::prior_handlers_; } int main(int argc, char *argv[]) { // Add timestamps to all debug messages - MessageTimestamper message_timestamper; + qSetMessagePattern ("[%{time yyyyMMdd HH:mm:ss.zzz t} %{if-debug}D%{endif}%{if-info}I%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{message}"); init_random_seed (); @@ -96,7 +103,7 @@ int main(int argc, char *argv[]) // Multiple instances communicate with jt9 via this QSharedMemory mem_jt9; - QApplication a(argc, argv); + ExceptionCatchingApplication a(argc, argv); try { setlocale (LC_NUMERIC, "C"); // ensure number forms are in @@ -108,7 +115,6 @@ int main(int argc, char *argv[]) a.setApplicationName ("WSJT-X"); a.setApplicationVersion (version ()); -#if QT_VERSION >= 0x050200 QCommandLineParser parser; parser.setApplicationDescription ("\n" PROJECT_SUMMARY_DESCRIPTION); auto help_option = parser.addHelpOption (); @@ -210,11 +216,11 @@ int main(int argc, char *argv[]) } } } -#endif #if WSJT_QDEBUG_TO_FILE // Open a trace file TraceFile trace_file {temp_dir.absoluteFilePath (a.applicationName () + "_trace.log")}; + qSetMessagePattern ("[%{time yyyyMMdd HH:mm:ss.zzz t} %{if-debug}D%{endif}%{if-info}I%{endif}%{if-warning}W%{endif}%{if-critical}C%{endif}%{if-fatal}F%{endif}] %{file}:%{line} - %{message}"); qDebug () << program_title (revision ()) + " - Program startup"; #endif @@ -264,6 +270,33 @@ int main(int argc, char *argv[]) } } + // create writeable data directory if not already there + auto writeable_data_dir = QDir {QStandardPaths::writableLocation (QStandardPaths::DataLocation)}; + if (!writeable_data_dir.mkpath (".")) + { + MessageBox::critical_message (nullptr, a.translate ("main", "Failed to create data directory"), + a.translate ("main", "path: \"%1\"").arg (writeable_data_dir.absolutePath ())); + throw std::runtime_error {"Failed to create data directory"}; + } + + // set up SQLite database + if (!QSqlDatabase::drivers ().contains ("QSQLITE")) + { + throw std::runtime_error {"Failed to find SQLite Qt driver"}; + } + auto db = QSqlDatabase::addDatabase ("QSQLITE"); + db.setDatabaseName (writeable_data_dir.absoluteFilePath ("db.sqlite")); + if (!db.open ()) + { + throw std::runtime_error {("Database Error: " + db.lastError ().text ()).toStdString ()}; + } + + // better performance traded for a risk of d/b corruption + // on system crash or application crash + // db.exec ("PRAGMA synchronous=OFF"); // system crash risk + // db.exec ("PRAGMA journal_mode=MEMORY"); // application crash risk + db.exec ("PRAGMA locking_mode=EXCLUSIVE"); + int result; do { @@ -347,12 +380,10 @@ int main(int argc, char *argv[]) } catch (std::exception const& e) { - MessageBox::critical_message (nullptr, a.translate ("main", "Fatal error"), e.what ()); std::cerr << "Error: " << e.what () << '\n'; } catch (...) { - MessageBox::critical_message (nullptr, a.translate ("main", "Unexpected fatal error")); std::cerr << "Unexpected fatal error\n"; throw; // hoping the runtime might tell us more about the exception } |
