mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-13 07:35:41 +08:00
Refactor crashpad initialization
Crashpad handler was initialized quite late since we were waiting for settings. However the settings in crashpad are persistent itself, hence we can start it at the earliest possible time and change the upload settings independently later. The only prerequisite is to have QCoreApplication constructed in order for the application name to be available. ref T652
This commit is contained in:
committed by
Klaus Basan
parent
99bcee87aa
commit
a7509669f9
18
src/blackmisc/appstarttime.cpp
Normal file
18
src/blackmisc/appstarttime.cpp
Normal file
@@ -0,0 +1,18 @@
|
||||
/* Copyright (C) 2019
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated,
|
||||
* or distributed except according to the terms contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "appstarttime.h"
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
const QDateTime &getApplicationStartTimeUtc()
|
||||
{
|
||||
static const QDateTime gApplicationStartTimeUtc = QDateTime::currentDateTimeUtc();
|
||||
return gApplicationStartTimeUtc;
|
||||
}
|
||||
}
|
||||
24
src/blackmisc/appstarttime.h
Normal file
24
src/blackmisc/appstarttime.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/* Copyright (C) 2019
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated,
|
||||
* or distributed except according to the terms contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKMISC_APPSTARTTIME_H
|
||||
#define BLACKMISC_APPSTARTTIME_H
|
||||
|
||||
#include "blackmisc/blackmiscexport.h"
|
||||
|
||||
#include <QDateTime>
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
//! Get the application start time in UTC.
|
||||
BLACKMISC_EXPORT const QDateTime &getApplicationStartTimeUtc();
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -70,6 +70,10 @@ SOURCES += *.cpp \
|
||||
$$PWD/test/*.cpp \
|
||||
$$PWD/weather/*.cpp
|
||||
|
||||
|
||||
INCLUDEPATH *= $$EXTERNALSROOT/common/include/crashpad
|
||||
INCLUDEPATH *= $$EXTERNALSROOT/common/include/crashpad/mini_chromium
|
||||
|
||||
win32 {
|
||||
LIBS *= -lShell32 -lDbghelp -lversion
|
||||
# Remove the one below once the Reg functions are removed again from CIdentifier
|
||||
@@ -79,6 +83,13 @@ win32-g++ {
|
||||
LIBS *= -lpsapi
|
||||
}
|
||||
|
||||
msvc {
|
||||
CONFIG(debug, debug|release): LIBS *= -lclientd -lutild -lbased -lRpcrt4 -lAdvapi32
|
||||
CONFIG(release, debug|release): LIBS *= -lclient -lutil -lbase -lRpcrt4 -lAdvapi32
|
||||
}
|
||||
macx: LIBS += -lclient -lutil -lbase -lbsm -framework Security -framework CoreFoundation -framework ApplicationServices -framework Foundation
|
||||
unix:!macx: LIBS *= -lclient -lutil -lbase
|
||||
|
||||
DESTDIR = $$DestRoot/lib
|
||||
DLLDESTDIR = $$DestRoot/bin
|
||||
|
||||
|
||||
189
src/blackmisc/crashhandler.cpp
Normal file
189
src/blackmisc/crashhandler.cpp
Normal file
@@ -0,0 +1,189 @@
|
||||
/* Copyright (C) 2019
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated,
|
||||
* or distributed except according to the terms contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
#include "crashhandler.h"
|
||||
|
||||
#include "blackconfig/buildconfig.h"
|
||||
#include "blackmisc/appstarttime.h"
|
||||
#include "blackmisc/directoryutils.h"
|
||||
#include "blackmisc/logmessage.h"
|
||||
#include "blackmisc/filelogger.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QFileInfo>
|
||||
#include <QStringBuilder>
|
||||
|
||||
#ifdef BLACK_USE_CRASHPAD
|
||||
#if defined(Q_OS_WIN) && !defined(NOMINMAX)
|
||||
#define NOMINMAX
|
||||
#endif
|
||||
#include "crashpad/client/crashpad_client.h"
|
||||
#include "crashpad/client/crash_report_database.h"
|
||||
#include "crashpad/client/settings.h"
|
||||
#include "crashpad/client/simulate_crash.h"
|
||||
#endif
|
||||
|
||||
using namespace BlackConfig;
|
||||
using namespace BlackMisc;
|
||||
using namespace crashpad;
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
CCrashHandler *CCrashHandler::instance()
|
||||
{
|
||||
static CCrashHandler crashHandler;
|
||||
return &crashHandler;
|
||||
}
|
||||
|
||||
CCrashHandler::~CCrashHandler()
|
||||
{ }
|
||||
|
||||
#ifdef BLACK_USE_CRASHPAD
|
||||
//! Convert to file path
|
||||
base::FilePath qstringToFilePath(const QString &str)
|
||||
{
|
||||
# ifdef Q_OS_WIN
|
||||
return base::FilePath(str.toStdWString());
|
||||
# else
|
||||
return base::FilePath(str.toStdString());
|
||||
# endif
|
||||
}
|
||||
#endif
|
||||
|
||||
void CCrashHandler::init()
|
||||
{
|
||||
#ifdef BLACK_USE_CRASHPAD
|
||||
static const QString crashpadHandler(CBuildConfig::isRunningOnWindowsNtPlatform() ? "swift_crashpad_handler.exe" : "swift_crashpad_handler");
|
||||
static const QString handler = CFileUtils::appendFilePaths(CDirectoryUtils::binDirectory(), crashpadHandler);
|
||||
const QString database = CDirectoryUtils::crashpadDatabaseDirectory();
|
||||
const QString metrics = CDirectoryUtils::crashpadMetricsDirectory();
|
||||
|
||||
if (!QFileInfo::exists(handler)) { return; }
|
||||
|
||||
const std::string serverUrl("http://swift-project.sp.backtrace.io:6097/");
|
||||
std::map<std::string, std::string> annotations;
|
||||
|
||||
// Backtrace annotations
|
||||
annotations["token"] = "b15efd93e290be3cf5d39750cadc092b651327ff0c027b80abd75e0ee50df1da";
|
||||
annotations["format"] = "minidump";
|
||||
annotations["version"] = CBuildConfig::getVersionString().toStdString();
|
||||
annotations["platform"] = CBuildConfig::getPlatformString().toStdString();
|
||||
annotations["qtversion"] = QT_VERSION_STR;
|
||||
|
||||
// add our logfile
|
||||
const QString logAttachment = QStringLiteral("--attachment=attachment_%1=%2").arg(CFileLogger::getLogFileName(), CFileLogger::getLogFilePath());
|
||||
|
||||
std::vector<std::string> arguments;
|
||||
arguments.push_back(logAttachment.toStdString());
|
||||
|
||||
// and the simplified crash info if any
|
||||
const QString crashInfoFileName("swiftcrashinfo.txt");
|
||||
const QString crashInfoFilePath(CFileUtils::appendFilePaths(CFileUtils::stripFileFromPath(CFileLogger::getLogFilePath()), crashInfoFileName));
|
||||
m_crashAndLogInfo.setLogPathAndFileName(crashInfoFilePath);
|
||||
const QString crashAttachment = QStringLiteral("--attachment=attachment_%1=%2").arg(crashInfoFileName, crashInfoFilePath);
|
||||
arguments.push_back(crashAttachment.toStdString());
|
||||
|
||||
// for testing purposes
|
||||
if (CBuildConfig::isLocalDeveloperDebugBuild())
|
||||
{
|
||||
arguments.push_back("--no-rate-limit");
|
||||
}
|
||||
|
||||
QDir().mkpath(database);
|
||||
|
||||
m_crashReportDatabase = CrashReportDatabase::Initialize(qstringToFilePath(database));
|
||||
m_crashpadClient = std::make_unique<CrashpadClient>();
|
||||
m_crashpadClient->StartHandler(qstringToFilePath(handler),
|
||||
qstringToFilePath(database),
|
||||
qstringToFilePath(metrics),
|
||||
serverUrl,
|
||||
annotations,
|
||||
arguments,
|
||||
false, true);
|
||||
|
||||
this->crashAndLogAppendInfo(u"Init crash info at " % QDateTime::currentDateTimeUtc().toString());
|
||||
#endif
|
||||
}
|
||||
|
||||
void CCrashHandler::setUploadsEnabled(bool enable)
|
||||
{
|
||||
#ifdef BLACK_USE_CRASHPAD
|
||||
if (!m_crashReportDatabase) { return; }
|
||||
crashpad::Settings *settings = m_crashReportDatabase->GetSettings();
|
||||
settings->SetUploadsEnabled(enable);
|
||||
#else
|
||||
Q_UNUSED(enable);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool CCrashHandler::isCrashDumpUploadEnabled() const
|
||||
{
|
||||
#ifdef BLACK_USE_CRASHPAD
|
||||
if (!m_crashReportDatabase) { return false; }
|
||||
crashpad::Settings *settings = m_crashReportDatabase->GetSettings();
|
||||
bool enabled = false;
|
||||
bool ok = settings->GetUploadsEnabled(&enabled);
|
||||
return ok && enabled;
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
void CCrashHandler::triggerCrashInfoWrite()
|
||||
{
|
||||
m_crashAndLogInfo.triggerWritingFile();
|
||||
}
|
||||
|
||||
void CCrashHandler::setCrashInfo(const CCrashInfo &info)
|
||||
{
|
||||
m_crashAndLogInfo = info;
|
||||
m_dsCrashAndLogInfo.inputSignal();
|
||||
}
|
||||
|
||||
void CCrashHandler::crashAndLogInfoUserName(const QString &name)
|
||||
{
|
||||
m_crashAndLogInfo.setUserName(name);
|
||||
m_dsCrashAndLogInfo.inputSignal();
|
||||
}
|
||||
|
||||
void CCrashHandler::crashAndLogInfoSimulator(const QString &simulator)
|
||||
{
|
||||
m_crashAndLogInfo.setSimulatorString(simulator);
|
||||
m_dsCrashAndLogInfo.inputSignal();
|
||||
}
|
||||
|
||||
void CCrashHandler::crashAndLogInfoFlightNetwork(const QString &flightNetwork)
|
||||
{
|
||||
m_crashAndLogInfo.setFlightNetworkString(flightNetwork);
|
||||
m_dsCrashAndLogInfo.inputSignal();
|
||||
}
|
||||
|
||||
void CCrashHandler::crashAndLogAppendInfo(const QString &info)
|
||||
{
|
||||
m_crashAndLogInfo.appendInfo(info);
|
||||
m_dsCrashAndLogInfo.inputSignal();
|
||||
}
|
||||
|
||||
void CCrashHandler::simulateCrash()
|
||||
{
|
||||
#ifdef BLACK_USE_CRASHPAD
|
||||
CLogMessage(this).info(u"Simulated crash dump!");
|
||||
m_crashAndLogInfo.appendInfo("Simulated crash dump!");
|
||||
m_crashAndLogInfo.writeToFile();
|
||||
CRASHPAD_SIMULATE_CRASH();
|
||||
// real crash
|
||||
// raise(SIGSEGV); #include <signal.h>
|
||||
#else
|
||||
CLogMessage(this).warning(u"This compiler or platform does not support crashpad. Cannot simulate crash dump!");
|
||||
#endif
|
||||
}
|
||||
|
||||
CCrashHandler::CCrashHandler(QObject *parent) :
|
||||
QObject(parent)
|
||||
{ }
|
||||
}
|
||||
92
src/blackmisc/crashhandler.h
Normal file
92
src/blackmisc/crashhandler.h
Normal file
@@ -0,0 +1,92 @@
|
||||
/* Copyright (C) 2019
|
||||
* swift project Community / Contributors
|
||||
*
|
||||
* This file is part of swift project. It is subject to the license terms in the LICENSE file found in the top-level
|
||||
* directory of this distribution. No part of swift project, including this file, may be copied, modified, propagated,
|
||||
* or distributed except according to the terms contained in the LICENSE file.
|
||||
*/
|
||||
|
||||
//! \file
|
||||
|
||||
#ifndef BLACKMISC_CRASHHANDLER_H
|
||||
#define BLACKMISC_CRASHHANDLER_H
|
||||
|
||||
#include "blackmisc/blackmiscexport.h"
|
||||
#include "blackmisc/crashinfo.h"
|
||||
#include "blackmisc/digestsignal.h"
|
||||
|
||||
#include <memory>
|
||||
|
||||
#if !defined(Q_CC_MINGW)
|
||||
#define BLACK_USE_CRASHPAD
|
||||
#endif
|
||||
|
||||
namespace crashpad
|
||||
{
|
||||
class CrashpadClient;
|
||||
class CrashReportDatabase;
|
||||
}
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
//! Crash handler
|
||||
class BLACKMISC_EXPORT CCrashHandler : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
|
||||
public:
|
||||
//! Get singleton instance
|
||||
static CCrashHandler *instance();
|
||||
|
||||
//! Destructor
|
||||
virtual ~CCrashHandler();
|
||||
|
||||
//! Initialize and start crashpad handler process
|
||||
void init();
|
||||
|
||||
//! Enable/disable automatic uploading
|
||||
void setUploadsEnabled(bool enable);
|
||||
|
||||
//! Is automatic dump uploading enabled?
|
||||
bool isCrashDumpUploadEnabled() const;
|
||||
|
||||
// ----------------------- Crash info ---------------------------------
|
||||
|
||||
//! Extra annotation for crash to easier identify annotation
|
||||
void setCrashInfo(const BlackMisc::CCrashInfo &info);
|
||||
|
||||
//! Set user name for crash info
|
||||
void crashAndLogInfoUserName(const QString &name);
|
||||
|
||||
//! Set simulator string in crash info
|
||||
void crashAndLogInfoSimulator(const QString &simulator);
|
||||
|
||||
//! Set flight network in crash info
|
||||
void crashAndLogInfoFlightNetwork(const QString &flightNetwork);
|
||||
|
||||
//! Append crash info
|
||||
void crashAndLogAppendInfo(const QString &info);
|
||||
|
||||
//! Get crash info
|
||||
const BlackMisc::CCrashInfo &getCrashInfo() const { return m_crashAndLogInfo; }
|
||||
|
||||
//! Simulate a crash
|
||||
void simulateCrash();
|
||||
|
||||
private:
|
||||
CCrashHandler(QObject *parent = nullptr);
|
||||
|
||||
// crash info
|
||||
void triggerCrashInfoWrite();
|
||||
|
||||
BlackMisc::CCrashInfo m_crashAndLogInfo; //!< info representing details
|
||||
BlackMisc::CDigestSignal m_dsCrashAndLogInfo { this, &CCrashHandler::triggerCrashInfoWrite, 10000, 5 };
|
||||
|
||||
#ifdef BLACK_USE_CRASHPAD
|
||||
std::unique_ptr<crashpad::CrashpadClient> m_crashpadClient;
|
||||
std::unique_ptr<crashpad::CrashReportDatabase> m_crashReportDatabase;
|
||||
#endif
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -7,8 +7,10 @@
|
||||
*/
|
||||
|
||||
#include "blackconfig/buildconfig.h"
|
||||
#include "blackmisc/appstarttime.h"
|
||||
#include "blackmisc/filelogger.h"
|
||||
#include "blackmisc/loghandler.h"
|
||||
#include "blackmisc/directoryutils.h"
|
||||
|
||||
#include <QCoreApplication>
|
||||
#include <QDateTime>
|
||||
@@ -26,22 +28,33 @@ using namespace BlackConfig;
|
||||
|
||||
namespace BlackMisc
|
||||
{
|
||||
CFileLogger::CFileLogger(QObject *parent) :
|
||||
CFileLogger(QCoreApplication::applicationName(), QString(), parent)
|
||||
//! Get application name
|
||||
QString applicationName()
|
||||
{
|
||||
// void
|
||||
static const QString applicationName = QFileInfo(QCoreApplication::applicationFilePath()).completeBaseName();
|
||||
return applicationName;
|
||||
}
|
||||
|
||||
CFileLogger::CFileLogger(const QString &applicationName, const QString &logPath, QObject *parent) :
|
||||
QObject(parent),
|
||||
m_logFile(this),
|
||||
m_applicationName(applicationName),
|
||||
m_logPath(logPath)
|
||||
//! Get log file name
|
||||
QString logFileName()
|
||||
{
|
||||
if (!m_logPath.isEmpty()) { QDir::root().mkpath(m_logPath); }
|
||||
static const QString fileName = applicationName() %
|
||||
QLatin1String("_") %
|
||||
getApplicationStartTimeUtc().toString(QStringLiteral("yyMMddhhmmss")) %
|
||||
QLatin1String("_") %
|
||||
QString::number(QCoreApplication::applicationPid()) %
|
||||
QLatin1String(".log");
|
||||
return fileName;
|
||||
}
|
||||
|
||||
CFileLogger::CFileLogger(QObject *parent) :
|
||||
QObject(parent),
|
||||
m_logFile(this)
|
||||
{
|
||||
Q_ASSERT(! applicationName().isEmpty());
|
||||
QDir::root().mkpath(CDirectoryUtils::logDirectory());
|
||||
removeOldLogFiles();
|
||||
if (!m_logPath.isEmpty() && !m_logPath.endsWith('/')) { m_logPath += '/'; }
|
||||
m_logFile.setFileName(getFullFileName());
|
||||
m_logFile.setFileName(getLogFilePath());
|
||||
m_logFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
|
||||
m_stream.setDevice(&m_logFile);
|
||||
m_stream.setCodec("UTF-8");
|
||||
@@ -66,6 +79,11 @@ namespace BlackMisc
|
||||
}
|
||||
}
|
||||
|
||||
QString CFileLogger::getLogFileName()
|
||||
{
|
||||
return logFileName();
|
||||
}
|
||||
|
||||
void CFileLogger::ps_writeStatusMessageToFile(const BlackMisc::CStatusMessage &statusMessage)
|
||||
{
|
||||
if (statusMessage.isEmpty()) { return; }
|
||||
@@ -85,26 +103,17 @@ namespace BlackMisc
|
||||
writeContentToFile(finalContent);
|
||||
}
|
||||
|
||||
QString CFileLogger::getFullFileName()
|
||||
QString CFileLogger::getLogFilePath()
|
||||
{
|
||||
QString filePath;
|
||||
Q_ASSERT(!m_applicationName.isEmpty());
|
||||
if (!m_logPath.isEmpty()) filePath += m_logPath;
|
||||
|
||||
m_fileName += m_applicationName;
|
||||
m_fileName += QLatin1String("_");
|
||||
m_fileName += QDateTime::currentDateTime().toString(QStringLiteral("yyMMddhhmmss"));
|
||||
m_fileName += QLatin1String("_");
|
||||
m_fileName += QString::number(QCoreApplication::applicationPid());
|
||||
m_fileName += QLatin1String(".log");
|
||||
return filePath + m_fileName;
|
||||
QString filePath = CDirectoryUtils::logDirectory() % '/' % logFileName();
|
||||
return filePath;
|
||||
}
|
||||
|
||||
void CFileLogger::removeOldLogFiles()
|
||||
{
|
||||
QString nameFilter(m_applicationName);
|
||||
QString nameFilter(applicationName());
|
||||
nameFilter += QLatin1String("*.log");
|
||||
QDir dir(m_logPath, nameFilter, QDir::Name, QDir::Files);
|
||||
QDir dir(CDirectoryUtils::logDirectory(), nameFilter, QDir::Name, QDir::Files);
|
||||
|
||||
QDateTime now = QDateTime::currentDateTime();
|
||||
for (const auto &logFileInfo : dir.entryInfoList())
|
||||
@@ -118,7 +127,7 @@ namespace BlackMisc
|
||||
|
||||
void CFileLogger::writeHeaderToFile()
|
||||
{
|
||||
m_stream << "This is " << m_applicationName;
|
||||
m_stream << "This is " << applicationName();
|
||||
m_stream << " version " << CBuildConfig::getVersionString();
|
||||
m_stream << " running on " << QSysInfo::prettyProductName();
|
||||
m_stream << " " << QSysInfo::currentCpuArchitecture() << endl;
|
||||
|
||||
@@ -32,16 +32,6 @@ namespace BlackMisc
|
||||
//! Filename defaults to QCoreApplication::applicationName() and path to "."
|
||||
CFileLogger(QObject *parent = nullptr);
|
||||
|
||||
/*!
|
||||
* Constructor
|
||||
* \param applicationName Use the applications name without any extension.
|
||||
* A timestamp and extension will be added automatically.
|
||||
* \param logPath Path the log files is written to. If you leave this empty, the
|
||||
* file will be written in the working directory of the binary.
|
||||
* \param parent QObject parent
|
||||
*/
|
||||
CFileLogger(const QString &applicationName, const QString &logPath, QObject *parent = nullptr);
|
||||
|
||||
//! Destructor.
|
||||
virtual ~CFileLogger();
|
||||
|
||||
@@ -51,20 +41,18 @@ namespace BlackMisc
|
||||
//! Close file
|
||||
void close();
|
||||
|
||||
//! Get the current log file path
|
||||
QString getLogFilePath() const { return m_logFile.fileName(); }
|
||||
//! Get the log file name
|
||||
static QString getLogFileName();
|
||||
|
||||
//! Get the current log file name
|
||||
QString getLogFileName() const { return m_fileName; }
|
||||
//! Get the log file path (including its name)
|
||||
static QString getLogFilePath();
|
||||
|
||||
private slots:
|
||||
//! Write single status message to file
|
||||
void ps_writeStatusMessageToFile(const BlackMisc::CStatusMessage &statusMessage);
|
||||
|
||||
private:
|
||||
QString getFullFileName();
|
||||
void removeOldLogFiles();
|
||||
|
||||
void writeHeaderToFile();
|
||||
void writeContentToFile(const QString &content);
|
||||
|
||||
@@ -72,8 +60,6 @@ namespace BlackMisc
|
||||
QFile m_logFile;
|
||||
QString m_fileName;
|
||||
QTextStream m_stream;
|
||||
QString m_applicationName;
|
||||
QString m_logPath; //!< Empty by default. Hence the working directory "." is used
|
||||
QString m_previousCategories;
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user