Skip to content

Commit 02df0d3

Browse files
HTRamseyCopilot
andcommitted
Update src/Utilities/QGCLogging.cc
Co-authored-by: Copilot <[email protected]>
1 parent b70942a commit 02df0d3

File tree

5 files changed

+129
-90
lines changed

5 files changed

+129
-90
lines changed

src/Utilities/Platform.cc

Lines changed: 98 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,112 @@
1+
/****************************************************************************
2+
*
3+
* (c) 2009-2024 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4+
*
5+
* QGroundControl is licensed according to the terms in the file
6+
* COPYING.md in the root of the source code directory.
7+
*
8+
****************************************************************************/
9+
110
#include "Platform.h"
211

12+
#include <QtCore/QCoreApplication>
13+
314
#ifdef Q_OS_MAC
15+
416
#include <CoreFoundation/CoreFoundation.h>
517

618
void Platform::disableAppNapViaInfoDict()
719
{
820
CFBundleRef bundle = CFBundleGetMainBundle();
9-
if (!bundle) return;
21+
if (!bundle) {
22+
return;
23+
}
1024

11-
// CFBundleGetInfoDictionary returns the dictionary the OS already
12-
// parsed from Info.plist. Cast it to mutable so we can tweak it.
1325
CFMutableDictionaryRef infoDict = (CFMutableDictionaryRef) CFBundleGetInfoDictionary(bundle);
14-
15-
// Inject the key → true
1626
if (infoDict) {
17-
CFDictionarySetValue(infoDict,
18-
CFSTR("NSAppSleepDisabled"),
19-
kCFBooleanTrue);
27+
CFDictionarySetValue(infoDict, CFSTR("NSAppSleepDisabled"), kCFBooleanTrue);
28+
}
29+
}
30+
31+
#elif defined(Q_OS_WIN)
32+
33+
#include <qt_windows.h>
34+
35+
#ifdef Q_CC_MSVC
36+
#include <crtdbg.h>
37+
38+
static int __cdecl WindowsCrtReportHook(int reportType, char *message, int *returnValue)
39+
{
40+
Q_UNUSED(message);
41+
42+
// For asserts, suppress any interactive break/GUI. Keep process running.
43+
if (reportType == _CRT_ASSERT) {
44+
if (returnValue) {
45+
*returnValue = 0; // continue execution
46+
}
47+
return TRUE; // handled: no further CRT processing
2048
}
49+
50+
// For warnings/errors, let CRT continue with our non-GUI report mode.
51+
return FALSE;
2152
}
53+
54+
#endif
55+
56+
static void WindowsInvalidParameterHandler(const wchar_t *expression,
57+
const wchar_t *function,
58+
const wchar_t *file,
59+
unsigned int line,
60+
uintptr_t pReserved)
61+
{
62+
Q_UNUSED(expression);
63+
Q_UNUSED(function);
64+
Q_UNUSED(file);
65+
Q_UNUSED(line);
66+
Q_UNUSED(pReserved);
67+
// Do nothing
68+
}
69+
70+
static void WindowsSigIntHandler()
71+
{
72+
qDebug() << "Ctrl-C received.";
73+
QCoreApplication::quit();
74+
}
75+
76+
static BOOL WINAPI WindowsConsoleCtrlHandler(DWORD CEvent)
77+
{
78+
switch (CEvent) {
79+
case CTRL_C_EVENT:
80+
WindowsSigIntHandler();
81+
return TRUE;
82+
default:
83+
break;
84+
}
85+
86+
return FALSE;
87+
}
88+
89+
void Platform::setWindowsNativeFunctions(bool quietWindowsAsserts)
90+
{
91+
if (quietWindowsAsserts) {
92+
(void) _set_invalid_parameter_handler(WindowsInvalidParameterHandler);
93+
(void) SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOGPFAULTERRORBOX | SEM_NOOPENFILEERRORBOX);
94+
#ifdef Q_CC_MSVC
95+
(void) _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
96+
(void) _CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, WindowsCrtReportHook);
97+
#endif
98+
}
99+
100+
(void) SetConsoleCtrlHandler((PHANDLER_ROUTINE) WindowsConsoleCtrlHandler, TRUE);
101+
102+
// TODO: Consider SetUnhandledExceptionFilter(windowsFaultHandler), _set_abort_behavior(0, _WRITE_ABORT_MSG | _CALL_REPORTFAULT), etc.
103+
// (void) _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_FILE);
104+
// (void) _CrtSetReportFile(_CRT_ASSERT, _CRTDBG_FILE_STDERR);
105+
// (void) _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_FILE);
106+
// (void) _CrtSetReportFile(_CRT_ERROR, _CRTDBG_FILE_STDERR);
107+
// (void) _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
108+
// (void) _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDERR);
109+
// (void) _CrtSetReportHook2(_CRT_RPTHOOK_REMOVE, WindowsCrtReportHook);
110+
}
111+
22112
#endif

src/Utilities/Platform.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,20 @@
1+
/****************************************************************************
2+
*
3+
* (c) 2009-2024 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4+
*
5+
* QGroundControl is licensed according to the terms in the file
6+
* COPYING.md in the root of the source code directory.
7+
*
8+
****************************************************************************/
9+
110
#pragma once
211

312
#include <QtCore/QtSystemDetection>
413

514
namespace Platform {
615
#ifdef Q_OS_MAC
7-
/// Prevent Apple's app nap from screwing us over
8-
/// tip: the domain can be cross-checked on the command line with <defaults domains>
916
void disableAppNapViaInfoDict();
17+
#elif defined(Q_OS_WIN)
18+
void setWindowsNativeFunctions(bool quietWindowsAsserts);
1019
#endif
1120
}

src/Utilities/QGCLogging.cc

Lines changed: 2 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include "QGCApplication.h"
1313
#include "QGCLoggingCategory.h"
1414
#include "SettingsManager.h"
15+
#include "Platform.h"
1516

1617
#include <QtConcurrent/QtConcurrentRun>
1718
#include <QtCore/QGlobalStatic>
@@ -20,51 +21,6 @@
2021

2122
QGC_LOGGING_CATEGORY(QGCLoggingLog, "qgc.utilities.qgclogging")
2223

23-
#ifdef Q_OS_WIN
24-
25-
#include <crtdbg.h>
26-
#include <windows.h>
27-
#include <iostream>
28-
29-
/// CRT Report Hook installed using _CrtSetReportHook. We install this hook when
30-
/// we don't want asserts to pop a dialog on windows.
31-
static int __cdecl WindowsCrtReportHook(int reportType, char *message, int *returnValue)
32-
{
33-
int nRet = FALSE;
34-
35-
printf("CRT report hook 1.\n");
36-
printf("CRT report type is \"");
37-
38-
switch (reportType) {
39-
case _CRT_ASSERT:
40-
printf("_CRT_ASSERT");
41-
// nRet = TRUE; // Always stop for this type of report
42-
break;
43-
case _CRT_WARN:
44-
printf("_CRT_WARN");
45-
break;
46-
case _CRT_ERROR:
47-
printf("_CRT_ERROR");
48-
break;
49-
default:
50-
printf("???Unknown???");
51-
break;
52-
}
53-
54-
printf("\".\nCRT report message is:\n\t");
55-
printf("%s", message);
56-
57-
std::cerr << message << std::endl;
58-
59-
if (returnValue) {
60-
*returnValue = 0;
61-
}
62-
63-
return nRet;
64-
}
65-
66-
#endif
67-
6824
Q_GLOBAL_STATIC(QGCLogging, _qgcLogging)
6925

7026
static QtMessageHandler defaultHandler = nullptr;
@@ -114,16 +70,8 @@ QGCLogging::~QGCLogging()
11470
qCDebug(QGCLoggingLog) << this;
11571
}
11672

117-
void QGCLogging::installHandler(bool quietWindowsAsserts)
73+
void QGCLogging::installHandler()
11874
{
119-
#ifdef Q_OS_WIN
120-
if (quietWindowsAsserts) {
121-
_CrtSetReportHook(WindowsCrtReportHook);
122-
}
123-
#else
124-
Q_UNUSED(quietWindowsAsserts)
125-
#endif
126-
12775
// Define the format for qDebug/qWarning/etc output
12876
qSetMessagePattern(QStringLiteral("%{category}:: %{time process} - %{type}: %{message} (%{function}:%{line})"));
12977

src/Utilities/QGCLogging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class QGCLogging : public QStringListModel
2828
static QGCLogging *instance();
2929

3030
/// Install Qt message handler to route logs through this class
31-
static void installHandler(bool quietWindowsAsserts);
31+
static void installHandler();
3232

3333
/// Write current log messages to a file asynchronously
3434
Q_INVOKABLE void writeMessages(const QString &destFile);

src/main.cc

Lines changed: 17 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,6 @@
1717
#include "MavlinkSettings.h"
1818
#include "Platform.h"
1919

20-
#ifdef Q_OS_WIN
21-
#include <windows.h>
22-
#endif
23-
2420
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
2521
#include <QtWidgets/QMessageBox>
2622
#include "RunGuard.h"
@@ -75,6 +71,17 @@ int main(int argc, char *argv[])
7571

7672
ParseCmdLineOptions(argc, argv, rgCmdLineOptions, std::size(rgCmdLineOptions), false);
7773

74+
#ifdef QGC_UNITTEST_BUILD
75+
if (stressUnitTests) {
76+
runUnitTests = true;
77+
}
78+
#ifdef Q_OS_WIN
79+
if (runUnitTests) {
80+
quietWindowsAsserts = true;
81+
}
82+
#endif
83+
#endif
84+
7885
#if !defined(Q_OS_ANDROID) && !defined(Q_OS_IOS)
7986
// We make the runguard key different for custom and non custom
8087
// builds, so they can be executed together in the same device.
@@ -114,23 +121,19 @@ int main(int argc, char *argv[])
114121
}
115122
#endif
116123

124+
#ifdef Q_OS_MACOS
125+
Platform::disableAppNapViaInfoDict();
126+
#endif
127+
117128
#ifdef Q_OS_WIN
118129
if (!qEnvironmentVariableIsSet("QT_WIN_DEBUG_CONSOLE")) {
119130
(void) qputenv("QT_WIN_DEBUG_CONSOLE", "attach"); // new
120131
}
121-
#endif
122-
123-
QGCLogging::installHandler(quietWindowsAsserts);
124132

125-
#ifdef QGC_UNITTEST_BUILD
126-
if (stressUnitTests) {
127-
runUnitTests = true;
128-
}
133+
Platform::setWindowsNativeFunctions(quietWindowsAsserts);
129134
#endif
130135

131-
#ifdef Q_OS_MACOS
132-
Platform::disableAppNapViaInfoDict();
133-
#endif
136+
QGCLogging::installHandler();
134137

135138
#ifdef Q_OS_WIN
136139
// Set our own OpenGL buglist
@@ -147,18 +150,7 @@ int main(int argc, char *argv[])
147150
break;
148151
}
149152
}
150-
151-
#ifdef QGC_UNITTEST_BUILD
152-
if (runUnitTests) {
153-
// Don't pop up Windows Error Reporting dialog when app crashes.
154-
const DWORD dwMode = SetErrorMode(SEM_NOGPFAULTERRORBOX);
155-
SetErrorMode(dwMode | SEM_NOGPFAULTERRORBOX);
156-
(void) _CrtSetReportMode(_CRT_ASSERT, _CRTDBG_MODE_DEBUG);
157-
(void) _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_DEBUG);
158-
(void) _CrtSetReportMode(_CRT_ERROR, _CRTDBG_MODE_DEBUG);
159-
}
160153
#endif
161-
#endif // Q_OS_WIN
162154

163155
QGCApplication app(argc, argv, runUnitTests, simpleBootTest);
164156

0 commit comments

Comments
 (0)