Skip to content

Commit

Permalink
Add breadcrumbs automatically when printing to logs (#522)
Browse files Browse the repository at this point in the history
* Add breadcrumbs automatically when printing to logs

* Update package snapshot

* Update changelog

* Add missing include
  • Loading branch information
tustanivsky authored Apr 2, 2024
1 parent ae5ea63 commit 5f5234f
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
75 changes: 75 additions & 0 deletions plugin-dev/Source/Sentry/Private/SentryOutputDevice.cpp
Original file line number Diff line number Diff line change
@@ -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<USentrySubsystem>();
if(!SentrySubsystem || !SentrySubsystem->IsEnabled())
{
return;
}

SentrySubsystem->AddBreadcrumbWithParams(V, Category.ToString(), FString(), TMap<FString, FString>(), BreadcrumbLevel);
}

bool FSentryOutputDevice::CanBeUsedOnAnyThread() const
{
return true;
}

bool FSentryOutputDevice::CanBeUsedOnMultipleThreads() const
{
return true;
}

bool FSentryOutputDevice::CanBeUsedOnPanicThread() const
{
return true;
}
14 changes: 14 additions & 0 deletions plugin-dev/Source/Sentry/Private/SentrySubsystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand All @@ -128,6 +137,11 @@ void USentrySubsystem::InitializeWithSettings(const FConfigureSettingsDelegate&

void USentrySubsystem::Close()
{
if(GLog && OutputDevice)
{
GLog->RemoveOutputDevice(OutputDevice.Get());
}

if (!SubsystemNativeImpl || !SubsystemNativeImpl->IsEnabled())
return;

Expand Down
15 changes: 15 additions & 0 deletions plugin-dev/Source/Sentry/Public/SentryOutputDevice.h
Original file line number Diff line number Diff line change
@@ -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;
};
32 changes: 31 additions & 1 deletion plugin-dev/Source/Sentry/Public/SentrySettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 3 additions & 0 deletions plugin-dev/Source/Sentry/Public/SentrySubsystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class USentryTraceSampler;
class USentryTransactionContext;

class ISentrySubsystem;
class FSentryOutputDevice;

DECLARE_DYNAMIC_DELEGATE_OneParam(FConfigureSettingsDelegate, USentrySettings*, Settings);

Expand Down Expand Up @@ -295,6 +296,8 @@ class SENTRY_API USentrySubsystem : public UEngineSubsystem
private:
TSharedPtr<ISentrySubsystem> SubsystemNativeImpl;

TSharedPtr<FSentryOutputDevice> OutputDevice;

UPROPERTY()
USentryBeforeSendHandler* BeforeSendHandler;

Expand Down
2 changes: 2 additions & 0 deletions scripts/packaging/package-github.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions scripts/packaging/package-marketplace.snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 5f5234f

Please sign in to comment.