-
Notifications
You must be signed in to change notification settings - Fork 84
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #638 from SnipUndercover/capture-stderr
- Loading branch information
Showing
3 changed files
with
66 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,53 +1,88 @@ | ||
using System.IO; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Text; | ||
|
||
namespace Celeste.Mod.Helpers { | ||
public class LogWriter : TextWriter { | ||
public class LogWriter : IDisposable { | ||
|
||
public TextWriter STDOUT; | ||
public OutputStreamCapture STDOUT; | ||
public OutputStreamCapture STDERR; | ||
public TextWriter File; | ||
|
||
public override Encoding Encoding { | ||
get { | ||
return STDOUT?.Encoding ?? File?.Encoding; | ||
} | ||
private bool captureStarted = false; | ||
|
||
public LogWriter(TextWriter @out, TextWriter err, TextWriter file, bool beginCapture = true) { | ||
STDOUT = new OutputStreamCapture(@out, this); | ||
STDERR = new OutputStreamCapture(err, this); | ||
File = file; | ||
if (beginCapture) | ||
BeginCapture(); | ||
} | ||
|
||
public Encoding Encoding => STDOUT.Encoding ?? STDERR.Encoding ?? File.Encoding; | ||
|
||
public void BeginCapture() { | ||
if (captureStarted) | ||
return; | ||
Console.SetOut(STDOUT); | ||
Console.SetError(STDERR); | ||
captureStarted = true; | ||
} | ||
|
||
public void EndCapture() { | ||
if (!captureStarted) | ||
return; | ||
Console.SetOut(STDOUT.Stream); | ||
Console.SetError(STDERR.Stream); | ||
captureStarted = false; | ||
} | ||
|
||
public void Dispose() { | ||
EndCapture(); | ||
// TextWriter.Close has no effect if the stream is a console stream | ||
STDOUT?.Close(); | ||
STDERR?.Close(); | ||
File?.Close(); | ||
} | ||
} | ||
|
||
public class OutputStreamCapture : TextWriter { | ||
public TextWriter Stream; | ||
public LogWriter Writer; | ||
|
||
public OutputStreamCapture(TextWriter stream, LogWriter writer) { | ||
Stream = stream; | ||
Writer = writer; | ||
} | ||
|
||
public override Encoding Encoding => Stream.Encoding; | ||
|
||
public override void Write(string value) { | ||
STDOUT?.Write(value); | ||
File?.Write(value); | ||
File?.Flush(); | ||
Stream.Write(value); | ||
Writer.File.Write(value); | ||
} | ||
|
||
public override void WriteLine(string value) { | ||
STDOUT?.WriteLine(value); | ||
File?.WriteLine(value); | ||
File?.Flush(); | ||
Stream.WriteLine(value); | ||
Writer.File.WriteLine(value); | ||
} | ||
|
||
public override void Write(char value) { | ||
STDOUT?.Write(value); | ||
File?.Write(value); | ||
File?.Flush(); | ||
Stream.Write(value); | ||
Writer.File.Write(value); | ||
} | ||
|
||
public override void Write(char[] buffer, int index, int count) { | ||
STDOUT?.Write(buffer, index, count); | ||
File?.Write(buffer, index, count); | ||
File?.Flush(); | ||
Stream.Write(buffer, index, count); | ||
Writer.File.Write(buffer, index, count); | ||
} | ||
|
||
public override void Flush() { | ||
STDOUT?.Flush(); | ||
File?.Flush(); | ||
} | ||
|
||
public override void Close() { | ||
STDOUT?.Close(); | ||
STDOUT = null; | ||
File?.Close(); | ||
File = null; | ||
Stream.Flush(); | ||
Writer.File.Flush(); | ||
} | ||
|
||
public override void Close() => Stream.Close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters