2020-07-23 13:56:01 +02:00
|
|
|
using System.Text;
|
|
|
|
|
|
|
|
namespace ExtremelySimpleLogger {
|
|
|
|
/// <summary>
|
|
|
|
/// A <see cref="Sink"/> that writes output to a string, which can be queried using <see cref="Value"/>.
|
|
|
|
/// Note that this uses a <see cref="StringBuilder"/> internally for performance.
|
|
|
|
/// </summary>
|
|
|
|
public class StringSink : Sink {
|
|
|
|
|
|
|
|
/// <summary>
|
2021-11-04 18:11:56 +01:00
|
|
|
/// The string that this sink currently contains, constructed from the underlying <see cref="StringBuilder"/>.
|
2020-07-23 13:56:01 +02:00
|
|
|
/// Can be cleared using <see cref="Clear"/>.
|
|
|
|
/// </summary>
|
2020-07-29 14:10:49 +02:00
|
|
|
public string Value {
|
|
|
|
get {
|
|
|
|
lock (this.builder)
|
|
|
|
return this.builder.ToString();
|
|
|
|
}
|
|
|
|
}
|
2021-11-04 18:11:56 +01:00
|
|
|
/// <summary>
|
|
|
|
/// The <see cref="StringBuilder"/> used to collect logged data from this sink.
|
|
|
|
/// To get its value directly, you can use <see cref="Value"/>.
|
|
|
|
/// </summary>
|
|
|
|
public StringBuilder StringBuilder {
|
|
|
|
get {
|
|
|
|
lock (this.builder)
|
|
|
|
return this.builder;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private readonly StringBuilder builder = new StringBuilder();
|
2020-07-23 13:56:01 +02:00
|
|
|
|
|
|
|
/// <summary>
|
2022-10-05 18:13:29 +02:00
|
|
|
/// Logs the given message, which has already been formatted using <see cref="Sink.Formatter"/> or <see cref="Logger.DefaultFormatter"/>.
|
2020-07-23 13:56:01 +02:00
|
|
|
/// </summary>
|
2020-07-29 14:23:23 +02:00
|
|
|
/// <param name="logger">The logger that the message was passed to</param>
|
|
|
|
/// <param name="level">The importance level of this message</param>
|
2020-07-23 13:56:01 +02:00
|
|
|
/// <param name="s">The message to log</param>
|
2020-07-29 14:23:23 +02:00
|
|
|
protected override void Log(Logger logger, LogLevel level, string s) {
|
2020-07-29 14:10:49 +02:00
|
|
|
lock (this.builder)
|
|
|
|
this.builder.AppendLine(s);
|
2020-07-23 13:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Clears the string that this sink currently contains.
|
|
|
|
/// After this call, <see cref="Value"/> will be empty.
|
|
|
|
/// </summary>
|
|
|
|
public void Clear() {
|
2020-07-29 14:10:49 +02:00
|
|
|
lock (this.builder)
|
|
|
|
this.builder.Clear();
|
2020-07-23 13:56:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|