11using UnityEngine ;
2- using System . Runtime . InteropServices ;
32using UnityEngine . Diagnostics ;
43
54public class Bugfender : MonoBehaviour {
65 private const string SDK_TYPE = "unity" ;
7- private const int SDK_TYPE_VERSION = 30000 ;
6+ private const int SDK_TYPE_VERSION = 30001 ;
87
98 public string APP_KEY ;
109 public bool ENABLE_UI_EVENT_LOGGING = false ;
@@ -17,58 +16,19 @@ public class Bugfender : MonoBehaviour {
1716
1817 public enum LogLevel { Debug , Warning , Error , Trace , Info , Fatal } ;
1918
20- private const int SDK_VERSION = 20260119 ;
19+ private static bool BugfenderResourceFlagTrue ( string resourceName )
20+ {
21+ var asset = Resources . Load < TextAsset > ( resourceName ) ;
22+ return asset != null && string . Equals ( asset . text . Trim ( ) , "true" , System . StringComparison . OrdinalIgnoreCase ) ;
23+ }
24+
25+ private static bool ResourcesWantNativeLogCapture ( )
26+ {
27+ return BugfenderResourceFlagTrue ( "bugfender_native_log_capture" ) ;
28+ }
2129
2230#if UNITY_ANDROID && ! UNITY_EDITOR
2331 private static AndroidJavaClass bugfender ;
24- #elif UNITY_IOS && ! UNITY_EDITOR
25- [ DllImport ( "__Internal" ) ]
26- private static extern void BugfenderSetSDKType ( string sdkType , int version ) ;
27-
28- [ DllImport ( "__Internal" ) ]
29- private static extern void BugfenderActivateLogger ( string key , bool printToConsole , bool hideDeviceName , string apiURL , string baseURL ) ;
30-
31- [ DllImport ( "__Internal" ) ]
32- private static extern void BugfenderEnableUIEventLogging ( ) ;
33-
34- [ DllImport ( "__Internal" ) ]
35- private static extern void BugfenderEnableCrashReporting ( ) ;
36-
37- [ DllImport ( "__Internal" ) ]
38- private static extern void BugfenderSetDeviceString ( string key , string value ) ;
39-
40- [ DllImport ( "__Internal" ) ]
41- private static extern void BugfenderRemoveDeviceKey ( string key ) ;
42-
43- [ DllImport ( "__Internal" ) ]
44- private static extern void BugfenderLog ( int logLevel , string tag , string message ) ;
45-
46- [ DllImport ( "__Internal" ) ]
47- private static extern string BugfenderSendCrash ( string title , string text ) ;
48-
49- [ DllImport ( "__Internal" ) ]
50- private static extern string BugfenderSendIssue ( string title , string markdown ) ;
51-
52- [ DllImport ( "__Internal" ) ]
53- private static extern string BugfenderSendUserFeedback ( string subject , string message ) ;
54-
55- [ DllImport ( "__Internal" ) ]
56- private static extern void BugfenderSetMaximumLocalStorageSize ( ulong maximumLocalStorageSizeBytes ) ;
57-
58- [ DllImport ( "__Internal" ) ]
59- private static extern string BugfenderGetDeviceIdentifierUrl ( ) ;
60-
61- [ DllImport ( "__Internal" ) ]
62- private static extern string BugfenderGetSessionIdentifierUrl ( ) ;
63-
64- [ DllImport ( "__Internal" ) ]
65- private static extern void BugfenderSetForceEnabled ( bool enabled ) ;
66-
67- [ DllImport ( "__Internal" ) ]
68- private static extern void BugfenderForceSendOnce ( ) ;
69-
70- [ DllImport ( "__Internal" ) ]
71- private static extern void BugfenderSetSDKType ( string sdkType , int version ) ;
7232#endif
7333
7434 // Automatically called when scene starts
@@ -121,19 +81,24 @@ void Start()
12181 if ( debugAsset != null && string . Equals ( debugAsset . text . Trim ( ) , "true" , System . StringComparison . OrdinalIgnoreCase ) ) {
12282 try { bugfender . CallStatic ( "setDebugMode" , true ) ; } catch ( AndroidJavaException ) { /* ignore if not available */ }
12383 }
124- //bugfender.CallStatic ("enableLogcatLogging"); // optional, uncomment if you want it (Android only)
84+ if ( ResourcesWantNativeLogCapture ( ) ) {
85+ try { bugfender . CallStatic ( "enableLogcatLogging" ) ; } catch ( AndroidJavaException ) { }
86+ }
12587 }
12688
12789 }
12890 }
12991#elif UNITY_IOS && ! UNITY_EDITOR
130- BugfenderSetSDKType ( SDK_TYPE , SDK_TYPE_VERSION ) ;
131- BugfenderActivateLogger ( APP_KEY , PRINT_TO_CONSOLE , HIDE_DEVICE_NAME , API_URL , BASE_URL ) ;
92+ BugfenderNativeIos . SetSDKType ( SDK_TYPE , SDK_TYPE_VERSION ) ;
93+ BugfenderNativeIos . ActivateLogger ( APP_KEY , PRINT_TO_CONSOLE , HIDE_DEVICE_NAME , API_URL , BASE_URL ) ;
13294 if ( ENABLE_UI_EVENT_LOGGING ) {
133- BugfenderEnableUIEventLogging ( ) ;
95+ BugfenderNativeIos . EnableUIEventLogging ( ) ;
13496 }
13597 if ( ENABLE_CRASH_REPORTING ) {
136- BugfenderEnableCrashReporting ( ) ;
98+ BugfenderNativeIos . EnableCrashReporting ( ) ;
99+ }
100+ if ( ResourcesWantNativeLogCapture ( ) ) {
101+ BugfenderNativeIos . EnableNSLogLogging ( ) ;
137102 }
138103#endif
139104 /* Some examples on how to use Bugfender:
@@ -153,7 +118,7 @@ public static void SetDeviceString(string key, string value)
153118 bugfender . CallStatic ( "setDeviceString" , key , value ) ;
154119 }
155120#elif UNITY_IOS && ! UNITY_EDITOR
156- BugfenderSetDeviceString ( key , value ) ;
121+ BugfenderNativeIos . SetDeviceString ( key , value ) ;
157122#else
158123 Debug . Log ( "[BF] Set device key:" + key + " value:" + value ) ;
159124#endif
@@ -166,12 +131,29 @@ public static void RemoveDeviceKey(string key)
166131 bugfender . CallStatic ( "removeDeviceKey" , key ) ;
167132 }
168133#elif UNITY_IOS && ! UNITY_EDITOR
169- BugfenderRemoveDeviceKey ( key ) ;
134+ BugfenderNativeIos . RemoveDeviceKey ( key ) ;
170135#else
171136 Debug . Log ( "[BF] Remove device key: " + key ) ;
172137#endif
173138 }
174139
140+ /// <summary>
141+ /// Enables native system log capture for the current platform (Android logcat; iOS 15+ NSLog/OSLog).
142+ /// Call after the Bugfender component has initialized, or add <c>Assets/Resources/bugfender_native_log_capture.txt</c> with <c>true</c>.
143+ /// </summary>
144+ public static void EnableNativeLogCapture ( )
145+ {
146+ #if UNITY_ANDROID && ! UNITY_EDITOR
147+ if ( bugfender != null ) {
148+ try { bugfender . CallStatic ( "enableLogcatLogging" ) ; } catch ( AndroidJavaException ) { }
149+ }
150+ #elif UNITY_IOS && ! UNITY_EDITOR
151+ BugfenderNativeIos . EnableNSLogLogging ( ) ;
152+ #else
153+ Debug . Log ( "[BF] EnableNativeLogCapture is for Android or iOS device builds only." ) ;
154+ #endif
155+ }
156+
175157 public static void Log ( string message )
176158 {
177159 Log ( LogLevel . Debug , "" , message ) ;
@@ -187,7 +169,7 @@ public static void Log(LogLevel logLevel, string tag, string message)
187169 }
188170#elif UNITY_IOS && ! UNITY_EDITOR
189171 int intLevel = ( int ) logLevel ;
190- BugfenderLog ( intLevel , tag , message ) ;
172+ BugfenderNativeIos . Log ( intLevel , tag , message ) ;
191173#else
192174 Debug . Log ( "[BF] Sending log to Bugfender: [" + logLevel + "][" + tag + "] " + message ) ;
193175#endif
@@ -201,7 +183,7 @@ public static string SendCrash(string title, string text) {
201183 }
202184 return null ;
203185#elif UNITY_IOS && ! UNITY_EDITOR
204- return BugfenderSendCrash ( title , text ) ;
186+ return BugfenderNativeIos . SendCrash ( title , text ) ;
205187#else
206188 Debug . Log ( "[BF] Send crash: " + title + " : " + text ) ;
207189 return null ;
@@ -216,7 +198,7 @@ public static string SendIssue(string title, string text) {
216198 }
217199 return null ;
218200#elif UNITY_IOS && ! UNITY_EDITOR
219- return BugfenderSendIssue ( title , text ) ;
201+ return BugfenderNativeIos . SendIssue ( title , text ) ;
220202#else
221203 Debug . Log ( "[BF] Send issue: " + title + " : " + text ) ;
222204 return null ;
@@ -231,7 +213,7 @@ public static string SendUserFeedback(string subject, string message) {
231213 }
232214 return null ;
233215#elif UNITY_IOS && ! UNITY_EDITOR
234- return BugfenderSendUserFeedback ( subject , message ) ;
216+ return BugfenderNativeIos . SendUserFeedback ( subject , message ) ;
235217#else
236218 Debug . Log ( "[BF] Send user feedback: " + subject + " : " + message ) ;
237219 return null ;
@@ -246,7 +228,7 @@ public static void SetMaximumLocalStorageSize(ulong bytes)
246228 bugfender . CallStatic ( "setMaximumLocalStorageSize" , b ) ;
247229 }
248230#elif UNITY_IOS && ! UNITY_EDITOR
249- BugfenderSetMaximumLocalStorageSize ( bytes ) ;
231+ BugfenderNativeIos . SetMaximumLocalStorageSize ( bytes ) ;
250232#else
251233 Debug . Log ( "[BF] Set max storage size:" + bytes ) ;
252234#endif
@@ -260,7 +242,7 @@ public static string DeviceIdentifierUrl() {
260242 }
261243 return null ;
262244#elif UNITY_IOS && ! UNITY_EDITOR
263- return BugfenderGetDeviceIdentifierUrl ( ) ;
245+ return BugfenderNativeIos . GetDeviceIdentifierUrl ( ) ;
264246#else
265247 return null ;
266248#endif
@@ -274,7 +256,7 @@ public static string SessionIdentifierUrl() {
274256 }
275257 return null ;
276258#elif UNITY_IOS && ! UNITY_EDITOR
277- return BugfenderGetSessionIdentifierUrl ( ) ;
259+ return BugfenderNativeIos . GetSessionIdentifierUrl ( ) ;
278260#else
279261 return null ;
280262#endif
@@ -287,7 +269,7 @@ public static void SetForceEnabled(bool enabled)
287269 bugfender . CallStatic ( "setForceEnabled" , enabled ) ;
288270 }
289271#elif UNITY_IOS && ! UNITY_EDITOR
290- BugfenderSetForceEnabled ( enabled ) ;
272+ BugfenderNativeIos . SetForceEnabled ( enabled ) ;
291273#else
292274 Debug . Log ( "[BF] Set force enabled:" + enabled ) ;
293275#endif
@@ -300,7 +282,7 @@ public static void ForceSendOnce()
300282 bugfender . CallStatic ( "forceSendOnce" ) ;
301283 }
302284#elif UNITY_IOS && ! UNITY_EDITOR
303- BugfenderForceSendOnce ( ) ;
285+ BugfenderNativeIos . ForceSendOnce ( ) ;
304286#else
305287 Debug . Log ( "[BF] Force send once" ) ;
306288#endif
@@ -313,7 +295,7 @@ public static void SetSDKType(string sdkType, int version)
313295 bugfender . CallStatic ( "setSDKType" , sdkType , version ) ;
314296 }
315297#elif UNITY_IOS && ! UNITY_EDITOR
316- BugfenderSetSDKType ( sdkType , version ) ;
298+ BugfenderNativeIos . SetSDKType ( sdkType , version ) ;
317299#else
318300 Debug . Log ( "[BF] Set SDK type: " + sdkType + " version: " + version ) ;
319301#endif
0 commit comments