Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 681953a

Browse files
committed
C#: Reduce logging output and write arguments to separate files. Fix missing response file.
1 parent ec2bf91 commit 681953a

5 files changed

Lines changed: 50 additions & 41 deletions

File tree

csharp/extractor/Semmle.Extraction.CSharp/Analyser.cs

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -399,51 +399,28 @@ public void Dispose()
399399
/// </summary>
400400
public int TotalErrors => CompilationErrors + ExtractorErrors;
401401

402-
void AppendQuoted(StringBuilder sb, string s)
403-
{
404-
if (s.IndexOf(' ') != -1)
405-
sb.Append('\"').Append(s).Append('\"');
406-
else
407-
sb.Append(s);
408-
}
409-
410402
/// <summary>
411403
/// Logs detailed information about this invocation,
412404
/// in the event that errors were detected.
413405
/// </summary>
414406
/// <param name="roslynArgs">The arguments passed to Roslyn.</param>
415407
public void LogDiagnostics(string[] roslynArgs)
416408
{
417-
Logger.Log(Severity.Info, " Current working directory: {0}", Directory.GetCurrentDirectory());
418409
Logger.Log(Severity.Info, " Extractor: {0}", Environment.GetCommandLineArgs().First());
419410
if (extractor != null)
420411
Logger.Log(Severity.Info, " Extractor version: {0}", extractor.Version);
421-
var sb = new StringBuilder();
422-
sb.Append(" Expanded command line: ");
423-
bool first = true;
424-
foreach (var arg in Environment.GetCommandLineArgs().Skip(1))
425-
{
426-
if (arg[0] == '@')
427-
{
428-
foreach (var line in File.ReadAllLines(arg.Substring(1)))
429-
{
430-
if (first) first = false;
431-
else sb.Append(" ");
432-
sb.Append(line);
433-
}
434-
}
435-
else
436-
{
437-
if (first) first = false;
438-
else sb.Append(" ");
439-
AppendQuoted(sb, arg);
440-
}
441-
}
442-
Logger.Log(Severity.Info, sb.ToString());
412+
413+
Logger.Log(Severity.Info, " Current working directory: {0}", Directory.GetCurrentDirectory());
443414

444415
if (roslynArgs != null)
445416
Logger.Log(Severity.Info, $" Arguments to Roslyn: {string.Join(' ', roslynArgs)}");
446417

418+
// Create a new file in the log folder.
419+
var argsFile = Path.Combine(Extractor.GetCSharpLogDirectory(), $"csharp.{Path.GetRandomFileName()}.txt");
420+
421+
if (roslynArgs.ArchiveCommandLine(argsFile))
422+
Logger.Log(Severity.Info, $" Arguments have been written to {argsFile}");
423+
447424
foreach (var error in FilteredDiagnostics)
448425
{
449426
Logger.Log(Severity.Error, " Compilation error: {0}", error);

csharp/extractor/Semmle.Extraction.CSharp/CompilerVersion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public bool SkipExtraction
124124
/// <returns>Modified list of arguments.</returns>
125125
static IEnumerable<string> AddDefaultResponse(string responseFile, IEnumerable<string> args)
126126
{
127-
return SuppressDefaultResponseFile(args) && File.Exists(responseFile) ?
127+
return SuppressDefaultResponseFile(args) || !File.Exists(responseFile) ?
128128
args :
129129
new[] { "@" + responseFile }.Concat(args);
130130
}

csharp/extractor/Semmle.Extraction.CSharp/Extractor.cs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,11 @@ public LogProgressMonitor(ILogger logger)
3434

3535
public void Analysed(int item, int total, string source, string output, TimeSpan time, AnalysisAction action)
3636
{
37-
Logger.Log(Severity.Info, " {0} -> {1} ({2})", source, output,
38-
action == AnalysisAction.Extracted ? time.ToString() : action == AnalysisAction.Excluded ? "excluded" : "up to date");
37+
if (action != AnalysisAction.UpToDate)
38+
{
39+
Logger.Log(Severity.Info, " {0} ({2})", source, output,
40+
action == AnalysisAction.Extracted ? time.ToString() : action == AnalysisAction.Excluded ? "excluded" : "up to date");
41+
}
3942
}
4043

4144
public void MissingNamespace(string @namespace) { }
@@ -361,27 +364,28 @@ public static void ExtractStandalone(
361364
/// <summary>
362365
/// Gets the path to the `csharp.log` file written to by the C# extractor.
363366
/// </summary>
364-
public static string GetCSharpLogPath()
367+
public static string GetCSharpLogPath() =>
368+
Path.Combine(GetCSharpLogDirectory(), "csharp.log");
369+
370+
public static string GetCSharpLogDirectory()
365371
{
366372
string snapshot = Environment.GetEnvironmentVariable("ODASA_SNAPSHOT");
367373
string buildErrorDir = Environment.GetEnvironmentVariable("ODASA_BUILD_ERROR_DIR");
368374
string traps = Environment.GetEnvironmentVariable("TRAP_FOLDER");
369-
string output = "csharp.log";
370375
if (!string.IsNullOrEmpty(snapshot))
371376
{
372-
snapshot = Path.Combine(snapshot, "log");
373-
return Path.Combine(snapshot, output);
377+
return Path.Combine(snapshot, "log");
374378
}
375379
if (!string.IsNullOrEmpty(buildErrorDir))
376380
{
377381
// Used by `qltest`
378-
return Path.Combine(buildErrorDir, output);
382+
return buildErrorDir;
379383
}
380384
if (!string.IsNullOrEmpty(traps))
381385
{
382-
return Path.Combine(traps, output);
386+
return traps;
383387
}
384-
return output;
388+
return Directory.GetCurrentDirectory();
385389
}
386390
}
387391
}

csharp/extractor/Semmle.Extraction/Message.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,5 +14,7 @@ public struct Message
1414
public ISymbol symbol;
1515
public SyntaxNode node;
1616
public Exception exception;
17+
18+
public override string ToString() => message;
1719
}
1820
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System.Collections.Generic;
2+
using System.IO;
3+
using System.Linq;
4+
5+
namespace Semmle.Util
6+
{
7+
public static class CommandLineExtensions
8+
{
9+
/// <summary>
10+
/// Archives the first "@" argument in a list of command line arguments.
11+
/// Subsequent "@" arguments are ignored.
12+
/// </summary>
13+
/// <param name="commandLineArguments">The raw command line arguments.</param>
14+
/// <param name="filename">The full filename to write to.</param>
15+
/// <returns>True iff the file was written.</returns>
16+
public static bool ArchiveCommandLine(this IEnumerable<string> commandLineArguments, string filename)
17+
{
18+
foreach (var arg in commandLineArguments.Where(arg => arg[0] == '@').Select(arg => arg.Substring(1)))
19+
{
20+
File.Copy(arg, filename, true);
21+
return true;
22+
}
23+
return false;
24+
}
25+
}
26+
}

0 commit comments

Comments
 (0)