2026-03-08 20:22:53 +08:00
|
|
|
// The CMLeonOS Project (https://github.com/Leonmmcoset/CMLeonOS)
|
|
|
|
|
// Copyright (C) 2025-present LeonOS 2 Developer Team
|
|
|
|
|
//
|
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
//
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
2026-02-04 23:49:08 +08:00
|
|
|
using System;
|
2026-03-17 20:42:44 +08:00
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using XSharp.Assembler.x86.SSE;
|
2026-02-04 23:49:08 +08:00
|
|
|
|
|
|
|
|
namespace CMLeonOS.Logger
|
|
|
|
|
{
|
|
|
|
|
public class Logger
|
|
|
|
|
{
|
|
|
|
|
private static Logger _instance;
|
|
|
|
|
|
|
|
|
|
private LogLevel _minLogLevel;
|
|
|
|
|
private bool _enableConsoleOutput;
|
|
|
|
|
|
|
|
|
|
private Logger()
|
|
|
|
|
{
|
|
|
|
|
_minLogLevel = LogLevel.Info;
|
|
|
|
|
_enableConsoleOutput = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static Logger Instance
|
|
|
|
|
{
|
|
|
|
|
get
|
|
|
|
|
{
|
|
|
|
|
if (_instance == null)
|
|
|
|
|
{
|
|
|
|
|
_instance = new Logger();
|
|
|
|
|
}
|
|
|
|
|
return _instance;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public LogLevel MinLogLevel
|
|
|
|
|
{
|
|
|
|
|
get { return _minLogLevel; }
|
|
|
|
|
set { _minLogLevel = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool EnableConsoleOutput
|
|
|
|
|
{
|
|
|
|
|
get { return _enableConsoleOutput; }
|
|
|
|
|
set { _enableConsoleOutput = value; }
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Log(LogLevel level, string source, string message)
|
|
|
|
|
{
|
|
|
|
|
if (level < _minLogLevel)
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var entry = new LogEntry(level, source, message);
|
|
|
|
|
|
|
|
|
|
if (_enableConsoleOutput)
|
|
|
|
|
{
|
|
|
|
|
WriteToConsole(entry);
|
|
|
|
|
}
|
2026-03-13 21:30:02 +08:00
|
|
|
|
|
|
|
|
CMLeonOS.Logger.Log.Emit(level, source, message);
|
2026-02-04 23:49:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Debug(string source, string message)
|
|
|
|
|
{
|
|
|
|
|
Log(LogLevel.Debug, source, message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Info(string source, string message)
|
|
|
|
|
{
|
|
|
|
|
Log(LogLevel.Info, source, message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Warning(string source, string message)
|
|
|
|
|
{
|
|
|
|
|
Log(LogLevel.Warning, source, message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Error(string source, string message)
|
|
|
|
|
{
|
|
|
|
|
Log(LogLevel.Error, source, message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Success(string source, string message)
|
|
|
|
|
{
|
|
|
|
|
Log(LogLevel.Success, source, message);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void WriteToConsole(LogEntry entry)
|
|
|
|
|
{
|
2026-02-06 16:15:24 +08:00
|
|
|
if (Settings.SettingsManager.LoggerEnabled) {
|
2026-03-17 20:42:44 +08:00
|
|
|
ConsoleColor originalBackgroundColor = Console.BackgroundColor;
|
|
|
|
|
ConsoleColor originalForegroundColor = Console.ForegroundColor;
|
2026-02-06 16:15:24 +08:00
|
|
|
|
2026-03-17 20:42:44 +08:00
|
|
|
// 日志等级
|
|
|
|
|
Console.Write("[ ");
|
2026-02-06 16:15:24 +08:00
|
|
|
switch (entry.Level)
|
|
|
|
|
{
|
|
|
|
|
case LogLevel.Debug:
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Gray;
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel.Info:
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.DarkCyan;
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel.Warning:
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Yellow;
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel.Error:
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Red;
|
|
|
|
|
break;
|
|
|
|
|
case LogLevel.Success:
|
|
|
|
|
Console.ForegroundColor = ConsoleColor.Green;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2026-02-04 23:49:08 +08:00
|
|
|
|
2026-03-17 20:42:44 +08:00
|
|
|
// 我的审美就是依托
|
|
|
|
|
// // 日志等级
|
|
|
|
|
// Console.ForegroundColor = ConsoleColor.White;
|
|
|
|
|
// Console.Write(entry.ToStringLevelStr());
|
|
|
|
|
|
|
|
|
|
// // 空格
|
|
|
|
|
// // Console.BackgroundColor = originalBackgroundColor;
|
|
|
|
|
// // Console.Write(" ");
|
|
|
|
|
|
|
|
|
|
// // 日志来源
|
|
|
|
|
// Console.BackgroundColor = ConsoleColor.Gray;
|
|
|
|
|
// Console.ForegroundColor = ConsoleColor.White;
|
|
|
|
|
// Console.Write(entry.ToStringSource());
|
|
|
|
|
|
|
|
|
|
// // 日志内容
|
|
|
|
|
// Console.BackgroundColor = originalBackgroundColor;
|
|
|
|
|
// Console.ForegroundColor = ConsoleColor.White;
|
|
|
|
|
// // 空格
|
|
|
|
|
// Console.Write(" ");
|
|
|
|
|
// Console.Write(entry.ToStringMessage());
|
|
|
|
|
// // 换行
|
|
|
|
|
// Console.WriteLine();
|
|
|
|
|
|
|
|
|
|
Console.Write(entry.ToStringLevelStr());
|
|
|
|
|
Console.ForegroundColor = originalForegroundColor;
|
|
|
|
|
Console.Write(" ] ");
|
|
|
|
|
|
|
|
|
|
// 日志来源
|
|
|
|
|
Console.Write($"( {entry.Source} ) ");
|
|
|
|
|
|
|
|
|
|
// 日志内容
|
|
|
|
|
Console.Write(entry.ToStringMessage());
|
|
|
|
|
|
|
|
|
|
// 换行
|
|
|
|
|
Console.WriteLine();
|
|
|
|
|
|
|
|
|
|
Console.ForegroundColor = originalForegroundColor;
|
|
|
|
|
Console.BackgroundColor = originalBackgroundColor;
|
2026-02-06 16:15:24 +08:00
|
|
|
}
|
2026-02-04 23:49:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|