Skip to content

Commit

Permalink
Added logging level; (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
mismirnov authored May 12, 2023
1 parent 90c2826 commit a1187f9
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 18 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [unreleased]
### Added
- User ability to specify logging level

## [unreleased]
### Fixed
Expand Down
17 changes: 9 additions & 8 deletions Runtime/Scripts/BeaconSDK/BeaconConnectorDotNet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
using Newtonsoft.Json.Linq;
using UnityEngine;
using ILogger = Microsoft.Extensions.Logging.ILogger;
using Logger = Helpers.Logger;
using LogLevel = Microsoft.Extensions.Logging.LogLevel;

namespace BeaconSDK
Expand All @@ -31,7 +32,7 @@ public class BeaconConnectorDotNet : IBeaconConnector
public async void ConnectAccount()
{
var pathToDb = Path.Combine(Application.persistentDataPath, "beacon.db");
Debug.Log($"DB file stored in {pathToDb}");
Logger.LogDebug($"DB file stored in {pathToDb}");

var options = new BeaconOptions
{
Expand All @@ -47,16 +48,16 @@ public async void ConnectAccount()
_beaconDappClient.OnBeaconMessageReceived += OnBeaconDappClientMessageReceived;

await _beaconDappClient.InitAsync();
Debug.Log($"Dapp initialized: {_beaconDappClient.LoggedIn}");
Logger.LogInfo($"Dapp initialized: {_beaconDappClient.LoggedIn}");
_beaconDappClient.Connect();
Debug.Log($"Dapp connected: {_beaconDappClient.Connected}");
Logger.LogInfo($"Dapp connected: {_beaconDappClient.Connected}");

var activeAccountPermissions = _beaconDappClient.GetActiveAccount();
if (activeAccountPermissions != null)
{
var permissionsString = activeAccountPermissions.Scopes.Aggregate(string.Empty,
(res, scope) => res + $"{scope}, ") ?? string.Empty;
Debug.Log(
Logger.LogInfo(
$"We have active peer {activeAccountPermissions.AppMetadata.Name} with permissions {permissionsString}");

UnityMainThreadDispatcher.Enqueue(
Expand Down Expand Up @@ -134,11 +135,11 @@ public async void RequestTezosPermission(string networkName = "", string network
if (activePeer != null)
{
await _beaconDappClient.SendResponseAsync(activePeer.SenderId, permissionRequest);
Debug.Log("Permission request sent");
Logger.LogInfo("Permission request sent");
}
else
{
Debug.LogError("No active peer found");
Logger.LogError("No active peer found");
}
}

Expand All @@ -162,7 +163,7 @@ public async void RequestTezosOperation(string destination, string entryPoint =
var activeAccountPermissions = _beaconDappClient.GetActiveAccount();
if (activeAccountPermissions == null)
{
Debug.LogError("No active permissions");
Logger.LogError("No active permissions");
return;
}

Expand All @@ -177,7 +178,7 @@ public async void RequestTezosOperation(string destination, string entryPoint =
operationDetails: operationDetails,
sourceAddress: pubKey.Address);

Debug.Log("requesting operation: " + operationRequest);
Logger.LogDebug("requesting operation: " + operationRequest);
await _beaconDappClient.SendResponseAsync(activeAccountPermissions.SenderId, operationRequest);
}

Expand Down
60 changes: 60 additions & 0 deletions Runtime/Scripts/Helpers/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using UnityEngine;

namespace Helpers
{
public static class Logger
{
public enum LogLevel
{
None,
Error,
Warning,
Info,
Debug
}

public static LogLevel CurrentLogLevel = LogLevel.Debug;

public static void Log(string message, LogLevel logLevel = LogLevel.Info)
{
if (logLevel > CurrentLogLevel)
return;

switch (logLevel)
{
case LogLevel.Debug:
Debug.Log(message);
break;
case LogLevel.Info:
Debug.Log(message);
break;
case LogLevel.Warning:
Debug.LogWarning(message);
break;
case LogLevel.Error:
Debug.LogError(message);
break;
}
}

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

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

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

public static void LogError(string message)
{
Log(message, LogLevel.Error);
}
}
}
11 changes: 11 additions & 0 deletions Runtime/Scripts/Helpers/Logger.cs.meta

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

2 changes: 2 additions & 0 deletions Runtime/Scripts/TezosAPI/TezosSingleton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
using System.Text.Json;
using Beacon.Sdk.Beacon.Sign;
using BeaconSDK;
using Helpers;
using TezosAPI;
using TezosAPI.Models;
using TezosAPI.Models.Tokens;
Expand All @@ -22,6 +23,7 @@ protected override void Awake()
base.Awake();

_tezos = new Tezos();
Logger.CurrentLogLevel = Logger.LogLevel.Info;
}

public void ConnectWallet()
Expand Down
13 changes: 7 additions & 6 deletions Samples~/Scripts/DemoExample/Core/ExampleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Netezos.Encoding;
using UnityEngine;
using TezosAPI;
using Logger = Helpers.Logger;

public class ExampleManager : IExampleManager
{
Expand Down Expand Up @@ -45,7 +46,7 @@ public void FetchInventoryItems(Action<List<IItemModel>> callback)
CoroutineRunner.Instance.StartCoroutineWrapper(
_tezos.ReadView(contractAddress, entrypoint, input, result =>
{
Debug.Log("READING INVENTORY DATA");
Logger.LogDebug("READING INVENTORY DATA");
// deserialize the json data to inventory items
CoroutineRunner.Instance.StartCoroutineWrapper(
BeaconSDK.NetezosExtensions.HumanizeValue(result, _networkRPC, destination, "humanizeInventory",
Expand Down Expand Up @@ -187,8 +188,8 @@ public void BuyItem(string owner, int itemID)
new MichelineInt(itemID)
}
}.ToJson();

Debug.Log(destination + " " + entryPoint + parameter);
Logger.LogDebug(destination + " " + entryPoint + parameter);
_tezos.CallContract(contractAddress, entryPoint, parameter, 0);

#if UNITY_IOS || UNITY_ANDROID
Expand Down Expand Up @@ -249,7 +250,7 @@ private void GetSoftBalanceRoutine(Action<int> callback)

public void TransferItem(int itemID, int amount, string address)
{
Debug.Log("Transfering Item " + itemID + " from " + _tezos.GetActiveWalletAddress() + " to Address: " + address);
Logger.LogDebug("Transfering Item " + itemID + " from " + _tezos.GetActiveWalletAddress() + " to Address: " + address);

var sender = _tezos.GetActiveWalletAddress();
var entrypoint = "transfer";
Expand All @@ -264,7 +265,7 @@ public void TransferItem(int itemID, int amount, string address)

public void AddItemToMarket(int itemID, int price)
{
Debug.Log("Adding Item " + itemID + " to Market with the price of " + price);
Logger.LogDebug("Adding Item " + itemID + " to Market with the price of " + price);

var entryPoint = "addToMarket";

Expand Down Expand Up @@ -295,7 +296,7 @@ public void AddItemToMarket(int itemID, int price)

public void RemoveItemFromMarket(int itemID)
{
Debug.Log("Removing Item " + itemID + " from market.");
Logger.LogDebug("Removing Item " + itemID + " from market.");

var entryPoint = "removeFromMarket";

Expand Down
9 changes: 5 additions & 4 deletions Samples~/Scripts/DemoExample/InventoryManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using Logger = Helpers.Logger;

[RequireComponent(typeof(SnapController))]
public class InventoryManager : MonoBehaviour
Expand Down Expand Up @@ -51,7 +52,7 @@ private void AddItem(IItemModel itemModel)
if (itemRes != null)
newItem.Sprite = itemRes.ItemSprite;
else
Debug.LogError("Could find resource file for " + itemModel.ResourcePath);
Logger.LogError("Could find resource file for " + itemModel.ResourcePath);
newItem.Item = itemModel;
newItem.OnClick = OnItemClicked;
draggables.Add(newItem);
Expand All @@ -63,7 +64,7 @@ private void AddItem(IItemModel itemModel)
}
else
{
Debug.LogError("Trying to add an item but Inventory is full!");
Logger.LogError("Trying to add an item but Inventory is full!");
return;
}
}
Expand Down Expand Up @@ -149,7 +150,7 @@ private void SaveInventoryLocally()
if (equippedSlots[i].HasItem)
PlayerPrefs.SetInt(eqSaveLoc + i, equippedSlots[i].CurrentItemInSlot.Item.ID);
}
Debug.Log("Inventory saved locally.");
Logger.LogDebug("Inventory saved locally.");
}

private void LoadLocalInventory()
Expand All @@ -174,7 +175,7 @@ private void LoadLocalInventory()
_itemIDtoDraggable[itemID].SetCurrentSlot(equippedSlots[i]);
}
}
Debug.Log("Inventory loaded.");
Logger.LogDebug("Inventory loaded.");
}

public void UpdateStatsView()
Expand Down

0 comments on commit a1187f9

Please sign in to comment.