mirror of
https://git.battle-of-pip.de/root/vpr-mitarbeiterverwaltung.git
synced 2025-12-13 22:31:38 +01:00
Initial Commit
This commit is contained in:
95
DX86/Modules/MessageSender.cs
Normal file
95
DX86/Modules/MessageSender.cs
Normal file
@@ -0,0 +1,95 @@
|
||||
namespace DX86.Modules;
|
||||
/*
|
||||
* Copyright 2024 SnapixLP
|
||||
*/
|
||||
public class MessageSender
|
||||
{
|
||||
private string[] messageHistory = {};
|
||||
private bool saveOnly = false;
|
||||
|
||||
private string logfile;
|
||||
|
||||
public MessageSender(string logfile)
|
||||
{
|
||||
this.logfile = logfile;
|
||||
if (!File.Exists(logfile))
|
||||
{
|
||||
File.Create(logfile).Close();
|
||||
Log("[MessageSender] Log file created.");
|
||||
}
|
||||
Log("[MessageSender] Starting Logoutput to " + logfile);
|
||||
Log("[MessageSender] MessageSender initialized.");
|
||||
}
|
||||
|
||||
private async Task WriteToFileAsync(string message)
|
||||
{
|
||||
try
|
||||
{
|
||||
using (StreamWriter writer = new StreamWriter(logfile, true))
|
||||
{
|
||||
await writer.WriteLineAsync(message);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Error($"Failed to write to log file: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async void Log(string message)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
this.Send($"[{DateTime.Now}] [INFO ] {message}");
|
||||
}
|
||||
public async void Warn(string message)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
this.Send($"[{DateTime.Now}] [WARN ] {message}");
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
}
|
||||
public async void Error(string message)
|
||||
{
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
this.Send($"[{DateTime.Now}] [ERROR] {message}");
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
}
|
||||
|
||||
private async void Send(string message)
|
||||
{
|
||||
WriteToFileAsync(message);
|
||||
if (!saveOnly)
|
||||
{
|
||||
Console.WriteLine(message);
|
||||
}
|
||||
|
||||
messageHistory = messageHistory.Concat(new[] { message }).ToArray();
|
||||
if (messageHistory.Length > 50)
|
||||
{
|
||||
messageHistory = messageHistory.Skip(1).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public async void SendHistory()
|
||||
{
|
||||
|
||||
int i = 0;
|
||||
foreach (var s in messageHistory)
|
||||
{
|
||||
if (s.Contains("[INFO ]"))
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
if (s.Contains("[WARN ]"))
|
||||
Console.ForegroundColor = ConsoleColor.Yellow;
|
||||
if (s.Contains("[ERROR]"))
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"{s}");
|
||||
Console.ForegroundColor = ConsoleColor.Gray;
|
||||
}
|
||||
}
|
||||
|
||||
public void SaveOnly(bool newValue)
|
||||
{
|
||||
saveOnly = newValue;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user