|
| 1 | +/* |
| 2 | + * COPYRIGHT: See COPYING in the top level directory |
| 3 | + * PROJECT: CoreBuilder.FileManager |
| 4 | + * FILE: LogTailCommand.cs |
| 5 | + * PURPOSE: A simple log tail command that watches a file for new lines and prints them to the output. |
| 6 | + * PROGRAMMER: Peter Geinitz (Wayfarer) |
| 7 | + */ |
| 8 | + |
| 9 | +// ReSharper disable UnusedType.Global |
| 10 | + |
| 11 | +using System; |
| 12 | +using System.IO; |
| 13 | +using CoreBuilder.Helper; |
| 14 | +using CoreBuilder.Interface; |
| 15 | +using Weaver; |
| 16 | +using Weaver.Interfaces; |
| 17 | +using Weaver.Messages; |
| 18 | + |
| 19 | +namespace CoreBuilder.FileManager |
| 20 | +{ |
| 21 | + /// <inheritdoc /> |
| 22 | + /// <summary> |
| 23 | + /// A simple log tail command that watches a file for new lines and prints them to the output. |
| 24 | + /// </summary> |
| 25 | + /// <seealso cref="ICommand" /> |
| 26 | + public sealed class LogTailCommand : ICommand |
| 27 | + { |
| 28 | + /// <inheritdoc /> |
| 29 | + public string Name => "LogTail"; |
| 30 | + |
| 31 | + /// <inheritdoc /> |
| 32 | + public string Description => "Watches a file and prints newly appended lines."; |
| 33 | + |
| 34 | + /// <inheritdoc /> |
| 35 | + public string Namespace => "FileManager"; |
| 36 | + |
| 37 | + /// <inheritdoc /> |
| 38 | + public int ParameterCount => 1; |
| 39 | + |
| 40 | + /// <inheritdoc /> |
| 41 | + public CommandSignature Signature => new(Namespace, Name, ParameterCount); |
| 42 | + |
| 43 | + /// <summary> |
| 44 | + /// The output |
| 45 | + /// </summary> |
| 46 | + private readonly IEventOutput _output; |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// The watcher |
| 50 | + /// </summary> |
| 51 | + private FileSystemWatcher? _watcher; |
| 52 | + |
| 53 | + /// <summary> |
| 54 | + /// The last length |
| 55 | + /// </summary> |
| 56 | + private long _lastLength; |
| 57 | + |
| 58 | + /// <summary> |
| 59 | + /// Initializes a new instance of the <see cref="LogTailCommand"/> class. |
| 60 | + /// </summary> |
| 61 | + /// <param name="output">The output.</param> |
| 62 | + public LogTailCommand(IEventOutput? output = null) |
| 63 | + { |
| 64 | + _output = output ?? new ConsoleEventOutput(); |
| 65 | + } |
| 66 | + |
| 67 | + /// <inheritdoc /> |
| 68 | + public CommandResult Execute(params string[] args) |
| 69 | + { |
| 70 | + var filePath = args[0]; |
| 71 | + |
| 72 | + if (!File.Exists(filePath)) |
| 73 | + return CommandResult.Fail($"File does not exist: {filePath}"); |
| 74 | + |
| 75 | + var directory = Path.GetDirectoryName(filePath)!; |
| 76 | + var fileName = Path.GetFileName(filePath); |
| 77 | + |
| 78 | + _lastLength = new FileInfo(filePath).Length; |
| 79 | + |
| 80 | + _watcher = new FileSystemWatcher(directory) |
| 81 | + { |
| 82 | + Filter = fileName, |
| 83 | + NotifyFilter = NotifyFilters.Size | NotifyFilters.LastWrite, |
| 84 | + EnableRaisingEvents = true |
| 85 | + }; |
| 86 | + |
| 87 | + _watcher.Changed += (s, e) => ReadNewContent(filePath); |
| 88 | + |
| 89 | + return new CommandResult |
| 90 | + { |
| 91 | + Message = $"Watching file '{filePath}' for new content...", |
| 92 | + RequiresConfirmation = true, |
| 93 | + Feedback = new FeedbackRequest( |
| 94 | + prompt: "Send 'stop' to end watching.", |
| 95 | + options: new[] { "stop" }, |
| 96 | + onRespond: input => |
| 97 | + { |
| 98 | + if (input.Trim().Equals("stop", StringComparison.OrdinalIgnoreCase)) |
| 99 | + { |
| 100 | + StopWatching(); |
| 101 | + return CommandResult.Ok("Log watching stopped."); |
| 102 | + } |
| 103 | + |
| 104 | + return new CommandResult |
| 105 | + { |
| 106 | + Message = $"Unknown input '{input}'. Type 'stop'.", |
| 107 | + RequiresConfirmation = true |
| 108 | + }; |
| 109 | + }) |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + /// <summary> |
| 114 | + /// Reads the new content. |
| 115 | + /// </summary> |
| 116 | + /// <param name="filePath">The file path.</param> |
| 117 | + private void ReadNewContent(string filePath) |
| 118 | + { |
| 119 | + try |
| 120 | + { |
| 121 | + var fileInfo = new FileInfo(filePath); |
| 122 | + if (fileInfo.Length < _lastLength) |
| 123 | + { |
| 124 | + // file rotated or truncated |
| 125 | + _lastLength = 0; |
| 126 | + } |
| 127 | + |
| 128 | + using var fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite); |
| 129 | + fs.Seek(_lastLength, SeekOrigin.Begin); |
| 130 | + |
| 131 | + using var reader = new StreamReader(fs); |
| 132 | + var newText = reader.ReadToEnd(); |
| 133 | + |
| 134 | + if (!string.IsNullOrEmpty(newText)) |
| 135 | + _output.Write(newText.TrimEnd()); |
| 136 | + |
| 137 | + _lastLength = fileInfo.Length; |
| 138 | + } |
| 139 | + catch (Exception ex) |
| 140 | + { |
| 141 | + _output.Write($"[ERROR] {ex.Message}"); |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + /// <summary> |
| 146 | + /// Stops the watching. |
| 147 | + /// </summary> |
| 148 | + private void StopWatching() |
| 149 | + { |
| 150 | + if (_watcher != null) |
| 151 | + { |
| 152 | + _watcher.EnableRaisingEvents = false; |
| 153 | + _watcher.Dispose(); |
| 154 | + _watcher = null; |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + /// <inheritdoc /> |
| 159 | + public CommandResult InvokeExtension(string extensionName, params string[] args) |
| 160 | + => CommandResult.Fail($"'{Name}' has no extensions."); |
| 161 | + } |
| 162 | +} |
0 commit comments