-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathMacSentrySubsystem.cpp
93 lines (73 loc) · 2.68 KB
/
MacSentrySubsystem.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
#include "Mac/MacSentrySubsystem.h"
#include "SentryIdApple.h"
#include "SentryDefines.h"
#include "SentryModule.h"
#include "SentrySettings.h"
#include "Utils/SentryFileUtils.h"
#include "Misc/CoreDelegates.h"
#include "Misc/FileHelper.h"
#include "Misc/Paths.h"
void FMacSentrySubsystem::InitWithSettings(const USentrySettings* Settings, USentryBeforeSendHandler* BeforeSendHandler, USentryBeforeBreadcrumbHandler* BeforeBreadcrumbHandler,
USentryTraceSampler* TraceSampler)
{
FAppleSentrySubsystem::InitWithSettings(Settings, BeforeSendHandler, BeforeBreadcrumbHandler, TraceSampler);
if (IsEnabled() && isScreenshotAttachmentEnabled)
{
FCoreDelegates::OnHandleSystemError.AddLambda([this]()
{
TryCaptureScreenshot();
});
}
}
TSharedPtr<ISentryId> FMacSentrySubsystem::CaptureEnsure(const FString& type, const FString& message)
{
TSharedPtr<ISentryId> id = FAppleSentrySubsystem::CaptureEnsure(type, message);
if (isScreenshotAttachmentEnabled)
{
const FString& screenshotPath = TryCaptureScreenshot();
if (!screenshotPath.IsEmpty())
{
UploadAttachmentForEvent(id, screenshotPath, TEXT("screenshot.png"), true);
}
}
return id;
}
FString FMacSentrySubsystem::TryCaptureScreenshot() const
{
NSWindow* MainWindow = [NSApp mainWindow];
if (!MainWindow)
{
UE_LOG(LogSentrySdk, Error, TEXT("No main window found!"));
return FString("");
}
NSRect WindowRect = [MainWindow frame];
CGWindowID WindowID = (CGWindowID)[MainWindow windowNumber];
CGImageRef ScreenshotRef = CGWindowListCreateImage(WindowRect, kCGWindowListOptionIncludingWindow, WindowID, kCGWindowImageDefault);
if (!ScreenshotRef)
{
UE_LOG(LogSentrySdk, Error, TEXT("Failed to capture screenshot - invalid ScreenshotRef."));
return FString("");
}
NSBitmapImageRep* BitmapRep = [[NSBitmapImageRep alloc] initWithCGImage:ScreenshotRef];
NSData* ImageData = [BitmapRep representationUsingType:NSBitmapImageFileTypePNG properties:@{}];
TArray<uint8> ImageBytes;
uint32 SavedSize = (uint32)[ImageData length];
ImageBytes.AddUninitialized(SavedSize);
FPlatformMemory::Memcpy(ImageBytes.GetData(), [ImageData bytes], SavedSize);
CGImageRelease(ScreenshotRef);
FString ScreenshotPath = GetScreenshotPath();
if (!FFileHelper::SaveArrayToFile(ImageBytes, *ScreenshotPath))
{
UE_LOG(LogSentrySdk, Error, TEXT("Failed to save screenshot to: %s"), *ScreenshotPath);
return FString("");
}
return ScreenshotPath;
}
FString FMacSentrySubsystem::GetGameLogPath() const
{
return SentryFileUtils::GetGameLogPath();
}
FString FMacSentrySubsystem::GetLatestGameLog() const
{
return SentryFileUtils::GetGameLogBackupPath();
}