-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathIOSSentrySubsystem.cpp
136 lines (108 loc) · 4.53 KB
/
IOSSentrySubsystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "IOS/IOSSentrySubsystem.h"
#include "IOS/IOSAppDelegate.h"
#include "SentryDefines.h"
#include "SentrySettings.h"
#include "Utils/SentryScreenshotUtils.h"
#include "Utils/SentryFileUtils.h"
#include "HAL/FileManager.h"
#include "Misc/CoreDelegates.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
static FIOSSentrySubsystem* GIOSSentrySubsystem = nullptr;
struct sigaction DefaultSigIllHandler;
struct sigaction DefaultSigEmtHandler;
struct sigaction DefaultSigFpeHandler;
struct sigaction DefaultSigBusHandler;
struct sigaction DefaultSigSegvHandler;
struct sigaction DefaultSigSysHandler;
void SaveDefaultSignalHandlers()
{
sigaction(SIGILL, NULL, &DefaultSigIllHandler);
sigaction(SIGEMT, NULL, &DefaultSigEmtHandler);
sigaction(SIGFPE, NULL, &DefaultSigFpeHandler);
sigaction(SIGBUS, NULL, &DefaultSigBusHandler);
sigaction(SIGSEGV, NULL, &DefaultSigSegvHandler);
sigaction(SIGSYS, NULL, &DefaultSigSysHandler);
}
void RestoreDefaultSignalHandlers()
{
sigaction(SIGILL, &DefaultSigIllHandler, NULL);
sigaction(SIGEMT, &DefaultSigEmtHandler, NULL);
sigaction(SIGFPE, &DefaultSigFpeHandler, NULL);
sigaction(SIGBUS, &DefaultSigBusHandler, NULL);
sigaction(SIGSEGV, &DefaultSigSegvHandler, NULL);
sigaction(SIGSYS, &DefaultSigSysHandler, NULL);
}
static void IOSSentrySignalHandler(int Signal, siginfo_t *Info, void *Context)
{
if (GIOSSentrySubsystem && GIOSSentrySubsystem->IsEnabled())
{
GIOSSentrySubsystem->TryCaptureScreenshot();
}
RestoreDefaultSignalHandlers();
// Re-raise signal to default handler
raise(Signal);
}
void InstallSentrySignalHandler()
{
struct sigaction Action;
memset(&Action, 0, sizeof(Action));
Action.sa_sigaction = IOSSentrySignalHandler;
Action.sa_flags = SA_SIGINFO | SA_ONSTACK;
sigaction(SIGILL, &Action, NULL);
sigaction(SIGEMT, &Action, NULL);
sigaction(SIGFPE, &Action, NULL);
sigaction(SIGBUS, &Action, NULL);
sigaction(SIGSEGV, &Action, NULL);
sigaction(SIGSYS, &Action, NULL);
}
void FIOSSentrySubsystem::InitWithSettings(const USentrySettings* Settings, USentryBeforeSendHandler* BeforeSendHandler, USentryBeforeBreadcrumbHandler* BeforeBreadcrumbHandler,
USentryTraceSampler* TraceSampler)
{
GIOSSentrySubsystem = this;
SaveDefaultSignalHandlers();
InstallSentrySignalHandler();
FAppleSentrySubsystem::InitWithSettings(Settings, BeforeSendHandler, BeforeBreadcrumbHandler, TraceSampler);
}
FString FIOSSentrySubsystem::TryCaptureScreenshot() const
{
FString ScreenshotPath = GetScreenshotPath();
dispatch_sync(dispatch_get_main_queue(), ^{
UIGraphicsBeginImageContextWithOptions([IOSAppDelegate GetDelegate].RootView.bounds.size, NO, 2.0f);
[[IOSAppDelegate GetDelegate].RootView drawViewHierarchyInRect:[IOSAppDelegate GetDelegate].RootView.bounds afterScreenUpdates:YES];
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
NSData *ImageData = UIImagePNGRepresentation(image);
TArray<uint8> ImageBytes;
uint32 SavedSize = ImageData.length;
ImageBytes.AddUninitialized(SavedSize);
FPlatformMemory::Memcpy(ImageBytes.GetData(), [ImageData bytes], SavedSize);
if (!FFileHelper::SaveArrayToFile(ImageBytes, *ScreenshotPath))
{
UE_LOG(LogSentrySdk, Error, TEXT("Failed to save screenshot to: %s"), *ScreenshotPath);
}
});
return ScreenshotPath;
}
FString FIOSSentrySubsystem::GetGameLogPath() const
{
const FString& logFilePath = SentryFileUtils::GetGameLogPath();
return IFileManager::Get().FileExists(*logFilePath) ? logFilePath : NormalizeToPublicIOSPath(logFilePath);
}
FString FIOSSentrySubsystem::GetLatestGameLog() const
{
const FString logFilePath = SentryFileUtils::GetGameLogBackupPath();
return IFileManager::Get().FileExists(*logFilePath) ? logFilePath : NormalizeToPublicIOSPath(logFilePath);
}
FString FIOSSentrySubsystem::NormalizeToPublicIOSPath(const FString& logFilePath) const
{
// This is a workaround for iOS log file not being accessible via the path returned by engine's API.
// See https://github.com/getsentry/sentry-unreal/pull/732
static FString PublicWritePathBase = FString([NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]);
static FString PrivateWritePathBase = FString([NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES) objectAtIndex:0]);
if (logFilePath.StartsWith(PrivateWritePathBase))
{
return logFilePath.Replace(*PrivateWritePathBase, *PublicWritePathBase);
}
return logFilePath;
}