using System;
using System.Collections;
using System.IO;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Media;
using System.Windows.Threading;
using MPF.Frontend;
namespace MPF.UI.UserControls
{
public partial class LogOutput : UserControl
{
///
/// Document representing the text
///
private readonly FlowDocument _document;
///
/// Paragraph backing the log
///
private readonly Paragraph _paragraph;
///
/// Maximum number of inlines before trimming
///
private const int MaxInlineCount = 5000;
#if NET35
private Button? ClearButton => ItemHelper.FindChild(this, "ClearButton");
private RichTextBox? Output => ItemHelper.FindChild(this, "Output");
private ScrollViewer? OutputViewer => ItemHelper.FindChild(this, "OutputViewer");
private Button? SaveButton => ItemHelper.FindChild(this, "SaveButton");
#endif
public LogOutput()
{
#if NET40_OR_GREATER || NETCOREAPP
InitializeComponent();
#endif
// Update the internal state
_document = new FlowDocument()
{
Background = new SolidColorBrush(Color.FromArgb(0xFF, 0x20, 0x20, 0x20))
};
_paragraph = new Paragraph();
_document.Blocks.Add(_paragraph);
// Add handlers
OutputViewer!.SizeChanged += OutputViewerSizeChanged;
Output!.TextChanged += OnTextChanged;
ClearButton!.Click += OnClearButton;
SaveButton!.Click += OnSaveButton;
// Update the internal state
Output.Document = _document;
}
#region Logging
///
/// Enqueue text to the log with formatting
///
/// LogLevel for the log
/// Text to write to the log
public void EnqueueLog(LogLevel logLevel, string text)
{
// Null text gets ignored
if (text is null)
return;
// Create a new log line
var logLine = new LogLine(text, logLevel);
#if NET40
Dispatcher.Invoke(() =>
#else
Dispatcher.InvokeAsync(() =>
#endif
{
var run = logLine.GenerateRun();
_paragraph.Inlines.Add(run);
if (_paragraph.Inlines.Count > MaxInlineCount)
((IList)_paragraph.Inlines).RemoveAt(0);
});
}
///
/// Log line wrapper
///
internal readonly struct LogLine
{
public readonly string Text;
public readonly LogLevel LogLevel;
public LogLine(string text, LogLevel logLevel)
{
Text = text;
LogLevel = logLevel;
}
///
/// Get the foreground Brush for the current LogLevel
///
/// Brush representing the color
public Brush GetForegroundColor()
{
return LogLevel switch
{
LogLevel.SECRET => Brushes.DarkGray,
LogLevel.ERROR => Brushes.Red,
LogLevel.VERBOSE => Brushes.Yellow,
LogLevel.USER_SUCCESS => Brushes.ForestGreen,
LogLevel.USER_GENERIC => Brushes.White,
// Make unmatched log levels obvious
_ => Brushes.Pink,
};
}
///
/// Generate a Run object from the current LogLine
///
/// Run object based on internal values
public Run GenerateRun()
{
return new Run { Text = Text, Foreground = GetForegroundColor() };
}
}
#endregion
#region Helpers
///
/// Clear all inlines of the paragraph
///
private void ClearInlines() => _paragraph.Inlines.Clear();
///
/// Save all inlines to console.log
///
private void SaveInlines()
{
using var sw = new StreamWriter(File.OpenWrite("console.log"));
foreach (var inline in _paragraph.Inlines)
{
if (inline is Run run)
sw.Write(run.Text);
}
}
///
/// Scroll the current view to the bottom
///
public void ScrollToBottom() => OutputViewer!.ScrollToBottom();
#endregion
#region EventHandlers
private void OnClearButton(object sender, EventArgs e)
=> ClearInlines();
private void OnSaveButton(object sender, EventArgs e)
=> SaveInlines();
private void OnTextChanged(object sender, TextChangedEventArgs e)
=> ScrollToBottom();
private void OutputViewerSizeChanged(object sender, SizeChangedEventArgs e)
=> ScrollToBottom();
#endregion
}
}