Skip to content

Commit

Permalink
implement debug helper (and temporary fix) for #142
Browse files Browse the repository at this point in the history
  • Loading branch information
radj307 committed Dec 13, 2023
1 parent 91c1ba0 commit 1e1bf1b
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 2 deletions.
11 changes: 9 additions & 2 deletions VolumeControl.CoreAudio/AudioDeviceManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,14 @@ public AudioDeviceManager(DataFlow deviceDataFlow, MMDeviceEnumerator deviceEnum
// initialize devices
foreach (var mmDevice in _deviceEnumerator.EnumerateAudioEndPoints(DeviceDataFlow, DeviceState.Active))
{
CreateAndAddDeviceIfUnique(mmDevice);
try
{
CreateAndAddDeviceIfUnique(mmDevice);
}
catch (Exception ex)
{
FLog.Critical("An exception occurred while initializing an AudioDevice!", Log.Helpers.ObjectDebugger.ReflectionDump(mmDevice), ex);
}
}
}
/// <summary>
Expand Down Expand Up @@ -160,7 +167,7 @@ private bool RemoveDevice(AudioDevice device)
if (_devices.Remove(device))
{
NotifyDeviceRemovedFromList(device);

device.Dispose();

return true;
Expand Down
98 changes: 98 additions & 0 deletions VolumeControl.Log/Helpers/DumpObject.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System.Text;

namespace VolumeControl.Log.Helpers
{
public static class ObjectDebugger
{
[Flags]
public enum DumpContents
{
None = 0,
PublicProperties = 1,
NonPublicProperties = 2,
Properties = PublicProperties | NonPublicProperties,

PublicFields = 4,
NonPublicFields = 8,
Fields = PublicProperties | NonPublicFields,

All = Properties | Fields,
}
public static string ReflectionDump(object obj, DumpContents includes = DumpContents.All)
{
var type = obj.GetType();

var sb = new StringBuilder();

var objString = obj.ToString();
var typeString = type.ToString();

sb.AppendLine($"Object: {objString}{(objString?.Equals(typeString, StringComparison.Ordinal) ?? false ? string.Empty : $" ({typeString})")}");
sb.AppendLine();
// PROPERTIES
if (includes.HasFlag(DumpContents.PublicProperties))
{
sb.AppendLine(" Public Properties:");
foreach (var publicProperty in type.GetProperties(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
{
try
{
sb.AppendLine($" \"{publicProperty.Name}\": \"{publicProperty.GetValue(obj)}\"");
}
catch (Exception ex)
{
sb.AppendLine($" \"{publicProperty.Name}\": ( [GETTER EXCEPTION] {ex.ToString().ReplaceLineEndings(" [\\n] ")} )");
}
}
}
if (includes.HasFlag(DumpContents.NonPublicProperties))
{
sb.AppendLine(" Non-Public Properties:");
foreach (var nonPublicProperty in type.GetProperties(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance))
{
try
{
sb.AppendLine($" \"{nonPublicProperty.Name}\": \"{nonPublicProperty.GetValue(obj)}\"");
}
catch (Exception ex)
{
sb.AppendLine($" \"{nonPublicProperty.Name}\": ( [GETTER EXCEPTION] {ex.ToString().ReplaceLineEndings(" [\\n] ")} )");
}
}
}
// FIELDS
if (includes.HasFlag(DumpContents.PublicFields))
{
sb.AppendLine(" Public Fields:");
foreach (var publicField in type.GetFields(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance))
{
try
{
sb.AppendLine($" \"{publicField.Name}\": \"{publicField.GetValue(obj)}\"");
}
catch (Exception ex)
{
sb.AppendLine($" \"{publicField.Name}\": ( [GETTER EXCEPTION] {ex.ToString().ReplaceLineEndings(" [\\n] ")} )");
}
}
}
if (includes.HasFlag(DumpContents.NonPublicFields))
{
sb.AppendLine(" Non-Public Fields:");
foreach (var nonPublicField in type.GetFields(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance))
{
try
{
sb.AppendLine($" \"{nonPublicField.Name}\": \"{nonPublicField.GetValue(obj)}\"");
}
catch (Exception ex)
{
sb.AppendLine($" \"{nonPublicField.Name}\": ( [GETTER EXCEPTION] {ex.ToString().ReplaceLineEndings(" [\\n] ")} )");
}
}
}

return sb.ToString();
}
}
}

0 comments on commit 1e1bf1b

Please sign in to comment.