Skip to content

Commit

Permalink
Add scope class for desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
tustanivsky committed Jul 13, 2023
1 parent 5b0e8ba commit dea7630
Show file tree
Hide file tree
Showing 5 changed files with 311 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,19 @@ sentry_value_t SentryConvertorsDesktop::StringMapToNative(const TMap<FString, FS
return sentryObject;
}

sentry_value_t SentryConvertorsDesktop::StringArrayToNative(const TArray<FString>& array)
{
sentry_value_t sentryArray = sentry_value_new_list();

for (auto it = array.CreateConstIterator(); it; ++it)
{
const FString& ArrayItem = *it;
sentry_value_append(sentryArray, sentry_value_new_string(TCHAR_TO_ANSI(*ArrayItem)));
}

return sentryArray;
}

ESentryLevel SentryConvertorsDesktop::SentryLevelToUnreal(sentry_value_t level)
{
FString levelStr = FString(sentry_value_as_string(level));
Expand All @@ -67,6 +80,34 @@ ESentryLevel SentryConvertorsDesktop::SentryLevelToUnreal(sentry_value_t level)
return static_cast<ESentryLevel>(Enum->GetValueByName(FName(*levelStr)));
}

ESentryLevel SentryConvertorsDesktop::SentryLevelToUnreal(sentry_level_t level)
{
ESentryLevel Level = ESentryLevel::Debug;

switch (level)
{
case SENTRY_LEVEL_DEBUG:
Level = ESentryLevel::Debug;
break;
case SENTRY_LEVEL_INFO:
Level = ESentryLevel::Info;
break;
case SENTRY_LEVEL_WARNING:
Level = ESentryLevel::Warning;
break;
case SENTRY_LEVEL_ERROR:
Level = ESentryLevel::Error;
break;
case SENTRY_LEVEL_FATAL:
Level = ESentryLevel::Fatal;
break;
default:
UE_LOG(LogSentrySdk, Warning, TEXT("Unknown sentry level value used. Debug will be returned."));;
}

return Level;
}

USentryId* SentryConvertorsDesktop::SentryIdToUnreal(sentry_uuid_t id)
{
TSharedPtr<SentryIdDesktop> idNativeImpl = MakeShareable(new SentryIdDesktop(id));
Expand Down Expand Up @@ -100,6 +141,28 @@ TMap<FString, FString> SentryConvertorsDesktop::StringMapToUnreal(sentry_value_t
return unrealMap;
}

TArray<FString> SentryConvertorsDesktop::StringArrayToUnreal(sentry_value_t array)
{
TArray<FString> unrealArray;

FString arrayJsonString = FString(sentry_value_to_json(array));

TArray<TSharedPtr<FJsonValue>> jsonArray;
TSharedRef<TJsonReader<>> jsonReader = TJsonReaderFactory<>::Create(arrayJsonString);
bool bDeserializeSuccess = FJsonSerializer::Deserialize(jsonReader, jsonArray);
if (!bDeserializeSuccess) {
UE_LOG(LogSentrySdk, Error, TEXT("StringArrayToUnreal failed to deserialize array Json."));
return unrealArray;
}

for (auto it = jsonArray.CreateConstIterator(); it; ++it)
{
unrealArray.Add(it->Get()->AsString());
}

return unrealArray;
}

FString SentryConvertorsDesktop::SentryLevelToString(ESentryLevel level)
{
const UEnum* EnumPtr = StaticEnum<ESentryLevel>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ class SentryConvertorsDesktop
/** Conversions to native desktop (Windows/Mac) types */
static sentry_level_e SentryLevelToNative(ESentryLevel level);
static sentry_value_t StringMapToNative(const TMap<FString, FString>& map);
static sentry_value_t StringArrayToNative(const TArray<FString>& array );

/** Conversions from native desktop (Windows/Mac) types */
static ESentryLevel SentryLevelToUnreal(sentry_value_t level);
static ESentryLevel SentryLevelToUnreal(sentry_level_t level);
static USentryId* SentryIdToUnreal(sentry_uuid_t id);
static TMap<FString, FString> StringMapToUnreal(sentry_value_t map);
static TArray<FString> StringArrayToUnreal(sentry_value_t array);

/** Other conversions */
static FString SentryLevelToString(ESentryLevel level);
Expand Down
184 changes: 184 additions & 0 deletions plugin-dev/Source/Sentry/Private/Desktop/SentryScopeDesktop.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
// Copyright (c) 2023 Sentry. All Rights Reserved.

#include "SentryScopeDesktop.h"
#include "SentryBreadcrumbDesktop.h"
#include "SentryEventDesktop.h"

#include "SentryBreadcrumb.h"
#include "SentryAttachment.h"
#include "SentryEvent.h"

#include "Infrastructure/SentryConvertorsDesktop.h"

#if USE_SENTRY_NATIVE

SentryScopeDesktop::SentryScopeDesktop()
: LevelDesktop(ESentryLevel::Debug)
{
}

SentryScopeDesktop::~SentryScopeDesktop()
{
}

void SentryScopeDesktop::AddBreadcrumb(USentryBreadcrumb* breadcrumb)
{
BreadcrumbsDesktop.Add(breadcrumb->GetNativeImpl());
}

void SentryScopeDesktop::ClearBreadcrumbs()
{
BreadcrumbsDesktop.Empty();
}

void SentryScopeDesktop::AddAttachment(USentryAttachment* attachment)
{
// Not available for desktop
}

void SentryScopeDesktop::ClearAttachments()
{
// Not available for desktop
}

void SentryScopeDesktop::SetTagValue(const FString& key, const FString& value)
{
TagsDesktop.Add(key, value);
}

FString SentryScopeDesktop::GetTagValue(const FString& key) const
{
if(!TagsDesktop.Contains(key))
return FString();

return TagsDesktop[key];
}

void SentryScopeDesktop::RemoveTag(const FString& key)
{
TagsDesktop.Remove(key);
}

void SentryScopeDesktop::SetTags(const TMap<FString, FString>& tags)
{
TagsDesktop.Append(tags);
}

TMap<FString, FString> SentryScopeDesktop::GetTags() const
{
return TagsDesktop;
}

void SentryScopeDesktop::SetDist(const FString& dist)
{
Dist = dist;
}

FString SentryScopeDesktop::GetDist() const
{
return Dist;
}

void SentryScopeDesktop::SetEnvironment(const FString& environment)
{
Environment = environment;
}

FString SentryScopeDesktop::GetEnvironment() const
{
return Environment;
}

void SentryScopeDesktop::SetFingerprint(const TArray<FString>& fingerprint)
{
FingerprintDesktop = fingerprint;
}

TArray<FString> SentryScopeDesktop::GetFingerprint() const
{
return FingerprintDesktop;
}

void SentryScopeDesktop::SetLevel(ESentryLevel level)
{
LevelDesktop = level;
}

ESentryLevel SentryScopeDesktop::GetLevel() const
{

return LevelDesktop;
}

void SentryScopeDesktop::SetContext(const FString& key, const TMap<FString, FString>& values)
{
ContextsDesktop.Add(key, values);
}

void SentryScopeDesktop::RemoveContext(const FString& key)
{
if(!ContextsDesktop.Contains(key))
return;

ContextsDesktop.Remove(key);
}

void SentryScopeDesktop::SetExtraValue(const FString& key, const FString& value)
{
ExtraDesktop.Add(key, value);
}

FString SentryScopeDesktop::GetExtraValue(const FString& key) const
{
if(!ExtraDesktop.Contains(key))
return FString();

return ExtraDesktop[key];
}

void SentryScopeDesktop::RemoveExtra(const FString& key)
{
if(!ExtraDesktop.Contains(key))
return;

ExtraDesktop.Remove(key);
}

void SentryScopeDesktop::SetExtras(const TMap<FString, FString>& extras)
{
ExtraDesktop.Append(extras);
}

TMap<FString, FString> SentryScopeDesktop::GetExtras() const
{
return ExtraDesktop;
}

void SentryScopeDesktop::Clear()
{
Dist = FString();
Environment = FString();
FingerprintDesktop.Empty();
TagsDesktop.Empty();
ExtraDesktop.Empty();
ContextsDesktop.Empty();
BreadcrumbsDesktop.Empty();
LevelDesktop = ESentryLevel::Debug;
}

void SentryScopeDesktop::Apply(USentryEvent* event)
{
TSharedPtr<SentryEventDesktop> eventDesktop = StaticCastSharedPtr<SentryEventDesktop>(event->GetNativeImpl());

sentry_value_t nativeEvent = eventDesktop->GetNativeObject();

if(!Dist.IsEmpty())
sentry_value_set_by_key(nativeEvent, "dist", sentry_value_new_string(TCHAR_TO_ANSI(*Dist)));
if(!Environment.IsEmpty())
sentry_value_set_by_key(nativeEvent, "environment", sentry_value_new_string(TCHAR_TO_ANSI(*Environment)));

if(!FingerprintDesktop.IsEmpty())
sentry_value_set_by_key(nativeEvent, "fingerprint", SentryConvertorsDesktop::StringArrayToNative(FingerprintDesktop));
}

#endif
60 changes: 60 additions & 0 deletions plugin-dev/Source/Sentry/Private/Desktop/SentryScopeDesktop.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright (c) 2023 Sentry. All Rights Reserved.

#pragma once

#include "Interface/SentryScopeInterface.h"

#if USE_SENTRY_NATIVE

class USentryEvent;
class USentryBreadcrumb;
class USentryAttachment;

class ISentryBreadcrumb;

class SentryScopeDesktop : public ISentryScope
{
public:
SentryScopeDesktop();
virtual ~SentryScopeDesktop() override;

virtual void AddBreadcrumb(USentryBreadcrumb* breadcrumb) override;
virtual void ClearBreadcrumbs() override;
virtual void AddAttachment(USentryAttachment* attachment) override;
virtual void ClearAttachments() override;
virtual void SetTagValue(const FString& key, const FString& value) override;
virtual FString GetTagValue(const FString& key) const override;
virtual void RemoveTag(const FString& key) override;
virtual void SetTags(const TMap<FString, FString>& tags) override;
virtual TMap<FString, FString> GetTags() const override;
virtual void SetDist(const FString& dist) override;
virtual FString GetDist() const override;
virtual void SetEnvironment(const FString& environment) override;
virtual FString GetEnvironment() const override;
virtual void SetFingerprint(const TArray<FString>& fingerprint) override;
virtual TArray<FString> GetFingerprint() const override;
virtual void SetLevel(ESentryLevel level) override;
virtual ESentryLevel GetLevel() const override;
virtual void SetContext(const FString& key, const TMap<FString, FString>& values) override;
virtual void RemoveContext(const FString& key) override;
virtual void SetExtraValue(const FString& key, const FString& value) override;
virtual FString GetExtraValue(const FString& key) const override;
virtual void RemoveExtra(const FString& key) override;
virtual void SetExtras(const TMap<FString, FString>& extras) override;
virtual TMap<FString, FString> GetExtras() const override;
virtual void Clear() override;

void Apply(USentryEvent* event);

private:
FString Dist;
FString Environment;
TArray<FString> FingerprintDesktop;
TMap<FString, FString> TagsDesktop;
TMap<FString, FString> ExtraDesktop;
TMap<FString, TMap<FString, FString>> ContextsDesktop;
TArray<TSharedPtr<ISentryBreadcrumb>> BreadcrumbsDesktop;
ESentryLevel LevelDesktop;
};

#endif
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ void SentrySubsystemDesktop::AddBreadcrumb(USentryBreadcrumb* breadcrumb)

void SentrySubsystemDesktop::ClearBreadcrumbs()
{
UE_LOG(LogSentrySdk, Log, TEXT("CaptureMessageWithScope method is not supported for the current platform."));
UE_LOG(LogSentrySdk, Log, TEXT("ClearBreadcrumbs method is not supported for the current platform."));
}

USentryId* SentrySubsystemDesktop::CaptureMessage(const FString& message, ESentryLevel level)
Expand Down

0 comments on commit dea7630

Please sign in to comment.