C#: Changing logging of dotnet commands to Debug except for dotnet --info and friends.

This commit is contained in:
Michael Nebel
2024-04-15 10:24:47 +02:00
parent 72ffcf5f9c
commit ddfed6ea65
4 changed files with 24 additions and 21 deletions

View File

@@ -35,7 +35,7 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
private void Info() private void Info()
{ {
var res = dotnetCliInvoker.RunCommand("--info"); var res = dotnetCliInvoker.RunCommand("--info", silent: false);
if (!res) if (!res)
{ {
throw new Exception($"{dotnetCliInvoker.Exec} --info failed."); throw new Exception($"{dotnetCliInvoker.Exec} --info failed.");
@@ -91,13 +91,13 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
return dotnetCliInvoker.RunCommand(args); return dotnetCliInvoker.RunCommand(args);
} }
public IList<string> GetListedRuntimes() => GetResultList("--list-runtimes"); public IList<string> GetListedRuntimes() => GetResultList("--list-runtimes", null, false);
public IList<string> GetListedSdks() => GetResultList("--list-sdks"); public IList<string> GetListedSdks() => GetResultList("--list-sdks", null, false);
private IList<string> GetResultList(string args, string? workingDirectory = null) private IList<string> GetResultList(string args, string? workingDirectory = null, bool silent = true)
{ {
if (dotnetCliInvoker.RunCommand(args, workingDirectory, out var results)) if (dotnetCliInvoker.RunCommand(args, workingDirectory, out var results, silent))
{ {
return results; return results;
} }
@@ -316,4 +316,4 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
}); });
} }
} }
} }

View File

@@ -40,13 +40,13 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
return startInfo; return startInfo;
} }
private bool RunCommandAux(string args, string? workingDirectory, out IList<string> output) private bool RunCommandAux(string args, string? workingDirectory, out IList<string> output, bool silent)
{ {
var dirLog = string.IsNullOrWhiteSpace(workingDirectory) ? "" : $" in {workingDirectory}"; var dirLog = string.IsNullOrWhiteSpace(workingDirectory) ? "" : $" in {workingDirectory}";
logger.LogInfo($"Running {Exec} {args}{dirLog}"); logger.LogInfo($"Running {Exec} {args}{dirLog}");
var pi = MakeDotnetStartInfo(args, workingDirectory); var pi = MakeDotnetStartInfo(args, workingDirectory);
var threadId = Environment.CurrentManagedThreadId; var threadId = Environment.CurrentManagedThreadId;
void onOut(string s) => logger.LogInfo(s, threadId); void onOut(string s) => logger.Log(silent ? Severity.Debug : Severity.Info, s, threadId);
void onError(string s) => logger.LogError(s, threadId); void onError(string s) => logger.LogError(s, threadId);
var exitCode = pi.ReadOutput(out output, onOut, onError); var exitCode = pi.ReadOutput(out output, onOut, onError);
if (exitCode != 0) if (exitCode != 0)
@@ -57,13 +57,13 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
return true; return true;
} }
public bool RunCommand(string args) => public bool RunCommand(string args, bool silent) =>
RunCommandAux(args, null, out _); RunCommandAux(args, null, out _, silent);
public bool RunCommand(string args, out IList<string> output) => public bool RunCommand(string args, out IList<string> output, bool silent) =>
RunCommandAux(args, null, out output); RunCommandAux(args, null, out output, silent);
public bool RunCommand(string args, string? workingDirectory, out IList<string> output) => public bool RunCommand(string args, string? workingDirectory, out IList<string> output, bool silent) =>
RunCommandAux(args, workingDirectory, out output); RunCommandAux(args, workingDirectory, out output, silent);
} }
} }

View File

@@ -11,19 +11,22 @@ namespace Semmle.Extraction.CSharp.DependencyFetching
/// <summary> /// <summary>
/// Execute `dotnet <args>` and return true if the command succeeded, otherwise false. /// Execute `dotnet <args>` and return true if the command succeeded, otherwise false.
/// If `silent` is true the output of the command is logged as `debug` otherwise as `info`.
/// </summary> /// </summary>
bool RunCommand(string args); bool RunCommand(string args, bool silent = true);
/// <summary> /// <summary>
/// Execute `dotnet <args>` and return true if the command succeeded, otherwise false. /// Execute `dotnet <args>` and return true if the command succeeded, otherwise false.
/// The output of the command is returned in `output`. /// The output of the command is returned in `output`.
/// If `silent` is true the output of the command is logged as `debug` otherwise as `info`.
/// </summary> /// </summary>
bool RunCommand(string args, out IList<string> output); bool RunCommand(string args, out IList<string> output, bool silent = true);
/// <summary> /// <summary>
/// Execute `dotnet <args>` in `<workingDirectory>` and return true if the command succeeded, otherwise false. /// Execute `dotnet <args>` in `<workingDirectory>` and return true if the command succeeded, otherwise false.
/// The output of the command is returned in `output`. /// The output of the command is returned in `output`.
/// If `silent` is true the output of the command is logged as `debug` otherwise as `info`.
/// </summary> /// </summary>
bool RunCommand(string args, string? workingDirectory, out IList<string> output); bool RunCommand(string args, string? workingDirectory, out IList<string> output, bool silent = true);
} }
} }

View File

@@ -20,23 +20,23 @@ namespace Semmle.Extraction.Tests
public string Exec => "dotnet"; public string Exec => "dotnet";
public bool RunCommand(string args) public bool RunCommand(string args, bool silent)
{ {
lastArgs = args; lastArgs = args;
return Success; return Success;
} }
public bool RunCommand(string args, out IList<string> output) public bool RunCommand(string args, out IList<string> output, bool silent)
{ {
lastArgs = args; lastArgs = args;
output = this.output; output = this.output;
return Success; return Success;
} }
public bool RunCommand(string args, string? workingDirectory, out IList<string> output) public bool RunCommand(string args, string? workingDirectory, out IList<string> output, bool silent)
{ {
WorkingDirectory = workingDirectory ?? ""; WorkingDirectory = workingDirectory ?? "";
return RunCommand(args, out output); return RunCommand(args, out output, silent);
} }
public string GetLastArgs() => lastArgs; public string GetLastArgs() => lastArgs;