Skip to content

Commit

Permalink
Merge pull request #638 from SnipUndercover/capture-stderr
Browse files Browse the repository at this point in the history
  • Loading branch information
DemoJameson authored Jul 31, 2023
2 parents 4f91f68 + 82d77f2 commit 7138c23
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 52 deletions.
91 changes: 63 additions & 28 deletions Celeste.Mod.mm/Mod/Helpers/LogWriter.cs
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();
}
}
17 changes: 2 additions & 15 deletions Celeste.Mod.mm/Patches/Celeste.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,21 +173,8 @@ public static void Main(string[] args) {
Everest.PathLog = logfile;
using (Stream fileStream = new FileStream(logfile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite | FileShare.Delete))
using (StreamWriter fileWriter = new StreamWriter(fileStream, Console.OutputEncoding))
using (LogWriter logWriter = new LogWriter {
STDOUT = Console.Out,
File = fileWriter
}) {
try {
Console.SetOut(logWriter);

MainInner(args);
} finally {
if (logWriter.STDOUT != null) {
Console.SetOut(logWriter.STDOUT);
logWriter.STDOUT = null;
}
}
}
using (LogWriter logWriter = new LogWriter(Console.Out, Console.Error, fileWriter))
MainInner(args);

}

Expand Down
10 changes: 1 addition & 9 deletions MiniInstaller/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,7 @@ public static int Main(string[] args) {
File.Delete(PathLog);
using (Stream fileStream = File.OpenWrite(PathLog))
using (StreamWriter fileWriter = new StreamWriter(fileStream, Console.OutputEncoding))
using (LogWriter logWriter = new LogWriter {
STDOUT = Console.Out,
File = fileWriter
}) {
Console.SetOut(logWriter);

using (LogWriter logWriter = new LogWriter(Console.Out, Console.Error, fileWriter)) {
try {

if (!IsMonoVersionCompatible()) {
Expand Down Expand Up @@ -148,9 +143,6 @@ public static int Main(string[] args) {
Environment.SetEnvironmentVariable("MONOMOD_MODS", "");
Environment.SetEnvironmentVariable("MONOMOD_DEPENDENCY_MISSING_THROW", "");
}

Console.SetOut(logWriter.STDOUT);
logWriter.STDOUT = null;
}

return 0;
Expand Down

0 comments on commit 7138c23

Please sign in to comment.