/
Crashes.cs
385 lines (332 loc) · 11.7 KB
/
Crashes.cs
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// Licensed under the MIT license.
using Microsoft.AppCenter.Unity.Crashes.Internal;
using Microsoft.AppCenter.Unity.Internal.Utils;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
namespace Microsoft.AppCenter.Unity.Crashes
{
#if UNITY_IOS || UNITY_ANDROID
using RawType = System.IntPtr;
#else
using RawType = System.Type;
#endif
public class Crashes
{
// Used by App Center Unity Editor Extensions: https://github.com/Microsoft/AppCenter-SDK-Unity-Extension
public const string CrashesSDKVersion = "0.1.4";
private static bool _reportUnhandledExceptions = false;
private static readonly object _objectLock = new object();
#if UNITY_ANDROID
private static Queue<Exception> _unhandledExceptions = new Queue<Exception>();
private static bool _unhandledExceptionsExists = false;
#endif
public static void PrepareEventHandlers()
{
AppCenterBehavior.InitializingServices += Initialize;
AppCenterBehavior.InitializedAppCenterAndServices += HandleAppCenterInitialized;
}
public static void Initialize()
{
CrashesDelegate.SetDelegate();
}
public static void AddNativeType(List<RawType> nativeTypes)
{
CrashesInternal.AddNativeType(nativeTypes);
}
public static void TrackError(Exception exception, IDictionary<string, string> properties = null)
{
if (exception != null)
{
var exceptionWrapper = CreateWrapperException(exception);
if (properties == null || properties.Count == 0)
{
CrashesInternal.TrackException(exceptionWrapper.GetRawObject());
}
else
{
CrashesInternal.TrackException(exceptionWrapper.GetRawObject(), properties);
}
}
}
public static void OnHandleLog(string logString, string stackTrace, LogType type)
{
if (LogType.Assert == type || LogType.Exception == type || LogType.Error == type)
{
var exception = CreateWrapperException(logString, stackTrace, type);
CrashesInternal.TrackException(exception.GetRawObject());
}
}
#if !UNITY_WSA_10_0
public static void OnHandleUnresolvedException(object sender, UnhandledExceptionEventArgs args)
{
if (args == null || args.ExceptionObject == null)
{
return;
}
var exception = args.ExceptionObject as Exception;
if (exception != null)
{
Debug.Log("Unhandled exception: " + exception.ToString());
#if UNITY_ANDROID
lock (_unhandledExceptions)
{
_unhandledExceptions.Enqueue(exception);
_unhandledExceptionsExists = true;
}
#else
var exceptionWrapper = CreateWrapperException(exception);
CrashesInternal.TrackException(exceptionWrapper.GetRawObject());
#endif
}
}
#endif
public static AppCenterTask<bool> IsEnabledAsync()
{
return CrashesInternal.IsEnabledAsync();
}
public static AppCenterTask SetEnabledAsync(bool enabled)
{
return CrashesInternal.SetEnabledAsync(enabled);
}
public static void GenerateTestCrash()
{
CrashesInternal.GenerateTestCrash();
}
public static AppCenterTask<bool> HasCrashedInLastSession()
{
return CrashesInternal.HasCrashedInLastSession();
}
public static void DisableMachExceptionHandler()
{
CrashesInternal.DisableMachExceptionHandler();
}
public static ErrorReport LastSessionCrashReport()
{
return CrashesInternal.LastSessionCrashReport();
}
/// <summary>
/// Report unhandled exceptions, automatically captured by Unity, as handled errors
/// </summary>
/// <param name="enabled">Specify true to enable reporting of unhandled exceptions, automatically captured by Unity, as handled errors; otherwise, false.</param>
public static void ReportUnhandledExceptions(bool enabled)
{
if (_reportUnhandledExceptions == enabled)
{
return;
}
_reportUnhandledExceptions = enabled;
if (enabled)
{
SubscribeToUnhandledExceptions();
}
else
{
UnsubscribeFromUnhandledExceptions();
}
}
public static bool IsReportingUnhandledExceptions()
{
return _reportUnhandledExceptions;
}
#if ENABLE_IL2CPP
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
public delegate bool UserConfirmationHandler();
public static UserConfirmationHandler ShouldAwaitUserConfirmation
{
set
{
CrashesInternal.SetUserConfirmationHandler(value);
}
}
public enum ConfirmationResult { DontSend, Send, AlwaysSend };
public static void NotifyUserConfirmation(ConfirmationResult answer)
{
CrashesInternal.NotifyWithUserConfirmation(answer);
}
#if ENABLE_IL2CPP
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
public delegate bool ShouldProcessErrorReportHandler(ErrorReport errorReport);
public static ShouldProcessErrorReportHandler ShouldProcessErrorReport
{
set
{
CrashesDelegate.SetShouldProcessErrorReportHandler(value);
}
}
#if ENABLE_IL2CPP
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
public delegate ErrorAttachmentLog[] GetErrorAttachmentsHandler(ErrorReport errorReport);
public static GetErrorAttachmentsHandler GetErrorAttachments
{
set
{
CrashesDelegate.SetGetErrorAttachmentsHandler(value);
}
}
#if ENABLE_IL2CPP
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
public delegate void SendingErrorReportHandler(ErrorReport errorReport);
public static event SendingErrorReportHandler SendingErrorReport
{
add
{
lock (_objectLock)
{
CrashesDelegate.SendingErrorReport += value;
}
}
remove
{
lock (_objectLock)
{
CrashesDelegate.SendingErrorReport -= value;
}
}
}
#if ENABLE_IL2CPP
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
public delegate void SentErrorReportHandler(ErrorReport errorReport);
public static event SentErrorReportHandler SentErrorReport
{
add
{
lock (_objectLock)
{
CrashesDelegate.SentErrorReport += value;
}
}
remove
{
lock (_objectLock)
{
CrashesDelegate.SentErrorReport -= value;
}
}
}
#if ENABLE_IL2CPP
[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
#endif
public delegate void FailedToSendErrorReportHandler(ErrorReport errorReport);
public static event FailedToSendErrorReportHandler FailedToSendErrorReport
{
add
{
lock (_objectLock)
{
CrashesDelegate.FailedToSendErrorReport += value;
}
}
remove
{
lock (_objectLock)
{
CrashesDelegate.FailedToSendErrorReport -= value;
}
}
}
public static void StartCrashes()
{
CrashesInternal.StartCrashes();
}
private static void SubscribeToUnhandledExceptions()
{
#if !UNITY_EDITOR && !UNITY_WSA_10_0
Application.logMessageReceived += OnHandleLog;
System.AppDomain.CurrentDomain.UnhandledException += OnHandleUnresolvedException;
#endif
#if !UNITY_EDITOR && UNITY_ANDROID
UnityCoroutineHelper.StartCoroutine(SendUnhandledExceptionReports);
#endif
}
private static void UnsubscribeFromUnhandledExceptions()
{
#if !UNITY_EDITOR && !UNITY_WSA_10_0
Application.logMessageReceived -= OnHandleLog;
System.AppDomain.CurrentDomain.UnhandledException -= OnHandleUnresolvedException;
#endif
}
private static void HandleAppCenterInitialized()
{
if (_reportUnhandledExceptions)
{
SubscribeToUnhandledExceptions();
}
}
#if UNITY_ANDROID
private static IEnumerator SendUnhandledExceptionReports()
{
while (true)
{
if (!_reportUnhandledExceptions)
{
yield break;
}
if (_unhandledExceptionsExists)
{
Exception exception = null;
lock (_unhandledExceptions)
{
if (_unhandledExceptions.Count > 0)
{
exception = _unhandledExceptions.Dequeue();
}
if (_unhandledExceptions.Count == 0)
{
_unhandledExceptionsExists = false;
}
}
if (exception != null)
{
var exceptionWrapper = CreateWrapperException(exception);
CrashesInternal.TrackException(exceptionWrapper.GetRawObject());
}
}
yield return null;
}
}
#endif
private static WrapperException CreateWrapperException(Exception exception)
{
var exceptionWrapper = new WrapperException();
exceptionWrapper.SetWrapperSdkName(WrapperSdk.Name);
exceptionWrapper.SetStacktrace(exception.StackTrace);
exceptionWrapper.SetMessage(exception.Message);
exceptionWrapper.SetType(exception.GetType().ToString());
if (exception.InnerException != null)
{
var innerExceptionWrapper = CreateWrapperException(exception.InnerException).GetRawObject();
exceptionWrapper.SetInnerException(innerExceptionWrapper);
}
return exceptionWrapper;
}
private static WrapperException CreateWrapperException(string logString, string stackTrace, LogType type)
{
var exception = new WrapperException();
exception.SetWrapperSdkName(WrapperSdk.Name);
string sanitizedLogString = logString.Replace("\n", " ");
exception.SetMessage(sanitizedLogString);
exception.SetType(type.ToString());
string[] stacktraceLines = stackTrace.Split('\n');
string stackTraceString = "";
foreach (string line in stacktraceLines)
{
if (line.Length > 0)
{
stackTraceString += "at " + line + "\n";
}
}
exception.SetStacktrace(stackTraceString);
return exception;
}
}
}