Skip to content

Commit 4fcc02d

Browse files
Import & new logging implementation from C# SDK (#115)
* Imported new logging system from C# and added Unity-console supported IPubnubLogger implementation
1 parent 2109715 commit 4fcc02d

10 files changed

+61
-41
lines changed

.pubnub.yml

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
---
2-
version: v8.1.1
2+
version: v8.2.0
33
changelog:
4+
- date: 2025-03-12
5+
version: v8.2.0
6+
changes:
7+
- type: feature
8+
text: "Imported new logging system from C# and added Unity-console supported IPubnubLogger implementation."
49
- date: 2025-02-25
510
version: v8.1.1
611
changes:
@@ -760,7 +765,7 @@ sdks:
760765
distribution-type: package
761766
distribution-repository: git release
762767
package-name: PubNub.unitypackage
763-
location: https://github.com/pubnub/unity/releases/download/v8.1.1/PubNub.unitypackage
768+
location: https://github.com/pubnub/unity/releases/download/v8.2.0/PubNub.unitypackage
764769
requires:
765770
-
766771
name: "UnityEditor"
@@ -927,7 +932,7 @@ sdks:
927932
distribution-type: package
928933
distribution-repository: git release
929934
package-name: PubNub.unitypackage
930-
location: https://github.com/pubnub/unity/releases/download/v8.1.1/PubNub.unitypackage
935+
location: https://github.com/pubnub/unity/releases/download/v8.2.0/PubNub.unitypackage
931936
requires:
932937
-
933938
name: "UnityEditor"
Binary file not shown.

PubNubUnity/Assets/PubNub/Runtime/Util/NewtonsoftJsonUnity.cs

+14-20
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Globalization;
4-
using System.IO;
53
using System.Linq;
4+
using System.IO;
65
using System.Reflection;
6+
using System.Globalization;
77
using Newtonsoft.Json;
88
using Newtonsoft.Json.Linq;
99

10-
namespace PubnubApi.Unity.PubNub.Runtime.Util {
11-
public class NewtonsoftJsonUnity : IJsonPluggableLibrary
10+
namespace PubnubApi
11+
{
12+
public class NewtonsoftJsonUnity : IJsonPluggableLibrary
1213
{
1314
private readonly PNConfiguration config;
14-
private readonly IPubnubLog pubnubLog;
1515
private readonly JsonSerializerSettings defaultJsonSerializerSettings;
16+
private readonly PubnubLogModule logger;
1617

17-
public NewtonsoftJsonUnity(PNConfiguration pubnubConfig, IPubnubLog log)
18+
public NewtonsoftJsonUnity(PNConfiguration pubnubConfig)
1819
{
1920
this.config = pubnubConfig;
20-
this.pubnubLog = log;
2121
defaultJsonSerializerSettings = new JsonSerializerSettings { MaxDepth = 64 };
22+
logger = pubnubConfig.Logger;
2223
}
2324

2425
#region IJsonPlugableLibrary methods implementation
@@ -139,6 +140,7 @@ public object DeserializeToObject(object rawObject, Type type)
139140
{
140141
try
141142
{
143+
logger?.Debug("JsonNet Deserializing object data.");
142144
if (rawObject is JObject jObject)
143145
{
144146
return jObject.ToObject(type);
@@ -150,13 +152,14 @@ public object DeserializeToObject(object rawObject, Type type)
150152
}
151153
catch (Exception e)
152154
{
153-
LoggingMethod.WriteToLog(pubnubLog, $"[{DateTime.Now.ToString(CultureInfo.InvariantCulture)}] Error: DeserializeToObject exception {e}", config.LogVerbosity);
155+
logger?.Error($"Deserialize To Object failed with exception {e.Message}, stack trace {e.StackTrace}");
154156
return rawObject;
155157
}
156158
}
157159

158160
public object DeserializeToObject(string jsonString)
159161
{
162+
logger?.Debug("JsonNet Deserializing json string data.");
160163
object result = JsonConvert.DeserializeObject<object>(jsonString,
161164
new JsonSerializerSettings { DateParseHandling = DateParseHandling.None, MaxDepth = 64 });
162165
if (result.GetType().ToString() == "Newtonsoft.Json.Linq.JArray")
@@ -177,7 +180,7 @@ public object DeserializeToObject(string jsonString)
177180
result = objectContainer;
178181
}
179182
}
180-
183+
logger?.Debug("JsonNet Deserialsed json string data successfully.");
181184
return result;
182185
}
183186

@@ -207,21 +210,12 @@ private bool IsGenericTypeForMessage<T>()
207210
{
208211
bool ret = typeof(T).GetTypeInfo().IsGenericType &&
209212
typeof(T).GetGenericTypeDefinition() == typeof(PNMessageResult<>);
210-
211-
LoggingMethod.WriteToLog(pubnubLog,
212-
$"[{DateTime.Now.ToString(CultureInfo.InvariantCulture)}] typeof(T).GetTypeInfo().IsGenericType = {typeof(T).GetTypeInfo().IsGenericType}", config.LogVerbosity);
213-
if (typeof(T).GetTypeInfo().IsGenericType)
214-
{
215-
LoggingMethod.WriteToLog(pubnubLog,
216-
$"[{DateTime.Now.ToString(CultureInfo.InvariantCulture)}] typeof(T).GetGenericTypeDefinition() = {typeof(T).GetGenericTypeDefinition()}", config.LogVerbosity);
217-
}
218-
LoggingMethod.WriteToLog(pubnubLog,
219-
$"[{DateTime.Now.ToString(CultureInfo.InvariantCulture)}] IsGenericTypeForMessage = {ret}", config.LogVerbosity);
220213
return ret;
221214
}
222215

223216
private T DeserializeMessageToObjectBasedOnPlatform<T>(List<object> listObject)
224217
{
218+
logger?.Debug("JsonNet Deserializing Messages data.");
225219
T ret = default(T);
226220
Type dataType = typeof(T).GetTypeInfo().GenericTypeArguments[0];
227221
Type generic = typeof(PNMessageResult<>);
@@ -296,7 +290,7 @@ private T DeserializeMessageToObjectBasedOnPlatform<T>(List<object> listObject)
296290

297291
ret = (T)Convert.ChangeType(message, specific, CultureInfo.InvariantCulture);
298292
}
299-
293+
logger?.Debug("JsonNet Deserialized Messages successfully.");
300294
return ret;
301295
}
302296

PubNubUnity/Assets/PubNub/Runtime/Util/PNManagerBehaviour.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using PubnubApi.Unity.PubNub.Runtime.Util;
21
using UnityEngine;
32

43
namespace PubnubApi.Unity {
@@ -34,15 +33,14 @@ public Pubnub Initialize(string userId) {
3433

3534
pnConfiguration.UserId = userId;
3635
var pnConfig = ((PNConfiguration)pnConfiguration);
37-
if (pnConfiguration.LogToUnityConsole) {
38-
pnConfig.PubnubLog = new UnityPNLog();
39-
}
40-
4136
pubnub = pnConfiguration.EnableWebGLBuildMode
4237
? new Pubnub(pnConfig, httpTransportService: new UnityWebGLHttpClientService(),
4338
ipnsdkSource: new UnityPNSDKSource())
4439
: new Pubnub(pnConfig, ipnsdkSource: new UnityPNSDKSource());
45-
pubnub.SetJsonPluggableLibrary(new NewtonsoftJsonUnity(pnConfig, pnConfig.PubnubLog));
40+
if (pnConfiguration.LogToUnityConsole) {
41+
pubnub.SetLogger(new UnityPubNubLogger(pubnub.InstanceId));
42+
}
43+
pubnub.SetJsonPluggableLibrary(new NewtonsoftJsonUnity(pnConfig));
4644
pubnub.AddListener(listener);
4745
return pubnub;
4846
}

PubNubUnity/Assets/PubNub/Runtime/Util/UnityPNLog.cs

-9
This file was deleted.

PubNubUnity/Assets/PubNub/Runtime/Util/UnityPNSDKSource.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace PubnubApi.Unity
55
{
66
public class UnityPNSDKSource : IPNSDKSource {
77

8-
private const string build = "8.1.1";
8+
private const string build = "8.2.0";
99

1010
public string GetPNSDK() {
1111
#if(UNITY_IOS)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using System.Globalization;
3+
using PubnubApi;
4+
5+
public class UnityPubNubLogger : IPubnubLogger {
6+
7+
private string id;
8+
9+
public UnityPubNubLogger(string id) {
10+
this.id = id;
11+
}
12+
13+
public void Trace(string logMessage) {
14+
UnityEngine.Debug.Log($"{DateTime.Now.ToString(CultureInfo.InvariantCulture)} PubNub-{id} Trace {logMessage}");
15+
}
16+
17+
public void Debug(string logMessage) {
18+
UnityEngine.Debug.Log($"{DateTime.Now.ToString(CultureInfo.InvariantCulture)} PubNub-{id} Debug {logMessage}");
19+
}
20+
21+
public void Info(string logMessage) {
22+
UnityEngine.Debug.Log($"{DateTime.Now.ToString(CultureInfo.InvariantCulture)} PubNub-{id} Info {logMessage}");
23+
}
24+
25+
public void Warn(string logMessage) {
26+
UnityEngine.Debug.LogWarning($"{DateTime.Now.ToString(CultureInfo.InvariantCulture)} PubNub-{id} Warn {logMessage}");
27+
}
28+
29+
public void Error(string logMessage) {
30+
UnityEngine.Debug.LogError($"{DateTime.Now.ToString(CultureInfo.InvariantCulture)} PubNub-{id} Error {logMessage}");
31+
}
32+
}

PubNubUnity/Assets/PubNub/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "com.pubnub.sdk",
3-
"version": "8.1.1",
3+
"version": "8.2.0",
44
"displayName": "PubNub SDK",
55
"description": "PubNub Real-time Cloud-Hosted Push API and Push Notification Client Frameworks",
66
"unity": "2021.3",

VERSION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
8.1.1
1+
8.2.0

0 commit comments

Comments
 (0)