mirror of
https://github.com/Jackett/Jackett.git
synced 2025-09-17 17:34:09 +02:00
Refactor done
This commit is contained in:
64
src/Jackett/Services/ProcessService.cs
Normal file
64
src/Jackett/Services/ProcessService.cs
Normal file
@@ -0,0 +1,64 @@
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Jackett.Services
|
||||
{
|
||||
public interface IProcessService
|
||||
{
|
||||
void StartProcessAndLog(string exe, string args);
|
||||
}
|
||||
|
||||
public class ProcessService : IProcessService
|
||||
{
|
||||
private Logger logger;
|
||||
|
||||
public ProcessService(Logger l)
|
||||
{
|
||||
logger = l;
|
||||
}
|
||||
|
||||
public void StartProcessAndLog(string exe, string args)
|
||||
{
|
||||
var startInfo = new ProcessStartInfo()
|
||||
{
|
||||
CreateNoWindow = true,
|
||||
UseShellExecute = false,
|
||||
FileName = exe,
|
||||
Arguments = args,
|
||||
RedirectStandardError = true,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardInput = true
|
||||
};
|
||||
|
||||
var proc = Process.Start(startInfo);
|
||||
proc.OutputDataReceived += proc_OutputDataReceived;
|
||||
proc.ErrorDataReceived += proc_ErrorDataReceived;
|
||||
proc.BeginErrorReadLine();
|
||||
proc.BeginOutputReadLine();
|
||||
proc.WaitForExit();
|
||||
proc.OutputDataReceived -= proc_OutputDataReceived;
|
||||
proc.ErrorDataReceived -= proc_ErrorDataReceived;
|
||||
}
|
||||
|
||||
void proc_ErrorDataReceived(object sender, DataReceivedEventArgs e)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(e.Data))
|
||||
{
|
||||
logger.Error(e.Data);
|
||||
}
|
||||
}
|
||||
|
||||
void proc_OutputDataReceived(object sender, DataReceivedEventArgs e)
|
||||
{
|
||||
if (!string.IsNullOrWhiteSpace(e.Data))
|
||||
{
|
||||
logger.Debug(e.Data);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user