Skip to content

Commit c7c0fc0

Browse files
authored
feat: add Blueprint-callable SetAttribute/RemoveAttribute (#138)
1 parent eb4e691 commit c7c0fc0

4 files changed

Lines changed: 124 additions & 0 deletions

File tree

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,26 @@ void UMyUnrealCrasherGameInstance::Init()
281281

282282
</details>
283283

284+
#### Attributes (iOS and Android)
285+
286+
The BugSplat plugin automatically adds several attributes from the crash context at initialization time. These attributes are not updated automatically after initialization. If you'd like to update them or add new ones, use the `SetAttribute` and `RemoveAttribute` functions.
287+
288+
To call `SetAttribute` from C++, add `BugSplatRuntime` to your module's `PrivateDependencyModuleNames` in your `.Build.cs` file:
289+
290+
```csharp
291+
PrivateDependencyModuleNames.AddRange(new string[] { "BugSplatRuntime" });
292+
```
293+
294+
Then include the header and call `SetAttribute`:
295+
296+
```cpp
297+
#include "BugSplatAttributes.h"
298+
299+
UBugSplatAttributes::SetAttribute(TEXT("userId"), TEXT("12345"));
300+
```
301+
302+
`SetAttribute` is also available as a Blueprint node under the BugSplat category.
303+
284304
### Xbox and PlayStation
285305
286306
BugSplat can provide instructions for implementing Unreal crash reporting on Xbox and PlayStation. Please email us at [support@bugsplat.com](mailto:support@bugsplat.com) for more info.
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Copyright BugSplat. All Rights Reserved.
2+
3+
#include "BugSplatAttributes.h"
4+
#include "GenericPlatform/GenericPlatformCrashContext.h"
5+
6+
#if PLATFORM_IOS
7+
#import <BugSplat/BugSplat.h>
8+
#endif
9+
10+
#if PLATFORM_ANDROID
11+
#include "Android/AndroidJNI.h"
12+
#include "Android/AndroidApplication.h"
13+
#include "Android/AndroidJava.h"
14+
#endif
15+
16+
DEFINE_LOG_CATEGORY_STATIC(LogBugSplatAttributes, Log, All);
17+
18+
void UBugSplatAttributes::SetAttribute(const FString& Key, const FString& Value)
19+
{
20+
if (Key.IsEmpty())
21+
{
22+
UE_LOG(LogBugSplatAttributes, Warning, TEXT("SetAttribute called with empty key"));
23+
return;
24+
}
25+
26+
#if PLATFORM_IOS
27+
NSString* NsKey = [NSString stringWithUTF8String:TCHAR_TO_UTF8(*Key)];
28+
NSString* NsValue = [NSString stringWithUTF8String:TCHAR_TO_UTF8(*Value)];
29+
[[BugSplat shared] setValue:NsValue forAttribute:NsKey];
30+
#elif PLATFORM_ANDROID
31+
JNIEnv* Env = FAndroidApplication::GetJavaEnv();
32+
jclass BugSplatClass = FAndroidApplication::FindJavaClass("com/bugsplat/android/BugSplat");
33+
jmethodID SetAttributeMethod = FJavaWrapper::FindStaticMethod(Env, BugSplatClass, "setAttribute",
34+
"(Ljava/lang/String;Ljava/lang/String;)V", false);
35+
Env->CallStaticVoidMethod(BugSplatClass, SetAttributeMethod,
36+
*FJavaClassObject::GetJString(Key),
37+
*FJavaClassObject::GetJString(Value));
38+
Env->DeleteLocalRef(BugSplatClass);
39+
#else
40+
FGenericCrashContext::SetGameData(Key, Value);
41+
#endif
42+
43+
UE_LOG(LogBugSplatAttributes, Verbose, TEXT("SetAttribute: %s"), *Key);
44+
}
45+
46+
void UBugSplatAttributes::RemoveAttribute(const FString& Key)
47+
{
48+
if (Key.IsEmpty())
49+
{
50+
UE_LOG(LogBugSplatAttributes, Warning, TEXT("RemoveAttribute called with empty key"));
51+
return;
52+
}
53+
54+
#if PLATFORM_IOS
55+
NSString* NsKey = [NSString stringWithUTF8String:TCHAR_TO_UTF8(*Key)];
56+
[[BugSplat shared] setValue:nil forAttribute:NsKey];
57+
#elif PLATFORM_ANDROID
58+
JNIEnv* Env = FAndroidApplication::GetJavaEnv();
59+
jclass BugSplatClass = FAndroidApplication::FindJavaClass("com/bugsplat/android/BugSplat");
60+
jmethodID RemoveAttributeMethod = FJavaWrapper::FindStaticMethod(Env, BugSplatClass, "removeAttribute",
61+
"(Ljava/lang/String;)V", false);
62+
Env->CallStaticVoidMethod(BugSplatClass, RemoveAttributeMethod,
63+
*FJavaClassObject::GetJString(Key));
64+
Env->DeleteLocalRef(BugSplatClass);
65+
#else
66+
FGenericCrashContext::SetGameData(Key, TEXT(""));
67+
#endif
68+
69+
UE_LOG(LogBugSplatAttributes, Verbose, TEXT("RemoveAttribute: %s"), *Key);
70+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Copyright BugSplat. All Rights Reserved.
2+
3+
#pragma once
4+
5+
#include "Kismet/BlueprintFunctionLibrary.h"
6+
#include "BugSplatAttributes.generated.h"
7+
8+
UCLASS()
9+
class BUGSPLATRUNTIME_API UBugSplatAttributes : public UBlueprintFunctionLibrary
10+
{
11+
GENERATED_BODY()
12+
13+
public:
14+
/**
15+
* Set a custom attribute that will be included in crash reports.
16+
* Can be called at any time after BugSplat has been initialized.
17+
*
18+
* On Windows/macOS/Linux this sets game data on the crash context.
19+
* On iOS/Android this sets the attribute on the native BugSplat SDK.
20+
*
21+
* @param Key The attribute key.
22+
* @param Value The attribute value.
23+
*/
24+
UFUNCTION(BlueprintCallable, Category = "BugSplat")
25+
static void SetAttribute(const FString& Key, const FString& Value);
26+
27+
/**
28+
* Remove a custom attribute so it is no longer included in crash reports.
29+
*
30+
* @param Key The attribute key to remove.
31+
*/
32+
UFUNCTION(BlueprintCallable, Category = "BugSplat")
33+
static void RemoveAttribute(const FString& Key);
34+
};
90.3 KB
Binary file not shown.

0 commit comments

Comments
 (0)