Skip to content

Commit cfc5b91

Browse files
committed
simplify logging
1 parent e1f2c35 commit cfc5b91

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

src/Smdn.Net.AddressResolution/Smdn.Net.AddressResolution.Arp/ProcfsArpNmapScanMacAddressResolver.cs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// SPDX-FileCopyrightText: 2022 smdn <smdn@smdn.jp>
22
// SPDX-License-Identifier: MIT
33
using System;
4+
using System.Collections.Generic;
45
using System.Diagnostics;
56
using System.IO;
67
using System.Linq;
@@ -75,23 +76,28 @@ protected override async ValueTask ArpScanAsyncCore(CancellationToken cancellati
7576
nmapProcess.WaitForExit(); // TODO: cacellation
7677
#endif
7778

78-
if (Logger is not null && Logger.IsEnabled(LogLevel.Trace)) {
79-
for (
80-
var line = await nmapProcess.StandardOutput.ReadLineAsync().ConfigureAwait(false);
81-
line is not null;
82-
line = await nmapProcess.StandardOutput.ReadLineAsync().ConfigureAwait(false)
83-
) {
84-
Logger!.LogTrace("[nmap] {StdOut}", line);
79+
if (Logger is not null) {
80+
const LogLevel logLevelForStandardOutput = LogLevel.Trace;
81+
const LogLevel logLevelForStandardError = LogLevel.Error;
82+
83+
static IEnumerable<(StreamReader, LogLevel)> EnumerateLogTarget(StreamReader stdout, StreamReader stderr)
84+
{
85+
yield return (stdout, logLevelForStandardOutput);
86+
yield return (stderr, logLevelForStandardError);
8587
}
86-
}
8788

88-
if (Logger is not null && Logger.IsEnabled(LogLevel.Error)) {
89-
for (
90-
var line = await nmapProcess.StandardError.ReadLineAsync().ConfigureAwait(false);
91-
line is not null;
92-
line = await nmapProcess.StandardError.ReadLineAsync().ConfigureAwait(false)
93-
) {
94-
Logger!.LogError("[nmap] {StdErr}", line);
89+
foreach (var (stdio, logLevel) in EnumerateLogTarget(nmapProcess.StandardOutput, nmapProcess.StandardError)) {
90+
if (!Logger.IsEnabled(logLevel))
91+
continue;
92+
93+
for (; ;) {
94+
var line = await stdio.ReadLineAsync().ConfigureAwait(false);
95+
96+
if (line is null)
97+
break;
98+
99+
Logger.Log(logLevel, "[nmap] {Line}", line);
100+
}
95101
}
96102
}
97103
}

0 commit comments

Comments
 (0)