Skip to content

Commit 1014e10

Browse files
authored
Attachment support (#68)
* Attachment support on Android - iOS shouldn't compile * Updated the latest version of backtrace-android libraries and also adjust iOS integration code - code should now compile on iOS * Native attachment support * Attachment improvements * Updated label * Native client updates * Android libraries
1 parent 55c6efd commit 1014e10

18 files changed

+503
-406
lines changed
8 KB
Binary file not shown.
8 Bytes
Binary file not shown.
4 Bytes
Binary file not shown.
4 KB
Binary file not shown.
4 KB
Binary file not shown.

Editor/BacktraceConfigurationEditor.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public override void OnInspectorGUI()
6464
serializedObject.FindProperty("SendUnhandledGameCrashesOnGameStartup"),
6565
new GUIContent(BacktraceConfigurationLabels.LABEL_SEND_UNHANDLED_GAME_CRASHES_ON_STARTUP));
6666
#endif
67-
6867
EditorGUILayout.PropertyField(
6968
serializedObject.FindProperty("ReportFilterType"),
7069
new GUIContent(BacktraceConfigurationLabels.LABEL_REPORT_FILTER));
@@ -93,6 +92,13 @@ public override void OnInspectorGUI()
9392
EditorGUILayout.HelpBox("Please insert value greater or equal -1", MessageType.Error);
9493
}
9594
}
95+
96+
#if UNITY_ANDROID || UNITY_IOS
97+
EditorGUILayout.PropertyField(
98+
serializedObject.FindProperty("AttachmentPaths"),
99+
new GUIContent(BacktraceConfigurationLabels.LABEL_REPORT_ATTACHMENTS));
100+
#endif
101+
96102
#if !UNITY_SWITCH
97103
SerializedProperty enabled = serializedObject.FindProperty("Enabled");
98104
EditorGUILayout.PropertyField(enabled, new GUIContent(BacktraceConfigurationLabels.LABEL_ENABLE_DATABASE));

Editor/BacktraceConfigurationLabels.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ internal static class BacktraceConfigurationLabels
1818
"(Early access) Send Out of memory exceptions to Backtrace";
1919
#endif
2020
#endif
21+
internal static string LABEL_REPORT_ATTACHMENTS = "Report attachment paths";
2122
internal static string CAPTURE_NATIVE_CRASHES = "Capture native crashes";
2223
internal static string LABEL_REPORT_FILTER = "Filter reports";
2324
internal static string LABEL_NUMBER_OF_LOGS = "Collect last n game logs";

Runtime/BacktraceClient.cs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public class BacktraceClient : MonoBehaviour, IBacktraceClient
3131

3232
internal readonly Stack<BacktraceReport> BackgroundExceptions = new Stack<BacktraceReport>();
3333

34+
/// <summary>
35+
/// Client report attachments
36+
/// </summary>
37+
private List<string> _clientReportAttachments;
38+
3439
/// <summary>
3540
/// Attribute object accessor
3641
/// </summary>
@@ -50,6 +55,26 @@ public string this[string index]
5055
}
5156
}
5257

58+
/// <summary>
59+
/// Add attachment to managed reports.
60+
/// Note: this option won't add attachment to your native reports. You can add attachments to
61+
/// native reports only on BacktraceClient initialization.
62+
/// </summary>
63+
/// <param name="pathToAttachment">Path to attachment</param>
64+
public void AddAttachment(string pathToAttachment)
65+
{
66+
_clientReportAttachments.Add(pathToAttachment);
67+
}
68+
69+
/// <summary>
70+
/// Returns list of defined path to attachments stored by Backtrace client.
71+
/// </summary>
72+
/// <returns>List of client attachments</returns>
73+
public List<string> GetAttachments()
74+
{
75+
return _clientReportAttachments;
76+
}
77+
5378
/// <summary>
5479
/// Set client attributes that will be included in every report
5580
/// </summary>
@@ -271,6 +296,7 @@ internal ReportLimitWatcher ReportLimitWatcher
271296
/// </summary>
272297
/// <param name="configuration">Backtrace configuration scriptable object</param>
273298
/// <param name="attributes">Client side attributes</param>
299+
/// param name="attachments">List of attachments </param>
274300
/// <param name="gameObjectName">game object name</param>
275301
/// <returns>Backtrace client</returns>
276302
public static BacktraceClient Initialize(BacktraceConfiguration configuration, Dictionary<string, string> attributes = null, string gameObjectName = "BacktraceClient")
@@ -313,9 +339,24 @@ public static BacktraceClient Initialize(BacktraceConfiguration configuration, D
313339
/// <param name="gameObjectName">game object name</param>
314340
/// <returns>Backtrace client</returns>
315341
public static BacktraceClient Initialize(string url, string databasePath, Dictionary<string, string> attributes = null, string gameObjectName = "BacktraceClient")
342+
{
343+
return Initialize(url, databasePath, attributes, null, gameObjectName);
344+
}
345+
346+
/// <summary>
347+
/// Initialize new Backtrace integration with database path. Note - database path will be auto created by Backtrace Unity plugin
348+
/// </summary>
349+
/// <param name="url">Server url</param>
350+
/// <param name="databasePath">Database path</param>
351+
/// <param name="attributes">Client side attributes</param>
352+
/// <param name="attachments">Paths to attachments that Backtrace client will include in managed/native reports</param>
353+
/// <param name="gameObjectName">game object name</param>
354+
/// <returns>Backtrace client</returns>
355+
public static BacktraceClient Initialize(string url, string databasePath, Dictionary<string, string> attributes = null, string[] attachments = null, string gameObjectName = "BacktraceClient")
316356
{
317357
var configuration = ScriptableObject.CreateInstance<BacktraceConfiguration>();
318358
configuration.ServerUrl = url;
359+
configuration.AttachmentPaths = attachments;
319360
configuration.Enabled = true;
320361
configuration.DatabasePath = databasePath;
321362
configuration.CreateDatabase = true;
@@ -330,9 +371,23 @@ public static BacktraceClient Initialize(string url, string databasePath, Dictio
330371
/// <param name="gameObjectName">game object name</param>
331372
/// <returns>Backtrace client</returns>
332373
public static BacktraceClient Initialize(string url, Dictionary<string, string> attributes = null, string gameObjectName = "BacktraceClient")
374+
{
375+
return Initialize(url, attributes, new string[0], gameObjectName);
376+
}
377+
378+
/// <summary>
379+
/// Initialize new Backtrace integration
380+
/// </summary>
381+
/// <param name="url">Server url</param>
382+
/// <param name="attributes">Client side attributes</param>
383+
/// <param name="attachments">Paths to attachments that Backtrace client will include in managed/native reports</param>
384+
/// <param name="gameObjectName">game object name</param>
385+
/// <returns>Backtrace client</returns>
386+
public static BacktraceClient Initialize(string url, Dictionary<string, string> attributes = null, string[] attachments = null, string gameObjectName = "BacktraceClient")
333387
{
334388
var configuration = ScriptableObject.CreateInstance<BacktraceConfiguration>();
335389
configuration.ServerUrl = url;
390+
configuration.AttachmentPaths = attachments;
336391
configuration.Enabled = false;
337392
return Initialize(configuration, attributes, gameObjectName);
338393
}
@@ -358,7 +413,7 @@ public void Refresh()
358413
_current = Thread.CurrentThread;
359414
CaptureUnityMessages();
360415
_reportLimitWatcher = new ReportLimitWatcher(Convert.ToUInt32(Configuration.ReportPerMin));
361-
416+
_clientReportAttachments = Configuration.GetAttachmentPaths();
362417

363418
BacktraceApi = new BacktraceApi(
364419
credentials: new BacktraceCredentials(Configuration.GetValidServerUrl()),
@@ -668,6 +723,7 @@ private BacktraceData SetupBacktraceData(BacktraceReport report)
668723
: _backtraceLogManager.ToSourceCode();
669724

670725
report.AssignSourceCodeToReport(sourceCode);
726+
report.AttachmentPaths.AddRange(_clientReportAttachments);
671727

672728
// pass copy of dictionary to prevent overriding client attributes
673729
var result = report.ToBacktraceData(null, GameObjectDepth);

Runtime/Common/ClientPathHelper.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using System;
2+
using System.IO;
3+
using UnityEngine;
4+
5+
[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Backtrace.Unity.Tests.Runtime")]
6+
namespace Backtrace.Unity.Common
7+
{
8+
internal static class ClientPathHelper
9+
{
10+
/// <summary>
11+
/// Parse Backtrace client path string to full path
12+
/// </summary>
13+
/// <param name="path">Backtrace path with interpolated string and other Backtrace's improvements</param>
14+
/// <returns>Full path to Backtrace file/directory</returns>
15+
internal static string GetFulLPath(string path)
16+
{
17+
if (string.IsNullOrEmpty(path))
18+
{
19+
return string.Empty;
20+
}
21+
22+
return path.ParseInterpolatedString().GetFullPath();
23+
}
24+
25+
private static string ParseInterpolatedString(this string path)
26+
{
27+
// check if string has any interpolated substring
28+
var interpolationStart = path.IndexOf("${");
29+
if (interpolationStart == -1)
30+
{
31+
return path;
32+
}
33+
var interpolationEnd = path.IndexOf('}', interpolationStart);
34+
if (interpolationEnd == -1)
35+
{
36+
return path;
37+
}
38+
var interpolationValue = path.Substring(interpolationStart, interpolationEnd - interpolationStart + 1);
39+
if (string.IsNullOrEmpty(interpolationValue))
40+
{
41+
return path;
42+
}
43+
44+
switch (interpolationValue.ToLower())
45+
{
46+
case "${application.persistentdatapath}":
47+
return path.Replace(interpolationValue, Application.persistentDataPath);
48+
case "${application.datapath}":
49+
return path.Replace(interpolationValue, Application.dataPath);
50+
default:
51+
return path;
52+
}
53+
54+
}
55+
56+
private static string GetFullPath(this string path)
57+
{
58+
if (!Path.IsPathRooted(path))
59+
{
60+
path = Path.Combine(Application.persistentDataPath, path);
61+
}
62+
return Path.GetFullPath(path);
63+
64+
}
65+
}
66+
}
Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)