Skip to content

Commit

Permalink
Merge pull request #53 from osmanhadzic/implement-logging
Browse files Browse the repository at this point in the history
Implement logging
  • Loading branch information
harlem88 authored Nov 12, 2024
2 parents 5b3ad56 + 1a345e4 commit 8f076cd
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 20 deletions.
4 changes: 2 additions & 2 deletions AstarteDeviceSDKCSharp/Device/AstarteDevice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ public async Task Connect()
}
catch (AstarteCryptoException)
{
Trace.WriteLine("Regenerating the cert");
AstarteLogger.Debug("Regenerating the cert", this.GetType().Name);
try
{
await _pairingHandler.RequestNewCertificate();
Expand Down Expand Up @@ -395,7 +395,7 @@ public void OnTransportConnectionError(Exception ex)
{
if (ex is AstarteCryptoException)
{
Trace.WriteLine("Regenerating the cert");
AstarteLogger.Debug("Regenerating the cert", this.GetType().Name);
try
{
Task.Run(() => _pairingHandler.RequestNewCertificate());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,9 @@ public void ValidatePayload(string path, Dictionary<string, object> payload, Dat

if (astarteInterfaceMapping.Path is null)
{
Trace.WriteLine("Astarte mapping path " +
AstarteLogger.Warn("Astarte mapping path " +
"{" + astarteInterfaceMapping.Path + " } "
+ " is null.");
+ " is null.", this.GetType().Name);
continue;
}
if (!payload.ContainsKey(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void SetProperty(string path, object payload)
}
catch (AstartePropertyStorageException e)
{
Trace.WriteLine(e.Message);
AstarteLogger.Error(e.Message, this.GetType().Name);
}

if (!(storedValue?.PayloadEquality(payload) ?? false))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public void RemoveListener(IAstarteAggregateDatastreamEventListener listener)
{
if (serverValue is null)
{
Trace.WriteLine("Unable to build AstarteServerValue, serverValue was empty");
AstarteLogger.Error("Unable to build AstarteServerValue, serverValue was empty", this.GetType().Name);
return null;
}

Expand All @@ -63,7 +63,7 @@ public void RemoveListener(IAstarteAggregateDatastreamEventListener listener)

if (astartePayload is null)
{
Trace.WriteLine("Unable to build AstarteServerValue, astartePayload was empty");
AstarteLogger.Error("Unable to build AstarteServerValue, astartePayload was empty", this.GetType().Name);
return null;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void RemoveListener(IAstarteDatastreamEventListener listener)
AstarteInterfaceMapping? targetMapping = null;
if (serverValue is null)
{
Trace.WriteLine($"Unable to build AstarteServerValue, astartePayload was empty");
AstarteLogger.Error("Unable to build AstarteServerValue, astartePayload was empty", this.GetType().Name);
return null;
}

Expand Down Expand Up @@ -87,8 +87,9 @@ public void RemoveListener(IAstarteDatastreamEventListener listener)
}
else
{
Trace.WriteLine($"Got an unexpected path {interfacePath}"
+ "for interface {GetInterfaceName()}");
AstarteLogger.Warn($"Got an unexpected path {interfacePath}"
+ "for interface {GetInterfaceName()}", this.GetType().Name);

}
return astarteServerValue;
}
Expand Down
20 changes: 10 additions & 10 deletions AstarteDeviceSDKCSharp/Transport/MQTT/AstarteMqttTransport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ async Task OnConnectedAsync(MqttClientConnectedEventArgs args)
}
else
{
Trace.WriteLine("Transport Connected");
AstarteLogger.Info("Transport Connected", this.GetType().Name);
}

}
Expand Down Expand Up @@ -276,7 +276,7 @@ async Task OnDisconnectAsync(MqttClientDisconnectedEventArgs e)
}
else
{
Trace.WriteLine("The Connection was lost.");
AstarteLogger.Error("The Connection was lost.", this.GetType().Name);
}

}
Expand All @@ -285,8 +285,8 @@ private void OnMessageReceive(MqttApplicationMessageReceivedEventArgs e)
{
object? payload = null;

Trace.WriteLine("Incoming message: "
+ Encoding.UTF8.GetString(e.ApplicationMessage.Payload ?? new byte[0]));
AstarteLogger.Debug("Incoming message: "
+ Encoding.UTF8.GetString(e.ApplicationMessage.Payload ?? new byte[0]), this.GetType().Name);

if (!e.ApplicationMessage.Topic.Contains(_connectionInfo.GetClientId())
|| _messageListener == null)
Expand All @@ -313,7 +313,7 @@ private void OnMessageReceive(MqttApplicationMessageReceivedEventArgs e)
}
else
{
Trace.WriteLine("Unhandled control message!" + path);
AstarteLogger.Error("Unhandled control message!" + path, this.GetType().Name);
}
return;
}
Expand All @@ -323,7 +323,7 @@ private void OnMessageReceive(MqttApplicationMessageReceivedEventArgs e)

if (!astarteDevice.HasInterface(astarteInterface))
{
Trace.WriteLine("Got an unexpected interface!" + astarteInterface);
AstarteLogger.Warn("Got an unexpected interface!" + astarteInterface, this.GetType().Name);
return;
}

Expand All @@ -335,7 +335,7 @@ private void OnMessageReceive(MqttApplicationMessageReceivedEventArgs e)
decodedMessage = AstartePayload.Deserialize(e.ApplicationMessage.Payload);
if (decodedMessage is null)
{
Trace.WriteLine("Unable to get payload, decodedMessage was null");
AstarteLogger.Warn("Unable to get payload, decodedMessage was null ", this.GetType().Name);
return;
}
payload = decodedMessage.GetPayload();
Expand All @@ -362,7 +362,7 @@ private void OnMessageReceive(MqttApplicationMessageReceivedEventArgs e)

if (astarteServerValue == null)
{
Trace.WriteLine("Unable to get value, astarteServerValue was null");
AstarteLogger.Error("Unable to get value, astarteServerValue was null", this.GetType().Name);
return;
}

Expand All @@ -389,7 +389,7 @@ private void OnMessageReceive(MqttApplicationMessageReceivedEventArgs e)
}
catch (AstartePropertyStorageException ex)
{
Trace.WriteLine(ex.Message);
AstarteLogger.Error(ex.Message, this.GetType().Name);
}
}

Expand Down Expand Up @@ -434,7 +434,7 @@ private void HandlePurgeProperties(byte[] payload, AstarteDevice? astarteDevice)
}
catch (IOException e)
{
Trace.WriteLine(e.Message);
AstarteLogger.Error(e.Message, this.GetType().Name);
}

string purgePropertiesPayload = result.ToString();
Expand Down
59 changes: 59 additions & 0 deletions AstarteDeviceSDKCSharp/Utilities/Logger/AstarteLogger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* This file is part of Astarte.
*
* Copyright 2023-2024 SECO Mind Srl
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/

using System.Diagnostics;

public static class AstarteLogger
{
public static void Info(string message, string className)
{
Trace.TraceInformation(CreateMessage(message, className, AstarteLogLevel.Info));
}

public static void Error(string message, string className)
{
Trace.TraceInformation(CreateMessage(message, className, AstarteLogLevel.Error));
}

public static void Warn(string message, string className)
{
Trace.TraceInformation(CreateMessage(message, className, AstarteLogLevel.Warn));
}

public static void Debug(string message, string className)
{
Trace.TraceInformation(CreateMessage(message, className, AstarteLogLevel.Debug));
}

private static string CreateMessage(string message, string className, AstarteLogLevel loggerLevel)
{
return String.Format("{0} : {1} : {2} : {3}", DateTime.Now.ToString("MM/dd/yyyy HH:mm:ss"),
loggerLevel, className, message);
}
}

enum AstarteLogLevel
{
Info,
Error,
Warn,
Debug
}

0 comments on commit 8f076cd

Please sign in to comment.