public void WriteTest01() { using var sw1 = new StringWriter(); using var sw2 = new StringWriter(); using var tee = new TeeTextWriter(sw1, sw2); tee.Write("abc"); tee.Flush(); Assert.Equal("abc", sw1.ToString()); Assert.Equal("abc", sw2.ToString()); }
/// <summary> /// This causes any text written to the status log to also be written to 'writer' as well. /// </summary> public static void AttachWriterToLogStream(TextWriter writer) { if (s_logWriter == null) { s_logWriter = new TextEditorWriter(LogWindow.TextEditor); } if (writer != null) { if (!(s_logWriter is TextEditorWriter)) { throw new InvalidOperationException("Only one writer can be attached to the log stream simultaneously"); } s_logWriter = new TeeTextWriter(writer, s_logWriter); } }
public static void Initialize(string log_directory, string program_identifier, LogLevel cutoff_level, bool running_in_foreground) { Log.log_directory = log_directory; Log.program_identifier = program_identifier; Log.cutoff_level = cutoff_level; Log.running_in_foreground = running_in_foreground; PruneOldLogs (); log_name_prefix = String.Format ("{0:yyyy-MM-dd-HH-mm-ss}-", DateTime.Now); if (program_identifier.Length > 6) program_identifier_truncated = program_identifier.Substring (0, 6); else program_identifier_truncated = program_identifier; log_writer = NewLogWriter (program_identifier); exception_writer = NewDelayedLogWriter (program_identifier + "Exceptions"); TextWriter console_log_writer; console_log_writer = NewDelayedLogWriter (program_identifier + "Console"); TextWriter console_redirect_writer; if (running_in_foreground) { foreground_echo_writer = Console.Out; console_redirect_writer = new TeeTextWriter (Console.Out, console_log_writer); } else { console_redirect_writer = console_log_writer; } // Redirect the console output to a special file Console.SetOut (console_redirect_writer); Console.SetError (console_redirect_writer); if (! running_in_foreground) { // Now redirect the *actual* stdout/stderr to our main // log file. This is used to catch Mono crash reports // in our logs. Note that this doesn't override the // console redirection above, which is good. StreamWriter sw = (StreamWriter) log_writer; FileStream fs = (FileStream) sw.BaseStream; int fd = (int) fs.Handle; Mono.Unix.Native.Syscall.dup2 (fd, 1); // stdout Mono.Unix.Native.Syscall.dup2 (fd, 2); // stderr // If we are running in the background, redirect stdin to /dev/null FileStream dev_null_stream = new FileStream ("/dev/null", FileMode.Open, FileAccess.Read, FileShare.ReadWrite); TextReader dev_null_reader = new StreamReader (dev_null_stream); Console.SetIn (dev_null_reader); } }