C#: Reduce logging output and write arguments to separate files. Fix missing response file.

This commit is contained in:
calum 2018-10-31 11:56:47 +00:00
Родитель ec2bf914c8
Коммит 681953ae70
5 изменённых файлов: 50 добавлений и 41 удалений

Просмотреть файл

@ -399,14 +399,6 @@ namespace Semmle.Extraction.CSharp
/// </summary>
public int TotalErrors => CompilationErrors + ExtractorErrors;
void AppendQuoted(StringBuilder sb, string s)
{
if (s.IndexOf(' ') != -1)
sb.Append('\"').Append(s).Append('\"');
else
sb.Append(s);
}
/// <summary>
/// Logs detailed information about this invocation,
/// in the event that errors were detected.
@ -414,36 +406,21 @@ namespace Semmle.Extraction.CSharp
/// <param name="roslynArgs">The arguments passed to Roslyn.</param>
public void LogDiagnostics(string[] roslynArgs)
{
Logger.Log(Severity.Info, " Current working directory: {0}", Directory.GetCurrentDirectory());
Logger.Log(Severity.Info, " Extractor: {0}", Environment.GetCommandLineArgs().First());
if (extractor != null)
Logger.Log(Severity.Info, " Extractor version: {0}", extractor.Version);
var sb = new StringBuilder();
sb.Append(" Expanded command line: ");
bool first = true;
foreach (var arg in Environment.GetCommandLineArgs().Skip(1))
{
if (arg[0] == '@')
{
foreach (var line in File.ReadAllLines(arg.Substring(1)))
{
if (first) first = false;
else sb.Append(" ");
sb.Append(line);
}
}
else
{
if (first) first = false;
else sb.Append(" ");
AppendQuoted(sb, arg);
}
}
Logger.Log(Severity.Info, sb.ToString());
Logger.Log(Severity.Info, " Current working directory: {0}", Directory.GetCurrentDirectory());
if (roslynArgs != null)
Logger.Log(Severity.Info, $" Arguments to Roslyn: {string.Join(' ', roslynArgs)}");
// Create a new file in the log folder.
var argsFile = Path.Combine(Extractor.GetCSharpLogDirectory(), $"csharp.{Path.GetRandomFileName()}.txt");
if (roslynArgs.ArchiveCommandLine(argsFile))
Logger.Log(Severity.Info, $" Arguments have been written to {argsFile}");
foreach (var error in FilteredDiagnostics)
{
Logger.Log(Severity.Error, " Compilation error: {0}", error);

Просмотреть файл

@ -124,7 +124,7 @@ namespace Semmle.Extraction.CSharp
/// <returns>Modified list of arguments.</returns>
static IEnumerable<string> AddDefaultResponse(string responseFile, IEnumerable<string> args)
{
return SuppressDefaultResponseFile(args) && File.Exists(responseFile) ?
return SuppressDefaultResponseFile(args) || !File.Exists(responseFile) ?
args :
new[] { "@" + responseFile }.Concat(args);
}

Просмотреть файл

@ -34,8 +34,11 @@ namespace Semmle.Extraction.CSharp
public void Analysed(int item, int total, string source, string output, TimeSpan time, AnalysisAction action)
{
Logger.Log(Severity.Info, " {0} -> {1} ({2})", source, output,
action == AnalysisAction.Extracted ? time.ToString() : action == AnalysisAction.Excluded ? "excluded" : "up to date");
if (action != AnalysisAction.UpToDate)
{
Logger.Log(Severity.Info, " {0} ({2})", source, output,
action == AnalysisAction.Extracted ? time.ToString() : action == AnalysisAction.Excluded ? "excluded" : "up to date");
}
}
public void MissingNamespace(string @namespace) { }
@ -361,27 +364,28 @@ namespace Semmle.Extraction.CSharp
/// <summary>
/// Gets the path to the `csharp.log` file written to by the C# extractor.
/// </summary>
public static string GetCSharpLogPath()
public static string GetCSharpLogPath() =>
Path.Combine(GetCSharpLogDirectory(), "csharp.log");
public static string GetCSharpLogDirectory()
{
string snapshot = Environment.GetEnvironmentVariable("ODASA_SNAPSHOT");
string buildErrorDir = Environment.GetEnvironmentVariable("ODASA_BUILD_ERROR_DIR");
string traps = Environment.GetEnvironmentVariable("TRAP_FOLDER");
string output = "csharp.log";
if (!string.IsNullOrEmpty(snapshot))
{
snapshot = Path.Combine(snapshot, "log");
return Path.Combine(snapshot, output);
return Path.Combine(snapshot, "log");
}
if (!string.IsNullOrEmpty(buildErrorDir))
{
// Used by `qltest`
return Path.Combine(buildErrorDir, output);
return buildErrorDir;
}
if (!string.IsNullOrEmpty(traps))
{
return Path.Combine(traps, output);
return traps;
}
return output;
return Directory.GetCurrentDirectory();
}
}
}

Просмотреть файл

@ -14,5 +14,7 @@ namespace Semmle.Extraction
public ISymbol symbol;
public SyntaxNode node;
public Exception exception;
public override string ToString() => message;
}
}

Просмотреть файл

@ -0,0 +1,26 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace Semmle.Util
{
public static class CommandLineExtensions
{
/// <summary>
/// Archives the first "@" argument in a list of command line arguments.
/// Subsequent "@" arguments are ignored.
/// </summary>
/// <param name="commandLineArguments">The raw command line arguments.</param>
/// <param name="filename">The full filename to write to.</param>
/// <returns>True iff the file was written.</returns>
public static bool ArchiveCommandLine(this IEnumerable<string> commandLineArguments, string filename)
{
foreach (var arg in commandLineArguments.Where(arg => arg[0] == '@').Select(arg => arg.Substring(1)))
{
File.Copy(arg, filename, true);
return true;
}
return false;
}
}
}