Skip to content

Commit c8b6c52

Browse files
committed
Added new log system
1 parent 3f70876 commit c8b6c52

18 files changed

+223
-166
lines changed

MLAPI/Data/NetworkConfig.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Collections.Generic;
55
using UnityEngine;
66
using MLAPI.Data.Transports;
7+
using MLAPI.NetworkingManagerComponents.Core;
78

89
namespace MLAPI.Data
910
{

MLAPI/Data/NetworkPool.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using MLAPI.MonoBehaviours.Core;
2+
using MLAPI.NetworkingManagerComponents.Core;
23
using UnityEngine;
34

45
namespace MLAPI.Data
@@ -35,7 +36,7 @@ internal GameObject SpawnObject(Vector3 position, Quaternion rotation)
3536
go.SetActive(true);
3637
}
3738
}
38-
Debug.LogWarning("MLAPI: The pool " + poolId + " has ran out of space");
39+
LogHelper.LogWarning("MLAPI: The pool " + poolId + " has ran out of space", LogLevel.Normal);
3940
return null;
4041
}
4142
}

MLAPI/MLAPI.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@
9999
<Compile Include="NetworkingManagerComponents\Binary\BinaryHelpers.cs" />
100100
<Compile Include="NetworkingManagerComponents\Binary\BinarySerializer.cs" />
101101
<Compile Include="NetworkingManagerComponents\Binary\PrimitiveHasher.cs" />
102+
<Compile Include="NetworkingManagerComponents\Core\LogHelper.cs" />
102103
<Compile Include="NetworkingManagerComponents\Cryptography\CryptographyHelper.cs" />
103104
<Compile Include="NetworkingManagerComponents\Cryptography\DiffieHellman.cs" />
104105
<Compile Include="NetworkingManagerComponents\Cryptography\EllipticCurve.cs" />

MLAPI/MonoBehaviours/Core/NetworkedBehaviour.cs

Lines changed: 97 additions & 97 deletions
Large diffs are not rendered by default.

MLAPI/MonoBehaviours/Core/NetworkedObject.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ private void OnValidate()
1717
{
1818
if(string.IsNullOrEmpty(NetworkedPrefabName))
1919
{
20-
Debug.LogWarning("MLAPI: The NetworkedObject " + gameObject.name + " does not have a NetworkedPrefabName. It has been set to the gameObject name");
20+
LogHelper.LogWarning("MLAPI: The NetworkedObject " + gameObject.name + " does not have a NetworkedPrefabName. It has been set to the gameObject name", LogLevel.Normal);
2121
NetworkedPrefabName = gameObject.name;
2222
}
2323
}

MLAPI/MonoBehaviours/Core/NetworkingManager.cs

Lines changed: 30 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ public float NetworkTime
4141
/// </summary>
4242
public bool RunInBackground = true;
4343
/// <summary>
44+
/// The log level to use
45+
/// </summary>
46+
public LogLevel LogLevel = LogLevel.Normal;
47+
/// <summary>
4448
/// The singleton instance of the NetworkingManager
4549
/// </summary>
4650
public static NetworkingManager singleton
@@ -153,18 +157,21 @@ public bool IsClientConnected
153157

154158
private void OnValidate()
155159
{
160+
if (!Application.isPlaying)
161+
_singleton = this;
156162
if (NetworkConfig == null)
157163
return; //May occur when the component is added
164+
158165

159166
if(NetworkConfig.EnableSceneSwitching && !NetworkConfig.RegisteredScenes.Contains(SceneManager.GetActiveScene().name))
160167
{
161-
Debug.LogWarning("MLAPI: The active scene is not registered as a networked scene. The MLAPI has added it");
168+
LogHelper.LogWarning("MLAPI: The active scene is not registered as a networked scene. The MLAPI has added it", LogLevel.Normal);
162169
NetworkConfig.RegisteredScenes.Add(SceneManager.GetActiveScene().name);
163170
}
164171

165172
if(!NetworkConfig.EnableSceneSwitching && NetworkConfig.HandleObjectSpawning)
166173
{
167-
Debug.LogWarning("MLAPI: Please be aware that Scene objects are NOT supported if SceneManagement is turned on, even if HandleObjectSpawning is turned on");
174+
LogHelper.LogWarning("MLAPI: Please be aware that Scene objects are NOT supported if SceneManagement is turned on, even if HandleObjectSpawning is turned on", LogLevel.Normal);
168175
}
169176

170177
if(NetworkConfig.HandleObjectSpawning)
@@ -173,17 +180,17 @@ private void OnValidate()
173180
{
174181
if (string.IsNullOrEmpty(NetworkConfig.NetworkedPrefabs[i].name))
175182
{
176-
Debug.LogWarning("MLAPI: The NetworkedPrefab " + NetworkConfig.NetworkedPrefabs[i].prefab.name + " does not have a NetworkedPrefabName.");
183+
LogHelper.LogWarning("MLAPI: The NetworkedPrefab " + NetworkConfig.NetworkedPrefabs[i].prefab.name + " does not have a NetworkedPrefabName.", LogLevel.Normal);
177184
}
178185
}
179186
int playerPrefabCount = NetworkConfig.NetworkedPrefabs.Count(x => x.playerPrefab == true);
180187
if (playerPrefabCount == 0)
181188
{
182-
Debug.LogWarning("MLAPI: There is no NetworkedPrefab marked as a PlayerPrefab");
189+
LogHelper.LogWarning("MLAPI: There is no NetworkedPrefab marked as a PlayerPrefab", LogLevel.Normal);
183190
}
184191
else if (playerPrefabCount > 1)
185192
{
186-
Debug.LogWarning("MLAPI: Only one networked prefab can be marked as a player prefab");
193+
LogHelper.LogWarning("MLAPI: Only one networked prefab can be marked as a player prefab", LogLevel.Normal);
187194
}
188195
else
189196
NetworkConfig.PlayerPrefabName = NetworkConfig.NetworkedPrefabs.Find(x => x.playerPrefab == true).name;
@@ -257,7 +264,7 @@ private object Init(bool server)
257264
{
258265
if (networkedPrefabName.Contains(NetworkConfig.NetworkedPrefabs[i].name))
259266
{
260-
Debug.LogWarning("MLAPI: Duplicate NetworkedPrefabName " + NetworkConfig.NetworkedPrefabs[i].name);
267+
LogHelper.LogWarning("MLAPI: Duplicate NetworkedPrefabName " + NetworkConfig.NetworkedPrefabs[i].name, LogLevel.Normal);
261268
continue;
262269
}
263270
NetworkConfig.NetworkPrefabIds.Add(NetworkConfig.NetworkedPrefabs[i].name, i);
@@ -327,7 +334,7 @@ private object Init(bool server)
327334
{
328335
if (addedEncryptedChannels.Contains(NetworkConfig.Channels[i].Name))
329336
{
330-
Debug.LogWarning("MLAPI: Duplicate encrypted channel name " + NetworkConfig.Channels[i].Name);
337+
LogHelper.LogWarning("MLAPI: Duplicate encrypted channel name " + NetworkConfig.Channels[i].Name, LogLevel.Normal);
331338
continue;
332339
}
333340
if (NetworkConfig.Channels[i].Encrypted)
@@ -346,7 +353,7 @@ private object Init(bool server)
346353
{
347354
if (addedPassthroughMessages.Contains(NetworkConfig.MessageTypes[i].Name))
348355
{
349-
Debug.LogWarning("MLAPI: Duplicate passthrough message type " + NetworkConfig.MessageTypes[i].Name);
356+
LogHelper.LogWarning("MLAPI: Duplicate passthrough message type " + NetworkConfig.MessageTypes[i].Name, LogLevel.Normal);
350357
continue;
351358
}
352359
if (NetworkConfig.MessageTypes[i].Passthrough)
@@ -363,7 +370,7 @@ private object Init(bool server)
363370
{
364371
if (channelNames.Contains(internalChannels[i].Name))
365372
{
366-
Debug.LogWarning("MLAPI: Duplicate channel name: " + NetworkConfig.Channels[i].Name);
373+
LogHelper.LogWarning("MLAPI: Duplicate channel name: " + NetworkConfig.Channels[i].Name, LogLevel.Normal);
367374
continue;
368375
}
369376
int channelId = NetworkConfig.NetworkTransport.AddChannel(internalChannels[i].Type, settings);
@@ -445,7 +452,7 @@ private object Init(bool server)
445452
{
446453
if(channelNames.Contains(NetworkConfig.Channels[i].Name))
447454
{
448-
Debug.LogWarning("MLAPI: Duplicate channel name: " + NetworkConfig.Channels[i].Name);
455+
LogHelper.LogWarning("MLAPI: Duplicate channel name: " + NetworkConfig.Channels[i].Name, LogLevel.Normal);
449456
continue;
450457
}
451458
int channelId = NetworkConfig.NetworkTransport.AddChannel(NetworkConfig.Channels[i].Type, settings);
@@ -472,15 +479,15 @@ public void StartServer()
472479
{
473480
if (isServer || isClient)
474481
{
475-
Debug.LogWarning("MLAPI: Cannot start server while an instance is already running");
482+
LogHelper.LogWarning("MLAPI: Cannot start server while an instance is already running", LogLevel.Normal);
476483
return;
477484
}
478485

479486
if (NetworkConfig.ConnectionApproval)
480487
{
481488
if (ConnectionApprovalCallback == null)
482489
{
483-
Debug.LogWarning("MLAPI: No ConnectionApproval callback defined. Connection approval will timeout");
490+
LogHelper.LogWarning("MLAPI: No ConnectionApproval callback defined. Connection approval will timeout", LogLevel.Normal);
484491
}
485492
}
486493

@@ -502,7 +509,7 @@ public void StartClient()
502509
{
503510
if (isServer || isClient)
504511
{
505-
Debug.LogWarning("MLAPI: Cannot start client while an instance is already running");
512+
LogHelper.LogWarning("MLAPI: Cannot start client while an instance is already running", LogLevel.Normal);
506513
return;
507514
}
508515

@@ -576,15 +583,15 @@ public void StartHost(Vector3? pos = null, Quaternion? rot = null)
576583
{
577584
if (isServer || isClient)
578585
{
579-
Debug.LogWarning("MLAPI: Cannot start host while an instance is already running");
586+
LogHelper.LogWarning("MLAPI: Cannot start host while an instance is already running", LogLevel.Normal);
580587
return;
581588
}
582589

583590
if (NetworkConfig.ConnectionApproval)
584591
{
585592
if (ConnectionApprovalCallback == null)
586593
{
587-
Debug.LogWarning("MLAPI: No ConnectionApproval callback defined. Connection approval will timeout");
594+
LogHelper.LogWarning("MLAPI: No ConnectionApproval callback defined. Connection approval will timeout", LogLevel.Normal);
588595
}
589596
}
590597
object settings = Init(true);
@@ -613,7 +620,7 @@ private void OnEnable()
613620
{
614621
if (singleton != null)
615622
{
616-
Debug.LogWarning("MLAPI: Multiple NetworkingManagers");
623+
LogHelper.LogWarning("MLAPI: Multiple NetworkingManagers", LogLevel.Normal);
617624
Destroy(this);
618625
return;
619626
}
@@ -785,7 +792,7 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId)
785792
//Client tried to send a network message that was not the connection request before he was accepted.
786793
if (isServer && pendingClients.Contains(clientId) && messageType != 0)
787794
{
788-
Debug.LogWarning("MLAPI: Message recieved from clientId " + clientId + " before it has been accepted");
795+
LogHelper.LogWarning("MLAPI: Message recieved from clientId " + clientId + " before it has been accepted", LogLevel.Normal);
789796
return;
790797
}
791798

@@ -804,21 +811,21 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId)
804811
{
805812
if (isServer && isPassthrough && !NetworkConfig.PassthroughMessageHashSet.Contains(messageType))
806813
{
807-
Debug.LogWarning("MLAPI: Client " + clientId + " tried to send a passthrough message for a messageType not registered as passthrough");
814+
LogHelper.LogWarning("MLAPI: Client " + clientId + " tried to send a passthrough message for a messageType not registered as passthrough", LogLevel.Normal);
808815
messageReader.Dispose();
809816
return;
810817
}
811818
else if (isClient && isPassthrough && !NetworkConfig.PassthroughMessageHashSet.Contains(messageType))
812819
{
813-
Debug.LogWarning("MLAPI: Server tried to send a passthrough message for a messageType not registered as passthrough");
820+
LogHelper.LogWarning("MLAPI: Server tried to send a passthrough message for a messageType not registered as passthrough", LogLevel.Normal);
814821
messageReader.Dispose();
815822
return;
816823
}
817824
else if (isServer && isPassthrough)
818825
{
819826
if (!connectedClients.ContainsKey(passthroughTarget))
820827
{
821-
Debug.LogWarning("MLAPI: Passthrough message was sent with invalid target: " + passthroughTarget + " from client " + clientId);
828+
LogHelper.LogWarning("MLAPI: Passthrough message was sent with invalid target: " + passthroughTarget + " from client " + clientId, LogLevel.Normal);
822829
messageReader.Dispose();
823830
return;
824831
}
@@ -841,19 +848,19 @@ private void HandleIncomingData(uint clientId, byte[] data, int channelId)
841848
{
842849
if (!SpawnManager.spawnedObjects.ContainsKey(targetNetworkId))
843850
{
844-
Debug.LogWarning("MLAPI: No target for message found");
851+
LogHelper.LogWarning("MLAPI: No target for message found", LogLevel.Normal);
845852
messageReader.Dispose();
846853
return;
847854
}
848855
else if (!SpawnManager.spawnedObjects[targetNetworkId].targetMessageActions.ContainsKey(networkOrderId))
849856
{
850-
Debug.LogWarning("MLAPI: No target messageType for message found");
857+
LogHelper.LogWarning("MLAPI: No target messageType for message found", LogLevel.Normal);
851858
messageReader.Dispose();
852859
return;
853860
}
854861
else if (!SpawnManager.spawnedObjects[targetNetworkId].targetMessageActions[networkOrderId].ContainsKey(messageType))
855862
{
856-
Debug.LogWarning("MLAPI: No target found with the given messageType");
863+
LogHelper.LogWarning("MLAPI: No target found with the given messageType", LogLevel.Normal);
857864
messageReader.Dispose();
858865
return;
859866
}

MLAPI/MonoBehaviours/Prototyping/NetworkedAnimator.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using MLAPI.Data;
22
using MLAPI.MonoBehaviours.Core;
33
using MLAPI.NetworkingManagerComponents.Binary;
4+
using MLAPI.NetworkingManagerComponents.Core;
45
using System.Collections.Generic;
56
using System.IO;
67
using UnityEngine;
@@ -106,7 +107,7 @@ public override void NetworkStart()
106107
/// </summary>
107108
public void ResetParameterOptions()
108109
{
109-
Debug.Log("ResetParameterOptions");
110+
LogHelper.LogInfo("ResetParameterOptions", LogLevel.Normal);
110111
parameterSendBits = 0;
111112
animatorParameters = null;
112113
}

MLAPI/NetworkingManagerComponents/Binary/BinarySerializer.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
using MLAPI.Attributes;
22
using MLAPI.Data;
3+
using MLAPI.NetworkingManagerComponents.Core;
34
using System.Collections.Generic;
45
using System.Linq;
56
using System.Reflection;
6-
using UnityEngine;
77

88
namespace MLAPI.NetworkingManagerComponents.Binary
99
{
@@ -47,7 +47,7 @@ public static byte[] Serialize<T>(T instance)
4747
FieldType fieldType = FieldTypeHelper.GetFieldType(sortedFields[i].FieldType);
4848
if (fieldType == FieldType.Invalid)
4949
{
50-
Debug.LogWarning("MLAPI: The field " + sortedFields[i].Name + " will not be serialized as it's not of a supported type. Add the BinaryIgnore attribute to prevent this message from shwoing up.");
50+
LogHelper.LogWarning("MLAPI: The field " + sortedFields[i].Name + " will not be serialized as it's not of a supported type. Add the BinaryIgnore attribute to prevent this message from shwoing up.", LogLevel.Normal);
5151
continue;
5252
}
5353
FieldTypeHelper.WriteFieldType(writer, sortedFields[i].GetValue(instance), fieldType);
@@ -83,7 +83,7 @@ public static byte[] Serialize<T>(T instance)
8383
FieldType fieldType = FieldTypeHelper.GetFieldType(sortedFields[i].FieldType);
8484
if (fieldType == FieldType.Invalid)
8585
{
86-
Debug.LogWarning("MLAPI: The field " + sortedFields[i].Name + " will not be deserialized as it's not of a supported type. Add the BinaryIgnore attribute to prevent this message from shwoing up.");
86+
LogHelper.LogWarning("MLAPI: The field " + sortedFields[i].Name + " will not be deserialized as it's not of a supported type. Add the BinaryIgnore attribute to prevent this message from shwoing up.", LogLevel.Normal);
8787
continue;
8888
}
8989
sortedFields[i].SetValue(instance, FieldTypeHelper.ReadFieldType(reader, fieldType));
@@ -117,7 +117,7 @@ public static byte[] Serialize<T>(T instance)
117117
FieldType fieldType = FieldTypeHelper.GetFieldType(sortedFields[i].FieldType);
118118
if (fieldType == FieldType.Invalid)
119119
{
120-
Debug.LogWarning("MLAPI: The field " + sortedFields[i].Name + " will not be deserialized as it's not of a supported type. Add the BinaryIgnore attribute to prevent this message from shwoing up.");
120+
LogHelper.LogWarning("MLAPI: The field " + sortedFields[i].Name + " will not be deserialized as it's not of a supported type. Add the BinaryIgnore attribute to prevent this message from shwoing up.", LogLevel.Normal);
121121
continue;
122122
}
123123
sortedFields[i].SetValue(instance, FieldTypeHelper.ReadFieldType(reader, fieldType));

MLAPI/NetworkingManagerComponents/Binary/BitReader.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#pragma warning disable CS1591 // Missing XML comment for publicly visible type or member
2+
using MLAPI.NetworkingManagerComponents.Core;
23
using System;
34
using System.Collections.Generic;
45
using System.Runtime.InteropServices;
@@ -31,7 +32,7 @@ public static BitReader Get(byte[] readFrom)
3132
if (readerPool.Count == 0)
3233
{
3334
if (pools > 10)
34-
Debug.LogWarning("MLAPI: There are more than 10 BitReaders. Have you forgotten do dispose? (More readers hurt performance)");
35+
LogHelper.LogWarning("MLAPI: There are more than 10 BitReaders. Have you forgotten do dispose? (More readers hurt performance)", LogLevel.Normal);
3536
BitReader reader = new BitReader(readFrom);
3637
pools++;
3738
return reader;

0 commit comments

Comments
 (0)