Skip to content

Commit

Permalink
C#: Store extractor message in DB for limit reached
Browse files Browse the repository at this point in the history
  • Loading branch information
tamasvajk committed Aug 13, 2024
1 parent 0550056 commit 5f74ead
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 10 deletions.
24 changes: 17 additions & 7 deletions csharp/extractor/Semmle.Extraction/Entities/ExtractionMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,36 @@ internal class ExtractionMessage : FreshEntity
private static int messageCount = 0;

private readonly Message msg;
private readonly bool bypassLimit;

public ExtractionMessage(Context cx, Message msg) : base(cx)
public ExtractionMessage(Context cx, Message msg) : this(cx, msg, bypassLimit: false)
{
}

private ExtractionMessage(Context cx, Message msg, bool bypassLimit) : base(cx)
{
this.bypassLimit = bypassLimit;
this.msg = msg;
TryPopulate();
}

protected override void Populate(TextWriter trapFile)
{
var val = Interlocked.Increment(ref messageCount);
if (val > limit)
if (!bypassLimit)
{
if (val == limit + 1)
var val = Interlocked.Increment(ref messageCount);

Check notice

Code scanning / CodeQL

Static field written by instance method Note

Write to static field from instance method, property, or constructor.
if (val > limit)
{
Context.ExtractionContext.Logger.LogWarning($"Stopped logging extractor messages after reaching {limit}");
if (val == limit + 1)
{
Context.ExtractionContext.Logger.LogWarning($"Stopped logging extractor messages after reaching {limit}");
_ = new ExtractionMessage(Context, new Message($"Stopped logging extractor messages after reaching {limit}", null, null, null, Util.Logging.Severity.Warning), bypassLimit: true);
}
return;
}
return;
}

trapFile.extractor_messages(this, msg.Severity, "C# extractor", msg.Text, msg.EntityText ?? string.Empty,
trapFile.extractor_messages(this, msg.Severity, msg.Text, msg.EntityText ?? string.Empty,
msg.Location ?? Context.CreateLocation(), msg.StackTrace ?? string.Empty);
}
}
Expand Down
4 changes: 2 additions & 2 deletions csharp/extractor/Semmle.Extraction/Tuples.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ public static void containerparent(this System.IO.TextWriter trapFile, Folder pa
trapFile.WriteTuple("containerparent", parent, child);
}

internal static void extractor_messages(this System.IO.TextWriter trapFile, ExtractionMessage error, Semmle.Util.Logging.Severity severity, string origin, string errorMessage, string entityText, Location location, string stackTrace)
internal static void extractor_messages(this System.IO.TextWriter trapFile, ExtractionMessage error, Semmle.Util.Logging.Severity severity, string errorMessage, string entityText, Location location, string stackTrace)
{
trapFile.WriteTuple("extractor_messages", error, (int)severity, origin, errorMessage, entityText, location, stackTrace);
trapFile.WriteTuple("extractor_messages", error, (int)severity, "C# extractor", errorMessage, entityText, location, stackTrace);
}

public static void files(this System.IO.TextWriter trapFile, File file, string fullName)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
extractorMessages
| 5 |
| 6 |
compilerDiagnostics
| 4 |
extractorMessagesLeachedLimit
| Program.cs:1:1:1:0 | Stopped logging extractor messages after reaching 5 |
compilationInfo
| Compiler diagnostic count for CS0103 | 3.0 |
| Compiler diagnostic count for CS8019 | 7.0 |
4 changes: 4 additions & 0 deletions csharp/ql/integration-tests/all-platforms/standalone/Diag.ql
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ query predicate extractorMessages(int c) { c = count(ExtractorMessage msg) }

query predicate compilerDiagnostics(int c) { c = count(Diagnostic diag) }

query predicate extractorMessagesLeachedLimit(ExtractorMessage msg) {
msg.getText().indexOf("Stopped logging") = 0
}

query predicate compilationInfo(string key, float value) {
exists(Compilation c, string infoValue |
infoValue = c.getInfo(key) and key.matches("Compiler diagnostic count for%")
Expand Down

0 comments on commit 5f74ead

Please sign in to comment.