|
1 | 1 | // SPDX-FileCopyrightText: 2022 smdn <smdn@smdn.jp> |
2 | 2 | // SPDX-License-Identifier: MIT |
3 | 3 | using System; |
| 4 | +using System.Collections.Generic; |
4 | 5 | using System.Diagnostics; |
5 | 6 | using System.IO; |
6 | 7 | using System.Linq; |
@@ -75,23 +76,28 @@ protected override async ValueTask ArpScanAsyncCore(CancellationToken cancellati |
75 | 76 | nmapProcess.WaitForExit(); // TODO: cacellation |
76 | 77 | #endif |
77 | 78 |
|
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); |
85 | 87 | } |
86 | | - } |
87 | 88 |
|
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 | + } |
95 | 101 | } |
96 | 102 | } |
97 | 103 | } |
|
0 commit comments