Skip to content

Commit

Permalink
Add a bunch more logging
Browse files Browse the repository at this point in the history
  • Loading branch information
ChadNedzlek committed Jan 17, 2024
1 parent 4694f59 commit 4bdbd3a
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 23 deletions.
15 changes: 12 additions & 3 deletions CmdLine/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> saved = null;
Expand All @@ -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<PixelsDie> found = new();
try
Expand Down
13 changes: 0 additions & 13 deletions Pixels.Net.sln.DotSettings.user
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ABatteryLevelMessage_002Ecs_002Fl_003APixels_002ENet_003FMessages_003FBatteryLevelMessage_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ABleManager_002Ecs_002Fl_003APixels_002ENet_003FBle_003FBleManager_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003ANativeMethods_002Ecs_002Fl_003APixels_002ENet_003FInterop_003FNativeMethods_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003APixelsManager_002Ecs_002Fl_003APixels_002ENet_003FPixelsManager_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=7020124F_002D9FFC_002D4AC3_002D8F3D_002DAAB8E0240759_002Ff_003AProgram_002Ecs_002Fl_003ACmdLine_003FProgram_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=71189CE0_002D65D3_002D44C4_002D90CB_002D68337FA73254_002Ff_003AMainWindow_002Examl_002Ecs/@EntryIndexedValue">ExplicitlyExcluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=9FD9636E_002DFAAE_002D48A0_002D9327_002DF617324219B6_002Ff_003AProgram_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=C663A62D_002D72B1_002D4545_002D966A_002D206D276F8542_002Fd_003ABle_002Ff_003ABleAdapter_002Ecs/@EntryIndexedValue">ExplicitlyExcluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=C663A62D_002D72B1_002D4545_002D966A_002D206D276F8542_002Fd_003ABle_002Ff_003ABleManager_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=C663A62D_002D72B1_002D4545_002D966A_002D206D276F8542_002Fd_003AInterop_002Ff_003ABleUuid_002Ecs/@EntryIndexedValue">ExplicitlyExcluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=C663A62D_002D72B1_002D4545_002D966A_002D206D276F8542_002Fd_003AInterop_002Ff_003ANativeMethods_002Ecs/@EntryIndexedValue">ExplicitlyExcluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=C663A62D_002D72B1_002D4545_002D966A_002D206D276F8542_002Ff_003APixelsManager_002Ecs/@EntryIndexedValue">ForceIncluded</s:String>
<s:String x:Key="/Default/CodeInspection/ExcludedFiles/FilesAndFoldersToSkip2/=C663A62D_002D72B1_002D4545_002D966A_002D206D276F8542_002Ff_003ARollState_002Ecs/@EntryIndexedValue">ExplicitlyExcluded</s:String>
<s:String x:Key="/Default/Environment/AssemblyExplorer/XmlDocument/@EntryValue">&lt;AssemblyExplorer&gt;&#xD;
&lt;Assembly Path="C:\Users\chadnedz\repos\Pxiels.Net\Pixels.Net\simpleble-c.dll" /&gt;&#xD;
&lt;/AssemblyExplorer&gt;</s:String>
Expand Down
5 changes: 3 additions & 2 deletions VaettirNet.PixelsDice.Net/Ble/BleManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,20 @@ 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)
{
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)
Expand Down
1 change: 1 addition & 0 deletions VaettirNet.PixelsDice.Net/Ble/Dispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<DispatchRecord>();
_readyEvent = new AutoResetEvent(false);
var t = new Thread(ExecuteQueue)
Expand Down
55 changes: 55 additions & 0 deletions VaettirNet.PixelsDice.Net/Logger.cs
Original file line number Diff line number Diff line change
@@ -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);
}
}
55 changes: 50 additions & 5 deletions VaettirNet.PixelsDice.Net/PixelsManager.cs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -91,18 +93,32 @@ 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();
NativeMethods.GetService(peri, i, ref service).CheckSuccess();
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}");
}
}

Expand All @@ -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;
Expand All @@ -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
{
Expand All @@ -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();
}

Expand All @@ -172,4 +206,15 @@ public void Dispose()
{
_adapter?.Dispose();
}
}

public enum PixelsLogLevel
{
None = 0,
Fatal,
Error,
Warn,
Info,
Debug,
Verbose
}

0 comments on commit 4bdbd3a

Please sign in to comment.