Add managed thread ID to extractor log messages

This commit is contained in:
Tamas Vajk
2023-09-19 11:34:43 +02:00
parent dfd7f1e78b
commit edc93dfeb7
14 changed files with 85 additions and 107 deletions

View File

@@ -59,10 +59,12 @@ namespace Semmle.Util.Logging
{
private readonly StreamWriter writer;
private readonly Verbosity verbosity;
private readonly bool logThreadId;
public FileLogger(Verbosity verbosity, string outputFile)
public FileLogger(Verbosity verbosity, string outputFile, bool logThreadId)
{
this.verbosity = verbosity;
this.logThreadId = logThreadId;
try
{
@@ -93,7 +95,10 @@ namespace Semmle.Util.Logging
public void Log(Severity s, string text)
{
if (verbosity.Includes(s))
writer.WriteLine(GetSeverityPrefix(s) + text);
{
var threadId = this.logThreadId ? $"[{Environment.CurrentManagedThreadId:D3}] " : "";
writer.WriteLine(threadId + GetSeverityPrefix(s) + text);
}
}
}
@@ -103,10 +108,12 @@ namespace Semmle.Util.Logging
public sealed class ConsoleLogger : ILogger
{
private readonly Verbosity verbosity;
private readonly bool logThreadId;
public ConsoleLogger(Verbosity verbosity)
public ConsoleLogger(Verbosity verbosity, bool logThreadId)
{
this.verbosity = verbosity;
this.logThreadId = logThreadId;
}
public void Dispose() { }
@@ -136,7 +143,10 @@ namespace Semmle.Util.Logging
public void Log(Severity s, string text)
{
if (verbosity.Includes(s))
GetConsole(s).WriteLine(GetSeverityPrefix(s) + text);
{
var threadId = this.logThreadId ? $"[{Environment.CurrentManagedThreadId:D3}] " : "";
GetConsole(s).WriteLine(threadId + GetSeverityPrefix(s) + text);
}
}
}

View File

@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Diagnostics;
namespace Semmle.Util
@@ -7,9 +8,11 @@ namespace Semmle.Util
{
/// <summary>
/// Runs this process, and returns the exit code, as well as the contents
/// of stdout in <paramref name="stdout"/>.
/// of stdout in <paramref name="stdout"/>. If <paramref name="printToConsole"/>
/// is true, then stdout is printed to the console and each line is prefixed
/// with the thread id.
/// </summary>
public static int ReadOutput(this ProcessStartInfo pi, out IList<string> stdout)
public static int ReadOutput(this ProcessStartInfo pi, out IList<string> stdout, bool printToConsole)
{
stdout = new List<string>();
using var process = Process.Start(pi);
@@ -27,6 +30,10 @@ namespace Semmle.Util
s = process.StandardOutput.ReadLine();
if (s is not null)
{
if (printToConsole)
{
Console.WriteLine($"[{Environment.CurrentManagedThreadId:D3}] {s}");
}
stdout.Add(s);
}
}