private static TextWriter MakeDebugOutputTextWriter(String streamLabel) { TextWriter output = new __DebugOutputTextWriter(streamLabel); output.WriteLine("Output redirected to debugger from a bit bucket."); return TextWriter.Synchronized(output); }
// For console apps, the console handles are set to values like 3, 7, // and 11 OR if you've been created via CreateProcess, possibly -1 // or 0. -1 is definitely invalid, while 0 is probably invalid. // Also note each handle can independently be invalid or good. // For Windows apps, the console handles are set to values like 3, 7, // and 11 but are invalid handles - you may not write to them. However, // you can still spawn a Windows app via CreateProcess and read stdout // and stderr. // So, we always need to check each handle independently for validity // by trying to write or read to it, unless it is -1. // We do not do a security check here, under the assumption that this // cannot create a security hole, but only waste a user's time or // cause a possible denial of service attack. private static void InitializeStdOutError(bool stdout) { // Set up Console.Out or Console.Error. lock (InternalSyncObject) { if (stdout && _out != null) { return; } else if (!stdout && _error != null) { return; } TextWriter writer = null; #if USE_DEBUG_CONSOLE writer = new __DebugOutputTextWriter(); #else Stream s; if (stdout) { s = OpenStandardOutput(DefaultConsoleBufferSize); } else { s = OpenStandardError(DefaultConsoleBufferSize); } if (s == Stream.Null) { writer = TextWriter.Synchronized(StreamWriter.Null); } else { Encoding encoding = OutputEncoding; StreamWriter stdxxx = new StreamWriter(s, encoding, DefaultConsoleBufferSize, true); stdxxx.HaveWrittenPreamble = true; stdxxx.AutoFlush = true; writer = TextWriter.Synchronized(stdxxx); } #endif // USE_DEBUG_CONSOLE if (stdout) { _out = writer; } else { _error = writer; } } }