Skip to content

Commit c69dc4a

Browse files
authored
Refactor logging to use structured logging patterns (#1671)
Replaced string interpolation in logger calls with structured logging, removed redundant ToString() calls, and simplified string formatting throughout. Improved consistency and readability in log and exception messages.
1 parent 3483882 commit c69dc4a

4 files changed

Lines changed: 13 additions & 18 deletions

File tree

src/SIPSorceryMedia.FFmpeg/FFmpegCameraSource.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public unsafe FFmpegCameraSource(Camera camera)
4242
string inputFormat = RuntimeInformation.IsOSPlatform(OSPlatform.Windows) ? "dshow"
4343
: RuntimeInformation.IsOSPlatform(OSPlatform.Linux) ? "v4l2"
4444
: RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "avfoundation"
45-
: throw new NotSupportedException($"Cannot find adequate input format" +
46-
$" - OSArchitecture:[{RuntimeInformation.OSArchitecture}]" +
47-
$" - OSDescription:[{RuntimeInformation.OSDescription}]");
45+
: throw new NotSupportedException($"Cannot find adequate input format - OSArchitecture:[{RuntimeInformation.OSArchitecture}] - OSDescription:[{RuntimeInformation.OSDescription}]");
4846

4947
var _aVInputFormat = ffmpeg.av_find_input_format(inputFormat);
5048

@@ -126,7 +124,7 @@ public bool RestrictCameraOptions(Func<Dictionary<string, string>, bool> optFilt
126124
);
127125

128126
if (filtered is null)
129-
logger.LogWarning($"No camera/input device options to be used.");
127+
logger.LogWarning("No camera/input device options to be used.");
130128

131129
return SetCameraDeviceOptions(filtered);
132130
}

src/SIPSorceryMedia.FFmpeg/FFmpegScreenSource.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public unsafe FFmpegScreenSource(string path, Rectangle rect, int frameRate = 20
2828
{
2929
["offset_x"] = rect.X.ToString(),
3030
["offset_y"] = rect.Y.ToString(),
31-
["video_size"] = $"{rect.Width.ToString()}X{rect.Height.ToString()}",
31+
["video_size"] = $"{rect.Width}X{rect.Height}",
3232
["framerate"] = frameRate.ToString()
3333
};
3434
}
@@ -37,7 +37,7 @@ public unsafe FFmpegScreenSource(string path, Rectangle rect, int frameRate = 20
3737
inputFormat = "avfoundation";
3838
options = new Dictionary<string, string>()
3939
{
40-
["vf"] = $"crop={rect.Width.ToString()}:{rect.Height.ToString()}:{rect.X.ToString()}:{rect.Y.ToString()}",
40+
["vf"] = $"crop={rect.Width}:{rect.Height}:{rect.X}:{rect.Y}",
4141
["framerate"] = frameRate.ToString()
4242
};
4343
}
@@ -47,7 +47,7 @@ public unsafe FFmpegScreenSource(string path, Rectangle rect, int frameRate = 20
4747
//https://superuser.com/questions/1562228/how-to-specify-the-size-to-record-the-screen-with-ffmpeg
4848
options = new Dictionary<string, string>()
4949
{
50-
["video_size"] = $"{rect.Width.ToString()}X{rect.Height.ToString()}",
50+
["video_size"] = $"{rect.Width}X{rect.Height}",
5151
["grab_x"] = rect.X.ToString(),
5252
["grab_y"] = rect.Y.ToString(),
5353
["framerate"] = frameRate.ToString()

src/SIPSorceryMedia.FFmpeg/FfmpegInit.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public static void Initialise(FfmpegLogLevelEnum? logLevel = null, String? libPa
8383

8484
RegisterFFmpegBinaries(libPath);
8585

86-
logger.LogInformation($"FFmpeg version info: {ffmpeg.av_version_info()}");
86+
logger.LogInformation("FFmpeg version info: {VersionInfo}", ffmpeg.av_version_info());
8787

8888
if (logLevel.HasValue)
8989
{
@@ -105,10 +105,7 @@ internal static void SetFFmpegBinariesPath(string path)
105105
catch (Exception e)
106106
{
107107
throw new DllNotFoundException(
108-
"Check the dependencies of FFmpeg libraries and make sure they are " +
109-
"searchable by the operating system's library loader."
110-
+ "\nOn linux you can use 'ldd' & 'strace'."
111-
+ "\nOn Windows you can use 'Dependencies'."
108+
$"Check the dependencies of FFmpeg libraries and make sure they are searchable by the operating system's library loader.\nOn linux you can use 'ldd' & 'strace'.\nOn Windows you can use 'Dependencies'."
112109
, e);
113110
}
114111
}
@@ -124,12 +121,12 @@ internal static void RegisterFFmpegBinaries(String? libPath = null)
124121
string ffmpegExecutable = "ffmpeg";
125122
string? path = Environment.GetEnvironmentVariable("PATH")?
126123
.Split([';'], StringSplitOptions.RemoveEmptyEntries)
127-
.Where(s => File.Exists(Path.Combine(s, ffmpegExecutable)) || File.Exists(Path.Combine(s, ffmpegExecutable + ".exe")))
124+
.Where(s => File.Exists(Path.Combine(s, ffmpegExecutable)) || File.Exists(Path.Combine(s, $"{ffmpegExecutable}.exe")))
128125
.FirstOrDefault();
129126

130127
if (path != null)
131128
{
132-
logger.LogInformation($"FFmpeg binaries found in system path at: {path}");
129+
logger.LogInformation("FFmpeg binaries found in system path at: {Path}", path);
133130
SetFFmpegBinariesPath(path);
134131
return;
135132
}
@@ -142,7 +139,7 @@ internal static void RegisterFFmpegBinaries(String? libPath = null)
142139
var ffmpegBinaryPath = Path.Combine(current, probe);
143140
if (Directory.Exists(ffmpegBinaryPath))
144141
{
145-
logger.LogInformation($"FFmpeg binaries found in: {ffmpegBinaryPath}");
142+
logger.LogInformation("FFmpeg binaries found in: {Path}", ffmpegBinaryPath);
146143
SetFFmpegBinariesPath(ffmpegBinaryPath);
147144
return;
148145
}
@@ -154,7 +151,7 @@ internal static void RegisterFFmpegBinaries(String? libPath = null)
154151
{
155152
if (Directory.Exists(libPath))
156153
{
157-
logger.LogInformation($"FFmpeg binaries path set to: {libPath}");
154+
logger.LogInformation("FFmpeg binaries path set to: {Path}", libPath);
158155
SetFFmpegBinariesPath(libPath);
159156
return;
160157
}

src/SIPSorceryMedia.FFmpeg/Interop/MacOs/AvFoundation.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private static unsafe String GetAvFoundationLogsAboutDevicesList()
8585
{
8686
index = ln.IndexOf("]");
8787
string name = ln.Substring(index + 2);
88-
string path = ln.Substring(1, index - 1) + ":";
88+
string path = $"{ln.Substring(1, index - 1)}:";
8989

9090
Monitor monitor = new Monitor
9191
{
@@ -149,7 +149,7 @@ private static unsafe String GetAvFoundationLogsAboutDevicesList()
149149
{
150150
index = ln.IndexOf("]");
151151
string name = ln.Substring(index+2);
152-
string path = ln.Substring(1, index - 1) + ":";
152+
string path = $"{ln.Substring(1, index - 1)}:";
153153

154154
Camera camera = new Camera
155155
{

0 commit comments

Comments
 (0)