Skip to content

Commit d914c5a

Browse files
committed
Add local scope implementation for desktop
1 parent 6434596 commit d914c5a

10 files changed

+147
-284
lines changed

plugin-dev/Source/Sentry/Private/GenericPlatform/GenericPlatformSentryScope.cpp

Lines changed: 27 additions & 172 deletions
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,13 @@
1414
#if USE_SENTRY_NATIVE
1515

1616
FGenericPlatformSentryScope::FGenericPlatformSentryScope()
17-
: Level(ESentryLevel::Debug)
1817
{
18+
Scope = sentry_local_scope_new();
1919
}
2020

21-
FGenericPlatformSentryScope::FGenericPlatformSentryScope(const FGenericPlatformSentryScope& Scope)
21+
FGenericPlatformSentryScope::FGenericPlatformSentryScope(sentry_scope_t* scope)
22+
: Scope(scope)
2223
{
23-
Dist = Scope.Dist;
24-
Environment = Scope.Environment;
25-
Fingerprint = Scope.Fingerprint;
26-
Tags = Scope.Tags;
27-
Extra = Scope.Extra;
28-
Contexts = Scope.Contexts;
29-
Breadcrumbs = TRingBuffer<TSharedPtr<FGenericPlatformSentryBreadcrumb>>(Scope.Breadcrumbs);
30-
Level = Scope.Level;
3124
}
3225

3326
FGenericPlatformSentryScope::~FGenericPlatformSentryScope()
@@ -36,21 +29,11 @@ FGenericPlatformSentryScope::~FGenericPlatformSentryScope()
3629

3730
void FGenericPlatformSentryScope::AddBreadcrumb(TSharedPtr<ISentryBreadcrumb> breadcrumb)
3831
{
39-
FScopeLock Lock(&CriticalSection);
40-
41-
if (Breadcrumbs.Num() >= FSentryModule::Get().GetSettings()->MaxBreadcrumbs)
42-
{
43-
Breadcrumbs.PopFront();
44-
}
45-
46-
Breadcrumbs.Add(StaticCastSharedPtr<FGenericPlatformSentryBreadcrumb>(breadcrumb));
32+
sentry_scope_add_breadcrumb(Scope, StaticCastSharedPtr<FGenericPlatformSentryBreadcrumb>(breadcrumb)->GetNativeObject());
4733
}
4834

4935
void FGenericPlatformSentryScope::ClearBreadcrumbs()
5036
{
51-
FScopeLock Lock(&CriticalSection);
52-
53-
Breadcrumbs.Empty();
5437
}
5538

5639
void FGenericPlatformSentryScope::AddAttachment(TSharedPtr<ISentryAttachment> attachment)
@@ -65,237 +48,109 @@ void FGenericPlatformSentryScope::ClearAttachments()
6548

6649
void FGenericPlatformSentryScope::SetTagValue(const FString& key, const FString& value)
6750
{
68-
Tags.Add(key, value);
51+
sentry_scope_set_tag(Scope, TCHAR_TO_UTF8(*key), TCHAR_TO_UTF8(*value));
6952
}
7053

7154
FString FGenericPlatformSentryScope::GetTagValue(const FString& key) const
7255
{
73-
if (!Tags.Contains(key))
74-
return FString();
75-
76-
return Tags[key];
56+
return FString();
7757
}
7858

7959
void FGenericPlatformSentryScope::RemoveTag(const FString& key)
8060
{
81-
Tags.Remove(key);
8261
}
8362

8463
void FGenericPlatformSentryScope::SetTags(const TMap<FString, FString>& tags)
8564
{
86-
Tags.Append(tags);
65+
for (const auto& tagItem : tags)
66+
{
67+
SetTagValue(tagItem.Key, tagItem.Value);
68+
}
8769
}
8870

8971
TMap<FString, FString> FGenericPlatformSentryScope::GetTags() const
9072
{
91-
return Tags;
73+
return TMap<FString, FString>();
9274
}
9375

9476
void FGenericPlatformSentryScope::SetDist(const FString& dist)
9577
{
96-
Dist = dist;
78+
// Set via options during initialization
9779
}
9880

9981
FString FGenericPlatformSentryScope::GetDist() const
10082
{
101-
return Dist;
83+
return FString();
10284
}
10385

10486
void FGenericPlatformSentryScope::SetEnvironment(const FString& environment)
10587
{
106-
Environment = environment;
88+
// Set via options during initialization
10789
}
10890

10991
FString FGenericPlatformSentryScope::GetEnvironment() const
11092
{
111-
return Environment;
93+
return FString();
11294
}
11395

11496
void FGenericPlatformSentryScope::SetFingerprint(const TArray<FString>& fingerprint)
11597
{
116-
Fingerprint = fingerprint;
98+
sentry_scope_set_fingerprints(Scope, FGenericPlatformSentryConverters::StringArrayToNative(fingerprint));
11799
}
118100

119101
TArray<FString> FGenericPlatformSentryScope::GetFingerprint() const
120102
{
121-
return Fingerprint;
103+
return TArray<FString>();
122104
}
123105

124106
void FGenericPlatformSentryScope::SetLevel(ESentryLevel level)
125107
{
126-
Level = level;
108+
sentry_scope_set_level(Scope, FGenericPlatformSentryConverters::SentryLevelToNative(level));
127109
}
128110

129111
ESentryLevel FGenericPlatformSentryScope::GetLevel() const
130112
{
131-
132-
return Level;
113+
return ESentryLevel::Debug;
133114
}
134115

135116
void FGenericPlatformSentryScope::SetContext(const FString& key, const TMap<FString, FString>& values)
136117
{
137-
Contexts.Add(key, values);
118+
sentry_scope_set_context(Scope, TCHAR_TO_UTF8(*key), FGenericPlatformSentryConverters::StringMapToNative(values));
138119
}
139120

140121
void FGenericPlatformSentryScope::RemoveContext(const FString& key)
141122
{
142-
if (!Contexts.Contains(key))
143-
return;
144-
145-
Contexts.Remove(key);
146123
}
147124

148125
void FGenericPlatformSentryScope::SetExtraValue(const FString& key, const FString& value)
149126
{
150-
Extra.Add(key, value);
127+
sentry_scope_set_extra(Scope, TCHAR_TO_UTF8(*key), sentry_value_new_string(TCHAR_TO_UTF8(*value)));
151128
}
152129

153130
FString FGenericPlatformSentryScope::GetExtraValue(const FString& key) const
154131
{
155-
if (!Extra.Contains(key))
156-
return FString();
157-
158-
return Extra[key];
132+
return FString();
159133
}
160134

161135
void FGenericPlatformSentryScope::RemoveExtra(const FString& key)
162136
{
163-
if (!Extra.Contains(key))
164-
return;
165-
166-
Extra.Remove(key);
167137
}
168138

169139
void FGenericPlatformSentryScope::SetExtras(const TMap<FString, FString>& extras)
170140
{
171-
Extra.Append(extras);
141+
for (const auto& extraItem : extras)
142+
{
143+
SetExtraValue(extraItem.Key, extraItem.Value);
144+
}
172145
}
173146

174147
TMap<FString, FString> FGenericPlatformSentryScope::GetExtras() const
175148
{
176-
return Extra;
149+
return TMap<FString, FString>();
177150
}
178151

179152
void FGenericPlatformSentryScope::Clear()
180153
{
181-
Dist = FString();
182-
Environment = FString();
183-
Fingerprint.Empty();
184-
Tags.Empty();
185-
Extra.Empty();
186-
Contexts.Empty();
187-
Breadcrumbs.Empty();
188-
Level = ESentryLevel::Debug;
189-
}
190-
191-
void FGenericPlatformSentryScope::Apply(TSharedPtr<FGenericPlatformSentryEvent> event)
192-
{
193-
sentry_value_t nativeEvent = event->GetNativeObject();
194-
195-
sentry_value_t eventLevel = sentry_value_get_by_key(nativeEvent, "level");
196-
197-
FString scopeLevelStr = FGenericPlatformSentryConverters::SentryLevelToString(Level).ToLower();
198-
if (!scopeLevelStr.IsEmpty() && sentry_value_is_null(eventLevel))
199-
{
200-
sentry_value_set_by_key(nativeEvent, "level", sentry_value_new_string(TCHAR_TO_ANSI(*scopeLevelStr)));
201-
}
202-
203-
if (!Dist.IsEmpty())
204-
{
205-
sentry_value_set_by_key(nativeEvent, "dist", sentry_value_new_string(TCHAR_TO_ANSI(*Dist)));
206-
}
207-
208-
if (!Environment.IsEmpty())
209-
{
210-
sentry_value_set_by_key(nativeEvent, "environment", sentry_value_new_string(TCHAR_TO_ANSI(*Environment)));
211-
}
212-
213-
if (Fingerprint.Num() > 0)
214-
{
215-
sentry_value_set_by_key(nativeEvent, "fingerprint", FGenericPlatformSentryConverters::StringArrayToNative(Fingerprint));
216-
}
217-
218-
if (Tags.Num() > 0)
219-
{
220-
sentry_value_t tagsExtra = sentry_value_get_by_key(nativeEvent, "tags");
221-
if (sentry_value_is_null(tagsExtra))
222-
{
223-
sentry_value_set_by_key(nativeEvent, "tags", FGenericPlatformSentryConverters::StringMapToNative(Tags));
224-
}
225-
else
226-
{
227-
for (const auto& TagItem : Tags)
228-
{
229-
sentry_value_set_by_key(tagsExtra, TCHAR_TO_ANSI(*TagItem.Key), sentry_value_new_string(TCHAR_TO_ANSI(*TagItem.Value)));
230-
}
231-
}
232-
}
233-
234-
if (Extra.Num() > 0)
235-
{
236-
sentry_value_t eventExtra = sentry_value_get_by_key(nativeEvent, "extra");
237-
if (sentry_value_is_null(eventExtra))
238-
{
239-
sentry_value_set_by_key(nativeEvent, "extra", FGenericPlatformSentryConverters::StringMapToNative(Extra));
240-
}
241-
else
242-
{
243-
for (const auto& ExtraItem : Extra)
244-
{
245-
sentry_value_set_by_key(eventExtra, TCHAR_TO_ANSI(*ExtraItem.Key), sentry_value_new_string(TCHAR_TO_ANSI(*ExtraItem.Value)));
246-
}
247-
}
248-
}
249-
250-
if (Contexts.Num() > 0)
251-
{
252-
sentry_value_t eventContexts = sentry_value_get_by_key(nativeEvent, "contexts");
253-
if (sentry_value_is_null(eventContexts))
254-
{
255-
eventContexts = sentry_value_new_object();
256-
257-
for (const auto& ContextsItem : Contexts)
258-
{
259-
sentry_value_set_by_key(eventContexts, TCHAR_TO_ANSI(*ContextsItem.Key), FGenericPlatformSentryConverters::StringMapToNative(ContextsItem.Value));
260-
}
261-
262-
sentry_value_set_by_key(nativeEvent, "contexts", eventContexts);
263-
}
264-
else
265-
{
266-
for (const auto& ContextsItem : Contexts)
267-
{
268-
sentry_value_set_by_key(eventContexts, TCHAR_TO_ANSI(*ContextsItem.Key), FGenericPlatformSentryConverters::StringMapToNative(ContextsItem.Value));
269-
}
270-
}
271-
}
272-
273-
if (!Breadcrumbs.IsEmpty())
274-
{
275-
sentry_value_t eventBreadcrumbs = sentry_value_get_by_key(nativeEvent, "breadcrumbs");
276-
if (sentry_value_is_null(eventBreadcrumbs))
277-
{
278-
eventBreadcrumbs = sentry_value_new_list();
279-
280-
for (const auto& Breadcrumb : Breadcrumbs)
281-
{
282-
sentry_value_t nativeBreadcrumb = Breadcrumb->GetNativeObject();
283-
sentry_value_incref(nativeBreadcrumb);
284-
sentry_value_append(eventBreadcrumbs, nativeBreadcrumb);
285-
}
286-
287-
sentry_value_set_by_key(nativeEvent, "breadcrumbs", eventBreadcrumbs);
288-
}
289-
else
290-
{
291-
for (const auto& Breadcrumb : Breadcrumbs)
292-
{
293-
sentry_value_t nativeBreadcrumb = Breadcrumb->GetNativeObject();
294-
sentry_value_incref(nativeBreadcrumb);
295-
sentry_value_append(eventBreadcrumbs, nativeBreadcrumb);
296-
}
297-
}
298-
}
299154
}
300155

301156
#endif

plugin-dev/Source/Sentry/Private/GenericPlatform/GenericPlatformSentryScope.h

Lines changed: 3 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22

33
#pragma once
44

5-
#include "Containers/RingBuffer.h"
6-
#include "HAL/CriticalSection.h"
5+
#include "Convenience/GenericPlatformSentryInclude.h"
76

87
#include "Interface/SentryScopeInterface.h"
98

@@ -16,7 +15,7 @@ class FGenericPlatformSentryScope : public ISentryScope
1615
{
1716
public:
1817
FGenericPlatformSentryScope();
19-
FGenericPlatformSentryScope(const FGenericPlatformSentryScope& Scope);
18+
FGenericPlatformSentryScope(sentry_scope_t* scope);
2019
virtual ~FGenericPlatformSentryScope() override;
2120

2221
virtual void AddBreadcrumb(TSharedPtr<ISentryBreadcrumb> breadcrumb) override;
@@ -45,24 +44,8 @@ class FGenericPlatformSentryScope : public ISentryScope
4544
virtual TMap<FString, FString> GetExtras() const override;
4645
virtual void Clear() override;
4746

48-
void Apply(TSharedPtr<FGenericPlatformSentryEvent> event);
49-
5047
private:
51-
FString Dist;
52-
FString Environment;
53-
54-
TArray<FString> Fingerprint;
55-
56-
TMap<FString, FString> Tags;
57-
TMap<FString, FString> Extra;
58-
59-
TMap<FString, TMap<FString, FString>> Contexts;
60-
61-
TRingBuffer<TSharedPtr<FGenericPlatformSentryBreadcrumb>> Breadcrumbs;
62-
63-
ESentryLevel Level;
64-
65-
FCriticalSection CriticalSection;
48+
sentry_scope_t* Scope;
6649
};
6750

6851
typedef FGenericPlatformSentryScope FPlatformSentryScope;

0 commit comments

Comments
 (0)