Skip to content

Commit 9b4cdd0

Browse files
committed
Separate logging classes into separate files
1 parent 2c72d7e commit 9b4cdd0

File tree

9 files changed

+228
-209
lines changed

9 files changed

+228
-209
lines changed

csharp/extractor/Semmle.Util/Logger.cs

Lines changed: 0 additions & 208 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace Semmle.Util.Logging
2+
{
3+
/// <summary>
4+
/// A combined logger.
5+
/// </summary>
6+
public sealed class CombinedLogger : ILogger
7+
{
8+
private readonly ILogger logger1;
9+
private readonly ILogger logger2;
10+
11+
public CombinedLogger(ILogger logger1, ILogger logger2)
12+
{
13+
this.logger1 = logger1;
14+
this.logger2 = logger2;
15+
}
16+
17+
public void Dispose()
18+
{
19+
logger1.Dispose();
20+
logger2.Dispose();
21+
}
22+
23+
public void Log(Severity s, string text, int? threadId = null)
24+
{
25+
logger1.Log(s, text, threadId);
26+
logger2.Log(s, text, threadId);
27+
}
28+
}
29+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace Semmle.Util.Logging
5+
{
6+
/// <summary>
7+
/// A logger that outputs to stdout/stderr.
8+
/// </summary>
9+
public sealed class ConsoleLogger : ILogger
10+
{
11+
private readonly Verbosity verbosity;
12+
private readonly bool logThreadId;
13+
14+
public ConsoleLogger(Verbosity verbosity, bool logThreadId)
15+
{
16+
this.verbosity = verbosity;
17+
this.logThreadId = logThreadId;
18+
}
19+
20+
public void Dispose() { }
21+
22+
private static TextWriter GetConsole(Severity s)
23+
{
24+
return s == Severity.Error ? Console.Error : Console.Out;
25+
}
26+
27+
private static string GetSeverityPrefix(Severity s)
28+
{
29+
switch (s)
30+
{
31+
case Severity.Trace:
32+
case Severity.Debug:
33+
case Severity.Info:
34+
return "";
35+
case Severity.Warning:
36+
return "Warning: ";
37+
case Severity.Error:
38+
return "Error: ";
39+
default:
40+
throw new ArgumentOutOfRangeException(nameof(s));
41+
}
42+
}
43+
44+
public void Log(Severity s, string text, int? threadId = null)
45+
{
46+
if (verbosity.Includes(s))
47+
{
48+
threadId ??= Environment.CurrentManagedThreadId;
49+
50+
var prefix = this.logThreadId ? $"[{threadId:D3}] " : "";
51+
GetConsole(s).WriteLine(prefix + GetSeverityPrefix(s) + text);
52+
}
53+
}
54+
}
55+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
using System;
2+
using System.IO;
3+
4+
namespace Semmle.Util.Logging
5+
{
6+
/// <summary>
7+
/// A logger that outputs to a <code>csharp.log</code>
8+
/// file.
9+
/// </summary>
10+
public sealed class FileLogger : ILogger
11+
{
12+
private readonly StreamWriter writer;
13+
private readonly Verbosity verbosity;
14+
private readonly bool logThreadId;
15+
16+
public FileLogger(Verbosity verbosity, string outputFile, bool logThreadId)
17+
{
18+
this.verbosity = verbosity;
19+
this.logThreadId = logThreadId;
20+
21+
try
22+
{
23+
var dir = Path.GetDirectoryName(outputFile);
24+
if (!string.IsNullOrEmpty(dir) && !System.IO.Directory.Exists(dir))
25+
Directory.CreateDirectory(dir);
26+
writer = new PidStreamWriter(
27+
new FileStream(outputFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite, 8192));
28+
}
29+
catch (Exception ex) // lgtm[cs/catch-of-all-exceptions]
30+
{
31+
Console.Error.WriteLine("CodeQL: Couldn't initialise C# extractor output: " + ex.Message + "\n" + ex.StackTrace);
32+
Console.Error.Flush();
33+
throw;
34+
}
35+
}
36+
37+
public void Dispose()
38+
{
39+
writer.Dispose();
40+
}
41+
42+
private static string GetSeverityPrefix(Severity s)
43+
{
44+
return "[" + s.ToString().ToUpper() + "] ";
45+
}
46+
47+
public void Log(Severity s, string text, int? threadId = null)
48+
{
49+
if (verbosity.Includes(s))
50+
{
51+
threadId ??= Environment.CurrentManagedThreadId;
52+
53+
var prefix = this.logThreadId ? $"[{threadId:D3}] " : "";
54+
writer.WriteLine(prefix + GetSeverityPrefix(s) + text);
55+
}
56+
}
57+
}
58+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
3+
namespace Semmle.Util.Logging
4+
{
5+
/// <summary>
6+
/// A logger.
7+
/// </summary>
8+
public interface ILogger : IDisposable
9+
{
10+
/// <summary>
11+
/// Log the given text with the given severity.
12+
/// </summary>
13+
void Log(Severity s, string text, int? threadId = null);
14+
15+
void LogError(string text, int? threadId = null) => Log(Severity.Error, text, threadId);
16+
17+
void LogWarning(string text, int? threadId = null) => Log(Severity.Warning, text, threadId);
18+
19+
void LogInfo(string text, int? threadId = null) => Log(Severity.Info, text, threadId);
20+
21+
void LogDebug(string text, int? threadId = null) => Log(Severity.Debug, text, threadId);
22+
23+
void Log(Severity s, string text, params object?[] args) => Log(s, string.Format(text, args));
24+
}
25+
}

csharp/extractor/Semmle.Util/LoggerUtils.cs renamed to csharp/extractor/Semmle.Util/Logging/PidStreamWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.IO;
22
using System.Diagnostics;
33

4-
namespace Semmle.Util
4+
namespace Semmle.Util.Logging
55
{
66
/// <summary>
77
/// Utility stream writer that prefixes the current PID to some writes.

0 commit comments

Comments
 (0)