Files
qelectrotech-source-mirror/sources/qetapp.h
T
ispyisail 5dec36cb29 Add crash-time ring flush and a diagnostics export UI (discussion #644, steps 4-5)
Stacked on the steps 1-3 branch (feature-diagnostic-logging, PR #646).
Kept as its own PR rather than folded into that one, matching the
discussion's own framing: step 4 is explicitly "the highest-risk piece
... lands last, behind its own switch."

## Step 4 -- crash-time ring flush (CrashHandler)

Installs a handler for SIGSEGV/SIGABRT/SIGBUS/SIGFPE/SIGILL (POSIX) /
SetUnhandledExceptionFilter (Windows) that flushes the in-memory ring to
a fixed crash_dump.log before the process dies.

This required reworking LogRing (step 3) to be genuinely lock-free, not
just mutex-protected: a signal handler that blocks on a lock the
crashing thread (or another thread) already holds turns a clean crash
into a hang -- no ring dump *and* no core dump, worse than doing
nothing. append() now claims a slot with a single atomic fetch-add;
dumpToFd() reads the preallocated entries directly and writes them with
write(2) only, looping on EINTR/short writes. Accepted tradeoff: at most
one entry can be read torn if a crash lands mid-append into that exact
slot -- documented in logring.h, and the alternative (a seqlock to
detect and retry) wasn't judged worth the complexity for that window.

Other invariants implemented per the discussion:
- sigaltstack with a static 64 KiB buffer, SA_ONSTACK -- a stack-
  overflow SIGSEGV has no usable stack for a handler without one.
- Nothing under the actual handler touches Qt, QString or the
  allocator: the dump path and a small header (version/git/OS/Qt) are
  precomputed into fixed char buffers by install(), which runs once at
  startup in normal context.
- Atomic test-and-set so only the first crash writes a dump; a second
  concurrent/nested fault goes straight to restore-and-re-raise.
- After writing, the handler restores SIG_DFL and re-raises (POSIX) /
  returns EXCEPTION_CONTINUE_SEARCH (Windows) so the OS's own crash
  path -- core dump, Windows Error Reporting -- still runs. A handler
  that "fixed" the crash by swallowing the signal would destroy exactly
  the post-mortem evidence this whole design exists to preserve.

Tested in this environment: POSIX/Linux only, all five signals. Sent
each directly to a running process and confirmed (a) crash_dump.log is
written with the correct header and ring contents, mode 0600, and (b)
the process still terminates via the signal with the kernel's own
"core dumped" flag set (exit code 128+signal, confirmed for all five).
The Windows path is implemented per the discussion's guidance but is
untested -- no Windows build available in this sandbox.

## Step 5 -- getting the data back out

- QETApp::checkCrashDump(), called from checkBackupFiles() only when
  there's no stale project file to recover this run (so the two
  prompts never both show, per the discussion), offers an unretrieved
  crash dump via DiagnosticsReportDialog and then deletes it regardless
  of the user's choice -- offered exactly once.
- A new "Aide > Enregistrer un rapport de diagnostic..." action
  (QETMainWindow) builds the same kind of report from the *current*
  session (QetLogger::buildDiagnosticsReport(): header + this session's
  log file) for a manual "attach this to a bug report" flow, not tied
  to a crash.
- Both go through QetLogger::redact() before ever reaching the user:
  the one redaction implemented is a literal replace of the home
  directory with "~", since an absolute path under it leaks the
  account name. The discussion's fancier "optionally redact project
  filenames too" isn't attempted -- reliably telling a project path
  apart from arbitrary log text is a much fuzzier problem than a
  literal prefix match.
- DiagnosticsReportDialog shows the full (already-redacted) content
  before saving, per the discussion: "the user is about to attach this
  to a public tracker."

Verified in a real GUI session (Xvfb): triggered a SIGSEGV, relaunched,
confirmed the crash-report dialog appears with the right header/content,
confirmed it does not reappear on a second relaunch, and confirmed the
manual "Save report" action produces a correctly-formatted report and
saves it to a chosen path.

Built clean, no new warnings.

## Build systems

Registered in both: cmake/qet_compilation_vars.cmake, and
qelectrotech.pro. The .pro needed explicit globs for the new
sources/logging/ui/ subfolder -- sources/logging/*.{h,cpp} was already
globbed, but unlike the other ui/ subfolders that one had no entry of
its own, so diagnosticsreportdialog.{h,cpp} would not have been built
under qmake.
2026-08-06 23:04:27 +12:00

303 lines
9.5 KiB
C++

/*
Copyright 2006-2026 The QElectroTech Team
This file is part of QElectroTech.
QElectroTech is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
QElectroTech is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with QElectroTech. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef QET_APP_H
#define QET_APP_H
#include "ElementsCollection/elementslocation.h"
#include "qetarguments.h"
#include "titleblock/templatelocation.h"
#include <QByteArray>
#include <QPalette>
#include <QSystemTrayIcon>
#include <QTranslator>
class QSplashScreen;
class QMenu;
class QAction;
class QMainWindow;
#define QETAPP_COMMON_TBT_PROTOCOL "commontbt"
#define QETAPP_COMPANY_TBT_PROTOCOL "companytbt"
#define QETAPP_CUSTOM_TBT_PROTOCOL "customtbt"
class AboutQET;
class QETDiagramEditor;
class QETElementEditor;
class ElementsCollectionCache;
class TitleBlockTemplate;
class TitleBlockTemplatesCollection;
class TitleBlockTemplatesFilesCollection;
class QETProject;
class QETTitleBlockTemplateEditor;
class QTextOrientationSpinBoxWidget;
class RecentFiles;
/**
@brief The QETApp class
This class represents the QElectroTech application.
*/
class QETApp : public QObject
{
Q_OBJECT
// constructors, destructor
public:
QETApp();
~QETApp() override;
private:
QETApp(const QETApp &);
// methods
public:
static QETApp *instance();
void setLanguage(const QString &);
static QString langFromSetting ();
void switchLayout(Qt::LayoutDirection);
static void printHelp();
static void printVersion();
static void printLicense();
static ElementsCollectionCache *collectionCache();
static TitleBlockTemplatesFilesCollection *commonTitleBlockTemplatesCollection();
static TitleBlockTemplatesFilesCollection *companyTitleBlockTemplatesCollection();
static TitleBlockTemplatesFilesCollection *customTitleBlockTemplatesCollection();
static QList<TitleBlockTemplatesCollection *> availableTitleBlockTemplatesCollections();
static TitleBlockTemplatesCollection *titleBlockTemplatesCollection(const QString &);
static QString commonElementsDir();
static QString companyElementsDir();
static QString customElementsDir();
static QString commonElementsDirN();
static QString companyElementsDirN();
static QString customElementsDirN();
static void resetCollectionsPath();
static QString commonTitleBlockTemplatesDir();
static QString companyTitleBlockTemplatesDir();
static QString customTitleBlockTemplatesDir();
static QString userMacrosDir();
static bool registerProject(QETProject *);
static bool unregisterProject(QETProject *);
static QMap<uint, QETProject *> registeredProjects();
static QETProject *project(const uint &);
static int projectId(const QETProject *);
static QString configDir();
static QString dataDir();
static QString documentDir();
static QString pictureDir();
static QString languagesPath();
static QString realPath(const QString &);
static QString symbolicPath(const QString &);
static QStringList handledFileExtensions();
static QStringList handledFiles(const QList<QUrl> &);
static RecentFiles *projectsRecentFiles();
static RecentFiles *elementsRecentFiles();
#ifdef QET_ALLOW_OVERRIDE_CED_OPTION
public:
static void overrideCommonElementsDir(const QString &);
private:
/**
@brief common_elements_dir
Directory containing the common elements collection
*/
static QString m_overrided_common_elements_dir;
#endif
#ifdef QET_ALLOW_OVERRIDE_CTBTD_OPTION
public:
static void overrideCommonTitleBlockTemplatesDir(const QString &);
private:
/**
@brief common_tbt_dir_
Directory containing
the common title block templates collection
*/
static QString common_tbt_dir_;
#endif
#ifdef QET_ALLOW_OVERRIDE_CD_OPTION
public:
static void overrideConfigDir(const QString &);
private:
/**
@brief config_dir
Directory containing the user configuration
and the custom elements collection
*/
static QString config_dir;
#endif
#ifdef QET_ALLOW_OVERRIDE_DD_OPTION
public:
static void overrideDataDir(const QString &);
private:
/**
@brief data_dir
Directory containing the users data
and the custom elements collection
*/
static QString data_dir;
#endif
public:
static void overrideLangDir(const QString &);
/**
@brief lang_dir
Directory containing localization files.
*/
static QString lang_dir;
static QFont diagramTextsFont(qreal = -1.0);
static QFont diagramTextsItemFont(qreal = -1.0);
static QFont dynamicTextsItemFont(qreal = -1.0);
static QFont indiTextsItemFont (qreal = -1.0);
static QETDiagramEditor *diagramEditorForFile(const QString &);
static QETDiagramEditor *diagramEditorAncestorOf (const QWidget *child);
static QList<QETDiagramEditor *> diagramEditors();
static QETDiagramEditor* diagramEditor(QETProject *project);
static QList<QETElementEditor *> elementEditors();
static QList<QETElementEditor *> elementEditors(QETProject *);
static QList<QETTitleBlockTemplateEditor *> titleBlockTemplateEditors();
static QList<QETTitleBlockTemplateEditor *> titleBlockTemplateEditors(QETProject *);
static QTextOrientationSpinBoxWidget *createTextOrientationSpinBoxWidget();
static TitleBlockTemplate *defaultTitleBlockTemplate();
#ifdef Q_OS_DARWIN
public:
bool eventFilter(QObject *object, QEvent *) override;
#endif
// attributes
private:
static QETApp *m_qetapp;
QTranslator qtTranslator;
QTranslator qetTranslator;
QSystemTrayIcon *m_qsti;
QSplashScreen *m_splash_screen;
QMenu *menu_systray;
QAction *quitter_qet;
QAction *reduce_appli;
QAction *restore_appli;
QAction *reduce_diagrams;
QAction *restore_diagrams;
QAction *reduce_elements;
QAction *restore_elements;
QAction *reduce_templates;
QAction *restore_templates;
QAction *new_diagram;
QAction *new_element;
QHash<QMainWindow *, QByteArray> window_geometries;
QHash<QMainWindow *, QByteArray> window_states;
bool every_editor_reduced;
bool every_diagram_reduced;
bool every_diagram_visible;
bool every_element_reduced;
bool every_element_visible;
bool every_template_reduced;
bool every_template_visible;
QSignalMapper signal_map;
QETArguments qet_arguments_; ///< Command-line arguments parser
/**
@brief non_interactive_execution_
Whether the application will end
without any user interaction
*/
bool non_interactive_execution_;
QPalette initial_palette_; ///< System color palette
static TitleBlockTemplatesFilesCollection *m_common_tbt_collection;
static TitleBlockTemplatesFilesCollection *m_company_tbt_collection;
static TitleBlockTemplatesFilesCollection *m_custom_tbt_collection;
static ElementsCollectionCache *collections_cache_;
static QMap<uint, QETProject *> registered_projects_;
static uint next_project_id;
static RecentFiles *m_projects_recent_files;
static RecentFiles *m_elements_recent_files;
static TitleBlockTemplate *default_titleblock_template_;
static QString m_common_element_dir;
static bool m_common_element_dir_is_set;
static QString m_company_element_dir;
static bool m_company_element_dir_is_set;
static QString m_custom_element_dir;
static bool m_custom_element_dir_is_set;
static QString m_user_company_tbt_dir;
static QString m_user_custom_tbt_dir;
static QString m_user_macros_dir;
public slots:
void systray(QSystemTrayIcon::ActivationReason);
void reduceEveryEditor();
void restoreEveryEditor();
void reduceDiagramEditors();
void restoreDiagramEditors();
void reduceElementEditors();
void restoreElementEditors();
void reduceTitleBlockTemplateEditors();
void restoreTitleBlockTemplateEditors();
void newDiagramEditor();
void newElementEditor();
bool closeEveryEditor();
void setMainWindowVisible(QMainWindow *, bool);
void invertMainWindowVisibility(QWidget *);
void useSystemPalette(bool);
void quitQET();
void checkRemainingWindows();
void openFiles(const QETArguments &);
void openProjectFiles(const QStringList &);
void openElementFiles(const QStringList &);
void openElementLocations(const QList<ElementsLocation> &);
void openTitleBlockTemplate(const TitleBlockTemplateLocation &, bool = false);
void openTitleBlockTemplate(const QString &);
void openTitleBlockTemplateFiles(const QStringList &);
void configureQET();
void aboutQET();
void showDiagnosticsReport();
void receiveMessage(int instanceId, QByteArray message);
private:
template <class T> QList<T *> detectWindows() const;
template <class T> void setMainWindowsVisible(bool);
QList<QWidget *> floatingToolbarsAndDocksForMainWindow(QMainWindow *) const;
void parseArguments();
void initSplashScreen();
void setSplashScreenStep(const QString & = QString());
void initLanguage();
void initFonts();
void initStyle();
void initConfiguration();
void initSystemTray();
void buildSystemTrayMenu();
void checkBackupFiles();
void checkCrashDump();
void fetchWindowStats(
const QList<QETDiagramEditor *> &,
const QList<QETElementEditor *> &,
const QList<QETTitleBlockTemplateEditor *> &
);
template <class T> void addWindowsListToMenu(QMenu *, const QList<T *> &);
static int projectIdFromString(const QString &);
static QETProject *projectFromString(const QString &);
};
#endif