aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Telemetry/Sources/TelemetryLogsStreamingSource.cs
diff options
context:
space:
mode:
authorRoy Ben Shabat <Roy.mail.net@gmail.com>2025-08-16 15:06:07 +0300
committerRoy Ben Shabat <Roy.mail.net@gmail.com>2025-08-16 15:06:07 +0300
commite9f317178f6454836d488cf1e65d04b20b02d334 (patch)
tree1347ae729401d2c5a136afb14a8b6ae8b74d0d9b /Software/Visual_Studio/Tango.Telemetry/Sources/TelemetryLogsStreamingSource.cs
parent25933e3fd940dbf89b1924383feabb3a5619846d (diff)
downloadTango-e9f317178f6454836d488cf1e65d04b20b02d334.tar.gz
Tango-e9f317178f6454836d488cf1e65d04b20b02d334.zip
Telemetry Logs.
Diffstat (limited to 'Software/Visual_Studio/Tango.Telemetry/Sources/TelemetryLogsStreamingSource.cs')
-rw-r--r--Software/Visual_Studio/Tango.Telemetry/Sources/TelemetryLogsStreamingSource.cs99
1 files changed, 99 insertions, 0 deletions
diff --git a/Software/Visual_Studio/Tango.Telemetry/Sources/TelemetryLogsStreamingSource.cs b/Software/Visual_Studio/Tango.Telemetry/Sources/TelemetryLogsStreamingSource.cs
new file mode 100644
index 000000000..8001caac8
--- /dev/null
+++ b/Software/Visual_Studio/Tango.Telemetry/Sources/TelemetryLogsStreamingSource.cs
@@ -0,0 +1,99 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using Tango.Integration.Operation;
+using Tango.Logging;
+using Tango.Telemetry.Telemetries;
+
+namespace Tango.Telemetry.Sources
+{
+ public class TelemetryLogsStreamingSource : TelemetryConfigurableSource<TelemetryLogsStreamingSourceConfig>, ITelemetryStreamingSource
+ {
+ private KeyValuePair<DateTime, String> _lastFirmwareMessage;
+
+ public IMachineOperator MachineOperator { get; }
+
+ public bool IsStarted { get; private set; }
+
+ public string Name { get; } = "Logs";
+
+ public bool RequiresTelemetryDuplicationTracking { get; }
+
+ public event EventHandler<TelemetryAvailableEventArgs> TelemetryAvailable;
+
+ public TelemetryLogsStreamingSource()
+ {
+ _lastFirmwareMessage = new KeyValuePair<DateTime, string>(DateTime.MinValue, String.Empty);
+ }
+
+ public TelemetryLogsStreamingSource(IMachineOperator machineOperator)
+ {
+ MachineOperator = machineOperator;
+ }
+
+ public void Start()
+ {
+ if (!IsStarted)
+ {
+ IsStarted = true;
+ LogManager.NewLog += LogManager_NewLog;
+ if (MachineOperator != null) MachineOperator.DebugLogAvailable += MachineOperator_DebugLogAvailable;
+ }
+ }
+
+ private void MachineOperator_DebugLogAvailable(object sender, PMR.Debugging.StartDebugLogResponse log)
+ {
+ if (IsStarted)
+ {
+ if (Config.Categories.Contains((LogCategory)log.Category)) return;
+
+ if (log.Message == _lastFirmwareMessage.Value && DateTime.UtcNow < _lastFirmwareMessage.Key.AddSeconds(1)) return;
+
+ _lastFirmwareMessage = new KeyValuePair<DateTime, string>(DateTime.UtcNow, log.Message);
+
+ TelemetryLog tLog = new TelemetryLog();
+ tLog.Time = DateTime.UtcNow;
+ tLog.Category = log.Category.ToString();
+ tLog.Class = log.FileName;
+ tLog.Method = $"[{log.ModuleId}] [{log.Filter}] [{log.Parameter}]";
+ tLog.Line = log.LineNumber;
+ tLog.Message = log.Message;
+ TelemetryAvailable?.Invoke(this, new TelemetryAvailableEventArgs() { TelemetryObject = tLog });
+ }
+ }
+
+ private void LogManager_NewLog(object sender, Logging.LogItemBase log)
+ {
+ if (IsStarted)
+ {
+ if (Config.Categories.Contains(log.Category)) return;
+
+ TelemetryLog tLog = new TelemetryLog();
+ tLog.Time = log.TimeStamp.ToUniversalTime();
+ tLog.Category = log.Category.ToString();
+ tLog.Class = log.ClassName;
+ tLog.Method = log.CallerMethodName;
+ tLog.Line = log.CallerLineNumber;
+ tLog.Message = log.Message;
+ TelemetryAvailable?.Invoke(this, new TelemetryAvailableEventArgs() { TelemetryObject = tLog });
+ }
+ }
+
+ public void Stop()
+ {
+ if (IsStarted)
+ {
+ IsStarted = false;
+ LogManager.NewLog -= LogManager_NewLog;
+ if (MachineOperator != null) MachineOperator.DebugLogAvailable -= MachineOperator_DebugLogAvailable;
+ }
+ }
+
+ public void Dispose()
+ {
+ Stop();
+ }
+ }
+}