Files
UnrealEngineUWP/Engine/Source/Programs/AutomationTool/AutomationUtils/ProcessUtils.cs
Ben Marsh 75fa4e9e6d Copying //UE4/Dev-Core to //UE4/Dev-Main (Source: //UE4/Dev-Core @ 3386108)
#rb none
#lockdown Nick.Penwarden

==========================
MAJOR FEATURES + CHANGES
==========================

Change 3345860 on 2017/03/14 by Daniel.Lamb

	Fixed crash when building DLC

	#test Cook paragon.

Change 3347324 on 2017/03/15 by Gil.Gribb

	UE4 - Removed old code relating to FAsyncArchive, FAsyncIOSubsystemBase and package level compression. The editor now uses the lowest levels on the new async IO scheme.

Change 3347331 on 2017/03/15 by Robert.Manuszewski

	Fix for a crash caused by GC killing BP class (due to no strong references) but its CDO is being kept alive because it was in the same cluster as the class and was not marked as pending kill.

	#jira UE-42732

Change 3347371 on 2017/03/15 by Graeme.Thornton

	Fix for runtime asset cache not invalidating files with an outdated version number

Change 3349161 on 2017/03/16 by Steve.Robb

	Generated UFUNCTION FNames no longer exported.
	Misc refactors of code generation.

Change 3349167 on 2017/03/16 by Steve.Robb

	Unused TBoolConstant removed (the more general TIntegralConstant should be used instead).

Change 3349274 on 2017/03/16 by Gil.Gribb

	UE4 - Fix loading a package that is already loaded.

Change 3349534 on 2017/03/16 by Ben.Marsh

	UBT: Check that the SN-DBS service is running before attempting to use it.

Change 3349612 on 2017/03/16 by Gil.Gribb

	UE4 - Increased estimate of summary size.

Change 3350021 on 2017/03/16 by Gil.Gribb

	UE4 - Fixed crash in signature checks when mounting pak files.

Change 3350052 on 2017/03/16 by Ben.Marsh

	Remove invalid characters from macro names before passing as macro values. Prevents compile errors for projects which have apostrophes in the name.

Change 3350360 on 2017/03/16 by Ben.Marsh

	UAT: Fix non-threadsafe access of ExeToTimeInMs when spawning external processes.

Change 3351670 on 2017/03/17 by Ben.Marsh

	UBT: Ignore all default libraries when creating import libs. Sometimes #pragma comment(lib, ...) directives can add force additional libraries onto the linker/librarian command line. We don't want or need these included when generating import libraries, but they can cause errors due to search paths not being able to find them.

Change 3352289 on 2017/03/17 by Ben.Marsh

	Fix issues working with > 2GB archives caused by truncation of the return value from FArchive::Tell() down to 32-bits.

Change 3352390 on 2017/03/17 by Ben.Marsh

	Remove unused/out of date binaries for CrashReporter.

Change 3352392 on 2017/03/17 by Ben.Marsh

	Remove UnrealDocTool binaries. This is distributed through a Visual Studio plugin now.

Change 3352410 on 2017/03/17 by Ben.Marsh

	Remove P4ChangeReporter. I don't believe this is used any more.

Change 3352450 on 2017/03/17 by Ben.Marsh

	Disable including CrashReporter by default when packaging projects. This is only useful with a CrashReporter backend set up, which only usually applies to Epic internal projects.

Change 3352455 on 2017/03/17 by Ben.Marsh

	Remove RegisterPII and TranslatedWordsCountEstimator executables. Don't believe these are used any more.

Change 3352940 on 2017/03/17 by Wes.Hunt

	Update CRP to not send Slack queue size updates unless the waiting time is greater  than 1 minute.
	#codereview: jin.zhang

Change 3353658 on 2017/03/20 by Steve.Robb

	Fix for crash when importing a BP which has a populated TMap with an enum class key.

Change 3354056 on 2017/03/20 by Steve.Robb

	TAssetPtr<T> can now be constructed from a nullptr without a full definition of T.

Change 3356111 on 2017/03/21 by Graeme.Thornton

	Fix for UE-34131
	 - Support double and fname stat types in UFE stat export to CSV

	#jira UE-34131

Change 3358584 on 2017/03/22 by Daniel.Lamb

	Fixed the garbage collection keep flags when cleaning the sandbox for iterative cooking.

	#test Cook shootergame

Change 3360379 on 2017/03/23 by Gil.Gribb

	UE4 - Avoid adding a linker annotation if it actually hasn't changed. Improves ConditionalBeginDestroy performance.

Change 3360623 on 2017/03/23 by Gil.Gribb

	UE4 - Change from MarcA to avoid a redudnant removal of PrimitiveComponent from the streaming managers during ConditionalBeginDestroy.

Change 3360627 on 2017/03/23 by Gil.Gribb

	UE4 - Optimized UObject hash tables for speed and space.

Change 3361183 on 2017/03/23 by Gil.Gribb

	UE4 - Fixed change to NotifyPrimitiveDetached so that it works in the editor.

Change 3361906 on 2017/03/23 by Steve.Robb

	Fix for a bad hint index when instantiating map property subobjects when the defaults has fewer but non-zero elements.

	#jira UE-43272

Change 3362839 on 2017/03/24 by Gil.Gribb

	UE4 - Fixed hash table lock optimization.

Change 3367348 on 2017/03/28 by Robert.Manuszewski

	Making sure streamed-in SoundWaves get added to GC clusters.

Change 3367386 on 2017/03/28 by Ben.Marsh

	EC: Pass the Semaphores property from a build type as a parameter to new build jobs.

Change 3367422 on 2017/03/28 by Ben.Marsh

	EC: Allow limiting the number of scheduled jobs that will be automatically run at a particular time. Each build type can have a 'Semaphores' property in the branch settings file, which will be copied to newly created jobs. Before scheduling new jobs, EC is queried for the 'Semaphores' property on any running jobs, and build types with existing semaphores will be skipped. Does not prevent jobs from being run manually.

Change 3367469 on 2017/03/28 by Ben.Marsh

	EC: Prevent multiple incremental jobs running at once.

Change 3367640 on 2017/03/28 by Ben.Marsh

	Plugins: Add an optional EngineVersion field back into the plugin descriptor. If set, the engine will warn if the plugin is not compatible with the current engine version. Plugins will set this field by default when packaging; pass -Unversioned to override.

Change 3367836 on 2017/03/28 by Uriel.Doyon

	Improved handled of references in the streaming manager

Change 3369354 on 2017/03/29 by Graeme.Thornton

	Added AES encrypt/decrypt functions that take a byte array for the key

Change 3369804 on 2017/03/29 by Ben.Marsh

	Remove incorrect "EngineVersion" settings from plugin descriptors.

Change 3370462 on 2017/03/29 by Ben.Marsh

	Editor: Install Visual Studio 2017 by default, instead of Visual Studio 2015. Changed to use ExecElevatedProcess() to prevent installer failing to run if the current user is not already an administrator.

	#jira UE-43467

Change 3371598 on 2017/03/30 by Ben.Marsh

	UBT: Fix message for missing toolchain in VS2017.

Change 3372827 on 2017/03/30 by Ben.Marsh

	BuildGraph: Output an error at the end of each step if any previous build products have been modified.

Change 3372947 on 2017/03/30 by Ben.Marsh

	[Merge] Always add the host editor platform as supported in an installed build. Not doing so prevents the build platform being registered in UBT, which prevents doing any platform-specific staging operations in UAT.

Change 3372958 on 2017/03/30 by Ben.Marsh

	[Merge] Simplify log output for cooks. Suppress additional timestamps from the editor when running through UAT.

Change 3372981 on 2017/03/30 by Ben.Marsh

	[Merge] Modular game fixes for UAT

	* Store list of executable names from the receipts instead of generating them from Target/Platform/Config/Architecture combination
	* Get full list of staged executables from receipts instead of assuming only non-code projects are in Engine
	* Always pass short project name as Bootstrap argument, so that modular game exe knows which project to start

Change 3373024 on 2017/03/30 by Ben.Marsh

	[Merge] Add an option to UAT (-CookOutputDir=...) and the cooker (-OutputDir=...) which allows overriding the output directory for cooked files, and fix situations where the directory becomes too deep.

Change 3373041 on 2017/03/30 by Ben.Marsh

	[Merge] Added UAT script to replace assets with another source
	Renamed ReplaceAssetsCommandlet to GenerateAssetsManifest as it now outputs a list of files and has nothing specific about replacing files

Change 3373052 on 2017/03/30 by Ben.Marsh

	[Merge] Changed CopyUsingDistillFileSet command so that it can use a pre-existing manifest file instead of running commandlet

Change 3373092 on 2017/03/30 by Ben.Marsh

	[Merge] Fixed crash attempting to load cooked static mesh in editor

Change 3373112 on 2017/03/30 by Ben.Marsh

	[Merge] Fixed crash caused by loading cooked StaticMesh in editor that didn't have any SourceModels

Change 3373132 on 2017/03/30 by Ben.Marsh

	[Merge] Added Additional Maps that are always cooked to the GenerateDistillFileSetsCommandlet

Change 3373138 on 2017/03/30 by Ben.Marsh

	[Merge] Fixed code issue with playback of cooked SoundCues
	Skip over code using editor only data when editor data has been stripped

Change 3373143 on 2017/03/30 by Ben.Marsh

	[Merge] Fixed crash when attempting to open multiple cooked assets

Change 3373156 on 2017/03/30 by Ben.Marsh

	[Merge] Added commandlet to replace game assets with those from another source (intended for cooked asset replacement)

Change 3373161 on 2017/03/30 by Ben.Marsh

	[Merge] Prevented crash by not attempting to Load Mips again if a package has cooked data

Change 3373168 on 2017/03/30 by Ben.Marsh

	[Merge] Fix output path for DLC pak file, so it can be discovered by the engine and automatically mounted (and to stop it colliding with the main game pak file).

Change 3373204 on 2017/03/30 by Ben.Marsh

	[Merge] Fix crash when switching levels in PIE, due to bulk data already having been discarded for cooked assets. Cooking sets BULKDATA_SingleUse for textures, but PIEing needs to keep bulk data around.

Change 3373209 on 2017/03/30 by Ben.Marsh

	[Merge] Fix missing material in mod editor for cooked assets.

Change 3373388 on 2017/03/30 by Ben.Marsh

	[Merge] Various improvements to the plugin browser and new plugin wizard from Robo Recall.

Change 3374200 on 2017/03/31 by Ben.Marsh

	[Merge] Latest OdinEditor plugin from //Odin/Main, to fix build failures. Re-made change to OdinUnrealEdEngine to remove dependencies on analytics.

Change 3374279 on 2017/03/31 by Ben.Marsh

	PR #3441: Invalid JSON in FeaturePacks (Contributed by projectgheist)

Change 3374331 on 2017/03/31 by Ben.Marsh

	UBT: Disable warning pragmas on Mono; not supported on current compiler.

	#jira UE-43451

Change 3375108 on 2017/03/31 by Ben.Marsh

	Removing another plugin EngineVersion property.

Change 3375126 on 2017/03/31 by Ben.Marsh

	Fix incorrect executable paths being generated for Windows.

Change 3375159 on 2017/03/31 by Graeme.Thornton

	Pak Index Encryption
	 - Added "-encryptindex" option to unrealpak which will encrypt the pak index, making the pak file unreadable without the associated decryption key
	 - Added "-encryptpakindex" option to UAT to force on index encryption
	 - Added "bEncryptPakIndex" setting to project packaging settings so pak encryption can be controlled via the editor

Change 3375197 on 2017/03/31 by Graeme.Thornton

	Enable pak index encryption in shootergame

Change 3375377 on 2017/03/31 by Ben.Marsh

	Add build node to submit updated UnrealPak binaries for Win64, Mac and Linux. Currently has to be run via a custom build on EC, with the target set to "Submit UnrealPak Binaries".

Change 3376418 on 2017/04/03 by Ben.Marsh

	BuildGraph: Always clear the cached node state when running locally without having to manually specify the -ClearHistory argument. The -Resume argument allows the previous behavior of continuing a previous build.

Change 3376447 on 2017/04/03 by Ben.Marsh

	Build: Remove some unused stream settings

Change 3376469 on 2017/04/03 by Ben.Marsh

	Build: Add a customizable field for the script to use for custom builds in every branch.

Change 3376654 on 2017/04/03 by Ben.Marsh

	Add a fatal error message containing the module with an outstanding reference when trying to unload it.

	#jira UE-42423

Change 3376747 on 2017/04/03 by Gil.Gribb

	UE4 - Fixed crash relating to FGenericAsyncReadFileHandle when not using the EDL.

Change 3377173 on 2017/04/03 by Ben.Marsh

	Make sure callstacks are written to stdout following a crash on a background thread.

Change 3377183 on 2017/04/03 by Ben.Marsh

	Removing support for building VS2013 targets. Ability to generate VS2013 project files is still allowed, but unsupported (via the -2013unsupported command line argument).

Change 3377280 on 2017/04/03 by Ben.Marsh

	Build: Post UGS badges for all UE4 development streams, with the project set to $(Branch)/...

Change 3377311 on 2017/04/03 by Ben.Marsh

	Build: Set the 'Semaphores' parameter for any jobs started from a schedule.

Change 3377326 on 2017/04/03 by Ben.Marsh

	UGS: Show badges which match an entire subtree if the project field ends with "...".

Change 3377392 on 2017/04/03 by Ben.Marsh

	Add badges to UE4/Main and UE4/Release streams, and change the names of the builds in development streams to distinguish them.

Change 3377895 on 2017/04/03 by Ben.Marsh

	EC: Send notification emails whenever UAT fails to compile.

Change 3377923 on 2017/04/03 by Ben.Marsh

	Build: Use a different semaphore for the common editors build target to the incremental compile build target.

Change 3378297 on 2017/04/04 by Graeme.Thornton

	Fix incorrect generation of UE_ENGINE_DIRECTORY in UBT

Change 3378301 on 2017/04/04 by Ben.Marsh

	UBT: Try enabling bAdaptiveUnityDisablesPCH by default, to reduce the number of build failures we see due to missing includes.

Change 3378460 on 2017/04/04 by Graeme.Thornton

	Remove dependency preloading system from sync and async loading paths

Change 3378535 on 2017/04/04 by Robert.Manuszewski

	Fix for audio crash when launching Ocean PIE after removing the audio chunk allocation in CL #3347324

	#jira UE-43544

Change 3378575 on 2017/04/04 by Robert.Manuszewski

	Making sure actor clusters are not created in non-cooked builds

	#jira UE-43617
	#jira UE-43614

Change 3378589 on 2017/04/04 by Robert.Manuszewski

	Disabling debug GC cluster logging

	#jira UE-43617

Change 3379118 on 2017/04/04 by Robert.Manuszewski

	Disabling actor clustering by default, keeping it on in Orion and Ocean

Change 3379815 on 2017/04/04 by Ben.Marsh

	Revert change to derive executable names from target receipts. While a better solution than making them up, Android relies on having the base executable names for supporting multiple architectures.

Change 3380811 on 2017/04/05 by Gil.Gribb

	UE4 - Put the special boot order things into baseengine.ini so that licensees and games can add to it.

Change 3383313 on 2017/04/06 by Uriel.Doyon

	Integrated CL 3372436 3372765 3373272 from Dev-Rendering
	#JIRA UE-43669

Change 3383531 on 2017/04/06 by Ben.Marsh

	UGS: Ignore failures when querying whether paths exist. Permissions can cause this folder to fail, even if it will succeed at a parent directory.

Change 3383786 on 2017/04/06 by Ben.Zeigler

	Back out changelist 3382694 and replace with CL #3383757 from bob.tellez:
	Fix memory stomping issue caused by removing a FFortProfileSynchronizeRequest from SynchronizeRequests in UFortRegisteredPlayerInfo::UpdateSynchronizeRequest before SynchronizeProfile had finished executing

Change 3385089 on 2017/04/07 by Gil.Gribb

	UE4 - Critical. Fixed memory leak in pak precacher.

[CL 3386123 by Ben Marsh in Main branch]
2017-04-10 11:00:33 -04:00

1116 lines
35 KiB
C#

// Copyright 1998-2017 Epic Games, Inc. All Rights Reserved.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Management;
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
namespace AutomationTool
{
public enum CtrlTypes
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
public interface IProcess
{
void StopProcess(bool KillDescendants = true);
bool HasExited { get; }
string GetProcessName();
}
/// <summary>
/// Tracks all active processes.
/// </summary>
public sealed class ProcessManager
{
public delegate bool CtrlHandlerDelegate(CtrlTypes EventType);
// @todo: Add mono support
[DllImport("Kernel32")]
public static extern bool SetConsoleCtrlHandler(CtrlHandlerDelegate Handler, bool Add);
/// <summary>
/// List of active (running) processes.
/// </summary>
private static List<IProcess> ActiveProcesses = new List<IProcess>();
/// <summary>
/// Synchronization object
/// </summary>
private static object SyncObject = new object();
/// <summary>
/// Creates a new process and adds it to the tracking list.
/// </summary>
/// <returns>New Process objects</returns>
public static IProcessResult CreateProcess(string AppName, bool bAllowSpew, string LogName, Dictionary<string, string> Env = null, UnrealBuildTool.LogEventType SpewVerbosity = UnrealBuildTool.LogEventType.Console, ProcessResult.SpewFilterCallbackType SpewFilterCallback = null)
{
var NewProcess = HostPlatform.Current.CreateProcess(LogName);
if (Env != null)
{
foreach (var EnvPair in Env)
{
if (NewProcess.StartInfo.EnvironmentVariables.ContainsKey(EnvPair.Key))
{
NewProcess.StartInfo.EnvironmentVariables.Remove(EnvPair.Key);
}
if (!String.IsNullOrEmpty(EnvPair.Value))
{
NewProcess.StartInfo.EnvironmentVariables.Add(EnvPair.Key, EnvPair.Value);
}
}
}
var Result = new ProcessResult(AppName, NewProcess, bAllowSpew, LogName, SpewVerbosity: SpewVerbosity, InSpewFilterCallback: SpewFilterCallback);
AddProcess(Result);
return Result;
}
public static void AddProcess(IProcess Proc)
{
lock (SyncObject)
{
ActiveProcesses.Add(Proc);
}
}
public static void RemoveProcess(IProcess Proc)
{
lock (SyncObject)
{
ActiveProcesses.Remove(Proc);
}
}
public static bool CanBeKilled(string ProcessName)
{
return !HostPlatform.Current.DontKillProcessList.Contains(ProcessName, StringComparer.InvariantCultureIgnoreCase);
}
/// <summary>
/// Kills all running processes.
/// </summary>
public static void KillAll()
{
List<IProcess> ProcessesToKill = new List<IProcess>();
lock (SyncObject)
{
foreach (var ProcResult in ActiveProcesses)
{
if (!ProcResult.HasExited)
{
ProcessesToKill.Add(ProcResult);
}
}
ActiveProcesses.Clear();
}
// Remove processes that can't be killed
for (int ProcessIndex = ProcessesToKill.Count - 1; ProcessIndex >= 0; --ProcessIndex )
{
var ProcessName = ProcessesToKill[ProcessIndex].GetProcessName();
if (!String.IsNullOrEmpty(ProcessName) && !CanBeKilled(ProcessName))
{
CommandUtils.LogLog("Ignoring process \"{0}\" because it can't be killed.", ProcessName);
ProcessesToKill.RemoveAt(ProcessIndex);
}
}
if(ProcessesToKill.Count > 0)
{
CommandUtils.LogLog("Trying to kill {0} spawned processes.", ProcessesToKill.Count);
foreach (var Proc in ProcessesToKill)
{
CommandUtils.LogLog(" {0}", Proc.GetProcessName());
}
if (CommandUtils.IsBuildMachine)
{
for (int Cnt = 0; Cnt < 9; Cnt++)
{
bool AllDone = true;
foreach (var Proc in ProcessesToKill)
{
try
{
if (!Proc.HasExited)
{
AllDone = false;
CommandUtils.LogLog("Waiting for process: {0}", Proc.GetProcessName());
}
}
catch (Exception)
{
CommandUtils.LogWarning("Exception Waiting for process");
AllDone = false;
}
}
try
{
if (ProcessResult.HasAnyDescendants(Process.GetCurrentProcess()))
{
AllDone = false;
CommandUtils.Log("Waiting for descendants of main process...");
}
}
catch (Exception Ex)
{
CommandUtils.LogWarning("Exception Waiting for descendants of main process. " + Ex);
AllDone = false;
}
if (AllDone)
{
break;
}
Thread.Sleep(10000);
}
}
foreach (var Proc in ProcessesToKill)
{
var ProcName = Proc.GetProcessName();
try
{
if (!Proc.HasExited)
{
CommandUtils.LogLog("Killing process: {0}", ProcName);
Proc.StopProcess(false);
}
}
catch (Exception Ex)
{
CommandUtils.LogWarning("Exception while trying to kill process {0}:", ProcName);
CommandUtils.LogWarning(LogUtils.FormatException(Ex));
}
}
try
{
if (CommandUtils.IsBuildMachine && ProcessResult.HasAnyDescendants(Process.GetCurrentProcess()))
{
CommandUtils.LogLog("current process still has descendants, trying to kill them...");
ProcessResult.KillAllDescendants(Process.GetCurrentProcess());
}
}
catch (Exception)
{
CommandUtils.LogWarning("Exception killing descendants of main process");
}
}
}
}
#region ProcessResult Helper Class
public interface IProcessResult : IProcess
{
void OnProcessExited();
void DisposeProcess();
void StdOut(object sender, DataReceivedEventArgs e);
void StdErr(object sender, DataReceivedEventArgs e);
int ExitCode{ get;set; }
string Output {get;}
Process ProcessObject {get;}
string ToString();
void WaitForExit();
}
/// <summary>
/// Class containing the result of process execution.
/// </summary>
public class ProcessResult : IProcessResult
{
public delegate string SpewFilterCallbackType(string Message);
private string Source = "";
private int ProcessExitCode = -1;
private StringBuilder ProcessOutput = new StringBuilder();
private bool AllowSpew = true;
private UnrealBuildTool.LogEventType SpewVerbosity = UnrealBuildTool.LogEventType.Console;
private SpewFilterCallbackType SpewFilterCallback = null;
private string AppName = String.Empty;
private Process Proc = null;
private AutoResetEvent OutputWaitHandle = new AutoResetEvent(false);
private AutoResetEvent ErrorWaitHandle = new AutoResetEvent(false);
private object ProcSyncObject;
public ProcessResult(string InAppName, Process InProc, bool bAllowSpew, string LogName, UnrealBuildTool.LogEventType SpewVerbosity = UnrealBuildTool.LogEventType.Console, SpewFilterCallbackType InSpewFilterCallback = null)
{
AppName = InAppName;
ProcSyncObject = new object();
Proc = InProc;
Source = LogName;
AllowSpew = bAllowSpew;
this.SpewVerbosity = SpewVerbosity;
SpewFilterCallback = InSpewFilterCallback;
if (Proc != null)
{
Proc.EnableRaisingEvents = false;
}
}
~ProcessResult()
{
if(Proc != null)
{
Proc.Dispose();
}
}
/// <summary>
/// Removes a process from the list of tracked processes.
/// </summary>
public void OnProcessExited()
{
ProcessManager.RemoveProcess(this);
}
/// <summary>
/// Log output of a remote process at a given severity.
/// To pretty up the output, we use a custom source so it will say the source of the process instead of this method name.
/// </summary>
/// <param name="Verbosity"></param>
/// <param name="Message"></param>
[MethodImplAttribute(MethodImplOptions.NoInlining)]
private void LogOutput(UnrealBuildTool.LogEventType Verbosity, string Message)
{
UnrealBuildTool.Log.WriteLine(1, Source, Verbosity, Message);
}
/// <summary>
/// Manually dispose of Proc and set it to null.
/// </summary>
public void DisposeProcess()
{
if(Proc != null)
{
Proc.Dispose();
Proc = null;
}
}
/// <summary>
/// Process.OutputDataReceived event handler.
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">Event args</param>
public void StdOut(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
if (AllowSpew)
{
if (SpewFilterCallback != null)
{
string FilteredSpew = SpewFilterCallback(e.Data);
if (FilteredSpew != null)
{
LogOutput(SpewVerbosity, FilteredSpew);
}
}
else
{
LogOutput(SpewVerbosity, e.Data);
}
}
lock (ProcSyncObject)
{
ProcessOutput.Append(e.Data);
ProcessOutput.Append(Environment.NewLine);
}
}
else
{
OutputWaitHandle.Set();
}
}
/// <summary>
/// Process.ErrorDataReceived event handler.
/// </summary>
/// <param name="sender">Sender</param>
/// <param name="e">Event args</param>
public void StdErr(object sender, DataReceivedEventArgs e)
{
if (e.Data != null)
{
if (SpewFilterCallback != null)
{
string FilteredSpew = SpewFilterCallback(e.Data);
if (FilteredSpew != null)
{
LogOutput(SpewVerbosity, FilteredSpew);
}
}
else
{
LogOutput(SpewVerbosity, e.Data);
}
lock (ProcSyncObject)
{
ProcessOutput.Append(e.Data);
ProcessOutput.Append(Environment.NewLine);
}
}
else
{
ErrorWaitHandle.Set();
}
}
/// <summary>
/// Convenience operator for getting the exit code value.
/// </summary>
/// <param name="Result"></param>
/// <returns>Process exit code.</returns>
public static implicit operator int(ProcessResult Result)
{
return Result.ExitCode;
}
/// <summary>
/// Gets or sets the process exit code.
/// </summary>
public int ExitCode
{
get { return ProcessExitCode; }
set { ProcessExitCode = value; }
}
/// <summary>
/// Gets all std output the process generated.
/// </summary>
public string Output
{
get
{
lock (ProcSyncObject)
{
return ProcessOutput.ToString();
}
}
}
public bool HasExited
{
get
{
bool bHasExited = true;
lock (ProcSyncObject)
{
if (Proc != null)
{
bHasExited = Proc.HasExited;
if (bHasExited)
{
ExitCode = Proc.ExitCode;
}
}
}
return bHasExited;
}
}
public Process ProcessObject
{
get { return Proc; }
}
/// <summary>
/// Thread-safe way of getting the process name
/// </summary>
/// <returns>Name of the process this object represents</returns>
public string GetProcessName()
{
string Name = null;
lock (ProcSyncObject)
{
try
{
if (Proc != null && !Proc.HasExited)
{
Name = Proc.ProcessName;
}
}
catch
{
// Ignore all exceptions
}
}
if (String.IsNullOrEmpty(Name))
{
Name = "[EXITED] " + AppName;
}
return Name;
}
/// <summary>
/// Object iterface.
/// </summary>
/// <returns>String representation of this object.</returns>
public override string ToString()
{
return ExitCode.ToString();
}
public void WaitForExit()
{
bool bProcTerminated = false;
bool bStdOutSignalReceived = false;
bool bStdErrSignalReceived = false;
// Make sure the process objeect is valid.
lock (ProcSyncObject)
{
bProcTerminated = (Proc == null);
}
// Keep checking if we got all output messages until the process terminates.
if (!bProcTerminated)
{
// Check messages
int MaxWaitUntilMessagesReceived = 120;
while (MaxWaitUntilMessagesReceived > 0 && !(bStdOutSignalReceived && bStdErrSignalReceived))
{
if (!bStdOutSignalReceived)
{
bStdOutSignalReceived = OutputWaitHandle.WaitOne(500);
}
if (!bStdErrSignalReceived)
{
bStdErrSignalReceived = ErrorWaitHandle.WaitOne(500);
}
// Check if the process terminated
lock (ProcSyncObject)
{
bProcTerminated = (Proc == null) || Proc.HasExited;
}
if (bProcTerminated)
{
// Process terminated but make sure we got all messages, don't wait forever though
MaxWaitUntilMessagesReceived--;
}
}
if (!(bStdOutSignalReceived && bStdErrSignalReceived))
{
CommandUtils.LogLog("Waited for a long time for output of {0}, some output may be missing; we gave up.", AppName);
}
// Double-check if the process terminated
lock (ProcSyncObject)
{
bProcTerminated = (Proc == null) || Proc.HasExited;
}
if (!bProcTerminated)
{
// The process did not terminate yet but we've read all output messages, wait until the process terminates
Proc.WaitForExit();
}
}
}
/// <summary>
/// Finds child processes of the current process.
/// </summary>
/// <param name="ProcessId"></param>
/// <param name="PossiblyRelatedId"></param>
/// <param name="VisitedPids"></param>
/// <returns></returns>
private static bool IsOurDescendant(Process ProcessToKill, int PossiblyRelatedId, HashSet<int> VisitedPids)
{
// check if we're the parent of it or its parent is our descendant
try
{
VisitedPids.Add(PossiblyRelatedId);
Process Parent = null;
using (ManagementObject ManObj = new ManagementObject(string.Format("win32_process.handle='{0}'", PossiblyRelatedId)))
{
ManObj.Get();
int ParentId = Convert.ToInt32(ManObj["ParentProcessId"]);
if (ParentId == 0 || VisitedPids.Contains(ParentId))
{
return false;
}
Parent = Process.GetProcessById(ParentId); // will throw an exception if not spawned by us or not running
}
if (Parent != null)
{
return Parent.Id == ProcessToKill.Id || IsOurDescendant(ProcessToKill, Parent.Id, VisitedPids); // could use ParentId, but left it to make the above var used
}
else
{
return false;
}
}
catch (Exception)
{
// This can happen if the pid is no longer valid which is ok.
return false;
}
}
/// <summary>
/// Kills all child processes of the specified process.
/// </summary>
/// <param name="ProcessId">Process id</param>
public static void KillAllDescendants(Process ProcessToKill)
{
bool bKilledAChild;
do
{
bKilledAChild = false;
// For some reason Process.GetProcesses() sometimes returns the same process twice
// So keep track of all processes we already tried to kill
var KilledPids = new HashSet<int>();
var AllProcs = Process.GetProcesses();
foreach (Process KillCandidate in AllProcs)
{
var VisitedPids = new HashSet<int>();
if (ProcessManager.CanBeKilled(KillCandidate.ProcessName) &&
!KilledPids.Contains(KillCandidate.Id) &&
IsOurDescendant(ProcessToKill, KillCandidate.Id, VisitedPids))
{
KilledPids.Add(KillCandidate.Id);
CommandUtils.LogLog("Trying to kill descendant pid={0}, name={1}", KillCandidate.Id, KillCandidate.ProcessName);
try
{
KillCandidate.Kill();
bKilledAChild = true;
}
catch (Exception Ex)
{
if(!KillCandidate.HasExited)
{
CommandUtils.LogWarning("Failed to kill descendant:");
CommandUtils.LogWarning(LogUtils.FormatException(Ex));
}
}
break; // exit the loop as who knows what else died, so let's get processes anew
}
}
} while (bKilledAChild);
}
/// <summary>
/// returns true if this process has any descendants
/// </summary>
/// <param name="ProcessToCheck">Process to check</param>
public static bool HasAnyDescendants(Process ProcessToCheck)
{
Process[] AllProcs = Process.GetProcesses();
foreach (Process KillCandidate in AllProcs)
{
// Silently skip InvalidOperationExceptions here, because it depends on the process still running. It may have terminated.
string ProcessName;
try
{
ProcessName = KillCandidate.ProcessName;
}
catch(InvalidOperationException)
{
continue;
}
// Check if it's still running
HashSet<int> VisitedPids = new HashSet<int>();
if (ProcessManager.CanBeKilled(ProcessName) && IsOurDescendant(ProcessToCheck, KillCandidate.Id, VisitedPids))
{
CommandUtils.LogLog("Descendant pid={0}, name={1}", KillCandidate.Id, ProcessName);
return true;
}
}
return false;
}
public void StopProcess(bool KillDescendants = true)
{
if (Proc != null)
{
Process ProcToKill = null;
// At this point any call to Proc memebers will result in an exception
// so null the object.
var ProcToKillName = GetProcessName();
lock (ProcSyncObject)
{
ProcToKill = Proc;
Proc = null;
}
// Now actually kill the process and all its descendants if requested
if (KillDescendants)
{
KillAllDescendants(ProcToKill);
}
try
{
ProcToKill.Kill();
ProcToKill.WaitForExit(60000);
if (!ProcToKill.HasExited)
{
CommandUtils.LogLog("Process {0} failed to exit.", ProcToKillName);
}
else
{
CommandUtils.LogLog("Process {0} successfully exited.", ProcToKillName);
OnProcessExited();
}
ProcToKill.Close();
}
catch (Exception Ex)
{
CommandUtils.LogWarning("Exception while trying to kill process {0}:", ProcToKillName);
CommandUtils.LogWarning(LogUtils.FormatException(Ex));
}
}
}
}
#endregion
public partial class CommandUtils
{
#region Statistics
private static Dictionary<string, int> ExeToTimeInMs = new Dictionary<string, int>();
public static void AddRunTime(string Exe, int TimeInMs)
{
lock(ExeToTimeInMs)
{
string Base = Path.GetFileName(Exe);
if (!ExeToTimeInMs.ContainsKey(Base))
{
ExeToTimeInMs.Add(Base, TimeInMs);
}
else
{
ExeToTimeInMs[Base] += TimeInMs;
}
}
}
public static void PrintRunTime()
{
lock(ExeToTimeInMs)
{
foreach (var Item in ExeToTimeInMs)
{
LogVerbose("Run: Total {0}s to run " + Item.Key, Item.Value / 1000);
}
ExeToTimeInMs.Clear();
}
}
#endregion
[Flags]
public enum ERunOptions
{
None = 0,
AllowSpew = 1 << 0,
AppMustExist = 1 << 1,
NoWaitForExit = 1 << 2,
NoStdOutRedirect = 1 << 3,
NoLoggingOfRunCommand = 1 << 4,
UTF8Output = 1 << 5,
/// When specified with AllowSpew, the output will be TraceEventType.Verbose instead of TraceEventType.Information
SpewIsVerbose = 1 << 6,
/// <summary>
/// If NoLoggingOfRunCommand is set, it normally suppresses the run duration output. This turns it back on.
/// </summary>
LoggingOfRunDuration = 1 << 7,
Default = AllowSpew | AppMustExist,
}
/// <summary>
/// Runs external program.
/// </summary>
/// <param name="App">Program filename.</param>
/// <param name="CommandLine">Commandline</param>
/// <param name="Input">Optional Input for the program (will be provided as stdin)</param>
/// <param name="Options">Defines the options how to run. See ERunOptions.</param>
/// <param name="Env">Environment to pass to program.</param>
/// <param name="FilterCallback">Callback to filter log spew before output.</param>
/// <returns>Object containing the exit code of the program as well as it's stdout output.</returns>
public static IProcessResult Run(string App, string CommandLine = null, string Input = null, ERunOptions Options = ERunOptions.Default, Dictionary<string, string> Env = null, ProcessResult.SpewFilterCallbackType SpewFilterCallback = null, string Identifier = null)
{
App = ConvertSeparators(PathSeparator.Default, App);
// Get the log name before allowing the platform to modify the app/command-line. We want mono apps to be written to a log named after the application and appear with the application prefix rather than "mono:".
string LogName = Identifier ?? Path.GetFileNameWithoutExtension(App);
HostPlatform.Current.SetupOptionsForRun(ref App, ref Options, ref CommandLine);
if (App == "ectool" || App == "zip" || App == "xcodebuild")
{
Options &= ~ERunOptions.AppMustExist;
}
// Check if the application exists, including the PATH directories.
if (Options.HasFlag(ERunOptions.AppMustExist) && !FileExists(Options.HasFlag(ERunOptions.NoLoggingOfRunCommand) ? true : false, App))
{
bool bExistsInPath = false;
if(!App.Contains(Path.DirectorySeparatorChar) && !App.Contains(Path.AltDirectorySeparatorChar))
{
string[] PathDirectories = Environment.GetEnvironmentVariable("PATH").Split(Path.PathSeparator);
foreach(string PathDirectory in PathDirectories)
{
string TryApp = Path.Combine(PathDirectory, App);
if(FileExists(Options.HasFlag(ERunOptions.NoLoggingOfRunCommand), TryApp))
{
App = TryApp;
bExistsInPath = true;
break;
}
}
}
if(!bExistsInPath)
{
throw new AutomationException("BUILD FAILED: Couldn't find the executable to Run: {0}", App);
}
}
var StartTime = DateTime.UtcNow;
UnrealBuildTool.LogEventType SpewVerbosity = Options.HasFlag(ERunOptions.SpewIsVerbose) ? UnrealBuildTool.LogEventType.Verbose : UnrealBuildTool.LogEventType.Console;
if (!Options.HasFlag(ERunOptions.NoLoggingOfRunCommand))
{
LogWithVerbosity(SpewVerbosity,"Run: " + App + " " + (String.IsNullOrEmpty(CommandLine) ? "" : CommandLine));
}
IProcessResult Result = ProcessManager.CreateProcess(App, Options.HasFlag(ERunOptions.AllowSpew), LogName, Env, SpewVerbosity:SpewVerbosity, SpewFilterCallback: SpewFilterCallback);
Process Proc = Result.ProcessObject;
bool bRedirectStdOut = (Options & ERunOptions.NoStdOutRedirect) != ERunOptions.NoStdOutRedirect;
Proc.StartInfo.FileName = App;
Proc.StartInfo.Arguments = String.IsNullOrEmpty(CommandLine) ? "" : CommandLine;
Proc.StartInfo.UseShellExecute = false;
if (bRedirectStdOut)
{
Proc.StartInfo.RedirectStandardOutput = true;
Proc.StartInfo.RedirectStandardError = true;
Proc.OutputDataReceived += Result.StdOut;
Proc.ErrorDataReceived += Result.StdErr;
}
Proc.StartInfo.RedirectStandardInput = Input != null;
Proc.StartInfo.CreateNoWindow = true;
if ((Options & ERunOptions.UTF8Output) == ERunOptions.UTF8Output)
{
Proc.StartInfo.StandardOutputEncoding = new System.Text.UTF8Encoding(false, false);
}
Proc.Start();
if (bRedirectStdOut)
{
Proc.BeginOutputReadLine();
Proc.BeginErrorReadLine();
}
if (String.IsNullOrEmpty(Input) == false)
{
Proc.StandardInput.WriteLine(Input);
Proc.StandardInput.Close();
}
if (!Options.HasFlag(ERunOptions.NoWaitForExit))
{
Result.WaitForExit();
var BuildDuration = (DateTime.UtcNow - StartTime).TotalMilliseconds;
AddRunTime(App, (int)(BuildDuration));
Result.ExitCode = Proc.ExitCode;
if (!Options.HasFlag(ERunOptions.NoLoggingOfRunCommand) || Options.HasFlag(ERunOptions.LoggingOfRunDuration))
{
LogWithVerbosity(SpewVerbosity,"Run: Took {0}s to run {1}, ExitCode={2}", BuildDuration / 1000, Path.GetFileName(App), Result.ExitCode);
}
Result.OnProcessExited();
Result.DisposeProcess();
}
else
{
Result.ExitCode = -1;
}
return Result;
}
/// <summary>
/// Gets a logfile name for a RunAndLog call
/// </summary>
/// <param name="Env">Environment to use.</param>
/// <param name="App">Executable to run</param>
/// <param name="LogName">Name of the logfile ( if null, executable name is used )</param>
/// <returns>The log file name.</returns>
public static string GetRunAndLogOnlyName(CommandEnvironment Env, string App, string LogName = null)
{
if (LogName == null)
{
LogName = Path.GetFileNameWithoutExtension(App);
}
return LogUtils.GetUniqueLogName(CombinePaths(Env.LogFolder, LogName));
}
/// <summary>
/// Runs external program and writes the output to a logfile.
/// </summary>
/// <param name="Env">Environment to use.</param>
/// <param name="App">Executable to run</param>
/// <param name="CommandLine">Commandline to pass on to the executable</param>
/// <param name="LogName">Name of the logfile ( if null, executable name is used )</param>
/// <param name="Input">Optional Input for the program (will be provided as stdin)</param>
/// <param name="Options">Defines the options how to run. See ERunOptions.</param>
/// <param name="FilterCallback">Callback to filter log spew before output.</param>
public static void RunAndLog(CommandEnvironment Env, string App, string CommandLine, string LogName = null, int MaxSuccessCode = 0, string Input = null, ERunOptions Options = ERunOptions.Default, Dictionary<string, string> EnvVars = null, ProcessResult.SpewFilterCallbackType SpewFilterCallback = null)
{
RunAndLog(App, CommandLine, GetRunAndLogOnlyName(Env, App, LogName), MaxSuccessCode, Input, Options, EnvVars, SpewFilterCallback);
}
/// <summary>
/// Exception class for child process commands failing
/// </summary>
public class CommandFailedException : AutomationException
{
public CommandFailedException(string Message) : base(Message)
{
}
public CommandFailedException(ExitCode ExitCode, string Message) : base(ExitCode, Message)
{
}
}
/// <summary>
/// Runs external program and writes the output to a logfile.
/// </summary>
/// <param name="App">Executable to run</param>
/// <param name="CommandLine">Commandline to pass on to the executable</param>
/// <param name="Logfile">Full path to the logfile, where the application output should be written to.</param>
/// <param name="Input">Optional Input for the program (will be provided as stdin)</param>
/// <param name="Options">Defines the options how to run. See ERunOptions.</param>
/// <param name="FilterCallback">Callback to filter log spew before output.</param>
public static string RunAndLog(string App, string CommandLine, string Logfile = null, int MaxSuccessCode = 0, string Input = null, ERunOptions Options = ERunOptions.Default, Dictionary<string, string> EnvVars = null, ProcessResult.SpewFilterCallbackType SpewFilterCallback = null)
{
IProcessResult Result = Run(App, CommandLine, Input, Options, EnvVars, SpewFilterCallback);
if (!String.IsNullOrEmpty(Result.Output) && Logfile != null)
{
WriteToFile(Logfile, Result.Output);
}
else if (Logfile == null)
{
Logfile = "[No logfile specified]";
}
else
{
Logfile = "[None!, no output produced]";
}
if (Result.ExitCode > MaxSuccessCode || Result.ExitCode < 0)
{
throw new CommandFailedException((ExitCode)Result.ExitCode, String.Format("Command failed (Result:{3}): {0} {1}. See logfile for details: '{2}' ",
App, CommandLine, Path.GetFileName(Logfile), Result.ExitCode));
}
if (!String.IsNullOrEmpty(Result.Output))
{
return Result.Output;
}
return "";
}
/// <summary>
/// Runs external program and writes the output to a logfile.
/// </summary>
/// <param name="App">Executable to run</param>
/// <param name="CommandLine">Commandline to pass on to the executable</param>
/// <param name="Logfile">Full path to the logfile, where the application output should be written to.</param>
/// <param name="FilterCallback">Callback to filter log spew before output.</param>
/// <returns>Whether the program executed successfully or not.</returns>
public static string RunAndLog(string App, string CommandLine, out int SuccessCode, string Logfile = null, Dictionary<string, string> EnvVars = null, ProcessResult.SpewFilterCallbackType SpewFilterCallback = null)
{
IProcessResult Result = Run(App, CommandLine, Env: EnvVars, SpewFilterCallback: SpewFilterCallback);
SuccessCode = Result.ExitCode;
if (Result.Output.Length > 0 && Logfile != null)
{
WriteToFile(Logfile, Result.Output);
}
if (!String.IsNullOrEmpty(Result.Output))
{
return Result.Output;
}
return "";
}
/// <summary>
/// Runs external program and writes the output to a logfile.
/// </summary>
/// <param name="Env">Environment to use.</param>
/// <param name="App">Executable to run</param>
/// <param name="CommandLine">Commandline to pass on to the executable</param>
/// <param name="LogName">Name of the logfile ( if null, executable name is used )</param>
/// <param name="FilterCallback">Callback to filter log spew before output.</param>
/// <returns>Whether the program executed successfully or not.</returns>
public static string RunAndLog(CommandEnvironment Env, string App, string CommandLine, out int SuccessCode, string LogName = null, Dictionary<string, string> EnvVars = null, ProcessResult.SpewFilterCallbackType SpewFilterCallback = null)
{
return RunAndLog(App, CommandLine, out SuccessCode, GetRunAndLogOnlyName(Env, App, LogName), EnvVars, SpewFilterCallback);
}
/// <summary>
/// Runs UAT recursively
/// </summary>
/// <param name="Env">Environment to use.</param>
/// <param name="CommandLine">Commandline to pass on to the executable</param>
public static string RunUAT(CommandEnvironment Env, string CommandLine)
{
// We want to redirect the output from recursive UAT calls into our normal log folder, but prefix everything with a unique identifier. To do so, we set the EnvVarNames.LogFolder environment
// variable to a subfolder of it, then copy its contents into the main folder with a prefix after it's finished. Start by finding a base name we can use to identify the output of this run.
string BaseLogSubdir = "Recur";
if (!String.IsNullOrEmpty(CommandLine))
{
int Space = CommandLine.IndexOf(" ");
if (Space > 0)
{
BaseLogSubdir = BaseLogSubdir + "_" + CommandLine.Substring(0, Space);
}
else if (CommandLine.Contains("-profile"))
{
string PathToProfile = CommandLine.Substring(CommandLine.IndexOf('=') + 1);
BaseLogSubdir = BaseLogSubdir + "_" + (Path.GetFileNameWithoutExtension(PathToProfile));
}
else
{
BaseLogSubdir = BaseLogSubdir + "_" + CommandLine;
}
}
BaseLogSubdir = BaseLogSubdir.Trim();
// Check if there are already log files which start with this prefix, and try to uniquify it if until there aren't.
int Index = 0;
string DirOnlyName = BaseLogSubdir;
string LogSubdir = CombinePaths(CmdEnv.LogFolder, DirOnlyName, "");
while (true)
{
var ExistingFiles = FindFiles(DirOnlyName + "*", false, CmdEnv.LogFolder);
if (ExistingFiles.Length == 0)
{
break;
}
Index++;
if (Index == 1000)
{
throw new AutomationException("Couldn't seem to create a log subdir {0}", LogSubdir);
}
DirOnlyName = String.Format("{0}_{1}_", BaseLogSubdir, Index);
LogSubdir = CombinePaths(CmdEnv.LogFolder, DirOnlyName, "");
}
// Get the stdout log file for this run, and create the subdirectory for all the other log output
string LogFile = CombinePaths(CmdEnv.LogFolder, DirOnlyName + ".log");
LogVerbose("Recursive UAT Run, in log folder {0}, main log file {1}", LogSubdir, LogFile);
CreateDirectory(LogSubdir);
// Run UAT with the log folder redirected through the environment
string App = CmdEnv.UATExe;
Log("Running {0} {1}", App, CommandLine);
var OSEnv = new Dictionary<string, string>();
OSEnv.Add(AutomationTool.EnvVarNames.LogFolder, LogSubdir);
OSEnv.Add("uebp_UATMutexNoWait", "1");
if (!IsBuildMachine)
{
OSEnv.Add(AutomationTool.EnvVarNames.LocalRoot, ""); // if we don't clear this out, it will think it is a build machine; it will rederive everything
}
IProcessResult Result = Run(App, CommandLine, null, ERunOptions.Default, OSEnv);
if (Result.Output.Length > 0)
{
WriteToFile(LogFile, Result.Output);
}
else
{
WriteToFile(LogFile, "[None!, no output produced]");
}
// Copy everything into the main log folder, using the prefix we decided on earlier.
LogVerbose("Flattening log folder {0}", LogSubdir);
var Files = FindFiles("*", true, LogSubdir);
string MyLogFolder = CombinePaths(CmdEnv.LogFolder, "");
foreach (var ThisFile in Files)
{
if (!ThisFile.StartsWith(MyLogFolder, StringComparison.InvariantCultureIgnoreCase))
{
throw new AutomationException("Can't rebase {0} because it doesn't start with {1}", ThisFile, MyLogFolder);
}
string NewFilename = ThisFile.Substring(MyLogFolder.Length).Replace("/", "_").Replace("\\", "_");
NewFilename = CombinePaths(CmdEnv.LogFolder, NewFilename);
if (FileExists_NoExceptions(NewFilename))
{
throw new AutomationException("Destination log file already exists? {0}", NewFilename);
}
CopyFile(ThisFile, NewFilename);
if (!FileExists_NoExceptions(NewFilename))
{
throw new AutomationException("Destination log file could not be copied {0}", NewFilename);
}
DeleteFile_NoExceptions(ThisFile);
}
DeleteDirectory_NoExceptions(LogSubdir);
if (Result.ExitCode != 0)
{
throw new CommandFailedException(String.Format("Recursive UAT Command failed (Result:{3}): {0} {1}. See logfile for details: '{2}' ",
App, CommandLine, Path.GetFileName(LogFile), Result.ExitCode));
}
return LogFile;
}
protected delegate bool ProcessLog(string LogText);
/// <summary>
/// Keeps reading a log file as it's being written to by another process until it exits.
/// </summary>
/// <param name="LogFilename">Name of the log file.</param>
/// <param name="LogProcess">Process that writes to the log file.</param>
/// <param name="OnLogRead">Callback used to process the recently read log contents.</param>
protected static void LogFileReaderProcess(string LogFilename, IProcessResult LogProcess, ProcessLog OnLogRead = null)
{
while (!FileExists(LogFilename) && !LogProcess.HasExited)
{
Log("Waiting for logging process to start...");
Thread.Sleep(2000);
}
Thread.Sleep(1000);
using (FileStream ProcessLog = File.Open(LogFilename, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
StreamReader LogReader = new StreamReader(ProcessLog);
bool bKeepReading = true;
// Read until the process has exited.
while (!LogProcess.HasExited && bKeepReading)
{
while (!LogReader.EndOfStream && bKeepReading)
{
string Output = LogReader.ReadToEnd();
if (Output != null && OnLogRead != null)
{
bKeepReading = OnLogRead(Output);
}
}
while (LogReader.EndOfStream && !LogProcess.HasExited && bKeepReading)
{
Thread.Sleep(250);
// Tick the callback so that it can respond to external events
if (OnLogRead != null)
{
bKeepReading = OnLogRead(null);
}
}
}
}
}
}
}