From 4bdbd3aca190a302cda1bda6c2cdcbee3ed14e90 Mon Sep 17 00:00:00 2001 From: Chad Nedzlek Date: Wed, 17 Jan 2024 14:00:29 -0800 Subject: [PATCH] Add a bunch more logging --- CmdLine/Program.cs | 15 ++++-- Pixels.Net.sln.DotSettings.user | 13 ----- VaettirNet.PixelsDice.Net/Ble/BleManager.cs | 5 +- VaettirNet.PixelsDice.Net/Ble/Dispatcher.cs | 1 + VaettirNet.PixelsDice.Net/Logger.cs | 55 +++++++++++++++++++++ VaettirNet.PixelsDice.Net/PixelsManager.cs | 55 +++++++++++++++++++-- 6 files changed, 121 insertions(+), 23 deletions(-) create mode 100644 VaettirNet.PixelsDice.Net/Logger.cs diff --git a/CmdLine/Program.cs b/CmdLine/Program.cs index ce9c7af..72654ff 100644 --- a/CmdLine/Program.cs +++ b/CmdLine/Program.cs @@ -22,10 +22,10 @@ public static async Task Main(string[] args) exit.Cancel(true); }; - BleLogLevel logLevel = BleLogLevel.Error; + PixelsLogLevel logLevel = PixelsLogLevel.Error; OptionSet options = new() { - { "verbose|v", "Verbose logging", _ => logLevel = BleLogLevel.Verbose } + { "verbose|v", "Verbose logging", _ => logLevel = PixelsLogLevel.Verbose } }; var rem = options.Parse(args); List saved = null; @@ -34,7 +34,16 @@ public static async Task Main(string[] args) saved = rem; } - BleManager.SetLogLevel(logLevel); + if (saved != null) + { + Console.WriteLine("Command line specified the following device IDs (will not find others) :"); + foreach (var s in saved) + { + Console.WriteLine($" {s}"); + } + } + + Logger.SetLogLevel(logLevel); var mgr = PixelsManager.Create(); List found = new(); try diff --git a/Pixels.Net.sln.DotSettings.user b/Pixels.Net.sln.DotSettings.user index ddd05ee..5db059c 100644 --- a/Pixels.Net.sln.DotSettings.user +++ b/Pixels.Net.sln.DotSettings.user @@ -1,17 +1,4 @@  - ForceIncluded - ForceIncluded - ForceIncluded - ForceIncluded - ForceIncluded - ExplicitlyExcluded - ForceIncluded - ExplicitlyExcluded - ForceIncluded - ExplicitlyExcluded - ExplicitlyExcluded - ForceIncluded - ExplicitlyExcluded <AssemblyExplorer> <Assembly Path="C:\Users\chadnedz\repos\Pxiels.Net\Pixels.Net\simpleble-c.dll" /> </AssemblyExplorer> diff --git a/VaettirNet.PixelsDice.Net/Ble/BleManager.cs b/VaettirNet.PixelsDice.Net/Ble/BleManager.cs index f3c8711..0b14967 100644 --- a/VaettirNet.PixelsDice.Net/Ble/BleManager.cs +++ b/VaettirNet.PixelsDice.Net/Ble/BleManager.cs @@ -46,6 +46,7 @@ public static BleManager Create() #else BleLogLevel.None; #endif + private static void SimpleBleLog(BleLogLevel level, IntPtr pModule, IntPtr pFile, uint line, IntPtr pFunction, IntPtr pMessage) { if (level > _logLevel) @@ -53,12 +54,12 @@ private static void SimpleBleLog(BleLogLevel level, IntPtr pModule, IntPtr pFile return; } - string module = Marshal.PtrToStringAnsi(pModule); string file = Marshal.PtrToStringAnsi(pFile); string function = Marshal.PtrToStringAnsi(pFunction); string message = Marshal.PtrToStringAnsi(pMessage); - Console.WriteLine($"[{level}] {module}: {file}:{line} in {function}: {message}"); + // ReSharper disable once ExplicitCallerInfoArgument + Logger.Instance.Log((PixelsLogLevel)level, message, file, (int)line, function); } public static void SetLogLevel(BleLogLevel level) diff --git a/VaettirNet.PixelsDice.Net/Ble/Dispatcher.cs b/VaettirNet.PixelsDice.Net/Ble/Dispatcher.cs index c85427e..82f1d70 100644 --- a/VaettirNet.PixelsDice.Net/Ble/Dispatcher.cs +++ b/VaettirNet.PixelsDice.Net/Ble/Dispatcher.cs @@ -52,6 +52,7 @@ private void PrepareBackgroundThread() { if (_queue != null) return; + Logger.Instance.Log(PixelsLogLevel.Info, "Initiating background dispatcher thread to handle thread apartments"); _queue = new ConcurrentQueue(); _readyEvent = new AutoResetEvent(false); var t = new Thread(ExecuteQueue) diff --git a/VaettirNet.PixelsDice.Net/Logger.cs b/VaettirNet.PixelsDice.Net/Logger.cs new file mode 100644 index 0000000..585a9b7 --- /dev/null +++ b/VaettirNet.PixelsDice.Net/Logger.cs @@ -0,0 +1,55 @@ +using System; +using System.Runtime.CompilerServices; +using VaettirNet.PixelsDice.Net.Ble; +using VaettirNet.PixelsDice.Net.Interop; + +namespace VaettirNet.PixelsDice.Net; + +public class Logger +{ + public static Logger Instance { get; } = new Logger(); + + private static PixelsLogLevel _logLevel = +#if DEBUG + PixelsLogLevel.Error; +#else + PixelsLogLevel.None; +#endif + + public void Log(PixelsLogLevel level, + FormattableString message, + [CallerFilePath] string file = null, + [CallerLineNumber] int line = 0, + [CallerMemberName] string member = null) + { + if (!ShouldLog(level)) + { + return; + } + + // ReSharper disable once ExplicitCallerInfoArgument + Log(level, message.ToString(), file, line, member); + } + + public void Log(PixelsLogLevel level, + string message, + [CallerFilePath] string file = null, + [CallerLineNumber] int line = 0, + [CallerMemberName] string member = null) + { + if (!ShouldLog(level)) + { + return; + } + + Console.WriteLine($"[{level}] {file}:{line} in {member}: {message}"); + } + + public bool ShouldLog(PixelsLogLevel level) => level <= _logLevel; + + public static void SetLogLevel(PixelsLogLevel logLevel) + { + _logLevel = logLevel; + BleManager.SetLogLevel((BleLogLevel)(int)logLevel); + } +} \ No newline at end of file diff --git a/VaettirNet.PixelsDice.Net/PixelsManager.cs b/VaettirNet.PixelsDice.Net/PixelsManager.cs index 20baeb0..f465e18 100644 --- a/VaettirNet.PixelsDice.Net/PixelsManager.cs +++ b/VaettirNet.PixelsDice.Net/PixelsManager.cs @@ -1,7 +1,9 @@ using System; using System.Collections.Generic; +using System.IO; using System.Linq; using System.Runtime.CompilerServices; +using System.Security.Cryptography; using System.Threading; using System.Threading.Channels; using System.Threading.Tasks; @@ -91,8 +93,15 @@ private bool IsDie(SafePeripheralHandle peripheral) static bool? CouldBeDisconnectedDie(SafePeripheralHandle peri) { var count = NativeMethods.GetServiceCount(peri); + if (Logger.Instance.ShouldLog(PixelsLogLevel.Verbose)) + { + var id = NativeMethods.GetPeripheralIdentifier(peri); + var address = NativeMethods.GetPeripheralAddress(peri); + Logger.Instance.Log(PixelsLogLevel.Verbose, $"Found device (id: {id}, address: {address})"); + } + bool foundInformationService = false; - bool foundPixeslService = false; + bool foundPixelsServices = false; for (nuint i = 0; i < count; i++) { BleService service = new BleService(); @@ -100,9 +109,16 @@ private bool IsDie(SafePeripheralHandle peripheral) if (service.Uuid.Value == PixelsId.InfoServiceId) { foundInformationService = true; - } else if (service.Uuid.Value == PixelsId.PixelsServiceId) + Logger.Instance.Log(PixelsLogLevel.Info, "Found Info service"); + } + else if (service.Uuid.Value == PixelsId.PixelsServiceId) + { + foundPixelsServices = true; + Logger.Instance.Log(PixelsLogLevel.Info, "Found Pixels service"); + } + else { - foundPixeslService = true; + Logger.Instance.Log(PixelsLogLevel.Verbose, $"Found irrelevant service: {service.Uuid.Value}"); } } @@ -115,10 +131,15 @@ private bool IsDie(SafePeripheralHandle peripheral) if (data.DataLength == 5) { foundBasicManufacturerData = true; + Logger.Instance.Log(PixelsLogLevel.Info, "Found manufacture data with 5 bytes"); + } + else + { + Logger.Instance.Log(PixelsLogLevel.Verbose, $"Found manufacture data with incorrect length {data.DataLength} bytes"); } } - if (foundPixeslService && foundInformationService) + if (foundPixelsServices && foundInformationService) return true; if (foundInformationService && dataCount == 1 && foundBasicManufacturerData) return null; @@ -130,6 +151,7 @@ static bool ConnectAndCheck(SafePeripheralHandle peri) bool foundPixelsService = false; bool foundNotifyCharacteristic = false; bool foundWriteCharacteristic = false; + Logger.Instance.Log(PixelsLogLevel.Verbose, "Connecting to device to scan other services"); NativeMethods.ConnectPeripheral(peri).CheckSuccess(); try { @@ -140,19 +162,31 @@ static bool ConnectAndCheck(SafePeripheralHandle peri) NativeMethods.GetService(peri, i, ref service).CheckSuccess(); if (service.Uuid.Value == PixelsId.PixelsServiceId) { + Logger.Instance.Log(PixelsLogLevel.Info, "Found active Pixels service"); foundPixelsService = true; for (nuint c = 0; c < service.CharacteristicCount; c++) { if (service.Characteristics[c].Uuid.Value == PixelsId.NotifyCharacteristicId) + { + Logger.Instance.Log(PixelsLogLevel.Info, "Found notify characteristic"); foundNotifyCharacteristic = true; - if (service.Characteristics[c].Uuid.Value == PixelsId.WriteCharacteristic) + } else if (service.Characteristics[c].Uuid.Value == PixelsId.WriteCharacteristic) + { + Logger.Instance.Log(PixelsLogLevel.Info, "Found write characteristic"); foundWriteCharacteristic = true; + } + else + { + Logger.Instance.Log(PixelsLogLevel.Verbose, + $"Found irrelevant characteristic: {service.Characteristics[c].Uuid.Value}"); + } } } } } finally { + Logger.Instance.Log(PixelsLogLevel.Verbose, "Disconnecting device"); NativeMethods.DisconnectPeripheral(peri).CheckSuccess(); } @@ -172,4 +206,15 @@ public void Dispose() { _adapter?.Dispose(); } +} + +public enum PixelsLogLevel +{ + None = 0, + Fatal, + Error, + Warn, + Info, + Debug, + Verbose } \ No newline at end of file