feat(GODT-2482): more attachment to relevant exceptions.

This commit is contained in:
Xavier Michelon
2023-03-13 14:55:59 +01:00
parent 4273405393
commit 48274ee178
9 changed files with 135 additions and 67 deletions

View File

@ -18,26 +18,13 @@
#include "LogUtils.h"
#include "BuildConfig.h"
#include <bridgepp/Log/LogUtils.h>
#include <bridgepp/BridgeUtils.h>
using namespace bridgepp;
namespace {
qsizetype const logFileTailMaxLength = 25 * 1024; ///< The maximum length of the portion of log returned by tailOfLatestBridgeLog()
}
//****************************************************************************************************************************************************
/// \return user logs directory used by bridge.
//****************************************************************************************************************************************************
QString userLogsDir() {
QString const path = QDir(bridgepp::userDataDir()).absoluteFilePath("logs");
QDir().mkpath(path);
return path;
}
//****************************************************************************************************************************************************
/// \return A reference to the log.
//****************************************************************************************************************************************************
@ -69,36 +56,3 @@ Log &initLog() {
return log;
}
//****************************************************************************************************************************************************
/// \brief Return the path of the latest bridge log.
/// \return The path of the latest bridge log file.
/// \return An empty string if no bridge log file was found.
//****************************************************************************************************************************************************
QString latestBridgeLogPath() {
QDir const logsDir(userLogsDir());
if (logsDir.isEmpty()) {
return QString();
}
QFileInfoList files = logsDir.entryInfoList({ "v*.log" }, QDir::Files); // could do sorting, but only by last modification time. we want to sort by creation time.
std::sort(files.begin(), files.end(), [](QFileInfo const &lhs, QFileInfo const &rhs) -> bool {
return lhs.birthTime() < rhs.birthTime();
});
return files.back().absoluteFilePath();
}
//****************************************************************************************************************************************************
/// Return the maxSize last bytes of the latest bridge log.
//****************************************************************************************************************************************************
QByteArray tailOfLatestBridgeLog() {
QString path = latestBridgeLogPath();
if (path.isEmpty()) {
return QByteArray();
}
QFile file(path);
return file.open(QIODevice::Text | QIODevice::ReadOnly) ? file.readAll().right(logFileTailMaxLength) : QByteArray();
}

View File

@ -24,7 +24,6 @@
bridgepp::Log &initLog(); ///< Initialize the application log.
QByteArray tailOfLatestBridgeLog(); ///< Return the last bytes of the last bridge log.
#endif //BRIDGE_GUI_LOG_UTILS_H

View File

@ -19,9 +19,9 @@
#include "QMLBackend.h"
#include "BuildConfig.h"
#include "EventStreamWorker.h"
#include "LogUtils.h"
#include <bridgepp/BridgeUtils.h>
#include <bridgepp/Exception/Exception.h>
#include <bridgepp/Log/LogUtils.h>
#include <bridgepp/GRPC/GRPCClient.h>
#include <bridgepp/Worker/Overseer.h>
#include <bridgepp/BridgeUtils.h>

View File

@ -27,6 +27,7 @@
#include <bridgepp/Exception/Exception.h>
#include <bridgepp/FocusGRPC/FocusGRPCClient.h>
#include <bridgepp/Log/Log.h>
#include <bridgepp/Log/LogUtils.h>
#include <bridgepp/ProcessMonitor.h>
@ -116,7 +117,7 @@ QQmlComponent *createRootQmlComponent(QQmlApplicationEngine &engine) {
rootComponent->loadUrl(QUrl(qrcQmlDir + "/Bridge.qml"));
if (rootComponent->status() != QQmlComponent::Status::Ready) {
QString const &err =rootComponent->errorString();
QString const &err = rootComponent->errorString();
app().log().error(err);
throw Exception("Could not load QML component", err);
}
@ -211,7 +212,7 @@ void focusOtherInstance() {
QString error;
if (!client.connectToServer(5000, sc.port, &error)) {
throw Exception(QString("Could not connect to bridge focus service for a raise call: %1").arg(error));
throw Exception("Could not connect to bridge focus service for a raise call.", error);
}
if (!client.raise().ok()) {
throw Exception(QString("The raise call to the bridge focus service failed."));
@ -220,7 +221,7 @@ void focusOtherInstance() {
catch (Exception const &e) {
app().log().error(e.qwhat());
auto uuid = reportSentryException("Exception occurred during focusOtherInstance()", e);
app().log().fatal(QString("reportID: %1 Captured exception: %2").arg(QByteArray(uuid.bytes, 16).toHex()).arg(e.qwhat()));
app().log().fatal(QString("reportID: %1 Captured exception: %2").arg(QByteArray(uuid.bytes, 16).toHex(), e.qwhat()));
}
}
@ -276,15 +277,12 @@ int main(int argc, char *argv[]) {
}
BridgeApp guiApp(argc, argv);
initSentry();
auto sentryCloser = qScopeGuard([] { sentry_close(); });
try {
QString const& configDir = bridgepp::userConfigDir();
// Init sentry.
initSentry();
auto sentryClose = qScopeGuard([] { sentry_close(); });
initQtApplication();
@ -311,7 +309,7 @@ int main(int argc, char *argv[]) {
if (!cliOptions.attach) {
if (isBridgeRunning()) {
throw Exception("An orphan instance of bridge is already running. Please terminate it and relaunch the application.",
QString(), QString(), tailOfLatestBridgeLog());
QString(), __FUNCTION__, tailOfLatestBridgeLog());
}
// before launching bridge, we remove any trailing service config file, because we need to make sure we get a newly generated one.
@ -345,7 +343,7 @@ int main(int argc, char *argv[]) {
std::unique_ptr<QQmlComponent> rootComponent(createRootQmlComponent(engine));
std::unique_ptr<QObject> rootObject(rootComponent->create(engine.rootContext()));
if (!rootObject) {
throw Exception("Could not create root object.");
throw Exception("Could not create QML root object.");
}
ProcessMonitor *bridgeMonitor = app().bridgeMonitor();