-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProcessOutput.cs
More file actions
141 lines (120 loc) · 4.08 KB
/
ProcessOutput.cs
File metadata and controls
141 lines (120 loc) · 4.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reactive;
using System.Reactive.Disposables;
using System.Reactive.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RunInTray
{
// Observable that produces a single value when the object is disposed.
class DisposeObservable : IDisposable, IObservable<Unit>
{
private List<IObserver<Unit>> observers = new List<IObserver<Unit>>();
// Subscribe an observer.
public IDisposable Subscribe(IObserver<Unit> observer)
{
observers.Add(observer);
return Disposable.Create(() => observers.Remove(observer));
}
// Dispose the object, producing a value.
public void Dispose()
{
// Observers may remove themselves during OnComplete, so make a copy of the array.
var localObservers = observers.ToArray();
// Produce value on and complete all observers.
foreach (var observer in localObservers)
{
observer.OnNext(Unit.Default);
observer.OnCompleted();
}
}
}
// Class for handling process stdout and stderr.
public class ProcessOutput : IDisposable
{
private IObservable<string> stdout;
private IObservable<string> stderr;
// Observable that will fire when we're disposed.
private DisposeObservable disposeObservable = new DisposeObservable();
private CompositeDisposable disposables = new CompositeDisposable();
public ProcessOutput(Process process, string friendlyName)
{
// Fire dispose observable when we are disposed.
disposables.Add(disposeObservable);
stdout = CreateOutputObservable(process, "OutputDataReceived");
stderr = CreateOutputObservable(process, "ErrorDataReceived");
// Make sure the log directory exists.
var logPath = GetLogPath(friendlyName);
Directory.CreateDirectory(Path.GetDirectoryName(logPath));
// Create log file with standard utf8 encoding.
var logWriter = new StreamWriter(logPath, false, Encoding.UTF8);
// Register observer to write the data to the log file.
stdout.Merge(stderr).Subscribe(
s => logWriter.Write(s),
() => logWriter.Dispose()
);
// Start async I/O.
process.BeginOutputReadLine();
process.BeginErrorReadLine();
}
public static string GetLogBaseDir()
{
// Put logs in the user's local application data folder.
return Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"RunInTray");
}
// Get the location for the logfile of a process.
private string GetLogPath(string friendlyName)
{
// Sanitise friendly name for use as a filename.
friendlyName = friendlyName.Replace("\"", "'");
friendlyName = friendlyName.Replace("<", "");
friendlyName = friendlyName.Replace(">", "");
friendlyName = friendlyName.Replace("|", "");
friendlyName = friendlyName.Replace(":", "");
friendlyName = friendlyName.Replace("*", "");
friendlyName = friendlyName.Replace("?", "");
friendlyName = friendlyName.Replace("\\", "-");
friendlyName = friendlyName.Replace("/", "-");
// Use current timestamp to prevent overwriting previous logs.
var timestamp = DateTime.Now.ToString("yyyy-MM-dd_HH-mm-ss");
return Path.Combine(
GetLogBaseDir(),
friendlyName,
timestamp + ".log");
}
public void Dispose()
{
disposables.Dispose();
}
// Observable that contains the contents of the output.
public IObservable<string> GetOutput()
{
return stdout;
}
public IObservable<string> GetErrorOutput()
{
return stderr;
}
// Create an observable for an output stream.
IObservable<string> CreateOutputObservable(object target, string eventName)
{
var observable = Observable.FromEventPattern<DataReceivedEventArgs>(target, eventName)
// Get the string data from the event.
// Also, add back the newline that is stripped.
.Select(e => e.EventArgs.Data + Environment.NewLine)
// Produce values until we are disposed.
.TakeUntil(disposeObservable)
;
// Use Replay so we don't lose output when the window isn't open.
var replay = observable.Replay();
disposables.Add(replay.Connect());
return replay;
}
}
}