From 212463cb12f258a893fc84f71926d2e7f55f1901 Mon Sep 17 00:00:00 2001 From: Joe Spiro Date: Tue, 20 Dec 2022 18:13:42 -0500 Subject: [PATCH] Changes made as part of "Supplement and complement crash reports with custom keys, logs and custom errors" --- Assets/Hamster/Prefabs/Menus/DebugMenu.prefab | 26 +----- Assets/Hamster/Prefabs/Menus/MainMenu.prefab | 22 ++--- Assets/Hamster/Scripts/MainGame.cs | 20 ++++- Assets/Hamster/Scripts/States/DebugMenu.cs | 90 +++++++++++++++++-- 4 files changed, 106 insertions(+), 52 deletions(-) diff --git a/Assets/Hamster/Prefabs/Menus/DebugMenu.prefab b/Assets/Hamster/Prefabs/Menus/DebugMenu.prefab index 9fd7c98..5c5af25 100644 --- a/Assets/Hamster/Prefabs/Menus/DebugMenu.prefab +++ b/Assets/Hamster/Prefabs/Menus/DebugMenu.prefab @@ -29,7 +29,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 11.575897} m_LocalScale: {x: 0.02, y: 0.02, z: 0.02} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 224647990851437630} m_Father: {fileID: 0} @@ -83,7 +82,7 @@ MonoBehaviour: m_FallbackScreenDPI: 96 m_DefaultSpriteDPI: 96 m_DynamicPixelsPerUnit: 2 - m_PresetInfoIsWorld: 0 + m_PresetInfoIsWorld: 1 --- !u!114 &3823003004682667166 MonoBehaviour: m_ObjectHideFlags: 0 @@ -134,7 +133,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 224317168898000186} m_RootOrder: 0 @@ -214,7 +212,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 224258547558102142} - {fileID: 224317168898000186} @@ -300,7 +297,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -20} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 224647990851437630} m_RootOrder: 0 @@ -381,7 +377,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -19.99998} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 224640650075360786} m_Father: {fileID: 224647990851437630} @@ -473,7 +468,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 7064658406785790662} m_RootOrder: 0 @@ -554,7 +548,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -19.99998} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 4109250507045347376} m_Father: {fileID: 224647990851437630} @@ -647,7 +640,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -0.19999981} m_LocalScale: {x: 0.02, y: 0.02, z: 0.02} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 6309219395377107617} m_Father: {fileID: 768178967663672101} @@ -740,7 +732,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -19.99998} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 8918396775147988388} m_Father: {fileID: 224647990851437630} @@ -830,7 +821,6 @@ Transform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -10} m_LocalScale: {x: 50, y: 50, z: 50} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 4938586899008836194} - {fileID: 1678484980996986306} @@ -865,7 +855,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 3542865490116833156} m_RootOrder: 0 @@ -945,7 +934,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 4938586899008836194} m_RootOrder: 0 @@ -1028,7 +1016,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -19.99998} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 3302991896177308354} m_Father: {fileID: 224647990851437630} @@ -1120,7 +1107,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 1678484980996986306} m_RootOrder: 0 @@ -1200,7 +1186,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 8008595033866840674} m_RootOrder: 0 @@ -1280,7 +1265,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 1769018353878161575} m_RootOrder: 0 @@ -1360,7 +1344,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 2633762835440639359} m_RootOrder: 0 @@ -1441,7 +1424,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -0.19999981} m_LocalScale: {x: 0.02, y: 0.02, z: 0.02} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 7649950905452481446} m_Father: {fileID: 768178967663672101} @@ -1533,7 +1515,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 4514937704372255736} m_RootOrder: 0 @@ -1614,7 +1595,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -19.99998} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 7348477961437658639} m_Father: {fileID: 224647990851437630} @@ -1707,7 +1687,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -19.99998} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 4211617212186313411} m_Father: {fileID: 224647990851437630} @@ -1800,7 +1779,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -19.99998} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 2395772332585807687} m_Father: {fileID: 224647990851437630} @@ -1893,7 +1871,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -19.99998} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 4478448215106773557} m_Father: {fileID: 224647990851437630} @@ -1985,7 +1962,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 7649016178413963064} m_RootOrder: 0 diff --git a/Assets/Hamster/Prefabs/Menus/MainMenu.prefab b/Assets/Hamster/Prefabs/Menus/MainMenu.prefab index 3b62f17..965dbca 100644 --- a/Assets/Hamster/Prefabs/Menus/MainMenu.prefab +++ b/Assets/Hamster/Prefabs/Menus/MainMenu.prefab @@ -28,7 +28,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 224921695750590384} m_RootOrder: 0 @@ -110,7 +109,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 224884339265297600} m_RootOrder: 0 @@ -191,7 +189,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -20} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 224480594825076448} m_Father: {fileID: 224703550482448650} @@ -284,7 +281,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -19.99998} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 224088100203456210} m_Father: {fileID: 224703550482448650} @@ -377,7 +373,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -19.99998} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 224071729476491318} m_Father: {fileID: 224703550482448650} @@ -470,7 +465,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 224564986093952686} - {fileID: 224884339265297600} @@ -577,7 +571,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -19.99998} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 224431914256755566} m_RootOrder: 1 @@ -658,7 +651,6 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 10} m_LocalScale: {x: 0.02, y: 0.02, z: 0.02} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 224703550482448650} - {fileID: 224764318859703644} @@ -713,7 +705,7 @@ MonoBehaviour: m_FallbackScreenDPI: 96 m_DefaultSpriteDPI: 96 m_DynamicPixelsPerUnit: 2 - m_PresetInfoIsWorld: 0 + m_PresetInfoIsWorld: 1 --- !u!114 &114284253540390844 MonoBehaviour: m_ObjectHideFlags: 0 @@ -761,7 +753,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 224247601441905012} m_RootOrder: 0 @@ -841,7 +832,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -20} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 224703550482448650} m_RootOrder: 0 @@ -921,7 +911,6 @@ RectTransform: m_LocalRotation: {x: 0, y: 0, z: 0, w: 1} m_LocalPosition: {x: 0, y: 0, z: 0} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: [] m_Father: {fileID: 77608933434497189} m_RootOrder: 0 @@ -991,7 +980,7 @@ GameObject: m_Icon: {fileID: 0} m_NavMeshLayer: 0 m_StaticEditorFlags: 0 - m_IsActive: 0 + m_IsActive: 1 --- !u!224 &77608933434497189 RectTransform: m_ObjectHideFlags: 0 @@ -1002,15 +991,14 @@ RectTransform: m_LocalRotation: {x: -0, y: -0, z: -0, w: 1} m_LocalPosition: {x: 0, y: 0, z: -19.99998} m_LocalScale: {x: 1, y: 1, z: 1} - m_ConstrainProportionsScale: 0 m_Children: - {fileID: 1992729626421901825} m_Father: {fileID: 224703550482448650} m_RootOrder: 4 m_LocalEulerAnglesHint: {x: 0, y: 0, z: 0} - m_AnchorMin: {x: 0, y: 1} - m_AnchorMax: {x: 0, y: 1} - m_AnchoredPosition: {x: 250, y: -173.5} + m_AnchorMin: {x: 0, y: 0} + m_AnchorMax: {x: 0, y: 0} + m_AnchoredPosition: {x: 0, y: 0} m_SizeDelta: {x: 160, y: 25} m_Pivot: {x: 0.5, y: 0.5} --- !u!222 &6013548410687448273 diff --git a/Assets/Hamster/Scripts/MainGame.cs b/Assets/Hamster/Scripts/MainGame.cs index b651d5b..0c65cde 100644 --- a/Assets/Hamster/Scripts/MainGame.cs +++ b/Assets/Hamster/Scripts/MainGame.cs @@ -17,6 +17,7 @@ using System.Collections; using System.Collections.Generic; using System.Threading.Tasks; +using Firebase.Extensions; namespace Hamster { @@ -76,7 +77,7 @@ public class MainGame : MonoBehaviour { void Start() { Screen.SetResolution(Screen.width / 2, Screen.height / 2, true); - InitializeCommonDataAndStartGame(); + InitializeFirebaseAndStartGame(); } void Update() { @@ -165,10 +166,25 @@ void InitializeCommonDataAndStartGame() stateManager.PushState(new States.MainMenu()); } + public Firebase.FirebaseApp app = null; + // Begins the firebase initialization process and afterwards, opens the main menu. private void InitializeFirebaseAndStartGame() { - throw new System.NotImplementedException(); + Firebase.FirebaseApp.CheckAndFixDependenciesAsync().ContinueWithOnMainThread( + previousTask => + { + var dependencyStatus = previousTask.Result; + if (dependencyStatus == Firebase.DependencyStatus.Available) { + // Create and hold a reference to your FirebaseApp, + app = Firebase.FirebaseApp.DefaultInstance; + InitializeCommonDataAndStartGame(); + } else { + UnityEngine.Debug.LogError( + $"Could not resolve all Firebase dependencies: {dependencyStatus}\n" + + "Firebase Unity SDK is not safe to use here"); + } + }); } // Sets Remote Config default values and fetchs new ones diff --git a/Assets/Hamster/Scripts/States/DebugMenu.cs b/Assets/Hamster/Scripts/States/DebugMenu.cs index 76d6957..477d6ea 100644 --- a/Assets/Hamster/Scripts/States/DebugMenu.cs +++ b/Assets/Hamster/Scripts/States/DebugMenu.cs @@ -18,6 +18,9 @@ using UnityEngine.Diagnostics; using System.Threading; +// Import Firebase +using Firebase.Crashlytics; + namespace Hamster.States { class DebugMenu : BaseState { private Menus.DebugMenuGUI menuComponent = null; @@ -103,21 +106,39 @@ private void TestCrash() void CrashNow() { - throw new System.NotImplementedException(); + TestCrash(); } // Caught nonfatal exceptions can be sent to Crashlytics // as full issues by using Crashlytics.LogException void LogNonfatalError() { - throw new System.NotImplementedException(); + try + { + throw new System.Exception($"Test exception thrown in {nameof(LogNonfatalError)}"); + } + catch(System.Exception exception) + { + Crashlytics.LogException(exception); + } } // Crashlytics allows you to add logs to your issues (Crashes, Errors and ANRs) // with the Crashlytics.Log command. These are strictly additive. void LogStringsAndCrashNow() { - throw new System.NotImplementedException(); + Crashlytics.Log($"This is the first of two descriptive strings in {nameof(LogStringsAndCrashNow)}"); + const bool RUN_OPTIONAL_PATH = false; + if(RUN_OPTIONAL_PATH) + { + Crashlytics.Log(" As it stands, this log should not appear in your records because it will never be called."); + } + else + { + Crashlytics.Log(" A log that will simply inform you which path of logic was taken. Akin to print debugging."); + } + Crashlytics.Log($"This is the second of two descriptive strings in {nameof(LogStringsAndCrashNow)}"); + TestCrash(); } // Crashlytics allows you to associate key-value pairs with callstacks. @@ -127,7 +148,21 @@ void LogStringsAndCrashNow() // into discrete chunks (aka "discretizing the input"). void SetAndOverwriteCustomKeyThenCrash() { - throw new System.NotImplementedException(); + const string CURRENT_TIME_KEY = "Current Time"; + System.TimeSpan currentTime = System.DateTime.Now.TimeOfDay; + Crashlytics.SetCustomKey( + CURRENT_TIME_KEY, + DayDivision.GetPartOfDay(currentTime).ToString() // Values must be strings + ); + + // Time Passes + currentTime += DayDivision.DURATION_THAT_ENSURES_PHASE_CHANGE; + + Crashlytics.SetCustomKey( + CURRENT_TIME_KEY, + DayDivision.GetPartOfDay(currentTime).ToString() + ); + TestCrash(); } // Uses logging and custom keys to help diagnose existing & potential ANRs. @@ -138,20 +173,59 @@ void SetAndOverwriteCustomKeyThenCrash() // Instead you will not have logged or recorded what method caused the ANR. // Thus, if the method name is unset, it occurred there void SetLogsAndKeysBeforeANR() - { - throw new System.NotImplementedException(); + { + System.Action WaitAndRecord = + (string methodName, long targetCallLength)=> + { + System.Diagnostics.Stopwatch stopWatch = new System.Diagnostics.Stopwatch(); + const string CURRENT_FUNCTION = "Current Async Function"; + + // Initialize key and start timing + Crashlytics.SetCustomKey(CURRENT_FUNCTION, methodName); + stopWatch.Start(); + + // The actual (simulated) work being timed. + BusyWaitSimulator.WaitOnSimulatedBlockingWork(targetCallLength); + + // Stop timing and unset key + stopWatch.Stop(); + + if(stopWatch.ElapsedMilliseconds>=BusyWaitSimulator.EXTREME_DURATION_MILLIS) + { + Crashlytics.Log($"'{methodName}' is long enough to cause an ANR on Android."); + if(Application.platform != RuntimePlatform.Android) + { + throw new System.InvalidOperationException( + $"Demo Exception: Call to {methodName} blocked the main thread for "+ + "significant time on an environment without 'ANRs'."); + } + } + else if(stopWatch.ElapsedMilliseconds>=BusyWaitSimulator.SEVERE_DURATION_MILLIS) + { + Crashlytics.Log($"'{methodName}' is long enough it may cause an ANR on Android."); + } + }; + + WaitAndRecord("DoSafeWork",1000L); + WaitAndRecord("DoSevereWork",BusyWaitSimulator.SEVERE_DURATION_MILLIS); + WaitAndRecord("DoExtremeWork",2*BusyWaitSimulator.EXTREME_DURATION_MILLIS); } // Log an event with a float parameter void LogProgressEventWithStringLiterals() { - throw new System.NotImplementedException(); + Firebase.Analytics.FirebaseAnalytics.LogEvent("progress", "percent", 0.4f); } // Log an event with an int parameter using predefined string constants void LogIntScoreWithBuiltInEventAndParams() { - throw new System.NotImplementedException(); + Firebase.Analytics.FirebaseAnalytics + .LogEvent( + Firebase.Analytics.FirebaseAnalytics.EventPostScore, + Firebase.Analytics.FirebaseAnalytics.ParameterScore, + 42 + ); } // Sets a Google Analytics user property 'subtitle_sentiment' to 'bored'