From 9771a55394020adafcf9203e4cb47667fed56d66 Mon Sep 17 00:00:00 2001 From: Daryl Ronningen Date: Sat, 19 Nov 2022 20:52:12 -0800 Subject: [PATCH] save logs in a list for a later status command --- netsd/Utils/StartService.cs | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/netsd/Utils/StartService.cs b/netsd/Utils/StartService.cs index 270877e..7bd1654 100644 --- a/netsd/Utils/StartService.cs +++ b/netsd/Utils/StartService.cs @@ -5,6 +5,7 @@ namespace netsd.Utils; public class StartService { + private readonly List _logs; private readonly ServiceParser _parseServiceFile; private readonly Process _process; private readonly string _servicePath; @@ -14,6 +15,7 @@ public class StartService _parseServiceFile = parseServiceFile; _servicePath = servicePath; _process = new Process(); + _logs = new List(); _process.StartInfo.FileName = _parseServiceFile.ExecStart?.Split(' ').First(); _process.StartInfo.Arguments = string.Join(" ", _parseServiceFile.ExecStart?.Split(' ').Skip(1)!); @@ -23,9 +25,9 @@ public class StartService _process.EnableRaisingEvents = true; _process.OutputDataReceived += (sender, args) => - Init.Logger.Information($"({Path.GetFileName(_servicePath)}) {args.Data}"); + _logs.Add(new Log { Type = LogType.StdOut, Message = args.Data! }); _process.ErrorDataReceived += (sender, args) => - Init.Logger.Error($"({Path.GetFileName(_servicePath)}) {args.Data}"); + _logs.Add(new Log { Type = LogType.StdErr, Message = args.Data! }); _process.Exited += (sender, args) => { if (_process.ExitCode != 0) @@ -49,3 +51,15 @@ public class StartService await _process.WaitForExitAsync(); } } + +public struct Log +{ + public LogType Type; + public string Message; +} + +public enum LogType +{ + StdOut = 1, + StdErr = 2, +}