using FileFlows.Plugin;
#if (DEBUG)
namespace PluginTestLibrary;
///
/// A logger for tests that stores the logs in memory
///
public class TestLogger : ILogger
{
private readonly List Messages = new();
///
/// Writes an information log message
///
/// the log parameters
public void ILog(params object[] args)
=> Log(LogType.Info, args);
///
public void Raw(params object[] args)
=> Log(LogType.Raw, args);
///
/// Writes an debug log message
///
/// the log parameters
public void DLog(params object[] args)
=> Log(LogType.Debug, args);
///
/// Writes an warning log message
///
/// the log parameters
public void WLog(params object[] args)
=> Log(LogType.Warning, args);
///
/// Writes an error log message
///
/// the log parameters
public void ELog(params object[] args)
=> Log(LogType.Error, args);
///
/// Gets the tail of the log
///
/// the number of messages to get
public string GetTail(int length = 50)
{
if (Messages.Count <= length)
return string.Join(Environment.NewLine, Messages);
return string.Join(Environment.NewLine, Messages.TakeLast(50));
}
///
/// Logs a message
///
/// the type of log to record
/// the arguments of the message
private void Log(LogType type, params object[] args)
{
string prefix = type == LogType.Raw ? string.Empty : type + " -> ";
string message = prefix + string.Join(", ", args.Select(x =>
x == null ? "null" :
x.GetType().IsPrimitive ? x.ToString() :
x is string ? x.ToString() :
System.Text.Json.JsonSerializer.Serialize(x)));
Writer?.Invoke(message);
Messages.Add(message);
}
///
/// Gets or sets an optional writer
///
public Action Writer { get; set; } = null!;
///
/// Returns the entire log as a string
///
/// the entire log
public override string ToString()
=> string.Join(Environment.NewLine, Messages);
///
/// Checks if the log contains the text
///
/// the text to check for
/// true if it contains it, otherwise false
public bool Contains(string text)
=> Messages.Any(x => x.Contains(text));
}
#endif