Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DX-3051] feat: add logging level #275

Merged
merged 2 commits into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions sample/Assets/Scripts/SelectAuthMethodScript.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using Immutable.Passport;
using Immutable.Passport.Core.Logging;

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

try
{
// Set the log level for the SDK
Passport.LogLevel = LogLevel.Info;

// Initialise Passport
string clientId = "ZJL7JvetcDFBNDlgRs5oJoxuAUUl6uQj";
string environment = Immutable.Passport.Model.Environment.SANDBOX;
Expand All @@ -79,6 +83,7 @@ private async void InitialisePassport(string redirectUri = null, string logoutRe
}
catch (Exception ex)
{
Debug.LogException(ex, this);
ShowOutput($"Initialise Passport error: {ex.Message}");
}
}
Expand Down
5 changes: 3 additions & 2 deletions sample/ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -769,7 +769,8 @@ PlayerSettings:
webGLDecompressionFallback: 0
webGLPowerPreference: 2
scriptingDefineSymbols:
Standalone:
Android: VUPLEX_STANDALONE
Standalone: VUPLEX_STANDALONE
additionalCompilerArguments:
Standalone:
- -nullable+
Expand Down Expand Up @@ -797,7 +798,7 @@ PlayerSettings:
allowUnsafeCode: 0
useDeterministicCompilation: 1
enableRoslynAnalyzers: 1
selectedPlatform: 0
selectedPlatform: 2
additionalIl2CppArgs:
scriptingRuntimeVersion: 1
gcIncremental: 1
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using System.Net;
using System;
using Cysharp.Threading.Tasks;
using System.Collections.Generic;
Expand All @@ -7,6 +6,8 @@
using UnityEngine;
using UnityEngine.Scripting;
using Immutable.Passport.Helpers;
using Immutable.Passport.Core.Logging;
using Immutable.Passport.Event;

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

// Call the function on the JS side
string js = $"callFunction(\"{requestJson}\")";
Debug.Log($"{TAG} Call {fxName} (request ID: {requestId}, js: {js})");

if (fxName != PassportAnalytics.TRACK)
{
string dataString = data != null ? $": {data}" : "";
PassportLogger.Info($"{TAG} Call {fxName} (request ID: {requestId}){dataString}");
}
YermekG marked this conversation as resolved.
Show resolved Hide resolved
else
{
PassportLogger.Debug($"{TAG} Call {fxName} (request ID: {requestId}): {js}");
}

webBrowserClient.ExecuteJs(js);
}

public void LaunchAuthURL(string url, string redirectUri)
{
Debug.Log($"{TAG} LaunchAuthURL : {url}");
PassportLogger.Info($"{TAG} LaunchAuthURL : {url}");
webBrowserClient.LaunchAuthURL(url, redirectUri);
}

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

private void InvokeOnUnityPostMessage(string message)
{
Debug.Log($"{TAG} InvokeOnUnityPostMessage: {message}");
HandleResponse(message);
}

private void InvokeOnAuthPostMessage(string message)
{
Debug.Log($"{TAG} InvokeOnAuthPostMessage: {message}");
PassportLogger.Info($"{TAG} Auth message received: {message}");
if (OnAuthPostMessage != null)
{
OnAuthPostMessage.Invoke(message);
Expand All @@ -138,7 +148,7 @@ private void InvokeOnAuthPostMessage(string message)

private void InvokeOnPostMessageError(string id, string message)
{
Debug.Log($"{TAG} InvokeOnPostMessageError id: {id} message: {message}");
PassportLogger.Info($"{TAG} Error message received ({id}): {message}");
if (OnPostMessageError != null)
{
OnPostMessageError.Invoke(id, message);
Expand All @@ -147,34 +157,50 @@ private void InvokeOnPostMessageError(string id, string message)

private void HandleResponse(string message)
{
Debug.Log($"{TAG} HandleResponse message: " + message);
PassportLogger.Debug($"{TAG} Handle response message: " + message);
BrowserResponse response = message.OptDeserializeObject<BrowserResponse>();

// Check if the reponse returned is valid and the task to return the reponse exists
if (response == null || String.IsNullOrEmpty(response.responseFor) || String.IsNullOrEmpty(response.requestId))
// Validate the deserialised response object
if (response == null || string.IsNullOrEmpty(response.responseFor) || string.IsNullOrEmpty(response.requestId))
{
string responseError = "Response from browser is incorrect. Check game bridge file.";
PassportLogger.Error(responseError);
throw new PassportException(responseError);
}

string logMessage = $"{TAG} Response for: {response.responseFor} (request ID: {response.requestId}) : {message}";
if (response.responseFor != PassportAnalytics.TRACK)
{
// Log info messages for valid responses not related to tracking
PassportLogger.Info(logMessage);
}
else
{
throw new PassportException($"Response from browser is incorrect. Check HTML/JS files.");
PassportLogger.Debug(logMessage);
}

// Special case to detect if index.js is loaded
// Handle special case where the response indicates that the browser is ready
if (response.responseFor == INIT && response.requestId == INIT_REQUEST_ID)
{
Debug.Log($"{TAG} Browser is ready");
PassportLogger.Info($"{TAG} Browser is ready");
if (OnReady != null)
{
OnReady.Invoke();
}
return;
}

// Handle the response if a matching task exists for the request ID
string requestId = response.requestId;
if (requestTaskMap.ContainsKey(requestId))
{
NotifyRequestResult(requestId, message);
}
else
{
throw new PassportException($"No TaskCompletionSource for request id {requestId} found.");
string errorMsg = $"No TaskCompletionSource for request id {requestId} found.";
PassportLogger.Error(errorMsg);
throw new PassportException(errorMsg);
}
}

Expand All @@ -199,7 +225,7 @@ private PassportException ParseError(BrowserResponse response)
}
catch (Exception ex)
{
Debug.LogError($"{TAG} Parse passport type error: {ex.Message}");
PassportLogger.Error($"{TAG} Parse passport type error: {ex.Message}");
}
return new PassportException(response.error ?? "Failed to parse error");
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "Immutable.Passport.Core.Logging",
"rootNamespace": "",
"references": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false,
"overrideReferences": false,
"precompiledReferences": [],
"autoReferenced": true,
"defineConstraints": [],
"versionDefines": [],
"noEngineReferences": false
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
namespace Immutable.Passport.Core.Logging
{
/// <summary>
/// Defines the logging levels used within the SDK to categorise the severity of log messages.
/// </summary>
public enum LogLevel
{
/// <summary>
/// Logs detailed information for debugging the SDK
/// </summary>
Debug,

/// <summary>
/// Logs general information about SDK operations
/// </summary>
Info,

/// <summary>
/// Logs warnings about potential issues or unexpected behaviour
/// </summary>
Warn,

/// <summary>
/// Logs errors indicating failures in SDK operations
/// </summary>
Error
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using UnityEngine;

namespace Immutable.Passport.Core.Logging
{
public static class PassportLogger
{
private const string TAG = "[Immutable]";

public static LogLevel CurrentLogLevel { get; set; } = LogLevel.Info;

public static void Log(LogLevel level, string message)
{
if (level < CurrentLogLevel)
{
return; // Don't log messages below the current log level
}

switch (level)
{
case LogLevel.Debug:
UnityEngine.Debug.Log($"{TAG} {message}");
break;
case LogLevel.Info:
UnityEngine.Debug.Log($"{TAG} {message}");
break;
case LogLevel.Warn:
UnityEngine.Debug.LogWarning($"{TAG} {message}");
break;
case LogLevel.Error:
UnityEngine.Debug.LogError($"{TAG} {message}");
break;
default:
break;
}
}

public static void Debug(string message)
{
Log(LogLevel.Debug, message);
}

public static void Info(string message)
{
Log(LogLevel.Info, message);
}

public static void Warn(string message)
{
Log(LogLevel.Warn, message);
}

public static void Error(string message)
{
Log(LogLevel.Error, message);
}
}

}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using System.Text;
using UnityEngine;
using Immutable.Passport.Core.Logging;

namespace Immutable.Passport.Helpers
{
Expand All @@ -19,7 +20,7 @@ public static T OptDeserializeObject<T>(this string json) where T : class
}
catch (Exception e)
{
Debug.Log($"Failed to deserialise {json}: {e.Message}");
PassportLogger.Debug($"Failed to deserialise {json}: {e.Message}");
return null;
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using UnityEngine;
using Immutable.Passport.Core.Logging;

namespace Immutable.Passport.Helpers
{
Expand All @@ -25,7 +25,7 @@ public static string GetQueryParameter(this Uri uri, string key)
}
catch (Exception e)
{
Debug.Log($"Failed to get query parameter {key}: {e.Message}");
PassportLogger.Debug($"Failed to get query parameter {key}: {e.Message}");
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"VoltstroStudios.UnityWebBrowser",
"UniTask",
"Immutable.Browser.Core",
"unity-webview"
"unity-webview",
"Immutable.Passport.Core.Logging"
],
"includePlatforms": [
"Android",
Expand Down
Loading
Loading