Browse Source

commit: add stdio sniffer

HOME 4 years ago
parent
commit
e9c6683507

+ 6 - 0
SoftwareDevelopAssistantTools.sln

@@ -12,6 +12,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
 		NuGet.config = NuGet.config
 	EndProjectSection
 EndProject
+Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StdioSniffer", "StdioSniffer\StdioSniffer.csproj", "{76ECB0D1-CA3C-4ECF-86DC-711E24FED6FE}"
+EndProject
 Global
 	GlobalSection(SolutionConfigurationPlatforms) = preSolution
 		Debug|Any CPU = Debug|Any CPU
@@ -26,6 +28,10 @@ Global
 		{9A38DF9F-2A22-413A-B51B-5647DCA8F131}.Debug|Any CPU.Build.0 = Debug|Any CPU
 		{9A38DF9F-2A22-413A-B51B-5647DCA8F131}.Release|Any CPU.ActiveCfg = Release|Any CPU
 		{9A38DF9F-2A22-413A-B51B-5647DCA8F131}.Release|Any CPU.Build.0 = Release|Any CPU
+		{76ECB0D1-CA3C-4ECF-86DC-711E24FED6FE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
+		{76ECB0D1-CA3C-4ECF-86DC-711E24FED6FE}.Debug|Any CPU.Build.0 = Debug|Any CPU
+		{76ECB0D1-CA3C-4ECF-86DC-711E24FED6FE}.Release|Any CPU.ActiveCfg = Release|Any CPU
+		{76ECB0D1-CA3C-4ECF-86DC-711E24FED6FE}.Release|Any CPU.Build.0 = Release|Any CPU
 	EndGlobalSection
 	GlobalSection(SolutionProperties) = preSolution
 		HideSolutionNode = FALSE

+ 71 - 0
StdioSniffer/Program.cs

@@ -0,0 +1,71 @@
+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();
+            }
+        }
+    }
+}

+ 36 - 0
StdioSniffer/Properties/AssemblyInfo.cs

@@ -0,0 +1,36 @@
+using System.Reflection;
+using System.Runtime.CompilerServices;
+using System.Runtime.InteropServices;
+
+// 有关程序集的一般信息由以下
+// 控制。更改这些特性值可修改
+// 与程序集关联的信息。
+[assembly: AssemblyTitle("StdioSniffer")]
+[assembly: AssemblyDescription("")]
+[assembly: AssemblyConfiguration("")]
+[assembly: AssemblyCompany("")]
+[assembly: AssemblyProduct("StdioSniffer")]
+[assembly: AssemblyCopyright("Copyright ©  2020")]
+[assembly: AssemblyTrademark("")]
+[assembly: AssemblyCulture("")]
+
+// 将 ComVisible 设置为 false 会使此程序集中的类型
+//对 COM 组件不可见。如果需要从 COM 访问此程序集中的类型
+//请将此类型的 ComVisible 特性设置为 true。
+[assembly: ComVisible(false)]
+
+// 如果此项目向 COM 公开,则下列 GUID 用于类型库的 ID
+[assembly: Guid("76ecb0d1-ca3c-4ecf-86dc-711e24fed6fe")]
+
+// 程序集的版本信息由下列四个值组成: 
+//
+//      主版本
+//      次版本
+//      生成号
+//      修订号
+//
+//可以指定所有这些值,也可以使用“生成号”和“修订号”的默认值
+//通过使用 "*",如下所示:
+// [assembly: AssemblyVersion("1.0.*")]
+[assembly: AssemblyVersion("1.0.0.0")]
+[assembly: AssemblyFileVersion("1.0.0.0")]

+ 62 - 0
StdioSniffer/StdioSniffer.csproj

@@ -0,0 +1,62 @@
+<?xml version="1.0" encoding="utf-8"?>
+<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+  <Import Project="C:\NuGetLocalRepo\ILRepack.2.0.18\build\ILRepack.props" Condition="Exists('C:\NuGetLocalRepo\ILRepack.2.0.18\build\ILRepack.props')" />
+  <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
+  <PropertyGroup>
+    <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
+    <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
+    <ProjectGuid>{76ECB0D1-CA3C-4ECF-86DC-711E24FED6FE}</ProjectGuid>
+    <OutputType>Exe</OutputType>
+    <RootNamespace>StdioSniffer</RootNamespace>
+    <AssemblyName>StdioSniffer</AssemblyName>
+    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
+    <FileAlignment>512</FileAlignment>
+    <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
+    <Deterministic>true</Deterministic>
+    <NuGetPackageImportStamp>
+    </NuGetPackageImportStamp>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugSymbols>true</DebugSymbols>
+    <DebugType>full</DebugType>
+    <Optimize>false</Optimize>
+    <OutputPath>bin\Debug\</OutputPath>
+    <DefineConstants>DEBUG;TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <LangVersion>8</LangVersion>
+  </PropertyGroup>
+  <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
+    <PlatformTarget>AnyCPU</PlatformTarget>
+    <DebugType>pdbonly</DebugType>
+    <Optimize>true</Optimize>
+    <OutputPath>bin\Release\</OutputPath>
+    <DefineConstants>TRACE</DefineConstants>
+    <ErrorReport>prompt</ErrorReport>
+    <WarningLevel>4</WarningLevel>
+    <LangVersion>8</LangVersion>
+  </PropertyGroup>
+  <ItemGroup>
+    <Compile Include="Program.cs" />
+    <Compile Include="Properties\AssemblyInfo.cs" />
+  </ItemGroup>
+  <ItemGroup>
+    <None Include="packages.config" />
+  </ItemGroup>
+  <ItemGroup>
+    <Reference Include="System" />
+  </ItemGroup>
+  <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
+  <Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
+    <PropertyGroup>
+      <ErrorText>这台计算机上缺少此项目引用的 NuGet 程序包。使用“NuGet 程序包还原”可下载这些程序包。有关更多信息,请参见 http://go.microsoft.com/fwlink/?LinkID=322105。缺少的文件是 {0}。</ErrorText>
+    </PropertyGroup>
+    <Error Condition="!Exists('C:\NuGetLocalRepo\ILRepack.2.0.18\build\ILRepack.props')" Text="$([System.String]::Format('$(ErrorText)', 'C:\NuGetLocalRepo\ILRepack.2.0.18\build\ILRepack.props'))" />
+  </Target>
+  <PropertyGroup>
+    <PostBuildEvent>if $(ConfigurationName) == Release if not exist $(TargetDir)Packed md $(TargetDir)Packed
+if $(ConfigurationName) == Release $(ILRepack) /ndebug /out:$(TargetDir)Packed\$(TargetFileName) $(TargetPath)
+if $(ConfigurationName) == Release if exist $(TargetDir)Packed\$(TargetFileName).config del $(TargetDir)Packed\$(TargetFileName).config</PostBuildEvent>
+  </PropertyGroup>
+</Project>

+ 4 - 0
StdioSniffer/packages.config

@@ -0,0 +1,4 @@
+<?xml version="1.0" encoding="utf-8"?>
+<packages>
+  <package id="ILRepack" version="2.0.18" targetFramework="net461" />
+</packages>