mirror of
https://github.com/Leonmmcoset/CMLeonOS.git
synced 2026-03-03 11:37:01 +00:00
日志系统3
This commit is contained in:
@@ -246,22 +246,19 @@ namespace CMLeonOS
|
||||
}
|
||||
|
||||
Console.WriteLine("--------------------------------");
|
||||
Console.WriteLine("Startup script execution completed.");
|
||||
_logger.Success("Kernel", "Startup script execution completed");
|
||||
}
|
||||
else
|
||||
{
|
||||
// 启动脚本不存在,创建空文件
|
||||
Console.WriteLine("Startup script not found, creating empty file...");
|
||||
_logger.Info("Kernel", "Startup script not found, creating empty file...");
|
||||
System.IO.File.WriteAllText(startupFilePath, "");
|
||||
Console.WriteLine("Created empty startup script at: " + startupFilePath);
|
||||
_logger.Info("Kernel", "Created empty startup script at 0:\\system\\startup.cm");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error("Kernel", $"Error executing startup script: {ex.Message}");
|
||||
ShowError($"Error executing startup script: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,14 +2,12 @@ using CosmosFtpServer;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using CMLeonOS.Logger;
|
||||
|
||||
namespace CMLeonOS
|
||||
{
|
||||
public class FileSystem
|
||||
{
|
||||
private string currentDirectory;
|
||||
private static CMLeonOS.Logger.Logger _logger = CMLeonOS.Logger.Logger.Instance;
|
||||
|
||||
public FileSystem()
|
||||
{
|
||||
@@ -26,9 +24,9 @@ namespace CMLeonOS
|
||||
if (string.IsNullOrEmpty(path))
|
||||
{
|
||||
currentDirectory = @"0:\";
|
||||
_logger.Info("FileSystem", "Changed to root directory");
|
||||
return;
|
||||
}
|
||||
|
||||
string fullPath = GetFullPath(path);
|
||||
|
||||
try
|
||||
@@ -36,16 +34,15 @@ namespace CMLeonOS
|
||||
if (Directory.Exists(fullPath))
|
||||
{
|
||||
currentDirectory = fullPath;
|
||||
_logger.Info("FileSystem", $"Changed directory to: {path}");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Warning("FileSystem", $"Directory not found: {path}");
|
||||
Console.WriteLine($"Directory not found: {path}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error("FileSystem", $"Error changing directory: {ex.Message}");
|
||||
Console.WriteLine($"Error changing directory: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -58,16 +55,16 @@ namespace CMLeonOS
|
||||
if (!Directory.Exists(fullPath))
|
||||
{
|
||||
Directory.CreateDirectory(fullPath);
|
||||
_logger.Info("FileSystem", $"Directory created: {path}");
|
||||
Console.WriteLine($"Directory created: {path}");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Warning("FileSystem", $"Directory already exists: {path}");
|
||||
Console.WriteLine($"Directory already exists: {path}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error("FileSystem", $"Error creating directory: {ex.Message}");
|
||||
Console.WriteLine($"Error creating directory: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -233,11 +230,11 @@ namespace CMLeonOS
|
||||
{
|
||||
// 创建或覆盖文件
|
||||
File.WriteAllText(fullPath, content);
|
||||
_logger.Info("FileSystem", $"File created: {path}");
|
||||
Console.WriteLine($"File created: {path}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error("FileSystem", $"Error creating file: {ex.Message}");
|
||||
Console.WriteLine($"Error creating file: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -250,16 +247,16 @@ namespace CMLeonOS
|
||||
if (File.Exists(fullPath))
|
||||
{
|
||||
File.WriteAllText(fullPath, content);
|
||||
_logger.Info("FileSystem", $"File written: {path}");
|
||||
Console.WriteLine($"File written: {path}");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Warning("FileSystem", $"File not found: {path}");
|
||||
Console.WriteLine($"File not found: {path}");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error("FileSystem", $"Error writing file: {ex.Message}");
|
||||
Console.WriteLine($"Error writing file: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,6 @@ using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using Sys = Cosmos.System;
|
||||
using CMLeonOS.Logger;
|
||||
|
||||
namespace CMLeonOS
|
||||
{
|
||||
@@ -24,16 +23,19 @@ namespace CMLeonOS
|
||||
private List<User> users;
|
||||
public bool fixmode = Kernel.FixMode;
|
||||
private User currentLoggedInUser;
|
||||
private static CMLeonOS.Logger.Logger _logger = CMLeonOS.Logger.Logger.Instance;
|
||||
|
||||
public void ShowError(string error)
|
||||
{
|
||||
_logger.Error("UserSystem", error);
|
||||
Console.ForegroundColor = ConsoleColor.Red;
|
||||
Console.WriteLine($"{error}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
|
||||
public void ShowSuccess(string message)
|
||||
{
|
||||
_logger.Success("UserSystem", message);
|
||||
Console.ForegroundColor = ConsoleColor.Green;
|
||||
Console.WriteLine($"{message}");
|
||||
Console.ResetColor();
|
||||
}
|
||||
|
||||
public static string HashPasswordSha256(string password)
|
||||
@@ -57,7 +59,6 @@ namespace CMLeonOS
|
||||
LoadUsers();
|
||||
|
||||
LoadHostname();
|
||||
_logger.Success("UserSystem", "User system initialized successfully");
|
||||
}
|
||||
|
||||
private void EnsureSysDirectoryExists()
|
||||
@@ -266,7 +267,6 @@ namespace CMLeonOS
|
||||
|
||||
public void FirstTimeSetup()
|
||||
{
|
||||
_logger.Info("UserSystem", "Starting first time setup");
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine(" First Time Setup");
|
||||
Console.WriteLine("====================================");
|
||||
@@ -320,7 +320,6 @@ namespace CMLeonOS
|
||||
users.Add(adminUser);
|
||||
SaveUsers();
|
||||
ShowSuccess("Admin user created successfully!");
|
||||
_logger.Info("UserSystem", $"Admin user '{username}' created");
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Please set system hostname:");
|
||||
@@ -339,7 +338,6 @@ namespace CMLeonOS
|
||||
users[0].Hostname = hostname;
|
||||
SaveHostname();
|
||||
ShowSuccess($"Hostname set to: {hostname}");
|
||||
_logger.Info("UserSystem", $"Hostname set to: {hostname}");
|
||||
}
|
||||
|
||||
Console.WriteLine();
|
||||
@@ -358,7 +356,6 @@ namespace CMLeonOS
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error("UserSystem", $"Error creating admin user: {ex.Message}");
|
||||
ShowError($"Error creating admin user: {ex.Message}");
|
||||
}
|
||||
}
|
||||
@@ -391,7 +388,6 @@ namespace CMLeonOS
|
||||
|
||||
public bool Login()
|
||||
{
|
||||
_logger.Info("UserSystem", "Starting login process");
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine(" System Login");
|
||||
Console.WriteLine("====================================");
|
||||
@@ -410,7 +406,6 @@ namespace CMLeonOS
|
||||
useFixMode = true;
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Fix Mode Activated");
|
||||
_logger.Warning("UserSystem", "Fix mode activated");
|
||||
Console.Write("Enter fix code: ");
|
||||
|
||||
string fixCode = "";
|
||||
@@ -439,12 +434,10 @@ namespace CMLeonOS
|
||||
if (fixCode == "FixMyComputer")
|
||||
{
|
||||
Console.WriteLine("Fix mode enabled!");
|
||||
_logger.Info("UserSystem", "Fix mode enabled with correct code");
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine("Invalid fix code. Exiting fix mode.");
|
||||
_logger.Warning("UserSystem", "Invalid fix code provided");
|
||||
useFixMode = false;
|
||||
}
|
||||
}
|
||||
@@ -478,7 +471,6 @@ namespace CMLeonOS
|
||||
if (foundUser == null)
|
||||
{
|
||||
ShowError("User not found.");
|
||||
_logger.Warning("UserSystem", $"Login failed: User '{username}' not found");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -490,12 +482,10 @@ namespace CMLeonOS
|
||||
if (foundUser.Password != hashedInputPassword)
|
||||
{
|
||||
ShowError("Invalid password.");
|
||||
_logger.Warning("UserSystem", $"Login failed: Invalid password for user '{username}'");
|
||||
return false;
|
||||
}
|
||||
|
||||
ShowSuccess("Login successful!");
|
||||
_logger.Info("UserSystem", $"User '{username}' logged in successfully");
|
||||
Console.Beep();
|
||||
|
||||
// 设置当前登录用户
|
||||
@@ -511,14 +501,12 @@ namespace CMLeonOS
|
||||
{
|
||||
// 如果读取按键失败,使用普通登录
|
||||
ShowError("Error reading key input. Using normal login.");
|
||||
_logger.Error("UserSystem", "Error reading key input during login");
|
||||
return false;
|
||||
}
|
||||
|
||||
// 如果使用了修复模式,返回true
|
||||
if (useFixMode)
|
||||
{
|
||||
_logger.Info("UserSystem", "Login bypassed via fix mode");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -527,7 +515,6 @@ namespace CMLeonOS
|
||||
|
||||
public bool AddUser(string args, bool isAdmin)
|
||||
{
|
||||
_logger.Info("UserSystem", $"Starting to add {(isAdmin ? "admin" : "user")}");
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine($" Add {(isAdmin ? "Admin" : "User")}");
|
||||
Console.WriteLine("====================================");
|
||||
@@ -549,7 +536,6 @@ namespace CMLeonOS
|
||||
if (user.Username.ToLower() == username.ToLower())
|
||||
{
|
||||
ShowError($"Error: User '{username}' already exists.");
|
||||
_logger.Warning("UserSystem", $"Add user failed: User '{username}' already exists");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -570,13 +556,11 @@ namespace CMLeonOS
|
||||
|
||||
ShowSuccess($"{(isAdmin ? "Admin" : "User")} '{username}' created successfully!");
|
||||
ShowSuccess("You shall restart the system to apply the changes.");
|
||||
_logger.Info("UserSystem", $"{(isAdmin ? "Admin" : "User")} '{username}' created successfully");
|
||||
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error("UserSystem", $"Error adding user: {ex.Message}");
|
||||
ShowError($"Error adding user: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
@@ -584,7 +568,6 @@ namespace CMLeonOS
|
||||
|
||||
public bool DeleteUser(string username)
|
||||
{
|
||||
_logger.Info("UserSystem", $"Starting to delete user '{username}'");
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine(" Delete User");
|
||||
Console.WriteLine("====================================");
|
||||
@@ -610,7 +593,6 @@ namespace CMLeonOS
|
||||
if (foundUser == null)
|
||||
{
|
||||
ShowError($"Error: User '{username}' not found.");
|
||||
_logger.Warning("UserSystem", $"Delete user failed: User '{username}' not found");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -627,7 +609,6 @@ namespace CMLeonOS
|
||||
if (foundUser.IsAdmin && adminCount <= 1)
|
||||
{
|
||||
ShowError("Error: Cannot delete the last admin user.");
|
||||
_logger.Warning("UserSystem", "Delete user failed: Cannot delete the last admin user");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -636,12 +617,10 @@ namespace CMLeonOS
|
||||
users.Remove(foundUser);
|
||||
SaveUsers();
|
||||
ShowSuccess($"User '{username}' deleted successfully!");
|
||||
_logger.Info("UserSystem", $"User '{username}' deleted successfully");
|
||||
return true;
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error("UserSystem", $"Error deleting user: {ex.Message}");
|
||||
ShowError($"Error deleting user: {ex.Message}");
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using CMLeonOS.Logger;
|
||||
|
||||
namespace CMLeonOS
|
||||
{
|
||||
@@ -10,7 +9,6 @@ namespace CMLeonOS
|
||||
private static EnvironmentVariableManager instance;
|
||||
private string envFilePath = @"0:\system\env.dat";
|
||||
private Dictionary<string, string> environmentVariables;
|
||||
private static CMLeonOS.Logger.Logger _logger = CMLeonOS.Logger.Logger.Instance;
|
||||
|
||||
private EnvironmentVariableManager()
|
||||
{
|
||||
@@ -47,12 +45,11 @@ namespace CMLeonOS
|
||||
environmentVariables[varName] = varValue;
|
||||
}
|
||||
}
|
||||
_logger.Info("EnvManager", $"Loaded {environmentVariables.Count} environment variables from file");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error("EnvManager", $"Error loading environment variables: {ex.Message}");
|
||||
Console.WriteLine($"Error loading environment variables: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,7 +57,8 @@ namespace CMLeonOS
|
||||
{
|
||||
try
|
||||
{
|
||||
_logger.Info("EnvManager", $"Saving {environmentVariables.Count} environment variables to file");
|
||||
Console.WriteLine($"Saving environment variables to: {envFilePath}");
|
||||
Console.WriteLine($"Variables to save: {environmentVariables.Count}");
|
||||
|
||||
// 构建文件内容
|
||||
string content = "";
|
||||
@@ -69,6 +67,9 @@ namespace CMLeonOS
|
||||
content += $"{kvp.Key}={kvp.Value}\n";
|
||||
}
|
||||
|
||||
Console.WriteLine("Environment variables content:");
|
||||
Console.WriteLine(content);
|
||||
|
||||
// 使用FileSystem的WriteFile方法来确保在Cosmos中正常工作
|
||||
// 注意:这里需要访问FileSystem实例,但EnvironmentVariableManager是独立的
|
||||
// 所以我们使用File.WriteAllText,但添加重试机制
|
||||
@@ -82,23 +83,25 @@ namespace CMLeonOS
|
||||
{
|
||||
File.WriteAllText(envFilePath, content);
|
||||
success = true;
|
||||
_logger.Info("EnvManager", "Environment variables saved successfully");
|
||||
Console.WriteLine("Environment variables saved successfully.");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
retryCount++;
|
||||
_logger.Warning("EnvManager", $"Save attempt {retryCount} failed: {ex.Message}");
|
||||
Console.WriteLine($"Save attempt {retryCount} failed: {ex.Message}");
|
||||
// Thread.Sleep(1000); // 等待1秒后重试
|
||||
}
|
||||
}
|
||||
|
||||
if (!success)
|
||||
{
|
||||
_logger.Error("EnvManager", "Failed to save environment variables after 3 attempts");
|
||||
Console.WriteLine("Failed to save environment variables after 3 attempts.");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.Error("EnvManager", $"Error saving environment variables: {ex.Message}");
|
||||
Console.WriteLine($"Error saving environment variables: {ex.Message}");
|
||||
Console.WriteLine($"Exception type: {ex.GetType().Name}");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -106,13 +109,13 @@ namespace CMLeonOS
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(varName))
|
||||
{
|
||||
_logger.Warning("EnvManager", "Variable name cannot be empty");
|
||||
Console.WriteLine("Error: Variable name cannot be empty");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
environmentVariables[varName] = varValue;
|
||||
SaveEnvironmentVariables();
|
||||
Console.WriteLine($"Environment variable '{varName}' set to '{varValue}'");
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -133,15 +136,20 @@ namespace CMLeonOS
|
||||
{
|
||||
if (environmentVariables.Count == 0)
|
||||
{
|
||||
_logger.Info("EnvManager", "No environment variables set");
|
||||
Console.WriteLine("No environment variables set.");
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Info("EnvManager", $"Listing {environmentVariables.Count} environment variables");
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine(" Environment Variables");
|
||||
Console.WriteLine("====================================");
|
||||
Console.WriteLine();
|
||||
foreach (var kvp in environmentVariables)
|
||||
{
|
||||
_logger.Info("EnvManager", $" {kvp.Key}={kvp.Value}");
|
||||
Console.WriteLine($" {kvp.Key}={kvp.Value}");
|
||||
}
|
||||
Console.WriteLine();
|
||||
Console.WriteLine($"Total: {environmentVariables.Count} variables");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -151,12 +159,12 @@ namespace CMLeonOS
|
||||
{
|
||||
environmentVariables.Remove(varName);
|
||||
SaveEnvironmentVariables();
|
||||
_logger.Info("EnvManager", $"Environment variable '{varName}' deleted");
|
||||
Console.WriteLine($"Environment variable '{varName}' deleted");
|
||||
return true;
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.Warning("EnvManager", $"Environment variable '{varName}' not found");
|
||||
Console.WriteLine($"Error: Environment variable '{varName}' not found");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user