添加项目文件。

This commit is contained in:
2025-02-25 22:28:49 +08:00
parent 07e26cc93e
commit 1a7bdb585a
32 changed files with 1601 additions and 0 deletions

173
Logs.cs Normal file
View File

@@ -0,0 +1,173 @@
using System.Collections.Concurrent;
using System.Text;
using MegghysAPI.Attributes;
using MegghysAPI.Entities;
namespace MegghysAPI
{
public class Logs
{
public readonly static string SavePath = Path.Combine(Environment.CurrentDirectory, "Logs");
public static string LogPath => Path.Combine(Environment.CurrentDirectory, "Logs");
public static string LogName => Path.Combine(SavePath, DateTime.Now.ToString("yyyy-MM-dd") + ".log");
public const ConsoleColor DefaultColor = ConsoleColor.Gray;
public static void Text(object text)
{
LogAndSave("[Normal]", ConsoleColor.Gray, true, text.ToString());
}
public static void Info(object text)
{
LogAndSave("[Info]", ConsoleColor.Yellow, true, text.ToString());
}
public static void Error(object text)
{
LogAndSave("[Error]", ConsoleColor.Red, true, text.ToString());
}
public static void Warn(object text)
{
LogAndSave("[Warn]", ConsoleColor.DarkYellow, true, text.ToString());
}
public static void Success(object text)
{
LogAndSave("[Success]", ConsoleColor.Green, true, text.ToString());
}
public static void Text(params LogString[] text)
{
LogAndSave("[Normal]", ConsoleColor.Gray, true, text);
}
public static void Info(params LogString[] text)
{
LogAndSave("[Info]", ConsoleColor.Yellow, true, text);
}
public static void Error(params LogString[] text)
{
LogAndSave("[Error]", ConsoleColor.Red, true, text);
}
public static void Warn(params LogString[] text)
{
LogAndSave("[Warn]", ConsoleColor.DarkYellow, true, text);
}
public static void Success(params LogString[] text)
{
LogAndSave("[Success]", ConsoleColor.Green, true, text);
}
public static void TextCondition(Func<bool> condition, params LogString[] text)
{
if (condition?.Invoke() ?? true)
LogAndSave("[Normal]", ConsoleColor.Gray, true, text);
}
public static void InfoCondition(Func<bool> condition, params LogString[] text)
{
if (condition?.Invoke() ?? true)
LogAndSave("[Info]", ConsoleColor.Yellow, true, text);
}
public static void ErrorCondition(Func<bool> condition, params LogString[] text)
{
if (condition?.Invoke() ?? true)
LogAndSave("[Error]", ConsoleColor.Red, true, text);
}
public static void WarnCondition(Func<bool> condition, params LogString[] text)
{
if (condition?.Invoke() ?? true)
LogAndSave("[Warn]", ConsoleColor.DarkYellow, true, text);
}
public static void SuccesCondition(Func<bool> condition, params LogString[] text)
{
if (condition?.Invoke() ?? true)
LogAndSave("[Success]", ConsoleColor.Green, true, text);
}
public static void Info(params (object message, ConsoleColor color)[] text)
{
LogAndSave("[Info]", ConsoleColor.Yellow, true, text.Select(t => new LogString(t.message, t.color)).ToArray());
}
public static void Text(params (object message, ConsoleColor color)[] text)
{
LogAndSave("[Normal]", ConsoleColor.Gray, true, text.Select(t => new LogString(t.message, t.color)).ToArray());
}
public static void Error(params (object message, ConsoleColor color)[] text)
{
LogAndSave("[Error]", ConsoleColor.Red, true, text.Select(t => new LogString(t.message, t.color)).ToArray());
}
public static void Warn(params (object message, ConsoleColor color)[] text)
{
LogAndSave("[Warn]", ConsoleColor.DarkYellow, true, text.Select(t => new LogString(t.message, t.color)).ToArray());
}
public static void Success(params (object message, ConsoleColor color)[] text)
{
LogAndSave("[Success]", ConsoleColor.Green, true, text.Select(t => new LogString(t.message, t.color)).ToArray());
}
internal static void Init()
{
if (!Directory.Exists(LogPath))
Directory.CreateDirectory(LogPath);
_ = Task.Run(LogLoop);
}
public static void LogNotDisplay(object message, string prefix = "[NotDisplay]")
{
File.AppendAllText(LogName, $"{DateTime.Now:yyyy-MM-dd-HH:mm:ss} - {prefix} {message}{Environment.NewLine}", Encoding.UTF8);
}
public static BlockingCollection<(string prefix, ConsoleColor color, bool save, LogString[] msg)> logQueue = new();
[AutoInit(Async = true)]
public static void LogLoop()
{
if (!Directory.Exists(LogPath))
{
Directory.CreateDirectory(LogPath);
}
while (true)
{
try
{
if (logQueue.TryTake(out var log))
{
var (prefix, color, save, message) = log;
if (save)
{
File.AppendAllText(LogName, $"{DateTime.Now:yyyy-MM-dd-HH:mm:ss} - {prefix} {string.Join("", message.Select(m => m.Text))}{Environment.NewLine}", Encoding.UTF8);
}
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write($"{DateTime.Now:HH:mm:ss} ");
lastColor = color;
Console.ForegroundColor = color;
Console.Write($"{prefix} ");
foreach (var item in message)
{
if (item.Color.HasValue)
{
if (lastColor != item.Color)
{
lastColor = item.Color;
Console.ForegroundColor = item.Color.Value;
}
}
else if (lastColor != color)
{
lastColor = color;
Console.ForegroundColor = color;
}
Console.Write($"{item.Text}");
}
Console.WriteLine();
}
else
Thread.Sleep(1);
}
catch (Exception ex)
{
Console.WriteLine(ex);
}
}
}
static ConsoleColor? lastColor = ConsoleColor.Gray;
public static void LogAndSave(string prefix = "[Log]", ConsoleColor color = DefaultColor, bool save = true, params LogString[] message)
{
try
{
logQueue.TryAdd((prefix, color, save, message));
}
catch (Exception ex) { Console.WriteLine(ex); }
}
}
}