Skip to content

Commit 658ac37

Browse files
authored
Merge pull request #275 from immutable/feat/logging
[DX-3051] feat: add logging level
2 parents 8a25d5f + a50a877 commit 658ac37

24 files changed

+349
-111
lines changed

sample/Assets/Scripts/SelectAuthMethodScript.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using UnityEngine.UI;
44
using UnityEngine.SceneManagement;
55
using Immutable.Passport;
6+
using Immutable.Passport.Core.Logging;
67

78
public class SelectAuthMethodScript : MonoBehaviour
89
{
@@ -68,6 +69,9 @@ private async void InitialisePassport(string redirectUri = null, string logoutRe
6869

6970
try
7071
{
72+
// Set the log level for the SDK
73+
Passport.LogLevel = LogLevel.Info;
74+
7175
// Initialise Passport
7276
string clientId = "ZJL7JvetcDFBNDlgRs5oJoxuAUUl6uQj";
7377
string environment = Immutable.Passport.Model.Environment.SANDBOX;
@@ -79,6 +83,7 @@ private async void InitialisePassport(string redirectUri = null, string logoutRe
7983
}
8084
catch (Exception ex)
8185
{
86+
Debug.LogException(ex, this);
8287
ShowOutput($"Initialise Passport error: {ex.Message}");
8388
}
8489
}

sample/ProjectSettings/ProjectSettings.asset

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -769,7 +769,8 @@ PlayerSettings:
769769
webGLDecompressionFallback: 0
770770
webGLPowerPreference: 2
771771
scriptingDefineSymbols:
772-
Standalone:
772+
Android: VUPLEX_STANDALONE
773+
Standalone: VUPLEX_STANDALONE
773774
additionalCompilerArguments:
774775
Standalone:
775776
- -nullable+
@@ -797,7 +798,7 @@ PlayerSettings:
797798
allowUnsafeCode: 0
798799
useDeterministicCompilation: 1
799800
enableRoslynAnalyzers: 1
800-
selectedPlatform: 0
801+
selectedPlatform: 2
801802
additionalIl2CppArgs:
802803
scriptingRuntimeVersion: 1
803804
gcIncremental: 1

src/Packages/Passport/Runtime/Scripts/Private/Core/BrowserCommunicationsManager.cs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System.Net;
21
using System;
32
using Cysharp.Threading.Tasks;
43
using System.Collections.Generic;
@@ -7,6 +6,8 @@
76
using UnityEngine;
87
using UnityEngine.Scripting;
98
using Immutable.Passport.Helpers;
9+
using Immutable.Passport.Core.Logging;
10+
using Immutable.Passport.Event;
1011

1112
namespace Immutable.Passport.Core
1213
{
@@ -95,13 +96,23 @@ private void CallFunction(string requestId, string fxName, string data = null)
9596

9697
// Call the function on the JS side
9798
string js = $"callFunction(\"{requestJson}\")";
98-
Debug.Log($"{TAG} Call {fxName} (request ID: {requestId}, js: {js})");
99+
100+
if (fxName != PassportAnalytics.TRACK)
101+
{
102+
string dataString = data != null ? $": {data}" : "";
103+
PassportLogger.Info($"{TAG} Call {fxName} (request ID: {requestId}){dataString}");
104+
}
105+
else
106+
{
107+
PassportLogger.Debug($"{TAG} Call {fxName} (request ID: {requestId}): {js}");
108+
}
109+
99110
webBrowserClient.ExecuteJs(js);
100111
}
101112

102113
public void LaunchAuthURL(string url, string redirectUri)
103114
{
104-
Debug.Log($"{TAG} LaunchAuthURL : {url}");
115+
PassportLogger.Info($"{TAG} LaunchAuthURL : {url}");
105116
webBrowserClient.LaunchAuthURL(url, redirectUri);
106117
}
107118

@@ -123,13 +134,12 @@ public void ClearStorage()
123134

124135
private void InvokeOnUnityPostMessage(string message)
125136
{
126-
Debug.Log($"{TAG} InvokeOnUnityPostMessage: {message}");
127137
HandleResponse(message);
128138
}
129139

130140
private void InvokeOnAuthPostMessage(string message)
131141
{
132-
Debug.Log($"{TAG} InvokeOnAuthPostMessage: {message}");
142+
PassportLogger.Info($"{TAG} Auth message received: {message}");
133143
if (OnAuthPostMessage != null)
134144
{
135145
OnAuthPostMessage.Invoke(message);
@@ -138,7 +148,7 @@ private void InvokeOnAuthPostMessage(string message)
138148

139149
private void InvokeOnPostMessageError(string id, string message)
140150
{
141-
Debug.Log($"{TAG} InvokeOnPostMessageError id: {id} message: {message}");
151+
PassportLogger.Info($"{TAG} Error message received ({id}): {message}");
142152
if (OnPostMessageError != null)
143153
{
144154
OnPostMessageError.Invoke(id, message);
@@ -147,34 +157,48 @@ private void InvokeOnPostMessageError(string id, string message)
147157

148158
private void HandleResponse(string message)
149159
{
150-
Debug.Log($"{TAG} HandleResponse message: " + message);
160+
PassportLogger.Debug($"{TAG} Handle response message: " + message);
151161
BrowserResponse response = message.OptDeserializeObject<BrowserResponse>();
152162

153-
// Check if the reponse returned is valid and the task to return the reponse exists
154-
if (response == null || String.IsNullOrEmpty(response.responseFor) || String.IsNullOrEmpty(response.requestId))
163+
// Validate the deserialised response object
164+
if (response == null || string.IsNullOrEmpty(response.responseFor) || string.IsNullOrEmpty(response.requestId))
165+
{
166+
throw new PassportException("Response from browser is incorrect. Check game bridge file.");
167+
}
168+
169+
string logMessage = $"{TAG} Response for: {response.responseFor} (request ID: {response.requestId}) : {message}";
170+
if (response.responseFor != PassportAnalytics.TRACK)
171+
{
172+
// Log info messages for valid responses not related to tracking
173+
PassportLogger.Info(logMessage);
174+
}
175+
else
155176
{
156-
throw new PassportException($"Response from browser is incorrect. Check HTML/JS files.");
177+
PassportLogger.Debug(logMessage);
157178
}
158179

159-
// Special case to detect if index.js is loaded
180+
// Handle special case where the response indicates that the browser is ready
160181
if (response.responseFor == INIT && response.requestId == INIT_REQUEST_ID)
161182
{
162-
Debug.Log($"{TAG} Browser is ready");
183+
PassportLogger.Info($"{TAG} Browser is ready");
163184
if (OnReady != null)
164185
{
165186
OnReady.Invoke();
166187
}
167188
return;
168189
}
169190

191+
// Handle the response if a matching task exists for the request ID
170192
string requestId = response.requestId;
171193
if (requestTaskMap.ContainsKey(requestId))
172194
{
173195
NotifyRequestResult(requestId, message);
174196
}
175197
else
176198
{
177-
throw new PassportException($"No TaskCompletionSource for request id {requestId} found.");
199+
string errorMsg = $"No TaskCompletionSource for request id {requestId} found.";
200+
PassportLogger.Error(errorMsg);
201+
throw new PassportException(errorMsg);
178202
}
179203
}
180204

@@ -199,7 +223,7 @@ private PassportException ParseError(BrowserResponse response)
199223
}
200224
catch (Exception ex)
201225
{
202-
Debug.LogError($"{TAG} Parse passport type error: {ex.Message}");
226+
PassportLogger.Error($"{TAG} Parse passport type error: {ex.Message}");
203227
}
204228
return new PassportException(response.error ?? "Failed to parse error");
205229
}

src/Packages/Passport/Runtime/Scripts/Private/Core/Logging.meta

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"name": "Immutable.Passport.Core.Logging",
3+
"rootNamespace": "",
4+
"references": [],
5+
"includePlatforms": [],
6+
"excludePlatforms": [],
7+
"allowUnsafeCode": false,
8+
"overrideReferences": false,
9+
"precompiledReferences": [],
10+
"autoReferenced": true,
11+
"defineConstraints": [],
12+
"versionDefines": [],
13+
"noEngineReferences": false
14+
}

src/Packages/Passport/Runtime/Scripts/Private/Core/Logging/Immutable.Passport.Core.Logging.asmdef.meta

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
namespace Immutable.Passport.Core.Logging
2+
{
3+
/// <summary>
4+
/// Defines the logging levels used within the SDK to categorise the severity of log messages.
5+
/// </summary>
6+
public enum LogLevel
7+
{
8+
/// <summary>
9+
/// Logs detailed information for debugging the SDK
10+
/// </summary>
11+
Debug,
12+
13+
/// <summary>
14+
/// Logs general information about SDK operations
15+
/// </summary>
16+
Info,
17+
18+
/// <summary>
19+
/// Logs warnings about potential issues or unexpected behaviour
20+
/// </summary>
21+
Warn,
22+
23+
/// <summary>
24+
/// Logs errors indicating failures in SDK operations
25+
/// </summary>
26+
Error
27+
}
28+
}

src/Packages/Passport/Runtime/Scripts/Private/Core/Logging/LogLevel.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using UnityEngine;
2+
3+
namespace Immutable.Passport.Core.Logging
4+
{
5+
public static class PassportLogger
6+
{
7+
private const string TAG = "[Immutable]";
8+
9+
public static LogLevel CurrentLogLevel { get; set; } = LogLevel.Info;
10+
11+
public static void Log(LogLevel level, string message)
12+
{
13+
if (level < CurrentLogLevel)
14+
{
15+
return; // Don't log messages below the current log level
16+
}
17+
18+
switch (level)
19+
{
20+
case LogLevel.Debug:
21+
UnityEngine.Debug.Log($"{TAG} {message}");
22+
break;
23+
case LogLevel.Info:
24+
UnityEngine.Debug.Log($"{TAG} {message}");
25+
break;
26+
case LogLevel.Warn:
27+
UnityEngine.Debug.LogWarning($"{TAG} {message}");
28+
break;
29+
case LogLevel.Error:
30+
UnityEngine.Debug.LogError($"{TAG} {message}");
31+
break;
32+
default:
33+
break;
34+
}
35+
}
36+
37+
public static void Debug(string message)
38+
{
39+
Log(LogLevel.Debug, message);
40+
}
41+
42+
public static void Info(string message)
43+
{
44+
Log(LogLevel.Info, message);
45+
}
46+
47+
public static void Warn(string message)
48+
{
49+
Log(LogLevel.Warn, message);
50+
}
51+
52+
public static void Error(string message)
53+
{
54+
Log(LogLevel.Error, message);
55+
}
56+
}
57+
58+
}

src/Packages/Passport/Runtime/Scripts/Private/Core/Logging/PassportLogger.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Packages/Passport/Runtime/Scripts/Private/Helpers/JsonHelpers.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using System.Linq;
44
using System.Text;
55
using UnityEngine;
6+
using Immutable.Passport.Core.Logging;
67

78
namespace Immutable.Passport.Helpers
89
{
@@ -19,7 +20,7 @@ public static T OptDeserializeObject<T>(this string json) where T : class
1920
}
2021
catch (Exception e)
2122
{
22-
Debug.Log($"Failed to deserialise {json}: {e.Message}");
23+
PassportLogger.Debug($"Failed to deserialise {json}: {e.Message}");
2324
return null;
2425
}
2526
}

src/Packages/Passport/Runtime/Scripts/Private/Helpers/UriHelpers.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
using System;
2-
using UnityEngine;
2+
using Immutable.Passport.Core.Logging;
33

44
namespace Immutable.Passport.Helpers
55
{
@@ -25,7 +25,7 @@ public static string GetQueryParameter(this Uri uri, string key)
2525
}
2626
catch (Exception e)
2727
{
28-
Debug.Log($"Failed to get query parameter {key}: {e.Message}");
28+
PassportLogger.Debug($"Failed to get query parameter {key}: {e.Message}");
2929
}
3030
return null;
3131
}

src/Packages/Passport/Runtime/Scripts/Private/Immutable.Passport.Runtime.Private.asmdef

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"VoltstroStudios.UnityWebBrowser",
66
"UniTask",
77
"Immutable.Browser.Core",
8-
"unity-webview"
8+
"unity-webview",
9+
"Immutable.Passport.Core.Logging"
910
],
1011
"includePlatforms": [
1112
"Android",

0 commit comments

Comments
 (0)