2020-11-23 16:51:05 +01:00
|
|
|
|
using Flowframes.IO;
|
2021-03-04 10:51:13 +01:00
|
|
|
|
using Flowframes.UI;
|
2020-11-23 16:51:05 +01:00
|
|
|
|
using System;
|
2021-05-09 14:48:41 +02:00
|
|
|
|
using System.Collections.Concurrent;
|
2020-11-23 16:51:05 +01:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.IO;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using System.Windows.Forms;
|
|
|
|
|
|
using DT = System.DateTime;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Flowframes
|
|
|
|
|
|
{
|
|
|
|
|
|
class Logger
|
|
|
|
|
|
{
|
|
|
|
|
|
public static TextBox textbox;
|
|
|
|
|
|
static string file;
|
2020-12-27 22:52:14 +01:00
|
|
|
|
public const string defaultLogName = "sessionlog";
|
2021-01-16 15:45:15 +01:00
|
|
|
|
public static long id;
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
2021-05-09 14:48:41 +02:00
|
|
|
|
public struct LogEntry
|
2020-11-23 16:51:05 +01:00
|
|
|
|
{
|
2021-05-09 14:48:41 +02:00
|
|
|
|
public string logMessage;
|
|
|
|
|
|
public bool hidden;
|
|
|
|
|
|
public bool replaceLastLine;
|
|
|
|
|
|
public string filename;
|
|
|
|
|
|
|
|
|
|
|
|
public LogEntry(string logMessageArg, bool hiddenArg = false, bool replaceLastLineArg = false, string filenameArg = "")
|
|
|
|
|
|
{
|
|
|
|
|
|
logMessage = logMessageArg;
|
|
|
|
|
|
hidden = hiddenArg;
|
|
|
|
|
|
replaceLastLine = replaceLastLineArg;
|
|
|
|
|
|
filename = filenameArg;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static ConcurrentQueue<LogEntry> logQueue = new ConcurrentQueue<LogEntry>();
|
|
|
|
|
|
|
|
|
|
|
|
public static void Log(string msg, bool hidden = false, bool replaceLastLine = false, string filename = "")
|
|
|
|
|
|
{
|
|
|
|
|
|
logQueue.Enqueue(new LogEntry(msg, hidden, replaceLastLine, filename));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static async Task Run()
|
|
|
|
|
|
{
|
|
|
|
|
|
while (true)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!logQueue.IsEmpty)
|
|
|
|
|
|
{
|
|
|
|
|
|
LogEntry entry;
|
|
|
|
|
|
|
|
|
|
|
|
if (logQueue.TryDequeue(out entry))
|
|
|
|
|
|
Show(entry);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static void Show(LogEntry entry)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(entry.logMessage))
|
2020-11-23 16:51:05 +01:00
|
|
|
|
return;
|
|
|
|
|
|
|
2021-05-09 14:48:41 +02:00
|
|
|
|
Console.WriteLine(entry.logMessage);
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
2021-05-09 14:48:41 +02:00
|
|
|
|
if (!entry.hidden && entry.replaceLastLine)
|
2021-03-04 10:51:13 +01:00
|
|
|
|
{
|
|
|
|
|
|
textbox.Suspend();
|
2021-04-25 21:12:45 +02:00
|
|
|
|
string[] lines = textbox.Text.SplitIntoLines();
|
|
|
|
|
|
textbox.Text = string.Join(Environment.NewLine, lines.Take(lines.Count() - 1).ToArray());
|
2021-03-04 10:51:13 +01:00
|
|
|
|
}
|
2020-11-23 16:51:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
catch { }
|
|
|
|
|
|
|
2021-05-09 14:48:41 +02:00
|
|
|
|
entry.logMessage = entry.logMessage.Replace("\n", Environment.NewLine);
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
2021-05-09 14:48:41 +02:00
|
|
|
|
if (!entry.hidden && textbox != null)
|
|
|
|
|
|
textbox.AppendText((textbox.Text.Length > 1 ? Environment.NewLine : "") + entry.logMessage);
|
2020-11-23 16:51:05 +01:00
|
|
|
|
|
2021-05-09 14:48:41 +02:00
|
|
|
|
if (entry.replaceLastLine)
|
2021-04-25 21:12:45 +02:00
|
|
|
|
{
|
|
|
|
|
|
textbox.Resume();
|
2021-05-09 14:48:41 +02:00
|
|
|
|
entry.logMessage = "[REPL] " + entry.logMessage;
|
2021-04-25 21:12:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2021-05-09 14:48:41 +02:00
|
|
|
|
if (!entry.hidden)
|
|
|
|
|
|
entry.logMessage = "[UI] " + entry.logMessage;
|
2021-03-04 10:51:13 +01:00
|
|
|
|
|
2021-05-09 14:48:41 +02:00
|
|
|
|
LogToFile(entry.logMessage, false, entry.filename);
|
2020-11-23 16:51:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-12-10 15:37:42 +01:00
|
|
|
|
public static void LogToFile(string logStr, bool noLineBreak, string filename)
|
2020-11-23 16:51:05 +01:00
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(filename))
|
2020-12-10 15:19:44 +01:00
|
|
|
|
filename = defaultLogName;
|
2020-12-27 22:52:14 +01:00
|
|
|
|
|
|
|
|
|
|
if (Path.GetExtension(filename) != ".txt")
|
|
|
|
|
|
filename = Path.ChangeExtension(filename, "txt");
|
2020-11-23 16:51:05 +01:00
|
|
|
|
file = Path.Combine(Paths.GetLogPath(), filename);
|
2020-12-10 16:10:52 +01:00
|
|
|
|
logStr = logStr.Replace(Environment.NewLine, " ").TrimWhitespaces();
|
2020-11-23 16:51:05 +01:00
|
|
|
|
string time = DT.Now.Month + "-" + DT.Now.Day + "-" + DT.Now.Year + " " + DT.Now.Hour + ":" + DT.Now.Minute + ":" + DT.Now.Second;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!noLineBreak)
|
2021-01-16 15:45:15 +01:00
|
|
|
|
File.AppendAllText(file, $"{Environment.NewLine}[{id}] [{time}]: {logStr}");
|
2020-11-23 16:51:05 +01:00
|
|
|
|
else
|
2020-12-10 15:37:42 +01:00
|
|
|
|
File.AppendAllText(file, " " + logStr);
|
2021-01-16 15:45:15 +01:00
|
|
|
|
id++;
|
2020-11-23 16:51:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
// this if fine, i forgot why
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-12-07 12:34:12 +01:00
|
|
|
|
|
2021-01-26 19:39:16 +01:00
|
|
|
|
public static void LogIfLastLineDoesNotContainMsg (string s, bool hidden = false, bool replaceLastLine = false, string filename = "")
|
|
|
|
|
|
{
|
|
|
|
|
|
if (!GetLastLine().Contains(s))
|
|
|
|
|
|
Log(s, hidden, replaceLastLine, filename);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-04 14:27:34 +01:00
|
|
|
|
public static void WriteToFile (string content, bool append, string filename)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrWhiteSpace(filename))
|
|
|
|
|
|
filename = defaultLogName;
|
|
|
|
|
|
|
|
|
|
|
|
if (Path.GetExtension(filename) != ".txt")
|
|
|
|
|
|
filename = Path.ChangeExtension(filename, "txt");
|
|
|
|
|
|
|
|
|
|
|
|
file = Path.Combine(Paths.GetLogPath(), filename);
|
|
|
|
|
|
|
|
|
|
|
|
string time = DT.Now.Month + "-" + DT.Now.Day + "-" + DT.Now.Year + " " + DT.Now.Hour + ":" + DT.Now.Minute + ":" + DT.Now.Second;
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
if (append)
|
|
|
|
|
|
File.AppendAllText(file, Environment.NewLine + time + ":" + Environment.NewLine + content);
|
|
|
|
|
|
else
|
|
|
|
|
|
File.WriteAllText(file, Environment.NewLine + time + ":" + Environment.NewLine + content);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-01-06 21:58:37 +01:00
|
|
|
|
public static void ClearLogBox ()
|
2020-12-07 12:34:12 +01:00
|
|
|
|
{
|
|
|
|
|
|
textbox.Text = "";
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static string GetLastLine ()
|
|
|
|
|
|
{
|
|
|
|
|
|
string[] lines = textbox.Text.SplitIntoLines();
|
2021-01-13 15:47:05 +01:00
|
|
|
|
if (lines.Length < 1)
|
|
|
|
|
|
return "";
|
2020-12-07 12:34:12 +01:00
|
|
|
|
return lines.Last();
|
|
|
|
|
|
}
|
2021-01-06 21:44:09 +01:00
|
|
|
|
|
|
|
|
|
|
public static void RemoveLastLine ()
|
|
|
|
|
|
{
|
|
|
|
|
|
textbox.Text = textbox.Text.Remove(textbox.Text.LastIndexOf(Environment.NewLine));
|
|
|
|
|
|
}
|
2020-11-23 16:51:05 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|