| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Diagnostics.CodeAnalysis; |
| | 4 | | using System.Linq; |
| | 5 | | using System.Text; |
| | 6 | | using Amusoft.DotnetNew.Tests.Diagnostics; |
| | 7 | | using Amusoft.DotnetNew.Tests.Interfaces; |
| | 8 | | using Amusoft.Toolkit.Threading; |
| | 9 | |
|
| | 10 | | namespace Amusoft.DotnetNew.Tests.Scopes; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Logging scope to capture any logs |
| | 14 | | /// </summary> |
| | 15 | | public class LoggingScope : AmbientScope<LoggingScope> |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Constructor |
| | 19 | | /// </summary> |
| | 20 | | /// <param name="connected">whether to connect the current logging scope to its parent</param> |
| 39 | 21 | | public LoggingScope(bool connected = true) |
| | 22 | | { |
| 39 | 23 | | Connected = connected; |
| 39 | 24 | | if (ParentScope?._rewriters is { Count: > 0 } rewriters) |
| | 25 | | { |
| 112 | 26 | | foreach (var rewriter in rewriters) |
| | 27 | | { |
| 43 | 28 | | AddRewriter(rewriter); |
| | 29 | | } |
| | 30 | | } |
| 39 | 31 | | } |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Whether to forward rewriters, invocations and results to parent scopes |
| | 35 | | /// </summary> |
| 119 | 36 | | public bool Connected { get; } |
| | 37 | |
|
| 39 | 38 | | private readonly List<ICommandData> _commandData = new(); |
| 39 | 39 | | private readonly HashSet<ICommandRewriter> _rewriters = new(); |
| | 40 | |
|
| | 41 | | internal void AddRewriter(ICommandRewriter rewriter) |
| | 42 | | { |
| 65 | 43 | | _rewriters.Add(rewriter); |
| 65 | 44 | | if(Connected && ParentScope is { } parentScope) |
| 3 | 45 | | parentScope.AddRewriter(rewriter); |
| 65 | 46 | | } |
| | 47 | |
|
| | 48 | | internal void AddResult(ICommandResult result) |
| | 49 | | { |
| 34 | 50 | | _commandData.Add(result); |
| 34 | 51 | | if(Connected && ParentScope is { } parentScope) |
| 3 | 52 | | parentScope.AddResult(result); |
| 34 | 53 | | } |
| | 54 | |
|
| | 55 | | internal void AddInvocation(ICommandInvocation command) |
| | 56 | | { |
| 20 | 57 | | _commandData.Add(command); |
| 20 | 58 | | if(Connected && ParentScope is { } parentScope) |
| 3 | 59 | | parentScope.AddInvocation(command); |
| 20 | 60 | | } |
| | 61 | |
|
| | 62 | | /// <summary> |
| | 63 | | /// prints the output as string |
| | 64 | | /// </summary> |
| | 65 | | /// <param name="kind"></param> |
| | 66 | | /// <returns></returns> |
| | 67 | | public string ToFullString(PrintKind kind) |
| | 68 | | { |
| 14 | 69 | | var sb = new StringBuilder(); |
| 14 | 70 | | Print(sb, kind); |
| 14 | 71 | | return sb.ToString(); |
| | 72 | | } |
| | 73 | |
|
| | 74 | | /// <summary> |
| | 75 | | /// Prints the collected data |
| | 76 | | /// </summary> |
| | 77 | | /// <param name="stringBuilder"></param> |
| | 78 | | /// <param name="kind"></param> |
| | 79 | | internal void Print(StringBuilder stringBuilder, PrintKind kind) |
| | 80 | | { |
| 17 | 81 | | var data = GetPrintables(); |
| | 82 | |
|
| 76 | 83 | | foreach (var printable in data) |
| | 84 | | { |
| 21 | 85 | | printable.Print(stringBuilder); |
| | 86 | | } |
| | 87 | |
|
| 17 | 88 | | ApplyRewriters(stringBuilder); |
| | 89 | |
|
| | 90 | | [ExcludeFromCodeCoverage] |
| | 91 | | IEnumerable<IPrintable> GetPrintables() |
| | 92 | | { |
| | 93 | | IEnumerable<IPrintable> printables = kind switch |
| | 94 | | { |
| | 95 | | PrintKind.All => _commandData, |
| | 96 | | PrintKind.Invocations => _commandData.OfType<ICommandInvocation>(), |
| | 97 | | PrintKind.Responses => _commandData.OfType<ICommandResult>(), |
| | 98 | | _ => throw new ArgumentOutOfRangeException(nameof(kind), kind, null) |
| | 99 | | }; |
| | 100 | | return printables; |
| | 101 | | } |
| 17 | 102 | | } |
| | 103 | |
|
| | 104 | | private void ApplyRewriters(StringBuilder stringBuilder) |
| | 105 | | { |
| 97 | 106 | | foreach (var commandRewriter in _rewriters.OrderBy(d => d.ExecutionOrder)) |
| | 107 | | { |
| 21 | 108 | | commandRewriter.Rewrite(stringBuilder); |
| | 109 | | } |
| 17 | 110 | | } |
| | 111 | |
|
| 27 | 112 | | internal static void TryAddRewriter(ICommandRewriter item) => Current?.AddRewriter(item); |
| | 113 | |
|
| 13 | 114 | | internal static void TryAddInvocation(string item) => TryAddInvocation(new DotnetCommand(item)); |
| | 115 | |
|
| 13 | 116 | | internal static void TryAddInvocation(ICommandInvocation item) => Current?.AddInvocation(item); |
| | 117 | |
|
| 26 | 118 | | internal static void TryAddResult(ICommandResult item) => Current?.AddResult(item); |
| | 119 | |
|
| 1 | 120 | | internal static string? ToFullString() => Current?.ToFullString(PrintKind.All); |
| | 121 | | } |