From bdb15cdf1dd42bbba198c07a2fff16238a761c79 Mon Sep 17 00:00:00 2001 From: huyufei Date: Wed, 13 Mar 2024 17:28:04 +0800 Subject: [PATCH] =?UTF-8?q?update:=20=E8=B0=83=E6=95=B4=E7=BB=84=E4=BB=B6?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96=E7=94=9F=E5=91=BD=E5=91=A8=E6=9C=9F?= =?UTF-8?q?,=20=E5=AE=8C=E5=96=84=20Excpetion=20=E4=B8=8A=E6=8A=A5?= =?UTF-8?q?=E7=BB=84=E4=BB=B6=E7=9A=84=E8=B0=83=E7=94=A8=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Runtime/Analytics/Analytics.Custom.cs | 4 +- .../GuruCore/Runtime/Analytics/Analytics.cs | 44 +++++++---- .../GuruCore/Runtime/Firebase/FirebaseUtil.cs | 12 ++- .../{GuruRepoter.cs => CrashlyticsAgent.cs} | 74 ++++++++++++++++--- ...poter.cs.meta => CrashlyticsAgent.cs.meta} | 0 .../GuruRemote/Runtime/RemoteConfigManager.cs | 13 +++- 6 files changed, 113 insertions(+), 34 deletions(-) rename Runtime/GuruCore/Runtime/Reporter/{GuruRepoter.cs => CrashlyticsAgent.cs} (50%) rename Runtime/GuruCore/Runtime/Reporter/{GuruRepoter.cs.meta => CrashlyticsAgent.cs.meta} (100%) diff --git a/Runtime/GuruCore/Runtime/Analytics/Analytics.Custom.cs b/Runtime/GuruCore/Runtime/Analytics/Analytics.Custom.cs index 4f28855..c77093b 100644 --- a/Runtime/GuruCore/Runtime/Analytics/Analytics.Custom.cs +++ b/Runtime/GuruCore/Runtime/Analytics/Analytics.Custom.cs @@ -59,9 +59,9 @@ namespace Guru _hasInited = true; } - catch (Exception e) + catch (Exception ex) { - Crashlytics.LogException(e); + LogCrashlytics(ex); } } diff --git a/Runtime/GuruCore/Runtime/Analytics/Analytics.cs b/Runtime/GuruCore/Runtime/Analytics/Analytics.cs index 5042e96..fc7a917 100644 --- a/Runtime/GuruCore/Runtime/Analytics/Analytics.cs +++ b/Runtime/GuruCore/Runtime/Analytics/Analytics.cs @@ -25,6 +25,8 @@ namespace Guru private static bool _isInited; //Analytics是否初始化完成 public static bool EnableDebugAnalytics; //允许Debug包上报打点 + + public static bool IsDebugMode => PlatformUtil.IsDebug(); private static bool IsEnable { @@ -39,14 +41,12 @@ namespace Guru return false; //开发环境打点不上报 - if (PlatformUtil.IsDebug() && !EnableDebugAnalytics) + if (IsDebugMode && !EnableDebugAnalytics) return false; return true; } } - - #region 初始化 @@ -54,7 +54,13 @@ namespace Guru { if (_isInited) return; _isInited = true; - Crashlytics.IsCrashlyticsCollectionEnabled = true; + + // -------- 初始化 Exception ---------- + CrashlyticsAgent.Install(); + + // ------- 初始化自打点 ---------- + InstallGuruAnalytics(IsDebug); + if (_defaultEventSetting == null) { @@ -67,13 +73,24 @@ namespace Guru }; } - if (_defaultEventSetting.EnableFirebaseAnalytics) + FirebaseUtil.onInitComplete += OnFirebaseCompleted; + } + + private static void OnFirebaseCompleted(bool success) + { + FirebaseUtil.onInitComplete -= OnFirebaseCompleted; + + if (success) { - FirebaseAnalytics.SetAnalyticsCollectionEnabled(true); - FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0)); - SetUserProperty(FirebaseAnalytics.UserPropertySignUpMethod, "Google"); - SetUserProperty(PropertyDeviceID, IPMConfig.IPM_DEVICE_ID); - SetUserProperty(PropertyFirstOpenTime, FirstOpenTime); + Crashlytics.IsCrashlyticsCollectionEnabled = true; + if (_defaultEventSetting.EnableFirebaseAnalytics) + { + FirebaseAnalytics.SetAnalyticsCollectionEnabled(true); + FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0)); + SetUserProperty(FirebaseAnalytics.UserPropertySignUpMethod, "Google"); + SetUserProperty(PropertyDeviceID, IPMConfig.IPM_DEVICE_ID); + SetUserProperty(PropertyFirstOpenTime, FirstOpenTime); + } } } @@ -91,8 +108,6 @@ namespace Guru new Parameter(FirebaseAnalytics.ParameterScreenClass, className), new Parameter(FirebaseAnalytics.ParameterScreenName, screenName) ); - - } #endregion @@ -255,7 +270,7 @@ namespace Guru } else { - Crashlytics.Log(msg); + CrashlyticsAgent.Log(msg); } } @@ -263,8 +278,7 @@ namespace Guru public static void LogCrashlytics(Exception exp) { if (!_isInited) return; - Crashlytics.LogException(exp); - Crashlytics.SetUserId(IPMConfig.IPM_UID); + CrashlyticsAgent.LogException(exp); } #endregion diff --git a/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.cs b/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.cs index 80ed20a..ab025b3 100644 --- a/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.cs +++ b/Runtime/GuruCore/Runtime/Firebase/FirebaseUtil.cs @@ -15,10 +15,15 @@ namespace Guru public static DependencyStatus DependencyStatus = DependencyStatus.UnavailableOther; public static bool IsFirebaseInitialized => DependencyStatus == DependencyStatus.Available; + public static Action onInitComplete; + public static void InitFirebase(Action callback, bool isDebug = false) { + _isReady = false; _isDebug = isDebug; - GuruRepoter.Install(); + Analytics.InitAnalytics(); // 打点提前初始化 + + // 初始化 Fireabse 依赖 FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => { DependencyStatus = task.Result; if (DependencyStatus == DependencyStatus.Available) @@ -31,15 +36,14 @@ namespace Guru { Log.E(LOG_TAG, "Could not resolve all Firebase dependencies: " + DependencyStatus); } + onInitComplete?.Invoke(_isReady); }); } private static void InitializeFirebaseComp() { InitCrashlytics(); // 老项目沿用此逻辑 InitRemoteConfig(); // 老项目沿用此逻辑 - Analytics.InstallGuruAnalytics(_isDebug); // 初始化Guru自打点 - Analytics.InitAnalytics(); - InitAdjustService(); + InitAdjustService(); // 初始化 Firebase 服务 if (IPMConfig.IPM_UID.IsNullOrEmpty()) { diff --git a/Runtime/GuruCore/Runtime/Reporter/GuruRepoter.cs b/Runtime/GuruCore/Runtime/Reporter/CrashlyticsAgent.cs similarity index 50% rename from Runtime/GuruCore/Runtime/Reporter/GuruRepoter.cs rename to Runtime/GuruCore/Runtime/Reporter/CrashlyticsAgent.cs index 2cad3f7..028a8c9 100644 --- a/Runtime/GuruCore/Runtime/Reporter/GuruRepoter.cs +++ b/Runtime/GuruCore/Runtime/Reporter/CrashlyticsAgent.cs @@ -6,9 +6,13 @@ namespace Guru using System; using System.Linq; - public class GuruRepoter + public static class CrashlyticsAgent { + private static bool _initOnce; private static bool _isReady; + + private static Queue _expCache; + private static bool _hasSetUser = false; /// /// 捕获列表 @@ -29,20 +33,46 @@ namespace Guru public static void Install() { - _isReady = true; - Crashlytics.IsCrashlyticsCollectionEnabled = true; + if (_initOnce) return; + _initOnce = true; + + _expCache = new Queue(20); + Application.logMessageReceived -= OnReceivedMessage; Application.logMessageReceived += OnReceivedMessage; + + Application.logMessageReceivedThreaded -= OnReceivedMessage; Application.logMessageReceivedThreaded += OnReceivedMessage; + + if (FirebaseUtil.IsReady) + { + OnFirebaseComplete(true); + return; + } + + FirebaseUtil.onInitComplete -= OnFirebaseComplete; + FirebaseUtil.onInitComplete += OnFirebaseComplete; } - private static void CheckReady() + private static void OnFirebaseComplete(bool success) { - if (_isReady) return; - Install(); + FirebaseUtil.onInitComplete -= OnFirebaseComplete; + if (success) + { + Crashlytics.IsCrashlyticsCollectionEnabled = true; + if (_expCache != null && _expCache.Count > 0) + { + while (_expCache.Count > 0) + { + LogException(_expCache.Dequeue()); + } + _expCache.Clear(); + } + _isReady = true; + + } } - private static string ToLogTypeString(LogType type) { switch (type) @@ -72,20 +102,44 @@ namespace Guru public static void LogException(Exception ex) { - CheckReady(); + if (!_isReady) + { + Install(); + _expCache.Enqueue(ex); + return; + } Crashlytics.LogException(ex); + CheckSetUser(); } public static void LogException(string msg) { - CheckReady(); Crashlytics.LogException(new Exception(msg)); } public static void Log(string msg) { - CheckReady(); + if (!_isReady) + { + Install(); + return; + } Crashlytics.Log(msg); + CheckSetUser(); } + + + private static void CheckSetUser() + { + if (!_hasSetUser) + { + if (!string.IsNullOrEmpty(IPMConfig.IPM_UID)) + { + Crashlytics.SetUserId(IPMConfig.IPM_UID); + _hasSetUser = true; + } + } + } + } } \ No newline at end of file diff --git a/Runtime/GuruCore/Runtime/Reporter/GuruRepoter.cs.meta b/Runtime/GuruCore/Runtime/Reporter/CrashlyticsAgent.cs.meta similarity index 100% rename from Runtime/GuruCore/Runtime/Reporter/GuruRepoter.cs.meta rename to Runtime/GuruCore/Runtime/Reporter/CrashlyticsAgent.cs.meta diff --git a/Runtime/GuruRemote/Runtime/RemoteConfigManager.cs b/Runtime/GuruRemote/Runtime/RemoteConfigManager.cs index c2e2dc1..a325917 100644 --- a/Runtime/GuruRemote/Runtime/RemoteConfigManager.cs +++ b/Runtime/GuruRemote/Runtime/RemoteConfigManager.cs @@ -49,13 +49,21 @@ namespace Guru // onFetchComplete 传参 true: 拉取成功 false: 拉取失败 public static void FetchAll(bool immediately = false) { - if (!_initOnce) Init(null, _fetchIntervalHours, _isDebug); + if (!_initOnce) + { + LogE("Mgr not ready, call Init first."); + return; + } Instance.FetchAllConfigs(immediately); } public static void AddDefaultValues(Dictionary dict) { - if (!_initOnce) return; + if (!_initOnce) + { + LogE("Mgr not ready, call Init first."); + return; + } Instance.AppendDefaultValues(dict); } @@ -500,7 +508,6 @@ namespace Guru #endregion - #region Log private static void LogI(string msg, params object[] args)