Skip to content

Commit 5e0a985

Browse files
committed
boost(AudioSwitcher): Improve the logging of the switching for Windows 11
1 parent b2e1de5 commit 5e0a985

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

SoundSwitch.Audio.Manager/Interop/Client/ExtendedPolicyClient.cs

+15-10
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Diagnostics;
43
using System.Runtime.InteropServices;
4+
using Serilog;
55
using SoundSwitch.Audio.Manager.Interop.Client.ClientException;
66
using SoundSwitch.Audio.Manager.Interop.Com.Base;
77
using SoundSwitch.Audio.Manager.Interop.Enum;
@@ -12,11 +12,17 @@ namespace SoundSwitch.Audio.Manager.Interop.Client
1212
{
1313
internal class ExtendedPolicyClient
1414
{
15-
private const string DEVINTERFACE_AUDIO_RENDER = "#{e6327cad-dcec-4949-ae8a-991e976a79d2}";
15+
private const string DEVINTERFACE_AUDIO_RENDER = "#{e6327cad-dcec-4949-ae8a-991e976a79d2}";
1616
private const string DEVINTERFACE_AUDIO_CAPTURE = "#{2eef81be-33fa-4800-9670-1cd474972c3f}";
17-
private const string MMDEVAPI_TOKEN = @"\\?\SWD#MMDEVAPI#";
17+
private const string MMDEVAPI_TOKEN = @"\\?\SWD#MMDEVAPI#";
1818

1919
private IAudioPolicyConfigFactory _sharedPolicyConfig;
20+
private readonly ILogger _log;
21+
22+
public ExtendedPolicyClient()
23+
{
24+
_log = Log.ForContext(GetType());
25+
}
2026

2127
private IAudioPolicyConfigFactory PolicyConfig
2228
{
@@ -38,18 +44,17 @@ private static string GenerateDeviceId(string deviceId, EDataFlow flow)
3844

3945
private static string UnpackDeviceId(string deviceId)
4046
{
41-
if (deviceId.StartsWith(MMDEVAPI_TOKEN)) deviceId = deviceId.Remove(0, MMDEVAPI_TOKEN.Length);
42-
if (deviceId.EndsWith(DEVINTERFACE_AUDIO_RENDER)) deviceId = deviceId.Remove(deviceId.Length - DEVINTERFACE_AUDIO_RENDER.Length);
47+
if (deviceId.StartsWith(MMDEVAPI_TOKEN)) deviceId = deviceId.Remove(0, MMDEVAPI_TOKEN.Length);
48+
if (deviceId.EndsWith(DEVINTERFACE_AUDIO_RENDER)) deviceId = deviceId.Remove(deviceId.Length - DEVINTERFACE_AUDIO_RENDER.Length);
4349
if (deviceId.EndsWith(DEVINTERFACE_AUDIO_CAPTURE)) deviceId = deviceId.Remove(deviceId.Length - DEVINTERFACE_AUDIO_CAPTURE.Length);
4450
return deviceId;
4551
}
4652

4753
public void SetDefaultEndPoint(string deviceId, EDataFlow flow, IEnumerable<ERole> roles, uint processId)
4854
{
49-
Trace.WriteLine($"ExtendedPolicyClient SetDefaultEndPoint {deviceId} [{flow}] {processId}");
55+
_log.Information("ExtendedPolicyClient SetDefaultEndPoint {DeviceId} [{Flow}] {ProcessId}", deviceId, flow, processId);
5056
try
5157
{
52-
5358
if (string.IsNullOrEmpty(deviceId))
5459
{
5560
return;
@@ -67,7 +72,7 @@ public void SetDefaultEndPoint(string deviceId, EDataFlow flow, IEnumerable<ERol
6772
}
6873
catch (Exception ex)
6974
{
70-
Trace.WriteLine($"{ex}");
75+
_log.Error(ex, "Can't set the default endpoint");
7176
}
7277
}
7378

@@ -85,7 +90,7 @@ public string GetDefaultEndPoint(EDataFlow flow, ERole role, uint processId)
8590
}
8691
catch (Exception ex)
8792
{
88-
Trace.WriteLine($"{ex}");
93+
_log.Error(ex, "Can't get the default enpoint");
8994
}
9095

9196
return null;
@@ -99,7 +104,7 @@ public void ResetAllSetEndpoint()
99104
}
100105
catch (Exception ex)
101106
{
102-
Trace.WriteLine($"{ex}");
107+
_log.Error(ex, "Can't reset all endpoints");
103108
}
104109
}
105110
}

0 commit comments

Comments
 (0)