update: 调整组件初始化生命周期, 完善 Excpetion 上报组件的调用逻辑
parent
be1c9cc327
commit
bdb15cdf1d
|
|
@ -59,9 +59,9 @@ namespace Guru
|
||||||
|
|
||||||
_hasInited = true;
|
_hasInited = true;
|
||||||
}
|
}
|
||||||
catch (Exception e)
|
catch (Exception ex)
|
||||||
{
|
{
|
||||||
Crashlytics.LogException(e);
|
LogCrashlytics(ex);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -25,6 +25,8 @@ namespace Guru
|
||||||
|
|
||||||
private static bool _isInited; //Analytics是否初始化完成
|
private static bool _isInited; //Analytics是否初始化完成
|
||||||
public static bool EnableDebugAnalytics; //允许Debug包上报打点
|
public static bool EnableDebugAnalytics; //允许Debug包上报打点
|
||||||
|
|
||||||
|
public static bool IsDebugMode => PlatformUtil.IsDebug();
|
||||||
|
|
||||||
private static bool IsEnable
|
private static bool IsEnable
|
||||||
{
|
{
|
||||||
|
|
@ -39,14 +41,12 @@ namespace Guru
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
//开发环境打点不上报
|
//开发环境打点不上报
|
||||||
if (PlatformUtil.IsDebug() && !EnableDebugAnalytics)
|
if (IsDebugMode && !EnableDebugAnalytics)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#region 初始化
|
#region 初始化
|
||||||
|
|
||||||
|
|
@ -54,7 +54,13 @@ namespace Guru
|
||||||
{
|
{
|
||||||
if (_isInited) return;
|
if (_isInited) return;
|
||||||
_isInited = true;
|
_isInited = true;
|
||||||
Crashlytics.IsCrashlyticsCollectionEnabled = true;
|
|
||||||
|
// -------- 初始化 Exception ----------
|
||||||
|
CrashlyticsAgent.Install();
|
||||||
|
|
||||||
|
// ------- 初始化自打点 ----------
|
||||||
|
InstallGuruAnalytics(IsDebug);
|
||||||
|
|
||||||
|
|
||||||
if (_defaultEventSetting == null)
|
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);
|
Crashlytics.IsCrashlyticsCollectionEnabled = true;
|
||||||
FirebaseAnalytics.SetSessionTimeoutDuration(new TimeSpan(0, 30, 0));
|
if (_defaultEventSetting.EnableFirebaseAnalytics)
|
||||||
SetUserProperty(FirebaseAnalytics.UserPropertySignUpMethod, "Google");
|
{
|
||||||
SetUserProperty(PropertyDeviceID, IPMConfig.IPM_DEVICE_ID);
|
FirebaseAnalytics.SetAnalyticsCollectionEnabled(true);
|
||||||
SetUserProperty(PropertyFirstOpenTime, FirstOpenTime);
|
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.ParameterScreenClass, className),
|
||||||
new Parameter(FirebaseAnalytics.ParameterScreenName, screenName)
|
new Parameter(FirebaseAnalytics.ParameterScreenName, screenName)
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
@ -255,7 +270,7 @@ namespace Guru
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Crashlytics.Log(msg);
|
CrashlyticsAgent.Log(msg);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -263,8 +278,7 @@ namespace Guru
|
||||||
public static void LogCrashlytics(Exception exp)
|
public static void LogCrashlytics(Exception exp)
|
||||||
{
|
{
|
||||||
if (!_isInited) return;
|
if (!_isInited) return;
|
||||||
Crashlytics.LogException(exp);
|
CrashlyticsAgent.LogException(exp);
|
||||||
Crashlytics.SetUserId(IPMConfig.IPM_UID);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
|
||||||
|
|
@ -15,10 +15,15 @@ namespace Guru
|
||||||
|
|
||||||
public static DependencyStatus DependencyStatus = DependencyStatus.UnavailableOther;
|
public static DependencyStatus DependencyStatus = DependencyStatus.UnavailableOther;
|
||||||
public static bool IsFirebaseInitialized => DependencyStatus == DependencyStatus.Available;
|
public static bool IsFirebaseInitialized => DependencyStatus == DependencyStatus.Available;
|
||||||
|
public static Action<bool> onInitComplete;
|
||||||
|
|
||||||
public static void InitFirebase(Action callback, bool isDebug = false)
|
public static void InitFirebase(Action callback, bool isDebug = false)
|
||||||
{
|
{
|
||||||
|
_isReady = false;
|
||||||
_isDebug = isDebug;
|
_isDebug = isDebug;
|
||||||
GuruRepoter.Install();
|
Analytics.InitAnalytics(); // 打点提前初始化
|
||||||
|
|
||||||
|
// 初始化 Fireabse 依赖
|
||||||
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
|
FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread(task => {
|
||||||
DependencyStatus = task.Result;
|
DependencyStatus = task.Result;
|
||||||
if (DependencyStatus == DependencyStatus.Available)
|
if (DependencyStatus == DependencyStatus.Available)
|
||||||
|
|
@ -31,15 +36,14 @@ namespace Guru
|
||||||
{
|
{
|
||||||
Log.E(LOG_TAG, "Could not resolve all Firebase dependencies: " + DependencyStatus);
|
Log.E(LOG_TAG, "Could not resolve all Firebase dependencies: " + DependencyStatus);
|
||||||
}
|
}
|
||||||
|
onInitComplete?.Invoke(_isReady);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
private static void InitializeFirebaseComp()
|
private static void InitializeFirebaseComp()
|
||||||
{
|
{
|
||||||
InitCrashlytics(); // 老项目沿用此逻辑
|
InitCrashlytics(); // 老项目沿用此逻辑
|
||||||
InitRemoteConfig(); // 老项目沿用此逻辑
|
InitRemoteConfig(); // 老项目沿用此逻辑
|
||||||
Analytics.InstallGuruAnalytics(_isDebug); // 初始化Guru自打点
|
InitAdjustService(); // 初始化 Firebase 服务
|
||||||
Analytics.InitAnalytics();
|
|
||||||
InitAdjustService();
|
|
||||||
|
|
||||||
if (IPMConfig.IPM_UID.IsNullOrEmpty())
|
if (IPMConfig.IPM_UID.IsNullOrEmpty())
|
||||||
{
|
{
|
||||||
|
|
|
||||||
|
|
@ -6,9 +6,13 @@ namespace Guru
|
||||||
using System;
|
using System;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
public class GuruRepoter
|
public static class CrashlyticsAgent
|
||||||
{
|
{
|
||||||
|
private static bool _initOnce;
|
||||||
private static bool _isReady;
|
private static bool _isReady;
|
||||||
|
|
||||||
|
private static Queue<Exception> _expCache;
|
||||||
|
private static bool _hasSetUser = false;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// 捕获列表
|
/// 捕获列表
|
||||||
|
|
@ -29,20 +33,46 @@ namespace Guru
|
||||||
|
|
||||||
public static void Install()
|
public static void Install()
|
||||||
{
|
{
|
||||||
_isReady = true;
|
if (_initOnce) return;
|
||||||
Crashlytics.IsCrashlyticsCollectionEnabled = true;
|
_initOnce = true;
|
||||||
|
|
||||||
|
_expCache = new Queue<Exception>(20);
|
||||||
|
|
||||||
|
Application.logMessageReceived -= OnReceivedMessage;
|
||||||
Application.logMessageReceived += OnReceivedMessage;
|
Application.logMessageReceived += OnReceivedMessage;
|
||||||
|
|
||||||
|
Application.logMessageReceivedThreaded -= 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;
|
FirebaseUtil.onInitComplete -= OnFirebaseComplete;
|
||||||
Install();
|
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)
|
private static string ToLogTypeString(LogType type)
|
||||||
{
|
{
|
||||||
switch (type)
|
switch (type)
|
||||||
|
|
@ -72,20 +102,44 @@ namespace Guru
|
||||||
|
|
||||||
public static void LogException(Exception ex)
|
public static void LogException(Exception ex)
|
||||||
{
|
{
|
||||||
CheckReady();
|
if (!_isReady)
|
||||||
|
{
|
||||||
|
Install();
|
||||||
|
_expCache.Enqueue(ex);
|
||||||
|
return;
|
||||||
|
}
|
||||||
Crashlytics.LogException(ex);
|
Crashlytics.LogException(ex);
|
||||||
|
CheckSetUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void LogException(string msg)
|
public static void LogException(string msg)
|
||||||
{
|
{
|
||||||
CheckReady();
|
|
||||||
Crashlytics.LogException(new Exception(msg));
|
Crashlytics.LogException(new Exception(msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void Log(string msg)
|
public static void Log(string msg)
|
||||||
{
|
{
|
||||||
CheckReady();
|
if (!_isReady)
|
||||||
|
{
|
||||||
|
Install();
|
||||||
|
return;
|
||||||
|
}
|
||||||
Crashlytics.Log(msg);
|
Crashlytics.Log(msg);
|
||||||
|
CheckSetUser();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private static void CheckSetUser()
|
||||||
|
{
|
||||||
|
if (!_hasSetUser)
|
||||||
|
{
|
||||||
|
if (!string.IsNullOrEmpty(IPMConfig.IPM_UID))
|
||||||
|
{
|
||||||
|
Crashlytics.SetUserId(IPMConfig.IPM_UID);
|
||||||
|
_hasSetUser = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -49,13 +49,21 @@ namespace Guru
|
||||||
// onFetchComplete 传参 true: 拉取成功 false: 拉取失败
|
// onFetchComplete 传参 true: 拉取成功 false: 拉取失败
|
||||||
public static void FetchAll(bool immediately = 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);
|
Instance.FetchAllConfigs(immediately);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void AddDefaultValues(Dictionary<string, object> dict)
|
public static void AddDefaultValues(Dictionary<string, object> dict)
|
||||||
{
|
{
|
||||||
if (!_initOnce) return;
|
if (!_initOnce)
|
||||||
|
{
|
||||||
|
LogE("Mgr not ready, call Init first.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
Instance.AppendDefaultValues(dict);
|
Instance.AppendDefaultValues(dict);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -500,7 +508,6 @@ namespace Guru
|
||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Log
|
#region Log
|
||||||
|
|
||||||
private static void LogI(string msg, params object[] args)
|
private static void LogI(string msg, params object[] args)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue