113 lines
3.2 KiB
C#
113 lines
3.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace FileWatcher
|
|
{
|
|
public sealed class Logger
|
|
{
|
|
private delegate void SetTextCallback(string str, Color? color);
|
|
|
|
#region Singleton
|
|
|
|
private static readonly Logger instance = new Logger();
|
|
|
|
static Logger()
|
|
{
|
|
}
|
|
|
|
public static Logger Instance
|
|
{
|
|
get
|
|
{
|
|
return instance;
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
private Logger()
|
|
{
|
|
}
|
|
|
|
public void Print(string str, Color? color = null)
|
|
{
|
|
if (Form1.Instance.textBox2.InvokeRequired)
|
|
{
|
|
SetTextCallback d = new SetTextCallback(Print);
|
|
Form1.Instance.textBox2.Invoke(d, new object[] { str, color });
|
|
}
|
|
else
|
|
{
|
|
Form1.Instance.textBox2.AppendText(str, color.HasValue ? color.Value : Color.Black);
|
|
Form1.Instance.textBox2.SelectionStart = Form1.Instance.textBox2.Text.Length;
|
|
Form1.Instance.textBox2.ScrollToCaret();
|
|
}
|
|
}
|
|
|
|
public void PrintLine(string str, Color? color = null)
|
|
{
|
|
if (Form1.Instance.textBox2.InvokeRequired)
|
|
{
|
|
SetTextCallback d = new SetTextCallback(PrintLine);
|
|
Form1.Instance.textBox2.Invoke(d, new object[] { str, color });
|
|
}
|
|
else
|
|
{
|
|
Form1.Instance.textBox2.AppendText(str + Environment.NewLine, color.HasValue ? color.Value : Color.Black);
|
|
Form1.Instance.textBox2.SelectionStart = Form1.Instance.textBox2.Text.Length;
|
|
Form1.Instance.textBox2.ScrollToCaret();
|
|
}
|
|
}
|
|
|
|
public void ErrorLine(string str, Color? color = null)
|
|
{
|
|
if (Form1.Instance.textBox2.InvokeRequired)
|
|
{
|
|
SetTextCallback d = new SetTextCallback(ErrorLine);
|
|
Form1.Instance.textBox2.Invoke(d, new object[] { str, color });
|
|
}
|
|
else
|
|
{
|
|
Form1.Instance.textBox2.AppendText(str + Environment.NewLine, color.HasValue ? color.Value : Color.Red);
|
|
Form1.Instance.textBox2.SelectionStart = Form1.Instance.textBox2.Text.Length;
|
|
Form1.Instance.textBox2.ScrollToCaret();
|
|
}
|
|
}
|
|
|
|
public void LogLine(string str)
|
|
{
|
|
Print(DateTime.Now.ToString("R") + " ", Color.Maroon);
|
|
PrintLine(str);
|
|
}
|
|
|
|
public void LogErrorLine(string str)
|
|
{
|
|
Print(DateTime.Now.ToString("R") + " ", Color.Maroon);
|
|
PrintLine(str, Color.Red);
|
|
}
|
|
|
|
static public void WriteLine(string str)
|
|
{
|
|
Logger.Instance.PrintLine(str);
|
|
}
|
|
|
|
static public void Error(string str)
|
|
{
|
|
Logger.Instance.ErrorLine(str);
|
|
}
|
|
|
|
static public void Log(string str)
|
|
{
|
|
Logger.Instance.LogLine(str);
|
|
}
|
|
|
|
static public void LogError(string str)
|
|
{
|
|
Logger.Instance.LogErrorLine(str);
|
|
}
|
|
}
|
|
}
|