日志系统2

This commit is contained in:
2026-02-05 01:43:30 +08:00
parent 1bfa4cf1f3
commit ba22066a23
6 changed files with 63 additions and 46 deletions

View File

@@ -19,13 +19,14 @@
<Profile>VMware</Profile> <Profile>VMware</Profile>
<Description>Use VMware Player or Workstation to deploy and debug.</Description> <Description>Use VMware Player or Workstation to deploy and debug.</Description>
<PxeInterface>192.168.0.8</PxeInterface> <PxeInterface>192.168.0.8</PxeInterface>
<CompressionType>Gzip</CompressionType> <CompressionType>None</CompressionType>
<BinFormat>Elf</BinFormat> <BinFormat>Elf</BinFormat>
<DebugEnabled>False</DebugEnabled> <DebugEnabled>False</DebugEnabled>
<CompileVBEMultiboot>False</CompileVBEMultiboot> <CompileVBEMultiboot>False</CompileVBEMultiboot>
<RemoveBootDebugOutput>False</RemoveBootDebugOutput> <RemoveBootDebugOutput>False</RemoveBootDebugOutput>
<CosmosDisableDebugger>true</CosmosDisableDebugger> <CosmosDisableDebugger>true</CosmosDisableDebugger>
<CosmosDebugLevel>None</CosmosDebugLevel> <CosmosDebugLevel>None</CosmosDebugLevel>
<OptimizationLevel>1</OptimizationLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>

View File

@@ -57,8 +57,9 @@ namespace CMLeonOS
Console.WriteLine(@" | |___| | | | |__| __/ (_) | | | | |_| |___) |"); Console.WriteLine(@" | |___| | | | |__| __/ (_) | | | | |_| |___) |");
Console.WriteLine(@" \____|_| |_|_____\___|\___/|_| |_|____/|____/ "); Console.WriteLine(@" \____|_| |_|_____\___|\___/|_| |_|____/|____/ ");
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("CMLeonOS Project"); Console.WriteLine("The CMLeonOS Project");
Console.WriteLine("By LeonOS 2 Developement Team"); Console.WriteLine("By LeonOS 2 Developement Team");
Console.WriteLine(@"-------------------------------------------------");
// 记录系统启动时间用于uptime命令 // 记录系统启动时间用于uptime命令
SystemStartTime = DateTime.Now; SystemStartTime = DateTime.Now;
@@ -223,7 +224,6 @@ namespace CMLeonOS
// 检查文件是否为空 // 检查文件是否为空
if (lines.Length == 0 || (lines.Length == 1 && string.IsNullOrWhiteSpace(lines[0]))) if (lines.Length == 0 || (lines.Length == 1 && string.IsNullOrWhiteSpace(lines[0])))
{ {
Console.WriteLine("Startup script is empty, skipping...");
_logger.Warning("Kernel", "Startup script is empty, skipping"); _logger.Warning("Kernel", "Startup script is empty, skipping");
return; return;
} }

View File

@@ -89,7 +89,7 @@ namespace CMLeonOS.Logger
Console.ForegroundColor = ConsoleColor.Gray; Console.ForegroundColor = ConsoleColor.Gray;
break; break;
case LogLevel.Info: case LogLevel.Info:
Console.ForegroundColor = ConsoleColor.Cyan; Console.ForegroundColor = ConsoleColor.DarkCyan;
break; break;
case LogLevel.Warning: case LogLevel.Warning:
Console.ForegroundColor = ConsoleColor.Yellow; Console.ForegroundColor = ConsoleColor.Yellow;

View File

@@ -2,12 +2,14 @@ using CosmosFtpServer;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using CMLeonOS.Logger;
namespace CMLeonOS namespace CMLeonOS
{ {
public class FileSystem public class FileSystem
{ {
private string currentDirectory; private string currentDirectory;
private static CMLeonOS.Logger.Logger _logger = CMLeonOS.Logger.Logger.Instance;
public FileSystem() public FileSystem()
{ {
@@ -24,9 +26,9 @@ namespace CMLeonOS
if (string.IsNullOrEmpty(path)) if (string.IsNullOrEmpty(path))
{ {
currentDirectory = @"0:\"; currentDirectory = @"0:\";
_logger.Info("FileSystem", "Changed to root directory");
return; return;
} }
string fullPath = GetFullPath(path); string fullPath = GetFullPath(path);
try try
@@ -34,15 +36,16 @@ namespace CMLeonOS
if (Directory.Exists(fullPath)) if (Directory.Exists(fullPath))
{ {
currentDirectory = fullPath; currentDirectory = fullPath;
_logger.Info("FileSystem", $"Changed directory to: {path}");
} }
else else
{ {
Console.WriteLine($"Directory not found: {path}"); _logger.Warning("FileSystem", $"Directory not found: {path}");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Error changing directory: {ex.Message}"); _logger.Error("FileSystem", $"Error changing directory: {ex.Message}");
} }
} }
@@ -55,16 +58,16 @@ namespace CMLeonOS
if (!Directory.Exists(fullPath)) if (!Directory.Exists(fullPath))
{ {
Directory.CreateDirectory(fullPath); Directory.CreateDirectory(fullPath);
Console.WriteLine($"Directory created: {path}"); _logger.Info("FileSystem", $"Directory created: {path}");
} }
else else
{ {
Console.WriteLine($"Directory already exists: {path}"); _logger.Warning("FileSystem", $"Directory already exists: {path}");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Error creating directory: {ex.Message}"); _logger.Error("FileSystem", $"Error creating directory: {ex.Message}");
} }
} }
@@ -230,11 +233,11 @@ namespace CMLeonOS
{ {
// 创建或覆盖文件 // 创建或覆盖文件
File.WriteAllText(fullPath, content); File.WriteAllText(fullPath, content);
Console.WriteLine($"File created: {path}"); _logger.Info("FileSystem", $"File created: {path}");
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Error creating file: {ex.Message}"); _logger.Error("FileSystem", $"Error creating file: {ex.Message}");
} }
} }
@@ -247,16 +250,16 @@ namespace CMLeonOS
if (File.Exists(fullPath)) if (File.Exists(fullPath))
{ {
File.WriteAllText(fullPath, content); File.WriteAllText(fullPath, content);
Console.WriteLine($"File written: {path}"); _logger.Info("FileSystem", $"File written: {path}");
} }
else else
{ {
Console.WriteLine($"File not found: {path}"); _logger.Warning("FileSystem", $"File not found: {path}");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Error writing file: {ex.Message}"); _logger.Error("FileSystem", $"Error writing file: {ex.Message}");
} }
} }

View File

@@ -5,6 +5,7 @@ using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Threading; using System.Threading;
using Sys = Cosmos.System; using Sys = Cosmos.System;
using CMLeonOS.Logger;
namespace CMLeonOS namespace CMLeonOS
{ {
@@ -23,19 +24,16 @@ namespace CMLeonOS
private List<User> users; private List<User> users;
public bool fixmode = Kernel.FixMode; public bool fixmode = Kernel.FixMode;
private User currentLoggedInUser; private User currentLoggedInUser;
private static CMLeonOS.Logger.Logger _logger = CMLeonOS.Logger.Logger.Instance;
public void ShowError(string error) public void ShowError(string error)
{ {
Console.ForegroundColor = ConsoleColor.Red; _logger.Error("UserSystem", error);
Console.WriteLine($"{error}");
Console.ResetColor();
} }
public void ShowSuccess(string message) public void ShowSuccess(string message)
{ {
Console.ForegroundColor = ConsoleColor.Green; _logger.Success("UserSystem", message);
Console.WriteLine($"{message}");
Console.ResetColor();
} }
public static string HashPasswordSha256(string password) public static string HashPasswordSha256(string password)
@@ -59,6 +57,7 @@ namespace CMLeonOS
LoadUsers(); LoadUsers();
LoadHostname(); LoadHostname();
_logger.Success("UserSystem", "User system initialized successfully");
} }
private void EnsureSysDirectoryExists() private void EnsureSysDirectoryExists()
@@ -267,6 +266,7 @@ namespace CMLeonOS
public void FirstTimeSetup() public void FirstTimeSetup()
{ {
_logger.Info("UserSystem", "Starting first time setup");
Console.WriteLine("===================================="); Console.WriteLine("====================================");
Console.WriteLine(" First Time Setup"); Console.WriteLine(" First Time Setup");
Console.WriteLine("===================================="); Console.WriteLine("====================================");
@@ -320,6 +320,7 @@ namespace CMLeonOS
users.Add(adminUser); users.Add(adminUser);
SaveUsers(); SaveUsers();
ShowSuccess("Admin user created successfully!"); ShowSuccess("Admin user created successfully!");
_logger.Info("UserSystem", $"Admin user '{username}' created");
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Please set system hostname:"); Console.WriteLine("Please set system hostname:");
@@ -338,6 +339,7 @@ namespace CMLeonOS
users[0].Hostname = hostname; users[0].Hostname = hostname;
SaveHostname(); SaveHostname();
ShowSuccess($"Hostname set to: {hostname}"); ShowSuccess($"Hostname set to: {hostname}");
_logger.Info("UserSystem", $"Hostname set to: {hostname}");
} }
Console.WriteLine(); Console.WriteLine();
@@ -356,6 +358,7 @@ namespace CMLeonOS
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.Error("UserSystem", $"Error creating admin user: {ex.Message}");
ShowError($"Error creating admin user: {ex.Message}"); ShowError($"Error creating admin user: {ex.Message}");
} }
} }
@@ -388,6 +391,7 @@ namespace CMLeonOS
public bool Login() public bool Login()
{ {
_logger.Info("UserSystem", "Starting login process");
Console.WriteLine("===================================="); Console.WriteLine("====================================");
Console.WriteLine(" System Login"); Console.WriteLine(" System Login");
Console.WriteLine("===================================="); Console.WriteLine("====================================");
@@ -406,6 +410,7 @@ namespace CMLeonOS
useFixMode = true; useFixMode = true;
Console.WriteLine(); Console.WriteLine();
Console.WriteLine("Fix Mode Activated"); Console.WriteLine("Fix Mode Activated");
_logger.Warning("UserSystem", "Fix mode activated");
Console.Write("Enter fix code: "); Console.Write("Enter fix code: ");
string fixCode = ""; string fixCode = "";
@@ -434,10 +439,12 @@ namespace CMLeonOS
if (fixCode == "FixMyComputer") if (fixCode == "FixMyComputer")
{ {
Console.WriteLine("Fix mode enabled!"); Console.WriteLine("Fix mode enabled!");
_logger.Info("UserSystem", "Fix mode enabled with correct code");
} }
else else
{ {
Console.WriteLine("Invalid fix code. Exiting fix mode."); Console.WriteLine("Invalid fix code. Exiting fix mode.");
_logger.Warning("UserSystem", "Invalid fix code provided");
useFixMode = false; useFixMode = false;
} }
} }
@@ -471,6 +478,7 @@ namespace CMLeonOS
if (foundUser == null) if (foundUser == null)
{ {
ShowError("User not found."); ShowError("User not found.");
_logger.Warning("UserSystem", $"Login failed: User '{username}' not found");
return false; return false;
} }
@@ -482,10 +490,12 @@ namespace CMLeonOS
if (foundUser.Password != hashedInputPassword) if (foundUser.Password != hashedInputPassword)
{ {
ShowError("Invalid password."); ShowError("Invalid password.");
_logger.Warning("UserSystem", $"Login failed: Invalid password for user '{username}'");
return false; return false;
} }
ShowSuccess("Login successful!"); ShowSuccess("Login successful!");
_logger.Info("UserSystem", $"User '{username}' logged in successfully");
Console.Beep(); Console.Beep();
// 设置当前登录用户 // 设置当前登录用户
@@ -501,12 +511,14 @@ namespace CMLeonOS
{ {
// 如果读取按键失败,使用普通登录 // 如果读取按键失败,使用普通登录
ShowError("Error reading key input. Using normal login."); ShowError("Error reading key input. Using normal login.");
_logger.Error("UserSystem", "Error reading key input during login");
return false; return false;
} }
// 如果使用了修复模式返回true // 如果使用了修复模式返回true
if (useFixMode) if (useFixMode)
{ {
_logger.Info("UserSystem", "Login bypassed via fix mode");
return true; return true;
} }
@@ -515,6 +527,7 @@ namespace CMLeonOS
public bool AddUser(string args, bool isAdmin) public bool AddUser(string args, bool isAdmin)
{ {
_logger.Info("UserSystem", $"Starting to add {(isAdmin ? "admin" : "user")}");
Console.WriteLine("===================================="); Console.WriteLine("====================================");
Console.WriteLine($" Add {(isAdmin ? "Admin" : "User")}"); Console.WriteLine($" Add {(isAdmin ? "Admin" : "User")}");
Console.WriteLine("===================================="); Console.WriteLine("====================================");
@@ -536,6 +549,7 @@ namespace CMLeonOS
if (user.Username.ToLower() == username.ToLower()) if (user.Username.ToLower() == username.ToLower())
{ {
ShowError($"Error: User '{username}' already exists."); ShowError($"Error: User '{username}' already exists.");
_logger.Warning("UserSystem", $"Add user failed: User '{username}' already exists");
return false; return false;
} }
} }
@@ -556,11 +570,13 @@ namespace CMLeonOS
ShowSuccess($"{(isAdmin ? "Admin" : "User")} '{username}' created successfully!"); ShowSuccess($"{(isAdmin ? "Admin" : "User")} '{username}' created successfully!");
ShowSuccess("You shall restart the system to apply the changes."); ShowSuccess("You shall restart the system to apply the changes.");
_logger.Info("UserSystem", $"{(isAdmin ? "Admin" : "User")} '{username}' created successfully");
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.Error("UserSystem", $"Error adding user: {ex.Message}");
ShowError($"Error adding user: {ex.Message}"); ShowError($"Error adding user: {ex.Message}");
return false; return false;
} }
@@ -568,6 +584,7 @@ namespace CMLeonOS
public bool DeleteUser(string username) public bool DeleteUser(string username)
{ {
_logger.Info("UserSystem", $"Starting to delete user '{username}'");
Console.WriteLine("===================================="); Console.WriteLine("====================================");
Console.WriteLine(" Delete User"); Console.WriteLine(" Delete User");
Console.WriteLine("===================================="); Console.WriteLine("====================================");
@@ -593,6 +610,7 @@ namespace CMLeonOS
if (foundUser == null) if (foundUser == null)
{ {
ShowError($"Error: User '{username}' not found."); ShowError($"Error: User '{username}' not found.");
_logger.Warning("UserSystem", $"Delete user failed: User '{username}' not found");
return false; return false;
} }
@@ -609,6 +627,7 @@ namespace CMLeonOS
if (foundUser.IsAdmin && adminCount <= 1) if (foundUser.IsAdmin && adminCount <= 1)
{ {
ShowError("Error: Cannot delete the last admin user."); ShowError("Error: Cannot delete the last admin user.");
_logger.Warning("UserSystem", "Delete user failed: Cannot delete the last admin user");
return false; return false;
} }
@@ -617,10 +636,12 @@ namespace CMLeonOS
users.Remove(foundUser); users.Remove(foundUser);
SaveUsers(); SaveUsers();
ShowSuccess($"User '{username}' deleted successfully!"); ShowSuccess($"User '{username}' deleted successfully!");
_logger.Info("UserSystem", $"User '{username}' deleted successfully");
return true; return true;
} }
catch (Exception ex) catch (Exception ex)
{ {
_logger.Error("UserSystem", $"Error deleting user: {ex.Message}");
ShowError($"Error deleting user: {ex.Message}"); ShowError($"Error deleting user: {ex.Message}");
return false; return false;
} }

View File

@@ -1,6 +1,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using CMLeonOS.Logger;
namespace CMLeonOS namespace CMLeonOS
{ {
@@ -9,6 +10,7 @@ namespace CMLeonOS
private static EnvironmentVariableManager instance; private static EnvironmentVariableManager instance;
private string envFilePath = @"0:\system\env.dat"; private string envFilePath = @"0:\system\env.dat";
private Dictionary<string, string> environmentVariables; private Dictionary<string, string> environmentVariables;
private static CMLeonOS.Logger.Logger _logger = CMLeonOS.Logger.Logger.Instance;
private EnvironmentVariableManager() private EnvironmentVariableManager()
{ {
@@ -45,11 +47,12 @@ namespace CMLeonOS
environmentVariables[varName] = varValue; environmentVariables[varName] = varValue;
} }
} }
_logger.Info("EnvManager", $"Loaded {environmentVariables.Count} environment variables from file");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Error loading environment variables: {ex.Message}"); _logger.Error("EnvManager", $"Error loading environment variables: {ex.Message}");
} }
} }
@@ -57,8 +60,7 @@ namespace CMLeonOS
{ {
try try
{ {
Console.WriteLine($"Saving environment variables to: {envFilePath}"); _logger.Info("EnvManager", $"Saving {environmentVariables.Count} environment variables to file");
Console.WriteLine($"Variables to save: {environmentVariables.Count}");
// 构建文件内容 // 构建文件内容
string content = ""; string content = "";
@@ -67,9 +69,6 @@ namespace CMLeonOS
content += $"{kvp.Key}={kvp.Value}\n"; content += $"{kvp.Key}={kvp.Value}\n";
} }
Console.WriteLine("Environment variables content:");
Console.WriteLine(content);
// 使用FileSystem的WriteFile方法来确保在Cosmos中正常工作 // 使用FileSystem的WriteFile方法来确保在Cosmos中正常工作
// 注意这里需要访问FileSystem实例但EnvironmentVariableManager是独立的 // 注意这里需要访问FileSystem实例但EnvironmentVariableManager是独立的
// 所以我们使用File.WriteAllText但添加重试机制 // 所以我们使用File.WriteAllText但添加重试机制
@@ -83,25 +82,23 @@ namespace CMLeonOS
{ {
File.WriteAllText(envFilePath, content); File.WriteAllText(envFilePath, content);
success = true; success = true;
Console.WriteLine("Environment variables saved successfully."); _logger.Info("EnvManager", "Environment variables saved successfully");
} }
catch (Exception ex) catch (Exception ex)
{ {
retryCount++; retryCount++;
Console.WriteLine($"Save attempt {retryCount} failed: {ex.Message}"); _logger.Warning("EnvManager", $"Save attempt {retryCount} failed: {ex.Message}");
// Thread.Sleep(1000); // 等待1秒后重试
} }
} }
if (!success) if (!success)
{ {
Console.WriteLine("Failed to save environment variables after 3 attempts."); _logger.Error("EnvManager", "Failed to save environment variables after 3 attempts");
} }
} }
catch (Exception ex) catch (Exception ex)
{ {
Console.WriteLine($"Error saving environment variables: {ex.Message}"); _logger.Error("EnvManager", $"Error saving environment variables: {ex.Message}");
Console.WriteLine($"Exception type: {ex.GetType().Name}");
} }
} }
@@ -109,13 +106,13 @@ namespace CMLeonOS
{ {
if (string.IsNullOrWhiteSpace(varName)) if (string.IsNullOrWhiteSpace(varName))
{ {
_logger.Warning("EnvManager", "Variable name cannot be empty");
Console.WriteLine("Error: Variable name cannot be empty"); Console.WriteLine("Error: Variable name cannot be empty");
return false; return false;
} }
environmentVariables[varName] = varValue; environmentVariables[varName] = varValue;
SaveEnvironmentVariables(); SaveEnvironmentVariables();
Console.WriteLine($"Environment variable '{varName}' set to '{varValue}'");
return true; return true;
} }
@@ -136,20 +133,15 @@ namespace CMLeonOS
{ {
if (environmentVariables.Count == 0) if (environmentVariables.Count == 0)
{ {
Console.WriteLine("No environment variables set."); _logger.Info("EnvManager", "No environment variables set");
} }
else else
{ {
Console.WriteLine("===================================="); _logger.Info("EnvManager", $"Listing {environmentVariables.Count} environment variables");
Console.WriteLine(" Environment Variables");
Console.WriteLine("====================================");
Console.WriteLine();
foreach (var kvp in environmentVariables) foreach (var kvp in environmentVariables)
{ {
Console.WriteLine($" {kvp.Key}={kvp.Value}"); _logger.Info("EnvManager", $" {kvp.Key}={kvp.Value}");
} }
Console.WriteLine();
Console.WriteLine($"Total: {environmentVariables.Count} variables");
} }
} }
@@ -159,12 +151,12 @@ namespace CMLeonOS
{ {
environmentVariables.Remove(varName); environmentVariables.Remove(varName);
SaveEnvironmentVariables(); SaveEnvironmentVariables();
Console.WriteLine($"Environment variable '{varName}' deleted"); _logger.Info("EnvManager", $"Environment variable '{varName}' deleted");
return true; return true;
} }
else else
{ {
Console.WriteLine($"Error: Environment variable '{varName}' not found"); _logger.Warning("EnvManager", $"Environment variable '{varName}' not found");
return false; return false;
} }
} }