1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- using System;
- using System.Diagnostics;
- using System.IO;
- using System.Linq;
- namespace StdioSniffer
- {
- internal class Program
- {
- private static void Main(string[] args)
- {
- var cmd = Environment.GetCommandLineArgs();
- var path = Path.GetDirectoryName(cmd[0]);
- var file = Path.GetFileNameWithoutExtension(cmd[0]);
- var ext = Path.GetExtension(cmd[0]);
- var target = Path.Combine(path, $"{file}-real{ext}");
- var targetArgs = string.Join(" ", cmd.Skip(1));
- var logPath = Path.Combine(path, $"{file}{ext}.stdio-sniff.{DateTime.Now:yyyyMMdd-HHmmss-fff}.log");
- using var fs = File.CreateText(logPath);
- fs.AutoFlush = true;
- fs.WriteLine($"CMDLINE:{targetArgs}");
- var process = new Process
- {
- StartInfo =
- {
- FileName = target,
- Arguments = targetArgs,
- UseShellExecute = false,
- CreateNoWindow = true,
- RedirectStandardError = true,
- RedirectStandardInput = true,
- RedirectStandardOutput = true
- }
- };
- process.OutputDataReceived += (sender, eventArgs) =>
- {
- var line = eventArgs.Data;
- fs.WriteLine($"STDOUT:{line}");
- Console.WriteLine(line);
- Console.Out.Flush();
- };
- process.ErrorDataReceived += (sender, eventArgs) =>
- {
- var line = eventArgs.Data;
- fs.WriteLine($"STDERR:{line}");
- Console.Error.WriteLine(line);
- Console.Error.Flush();
- };
- process.Start();
- process.BeginErrorReadLine();
- process.BeginOutputReadLine();
- while (false == process.HasExited)
- {
- var line = Console.ReadLine();
- fs.WriteLine($"STDIN:{line}");
- process.StandardInput.WriteLine(line);
- process.StandardInput.Flush();
- }
- }
- }
- }
|