diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ffd6b84..fa8ca8b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ ## Unreleased +### Features + +- Add breadcrumbs automatically when printing to logs ([#522](https://github.com/getsentry/sentry-unreal/pull/522)) + ### Dependencies - Bump CLI from v2.29.1 to v2.31.0 ([#512](https://github.com/getsentry/sentry-unreal/pull/512), [#515](https://github.com/getsentry/sentry-unreal/pull/515), [#517](https://github.com/getsentry/sentry-unreal/pull/517), [#524](https://github.com/getsentry/sentry-unreal/pull/524), [#525](https://github.com/getsentry/sentry-unreal/pull/525)) diff --git a/plugin-dev/Source/Sentry/Private/SentryOutputDevice.cpp b/plugin-dev/Source/Sentry/Private/SentryOutputDevice.cpp new file mode 100644 index 00000000..9b73f759 --- /dev/null +++ b/plugin-dev/Source/Sentry/Private/SentryOutputDevice.cpp @@ -0,0 +1,75 @@ +// Copyright (c) 2024 Sentry. All Rights Reserved. + +#include "SentryOutputDevice.h" + +#include "SentryBreadcrumb.h" +#include "SentryModule.h" +#include "SentrySettings.h" +#include "SentrySubsystem.h" + +#include "Engine/Engine.h" + +void FSentryOutputDevice::Serialize(const TCHAR* V, ELogVerbosity::Type Verbosity, const FName& Category) +{ + const USentrySettings* Settings = FSentryModule::Get().GetSettings(); + + bool bAddBreadcrumb; + + ESentryLevel BreadcrumbLevel = ESentryLevel::Debug; + + switch (Verbosity) + { + case ELogVerbosity::Fatal: + bAddBreadcrumb = Settings->AutomaticBreadcrumbsForLogs.bOnFatalLog; + BreadcrumbLevel = ESentryLevel::Fatal; + break; + case ELogVerbosity::Error: + bAddBreadcrumb = Settings->AutomaticBreadcrumbsForLogs.bOnErrorLog; + BreadcrumbLevel = ESentryLevel::Error; + break; + case ELogVerbosity::Warning: + bAddBreadcrumb = Settings->AutomaticBreadcrumbsForLogs.bOnWarningLog; + BreadcrumbLevel = ESentryLevel::Warning; + break; + case ELogVerbosity::Display: + case ELogVerbosity::Log: + bAddBreadcrumb = Settings->AutomaticBreadcrumbsForLogs.bOnInfoLog; + BreadcrumbLevel = ESentryLevel::Info; + break; + case ELogVerbosity::Verbose: + case ELogVerbosity::VeryVerbose: + bAddBreadcrumb = Settings->AutomaticBreadcrumbsForLogs.bOnDebugLog; + BreadcrumbLevel = ESentryLevel::Debug; + break; + default: + bAddBreadcrumb = false; + } + + if(!bAddBreadcrumb) + { + return; + } + + USentrySubsystem* SentrySubsystem = GEngine->GetEngineSubsystem(); + if(!SentrySubsystem || !SentrySubsystem->IsEnabled()) + { + return; + } + + SentrySubsystem->AddBreadcrumbWithParams(V, Category.ToString(), FString(), TMap(), BreadcrumbLevel); +} + +bool FSentryOutputDevice::CanBeUsedOnAnyThread() const +{ + return true; +} + +bool FSentryOutputDevice::CanBeUsedOnMultipleThreads() const +{ + return true; +} + +bool FSentryOutputDevice::CanBeUsedOnPanicThread() const +{ + return true; +} diff --git a/plugin-dev/Source/Sentry/Private/SentrySubsystem.cpp b/plugin-dev/Source/Sentry/Private/SentrySubsystem.cpp index 2e35d53d..11c15245 100644 --- a/plugin-dev/Source/Sentry/Private/SentrySubsystem.cpp +++ b/plugin-dev/Source/Sentry/Private/SentrySubsystem.cpp @@ -12,7 +12,9 @@ #include "SentryBeforeSendHandler.h" #include "SentryTraceSampler.h" #include "SentryTransactionContext.h" +#include "SentryOutputDevice.h" +#include "CoreGlobals.h" #include "Engine/World.h" #include "Misc/EngineVersion.h" #include "Misc/CoreDelegates.h" @@ -115,6 +117,13 @@ void USentrySubsystem::Initialize() PromoteTags(); ConfigureBreadcrumbs(); + + OutputDevice = MakeShareable(new FSentryOutputDevice()); + if(OutputDevice) + { + GLog->AddOutputDevice(OutputDevice.Get()); + GLog->SerializeBacklog(OutputDevice.Get()); + } } void USentrySubsystem::InitializeWithSettings(const FConfigureSettingsDelegate& OnConfigureSettings) @@ -128,6 +137,11 @@ void USentrySubsystem::InitializeWithSettings(const FConfigureSettingsDelegate& void USentrySubsystem::Close() { + if(GLog && OutputDevice) + { + GLog->RemoveOutputDevice(OutputDevice.Get()); + } + if (!SubsystemNativeImpl || !SubsystemNativeImpl->IsEnabled()) return; diff --git a/plugin-dev/Source/Sentry/Public/SentryOutputDevice.h b/plugin-dev/Source/Sentry/Public/SentryOutputDevice.h new file mode 100644 index 00000000..9372e5cb --- /dev/null +++ b/plugin-dev/Source/Sentry/Public/SentryOutputDevice.h @@ -0,0 +1,15 @@ +// Copyright (c) 2024 Sentry. All Rights Reserved. + +#pragma once + +#include "Misc/OutputDevice.h" + +class FSentryOutputDevice : public FOutputDevice +{ +public: + virtual void Serialize( const TCHAR* V, ELogVerbosity::Type Verbosity, const FName& Category) override; + + virtual bool CanBeUsedOnAnyThread() const override; + virtual bool CanBeUsedOnMultipleThreads() const override; + virtual bool CanBeUsedOnPanicThread() const override; +}; diff --git a/plugin-dev/Source/Sentry/Public/SentrySettings.h b/plugin-dev/Source/Sentry/Public/SentrySettings.h index be653cfd..9f430884 100644 --- a/plugin-dev/Source/Sentry/Public/SentrySettings.h +++ b/plugin-dev/Source/Sentry/Public/SentrySettings.h @@ -45,6 +45,32 @@ struct FAutomaticBreadcrumbs bool bOnUserActivityStringChanged = false; }; +USTRUCT(BlueprintType) +struct FAutomaticBreadcrumbsForLogs +{ + GENERATED_BODY() + + UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category = "General", + Meta = (DisplayName = "Fatal", ToolTip = "Flag indicating whether to automatically add breadcrumb when printing log message with Fatal verbosity level.")) + bool bOnFatalLog = true; + + UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category = "General", + Meta = (DisplayName = "Error", ToolTip = "Flag indicating whether to automatically add breadcrumb when printing log message with Error verbosity level.")) + bool bOnErrorLog = true; + + UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category = "General", + Meta = (DisplayName = "Warning", ToolTip = "Flag indicating whether to automatically add breadcrumb when printing log message with Warning verbosity level.")) + bool bOnWarningLog = true; + + UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category = "General", + Meta = (DisplayName = "Display/Log", ToolTip = "Flag indicating whether to automatically add breadcrumb when printing log message with Display/Log verbosity level.")) + bool bOnInfoLog = false; + + UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category = "General", + Meta = (DisplayName = "Verbose/VeryVerbose", ToolTip = "Flag indicating whether to automatically add breadcrumb when printing log message with Verbose/VeryVerbose verbosity level.")) + bool bOnDebugLog = false; +}; + USTRUCT(BlueprintType) struct FTagsPromotion { @@ -206,9 +232,13 @@ class SENTRY_API USentrySettings : public UObject int32 MaxBreadcrumbs; UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category = "General|Breadcrumbs", - Meta = (DisplayName = "Automatically add breadcrumbs")) + Meta = (DisplayName = "Automatically add breadcrumbs for events")) FAutomaticBreadcrumbs AutomaticBreadcrumbs; + UPROPERTY(Config, EditAnywhere, BlueprintReadWrite, Category = "General|Breadcrumbs", + Meta = (DisplayName = "Automatically add breadcrumbs for log messages with verbosity level")) + FAutomaticBreadcrumbsForLogs AutomaticBreadcrumbsForLogs; + UPROPERTY(Config, EditAnywhere, Category = "General|Release & Health", Meta = (DisplayName = "Enable automatic session tracking ", ToolTip = "Flag indicating whether the SDK should automatically start a new session when it is initialized.")) bool EnableAutoSessionTracking; diff --git a/plugin-dev/Source/Sentry/Public/SentrySubsystem.h b/plugin-dev/Source/Sentry/Public/SentrySubsystem.h index f11775fb..664a62f0 100644 --- a/plugin-dev/Source/Sentry/Public/SentrySubsystem.h +++ b/plugin-dev/Source/Sentry/Public/SentrySubsystem.h @@ -22,6 +22,7 @@ class USentryTraceSampler; class USentryTransactionContext; class ISentrySubsystem; +class FSentryOutputDevice; DECLARE_DYNAMIC_DELEGATE_OneParam(FConfigureSettingsDelegate, USentrySettings*, Settings); @@ -295,6 +296,8 @@ class SENTRY_API USentrySubsystem : public UEngineSubsystem private: TSharedPtr SubsystemNativeImpl; + TSharedPtr OutputDevice; + UPROPERTY() USentryBeforeSendHandler* BeforeSendHandler; diff --git a/scripts/packaging/package-github.snapshot b/scripts/packaging/package-github.snapshot index 9f70c92c..320e84f9 100644 --- a/scripts/packaging/package-github.snapshot +++ b/scripts/packaging/package-github.snapshot @@ -146,6 +146,7 @@ Source/Sentry/Private/SentryHint.cpp Source/Sentry/Private/SentryId.cpp Source/Sentry/Private/SentryLibrary.cpp Source/Sentry/Private/SentryModule.cpp +Source/Sentry/Private/SentryOutputDevice.cpp Source/Sentry/Private/SentrySamplingContext.cpp Source/Sentry/Private/SentryScope.cpp Source/Sentry/Private/SentrySettings.cpp @@ -175,6 +176,7 @@ Source/Sentry/Public/SentryHint.h Source/Sentry/Public/SentryId.h Source/Sentry/Public/SentryLibrary.h Source/Sentry/Public/SentryModule.h +Source/Sentry/Public/SentryOutputDevice.h Source/Sentry/Public/SentrySamplingContext.h Source/Sentry/Public/SentryScope.h Source/Sentry/Public/SentrySettings.h diff --git a/scripts/packaging/package-marketplace.snapshot b/scripts/packaging/package-marketplace.snapshot index 4742d3d4..e07e0b67 100644 --- a/scripts/packaging/package-marketplace.snapshot +++ b/scripts/packaging/package-marketplace.snapshot @@ -144,6 +144,7 @@ Source/Sentry/Private/SentryHint.cpp Source/Sentry/Private/SentryId.cpp Source/Sentry/Private/SentryLibrary.cpp Source/Sentry/Private/SentryModule.cpp +Source/Sentry/Private/SentryOutputDevice.cpp Source/Sentry/Private/SentrySamplingContext.cpp Source/Sentry/Private/SentryScope.cpp Source/Sentry/Private/SentrySettings.cpp @@ -173,6 +174,7 @@ Source/Sentry/Public/SentryHint.h Source/Sentry/Public/SentryId.h Source/Sentry/Public/SentryLibrary.h Source/Sentry/Public/SentryModule.h +Source/Sentry/Public/SentryOutputDevice.h Source/Sentry/Public/SentrySamplingContext.h Source/Sentry/Public/SentryScope.h Source/Sentry/Public/SentrySettings.h