From 19b92c85cbb563e4390cb5b2612fa6d97cb11dc4 Mon Sep 17 00:00:00 2001 From: MarcT1me Date: Wed, 25 Nov 2026 19:21:05 +0700 Subject: [PATCH 1/4] [KERNEL] [RUNTIME] [DOCS] describe the logging contracts, give modules the command line --- Docs/Architecture/Runtime.md | 2 +- .../Stellar.Kernel/LoggingSystem/ILogger.cs | 19 ++-- .../LoggingSystem/ILoggerCatalog.cs | 6 +- .../Stellar.Kernel/LoggingSystem/LogFormat.cs | 32 +++--- .../Stellar.Kernel/LoggingSystem/LogLevel.cs | 14 +-- .../LoggingSystem/LoggerExtensions.cs | 30 ++++-- .../LoggingSystem/LoggerFormat.cs | 68 +++++++------ .../Configuration/RuntimeSettings.cs | 29 ++++-- .../Lifecycle/ModuleStarter.cs | 17 ++-- .../Lifecycle/Stages/ConfigurationStage.cs | 7 +- .../ConfigurationTests.cs | 29 ++++-- .../ModuleStarterTests.cs | 98 +++++++++++++++++++ 12 files changed, 258 insertions(+), 93 deletions(-) create mode 100644 Runtime/tests/Stellar.Runtime.Tests/ModuleStarterTests.cs diff --git a/Docs/Architecture/Runtime.md b/Docs/Architecture/Runtime.md index 2f03918..4bc17b4 100644 --- a/Docs/Architecture/Runtime.md +++ b/Docs/Architecture/Runtime.md @@ -271,7 +271,7 @@ wins: | `ModuleInitializationParallelism` | `1` | How many modules of one level start at once | | `Verbose` | `false` | The runtime tells every step on the error output | -Settings the runtime does not know stay in `RuntimeConfiguration.Settings` for the modules. +Modules find all of them in `RuntimeConfiguration.Settings`, the command line included, by names in any case. ## Exit codes diff --git a/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/ILogger.cs b/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/ILogger.cs index 8018b26..2559f55 100644 --- a/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/ILogger.cs +++ b/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/ILogger.cs @@ -3,31 +3,34 @@ namespace Stellar.Kernel.LoggingSystem; /// -/// Engine logger +/// Takes messages: the logger of an engine, or one of those it gives them to (). /// +/// +/// Every member is safe to call from any thread. LoggerExtensions has a method for every level. +/// public interface ILogger : IQuant { /// - /// Create log with formatting + /// Logs a message in the format of its level (). /// - /// Log level - /// Log message + /// How serious the message is. + /// The message. void Log(LogLevel level, string message); /// - /// Create log without formatting + /// Logs a message as it is: no format and no level, so no level turns it away. /// - /// just message to write + /// The message. void LogWithoutFormat(string message); /// - /// TODO: summary + /// Starts taking messages. A logger may be started again after . /// void Start(); /// - /// TODO: summary + /// Stops taking messages, and writes out the ones taken before. /// void Stop(); } diff --git a/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/ILoggerCatalog.cs b/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/ILoggerCatalog.cs index 690b751..8cb32fe 100644 --- a/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/ILoggerCatalog.cs +++ b/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/ILoggerCatalog.cs @@ -3,7 +3,11 @@ namespace Stellar.Kernel.LoggingSystem; /// -/// TODO: summary +/// The loggers of one engine, by name: where its messages end up (a console, a file, a logger of a game). /// +/// +/// The logging module publishes it in the services of the engine. A module adds a logger when it starts and takes it +/// out when it stops; the one that added a logger owns it. +/// public interface ILoggerCatalog : ICatalog; diff --git a/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LogFormat.cs b/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LogFormat.cs index b611c6a..5f1e1fd 100644 --- a/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LogFormat.cs +++ b/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LogFormat.cs @@ -1,18 +1,24 @@ namespace Stellar.Kernel.LoggingSystem; /// -/// {0} - date yyyy-MMM-dd
-/// {1} - time HH:mm:ss.fff
-/// {2} - engine clock name
-/// {3} - engine clock time HH:mm:ss.fff
-/// {4} - LogLevel
-/// {5} - Engine instance name
-/// {6} - Logger instance name
-/// {7} - Thread ID
-/// {8} - C# Type name
-/// {9} - C# Method name
-/// {10} - message
+/// The format of the lines of one level: a composite format string (). ///
-/// Format string -/// Colorized Format string (ASCII) +/// +/// +/// PlaceholderWhat it is +/// {0}the date, yyyy-MM-dd +/// {1}the time, HH:mm:ss.fff +/// {2}the name of the engine clock +/// {3}the time of the engine clock, HH:mm:ss.fff +/// {4}the +/// {5}the name of the engine +/// {6}the name of the logger +/// {7}the identifier of the managed thread +/// {8}the type that logs; costs a walk of the stack, as {9} does +/// {9}the method that logs +/// {10}the message +/// +/// +/// The format string. +/// The same with ANSI colors, for consoles; null for none. public record struct LogFormat(string Format, string? ColorizedFormat = null); diff --git a/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LogLevel.cs b/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LogLevel.cs index 9957153..50e2c77 100644 --- a/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LogLevel.cs +++ b/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LogLevel.cs @@ -1,37 +1,37 @@ namespace Stellar.Kernel.LoggingSystem; /// -/// Level of log +/// How serious a message is, the least serious first: a logger that takes a level takes every one after it. /// public enum LogLevel { /// - /// Debug message or information + /// For those who work on the code: steps, values. /// Debug = 0, /// - /// Any Information + /// What happens in the course of things. /// Info = 1, /// - /// Operation succeed + /// Something worth telling succeeded. /// Success = 2, /// - /// A Warning + /// Something is wrong, and the engine goes on as if it were not. /// Warning = 3, /// - /// An Error + /// Something failed, and what depends on it does not work. /// Error = 4, /// - /// An Exception + /// Something failed with an exception, told with it. /// Exception = 5 } diff --git a/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LoggerExtensions.cs b/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LoggerExtensions.cs index 9b75f0e..b455c7a 100644 --- a/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LoggerExtensions.cs +++ b/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LoggerExtensions.cs @@ -4,53 +4,66 @@ namespace Stellar.Kernel.LoggingSystem; /// -/// TODO: summary +/// A method of for every level. /// public static class LoggerExtensions { /// - /// TODO: summary + /// Logs a message of : what happens in the course of things. /// + /// The logger. + /// The message. public static void Info(this ILogger logger, string message) { logger.Log(LogLevel.Info, message); } /// - /// TODO: summary + /// Logs a message of : for those who work on the code. /// + /// The logger. + /// The message. public static void Debug(this ILogger logger, string message) { logger.Log(LogLevel.Debug, message); } /// - /// TODO: summary + /// Logs a message of : a success worth telling. /// + /// The logger. + /// The message. public static void Success(this ILogger logger, string message) { logger.Log(LogLevel.Success, message); } /// - /// TODO: summary + /// Logs a message of : something wrong the engine goes on with. /// + /// The logger. + /// The message. public static void Warning(this ILogger logger, string message) { logger.Log(LogLevel.Warning, message); } /// - /// TODO: summary + /// Logs a message of : a failure. /// + /// The logger. + /// The message. public static void Error(this ILogger logger, string message) { logger.Log(LogLevel.Error, message); } /// - /// TODO: summary + /// Logs a message of with the exception behind it. /// + /// The logger. + /// What was being done. + /// The exception. public static void Exception(this ILogger logger, string message, Exception exception) { logger.Log(LogLevel.Exception, @@ -60,8 +73,9 @@ public static void Exception(this ILogger logger, string message, Exception exce } /// - /// TODO: summary + /// Logs an empty line. /// + /// The logger. public static void Separator(this ILogger logger) { logger.LogWithoutFormat(""); diff --git a/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LoggerFormat.cs b/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LoggerFormat.cs index f83eb4b..12319b2 100644 --- a/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LoggerFormat.cs +++ b/Kernel/src/Managed/Stellar.Kernel/LoggingSystem/LoggerFormat.cs @@ -3,27 +3,28 @@ namespace Stellar.Kernel.LoggingSystem; /// -/// Full logger formats alias +/// The formats of a logger, one per level. /// -/// Info log format -/// Debug log format -/// Success log format -/// Warning log format -/// Error log format -/// Exception log format +/// The format of . +/// The format of . +/// The format of . +/// The format of . +/// The format of . +/// The format of . public readonly record struct LoggerFormat( LogFormat Info, LogFormat Debug, LogFormat Success, LogFormat Warning, LogFormat Error, - LogFormat Exception) + LogFormat Exception +) { /// - /// Default Logger format + /// The formats a logger has when none are given: every placeholder of , colored by level. /// public static readonly LoggerFormat Default = new( - new() + new LogFormat { Format = "{0}:{1} | {2}:{3} | {4} | {5}:{6}:{7} : {8}.{9} - {10}", ColorizedFormat = "\e[32m{0}\e[0m" + @@ -34,7 +35,7 @@ public readonly record struct LoggerFormat( "\e[31m:\e[0m" + "\e[2;32m{3}\e[0m " + "\e[31m|\e[0m " + - "\e[1;38m{4}\e[0m " + + "\e[1;37m{4}\e[0m " + "\e[31m|\e[0m " + "\e[36m{5}\e[0m" + "\e[31m:\e[0m" + @@ -46,9 +47,9 @@ public readonly record struct LoggerFormat( "\e[31m.\e[0m" + "\e[36m{9}\e[0m " + "\e[31m-\e[0m " + - "\e[1;38m{10}\e[0m" + "\e[1;37m{10}\e[0m" }, - new() + new LogFormat { Format = "{0}:{1} | {2}:{3} | {4} | {5}:{6}:{7} : {8}.{9} - {10}", ColorizedFormat = "\e[32m{0}\e[0m" + @@ -73,7 +74,7 @@ public readonly record struct LoggerFormat( "\e[31m-\e[0m " + "\e[1;33m{10}\e[0m" }, - new() + new LogFormat { Format = "{0}:{1} | {2}:{3} | {4} | {5}:{6}:{7} : {8}.{9} - {10}", ColorizedFormat = "\e[32m{0}\e[0m" + @@ -98,7 +99,7 @@ public readonly record struct LoggerFormat( "\e[31m-\e[0m " + "\e[1;32m{10}\e[0m" }, - new() + new LogFormat { Format = "{0}:{1} | {2}:{3} | {4} | {5}:{6}:{7} : {8}.{9} - {10}", ColorizedFormat = "\e[32m{0}\e[0m" + @@ -123,7 +124,7 @@ public readonly record struct LoggerFormat( "\e[31m-\e[0m " + "\e[1;34m{10}\e[0m" }, - new() + new LogFormat { Format = "{0}:{1} | {2}:{3} | {4} | {5}:{6}:{7} : {8}.{9} - {10}", ColorizedFormat = "\e[32m{0}\e[0m" + @@ -146,9 +147,9 @@ public readonly record struct LoggerFormat( "\e[31m.\e[0m" + "\e[36m{9}\e[0m " + "\e[31m-\e[0m " + - "\e[1;38m{10}\e[0m" + "\e[1;37m{10}\e[0m" }, - new() + new LogFormat { Format = "{0}:{1} | {2}:{3} | {4} | {5}:{6}:{7} : {8}.{9} - {10}", ColorizedFormat = "\e[32m{0}\e[0m" + @@ -176,20 +177,23 @@ public readonly record struct LoggerFormat( ); /// - /// Gets LogFormat for a LogLevel + /// Gets the format of a level. /// - /// Log level - /// a LogFormat for given LogLevel - /// if LogLevel is out of LogLevel enum - public LogFormat ForLevel(LogLevel level) => level switch + /// The level. + /// The format of . + /// Thrown if is not one of . + public LogFormat ForLevel(LogLevel level) { - LogLevel.Info => Info, - LogLevel.Debug => Debug, - LogLevel.Success => Success, - LogLevel.Warning => Warning, - LogLevel.Error => Error, - LogLevel.Exception => Exception, - _ => throw new ArgumentException("LoggerFormat.ForLevel operate only levels writen in LogLevel enum", - nameof(level), null) - }; + return level switch + { + LogLevel.Info => Info, + LogLevel.Debug => Debug, + LogLevel.Success => Success, + LogLevel.Warning => Warning, + LogLevel.Error => Error, + LogLevel.Exception => Exception, + _ => throw new ArgumentException("LoggerFormat.ForLevel operate only levels writen in LogLevel enum", + nameof(level), null) + }; + } } diff --git a/Runtime/src/Managed/Stellar.Runtime/Configuration/RuntimeSettings.cs b/Runtime/src/Managed/Stellar.Runtime/Configuration/RuntimeSettings.cs index b22b9a4..d38e691 100644 --- a/Runtime/src/Managed/Stellar.Runtime/Configuration/RuntimeSettings.cs +++ b/Runtime/src/Managed/Stellar.Runtime/Configuration/RuntimeSettings.cs @@ -1,4 +1,5 @@ using System.Globalization; + using Stellar.Kernel.Configuration; namespace Stellar.Runtime.Configuration; @@ -77,18 +78,18 @@ private RuntimeSettings(Dictionary values) /// The settings. public static RuntimeSettings Create(RuntimeConfiguration? configuration, IEnumerable? arguments = null) { - var values = new Dictionary(StringComparer.OrdinalIgnoreCase); + Dictionary values = new(StringComparer.OrdinalIgnoreCase); if (configuration is not null) - foreach (var pair in configuration.Settings) + foreach (KeyValuePair pair in configuration.Settings) values[pair.Key] = pair.Value; - foreach (var argument in arguments ?? []) + foreach (string argument in arguments ?? []) { if (!argument.StartsWith(CommandLinePrefix, StringComparison.OrdinalIgnoreCase)) continue; - var setting = argument[CommandLinePrefix.Length..]; - var separator = setting.IndexOf('='); + string setting = argument[CommandLinePrefix.Length..]; + int separator = setting.IndexOf('='); if (separator > 0) values[setting[..separator]] = setting[(separator + 1)..]; @@ -109,15 +110,27 @@ public static RuntimeSettings Create(RuntimeConfiguration? configuration, IEnume return _values.GetValueOrDefault(name); } + /// + /// Gets every setting as modules see them: the project, then the command line. Names in any case. + /// + /// A copy. + public Dictionary ToDictionary() + { + return new Dictionary(_values, StringComparer.OrdinalIgnoreCase); + } + private bool GetBoolean(string name, bool fallback) { - return _values.TryGetValue(name, out var value) && bool.TryParse(value, out var result) ? result : fallback; + return _values.TryGetValue(name, out string? value) + && bool.TryParse(value, out bool result) + ? result + : fallback; } private double GetDouble(string name, double fallback) { - return _values.TryGetValue(name, out var value) && - double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var result) + return _values.TryGetValue(name, out string? value) + && double.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out double result) ? result : fallback; } diff --git a/Runtime/src/Managed/Stellar.Runtime/Lifecycle/ModuleStarter.cs b/Runtime/src/Managed/Stellar.Runtime/Lifecycle/ModuleStarter.cs index 92cc1fc..be06984 100644 --- a/Runtime/src/Managed/Stellar.Runtime/Lifecycle/ModuleStarter.cs +++ b/Runtime/src/Managed/Stellar.Runtime/Lifecycle/ModuleStarter.cs @@ -1,5 +1,6 @@ using System.Reflection; using System.Runtime.ExceptionServices; + using Stellar.Kernel.Configuration; using Stellar.Kernel.EntryPoint; using Stellar.Kernel.FailureSystem; @@ -36,7 +37,8 @@ public static void Start(RuntimeContext context, LoadedModule module) if (module.EntryPointType is null) return; - var entryPoint = Create(module.EntryPointType, context.Configuration ?? new RuntimeConfiguration()); + StellarEntryPoint entryPoint = + Create(module.EntryPointType, context.Configuration ?? new RuntimeConfiguration()); module.EntryPoint = entryPoint; if (entryPoint is IServiceHost host && context.Services is null) @@ -45,11 +47,11 @@ public static void Start(RuntimeContext context, LoadedModule module) RuntimeServices.Publish(context, host.Services); } - var services = context.Services; + IServiceCatalog? services = context.Services; var runContext = new RuntimeRunContext(context, services?.Find(), services?.Find(), services, context.Engine); - var code = entryPoint.Run(in runContext); + int code = entryPoint.Run(in runContext); // The module that owns the services may have brought a logger context.Log.Logger ??= context.Services?.Find(); @@ -77,7 +79,7 @@ public static void Stop(RuntimeContext context, LoadedModule module) { context.MainLoop?.Remove(entryPoint); - var reason = context.Control.IsStopRequested ? context.Control.StopReason : StopReason.Regular; + StopReason reason = context.Control.IsStopRequested ? context.Control.StopReason : StopReason.Regular; context.Catchers.Engine.Guard(() => { @@ -85,16 +87,18 @@ public static void Stop(RuntimeContext context, LoadedModule module) entryPoint.RequestStop(in stopContext); }, $"the stop of {module.Id}", FailureKind.Module, context); - context.Catchers.Engine.Guard(entryPoint.Dispose, $"the disposal of {module.Id}", FailureKind.Module, context); + context.Catchers.Engine.Guard(entryPoint.Dispose, $"the disposal of {module.Id}", FailureKind.Module, + context); if (entryPoint is IServiceHost host && ReferenceEquals(host.Services, context.Services)) { RuntimeServices.Withdraw(host.Services); context.Services = null; - context.Log.Logger = null; } module.EntryPoint = null; + + context.Log.Logger = context.Services?.Find(); } context.MarkStopped(module); @@ -112,6 +116,7 @@ public static void Stop(RuntimeContext context, LoadedModule module) private static bool TakesPartInFrames(StellarEntryPoint entryPoint) { + // ReSharper disable once SuspiciousTypeConversion.Global return entryPoint is IUpdatable or IFixedUpdatable or IRenderable; } diff --git a/Runtime/src/Managed/Stellar.Runtime/Lifecycle/Stages/ConfigurationStage.cs b/Runtime/src/Managed/Stellar.Runtime/Lifecycle/Stages/ConfigurationStage.cs index 0f4ddb6..87ed342 100644 --- a/Runtime/src/Managed/Stellar.Runtime/Lifecycle/Stages/ConfigurationStage.cs +++ b/Runtime/src/Managed/Stellar.Runtime/Lifecycle/Stages/ConfigurationStage.cs @@ -1,3 +1,4 @@ +using Stellar.Kernel.Configuration; using Stellar.Runtime.Configuration; using Stellar.Runtime.Failures; using Stellar.Runtime.Modules; @@ -20,10 +21,10 @@ public sealed class ConfigurationStage /// public ValueTask StartAsync(RuntimeContext context) { - var configuration = context.ConfigurationLocator.Read(context.Launch.ApplicationPath); - - context.Configuration = configuration; + RuntimeConfiguration configuration = + context.Configuration = context.ConfigurationLocator.Read(context.Launch.ApplicationPath); context.Settings = RuntimeSettings.Create(configuration, context.Launch.Arguments); + configuration.Settings = context.Settings.ToDictionary(); // applying Launch.Arguments to runtime config context.Log.Verbose |= context.Settings.Verbose; context.Plan = ModulePlan.From(configuration); diff --git a/Runtime/tests/Stellar.Runtime.Tests/ConfigurationTests.cs b/Runtime/tests/Stellar.Runtime.Tests/ConfigurationTests.cs index 063cebe..6f56ea4 100644 --- a/Runtime/tests/Stellar.Runtime.Tests/ConfigurationTests.cs +++ b/Runtime/tests/Stellar.Runtime.Tests/ConfigurationTests.cs @@ -17,7 +17,8 @@ private static string Fixture(string name) [Fact] public void AssemblyFile_ReadsTheAttribute_WithoutLoadingTheAssembly() { - var attribute = AssemblyFile.ReadConfigurationAttribute(Fixture("Stellar.Runtime.Tests.Application")); + StellarRuntimeConfigurationAttribute? attribute = + AssemblyFile.ReadConfigurationAttribute(Fixture("Stellar.Runtime.Tests.Application")); attribute.Should().NotBeNull(); attribute!.Source.Should().Be(RuntimeConfigurationSource.External); @@ -36,7 +37,8 @@ public void AssemblyFile_NoAttribute_IsNull() [Fact] public void Locator_ReadsAFileNextToTheAssembly() { - var configuration = new RuntimeConfigurationLocator().Read(Fixture("Stellar.Runtime.Tests.Application")); + RuntimeConfiguration configuration = + new RuntimeConfigurationLocator().Read(Fixture("Stellar.Runtime.Tests.Application")); configuration.EntryPoint.Should().Be("Stellar.Runtime.Tests.Application.Game"); configuration.Components.OfType().Single().Modules @@ -46,7 +48,8 @@ public void Locator_ReadsAFileNextToTheAssembly() [Fact] public void Locator_ReadsAnEmbeddedResource() { - var configuration = new RuntimeConfigurationLocator().Read(Fixture("Stellar.Runtime.Tests.Host")); + RuntimeConfiguration configuration = + new RuntimeConfigurationLocator().Read(Fixture("Stellar.Runtime.Tests.Host")); configuration.ProjectName.Should().Be("Host"); configuration.Settings["MaxFrames"].Should().Be("7"); @@ -56,14 +59,28 @@ public void Locator_ReadsAnEmbeddedResource() public void Locator_MissingFile_SaysWhere() { using var directory = new TempFolder(); - var copy = Path.Combine(directory.Path, "Stellar.Runtime.Tests.Application.dll"); + string copy = Path.Combine(directory.Path, "Stellar.Runtime.Tests.Application.dll"); File.Copy(Fixture("Stellar.Runtime.Tests.Application"), copy); - var action = () => new RuntimeConfigurationLocator().Read(copy); + Func action = () => new RuntimeConfigurationLocator().Read(copy); action.Should().Throw().WithMessage("*missing*stellar.runtime.config.json*"); } + [Fact] + public void Settings_ReachTheModules_WithTheCommandLine() + { + var configuration = new RuntimeConfiguration { Settings = { ["LogPath"] = "a.log", ["Custom"] = "x" } }; + + Dictionary settings = RuntimeSettings + .Create(configuration, ["--stellar:logpath=b.log", "--stellar:New=1"]).ToDictionary(); + + settings.Should().HaveCount(3); + settings["LogPath"].Should().Be("b.log"); + settings["CUSTOM"].Should().Be("x"); + settings["New"].Should().Be("1"); + } + [Fact] public void Settings_ProjectThenCommandLine() { @@ -104,7 +121,7 @@ public void Plan_RejectsDuplicates_AndWrongOrder() [Fact] public void Plan_WithoutModules_AsksForARebuild() { - var action = () => ModulePlan.From(new RuntimeConfiguration { ProjectName = "Old" }); + Func action = () => ModulePlan.From(new RuntimeConfiguration { ProjectName = "Old" }); action.Should().Throw().WithMessage("*rebuild*"); } diff --git a/Runtime/tests/Stellar.Runtime.Tests/ModuleStarterTests.cs b/Runtime/tests/Stellar.Runtime.Tests/ModuleStarterTests.cs new file mode 100644 index 0000000..61be659 --- /dev/null +++ b/Runtime/tests/Stellar.Runtime.Tests/ModuleStarterTests.cs @@ -0,0 +1,98 @@ +using System.Reflection; + +using Stellar.Kernel.Configuration; +using Stellar.Kernel.Configuration.Modules; +using Stellar.Kernel.EntryPoint; +using Stellar.Kernel.LoggingSystem; +using Stellar.Kernel.Services; +using Stellar.Runtime.Diagnostics; +using Stellar.Runtime.Hosting; +using Stellar.Runtime.Lifecycle; +using Stellar.Runtime.Modules; +using Stellar.Runtime.Threading; + +namespace Stellar.Runtime.Tests; + +public class ModuleStarterTests +{ + [Fact] + public void ALoggerWithdrawnByItsModule_IsNotWrittenToAnyMore() + { + var output = new StringWriter(); + using var dispatcher = new MainThreadDispatcher(); + using var control = new EngineControl(); + var context = + new RuntimeContext(new RuntimeLaunch("Tests"), new RuntimeLog(fallback: output), dispatcher, control) + { + Services = DispatchProxy.Create() + }; + var module = new LoadedModule(new RuntimeModuleData { Id = "Logging" }, typeof(Logging).Assembly, + typeof(Logging)); + + ModuleStarter.Start(context, module); + context.Log.Logger.Should().NotBeNull(); + + ModuleStarter.Stop(context, module); + context.Log.Error("after the logger"); + + output.ToString().Should().Contain("after the logger"); + } + + /// Publishes a logger, and takes it back when it stops. + public sealed class Logging(RuntimeConfiguration configuration) + : StellarEntryPoint("Tests/Logging", configuration) + { + private IServiceCatalog? _services; + + public override int Run(ref readonly TContext context) + { + _services = context.Services; + _services!.Set(DispatchProxy.Create()); + return 0; + } + + public override void RequestStop(ref readonly TContext context) + { + _services!.Remove(); + } + + public override void Dispose() + { + } + } + + public class Silent + : DispatchProxy + { + protected override object? Invoke(MethodInfo? method, object?[]? args) + { + return null; + } + } + + public class Services + : DispatchProxy + { + private readonly Dictionary _items = []; + + protected override object? Invoke(MethodInfo? method, object?[]? args) + { + var key = (Type)args![0]!; + + switch (method!.Name) + { + case "Set": + _items[key] = args[1]!; + return null; + case "Remove": + return _items.Remove(key); + case "TryGet": + bool found = _items.TryGetValue(key, out object? value); + args[1] = value; + return found; + default: + throw new NotSupportedException(method.Name); + } + } + } +} From 7499e0160bb1e0c236a45fc1b2a1eafcd1e545a5 Mon Sep 17 00:00:00 2001 From: MarcT1me Date: Wed, 25 Nov 2026 19:21:23 +0700 Subject: [PATCH 2/4] [SDK] [TOOLS] [DOCS] add marks in comments, pack a module before its tests --- Docs/Engine/Marks.md | 47 ++++ Docs/Engine/Modules.md | 6 +- Docs/Engine/toc.yml | 2 + SDK/src/Stellar.Cli/Commands/MarksCommands.cs | 165 +++++++++++++ .../Commands/Modules/ModuleCommands.cs | 61 ++--- SDK/src/Stellar.Cli/Program.cs | 20 +- .../Building/ModuleBuildPlanner.cs | 77 ++++--- SDK/src/Stellar.Tools/Marks/MarkCatalog.cs | 132 +++++++++++ SDK/src/Stellar.Tools/Marks/MarkRepository.cs | 217 ++++++++++++++++++ SDK/src/Stellar.Tools/Marks/MarkScanner.cs | 128 +++++++++++ SDK/tests/Stellar.Tools.Tests/MarkTests.cs | 198 ++++++++++++++++ SDK/tests/Stellar.Tools.Tests/ModuleTests.cs | 125 ++++++---- .../Stellar.Tools.Tests/TempDirectory.cs | 7 +- Shared/Marks/marks.json | 7 + 14 files changed, 1087 insertions(+), 105 deletions(-) create mode 100644 Docs/Engine/Marks.md create mode 100644 SDK/src/Stellar.Cli/Commands/MarksCommands.cs create mode 100644 SDK/src/Stellar.Tools/Marks/MarkCatalog.cs create mode 100644 SDK/src/Stellar.Tools/Marks/MarkRepository.cs create mode 100644 SDK/src/Stellar.Tools/Marks/MarkScanner.cs create mode 100644 SDK/tests/Stellar.Tools.Tests/MarkTests.cs create mode 100644 Shared/Marks/marks.json diff --git a/Docs/Engine/Marks.md b/Docs/Engine/Marks.md new file mode 100644 index 0000000..34317df --- /dev/null +++ b/Docs/Engine/Marks.md @@ -0,0 +1,47 @@ +# Marks + +- [README.md](../../README.md) + +A mark is metadata in a comment of the code: `#` and a name in angle brackets, capital letters, digits, `-` and `_`. +`MVP` as a mark in C# is a comment `// #` followed by ``. It is found in comments only, in C#, C++, shaders, +PowerShell, shell, YAML, TOML, XML, MSBuild and Markdown files. + +## Kinds + +| File | Whose | Committed | +|---|---|---| +| `Shared/Marks/marks.json` | of the engine, the same for everyone | yes | +| `config/marks.json` | of one machine; adds kinds and replaces those of the engine | never, git ignores `config/` | + +```json +{ "Marks": { "WIP": { "Level": "Release", "Description": "Not finished: it cannot be released" } } } +``` + +| Level | How far code with the mark goes | +|---|---| +| `Info` | anywhere | +| `Release` | into commits and `develop`, not into `master`, a release or the mirror | +| `Commit` | nowhere: not even into a commit | + +A mark of a kind that is in neither file is stopped everywhere: a typing error, or a kind of another machine. + +## Checking + +```powershell +stellar marks install --all # git checks every commit, merge and push of the core, the modules and the tools +stellar marks list --all # where the marks are +stellar marks check # the files of this repository +stellar marks check --staged # what the next commit changes +stellar marks check --all --stage release +stellar marks uninstall --all +``` + +| Where | What it checks | +|---|---| +| the hooks of git | what is committed, merged and pushed | +| `.github/workflows/marks.yml` | every push and pull request of the core; `master` as a release | +| `.github/workflows/release.yml` | before a release is tagged | +| `.github/workflows/mirror.yml` | the core and every component, before a snapshot | + +The hooks stop the commit when the `stellar` command is missing or older than the marks. They can be skipped with +`--no-verify`; the workflows cannot. A hook somebody else wrote is never replaced. diff --git a/Docs/Engine/Modules.md b/Docs/Engine/Modules.md index 957f2a3..c15ef83 100644 --- a/Docs/Engine/Modules.md +++ b/Docs/Engine/Modules.md @@ -20,11 +20,13 @@ stellar modules build --tests --pack |---|---| | `modules new` | Renders `Shared/Templates/Modules/