The Labs \ Source Viewer \ SSCLI \ System.IO \ __DebugOutputTextWriter

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. #if _DEBUG
  16. // This class writes to wherever OutputDebugString writes to. If you don't have
  17. // a Windows app (ie, something hosted in IE), you can use this to redirect Console
  18. // output for some good old-fashioned console spew in MSDEV's debug output window.
  19. using System;
  20. using System.IO;
  21. using System.Text;
  22. using System.Security;
  23. using System.Runtime.InteropServices;
  24. using Microsoft.Win32;
  25. using System.Globalization;
  26. namespace System.IO
  27. {
  28.     internal class __DebugOutputTextWriter : TextWriter
  29.     {
  30.         private readonly string _consoleType;
  31.        
  32.         internal __DebugOutputTextWriter(string consoleType) : base(CultureInfo.InvariantCulture)
  33.         {
  34.             _consoleType = consoleType;
  35.         }
  36.        
  37.         public override Encoding Encoding {
  38.             get {
  39.                 if (Marshal.SystemDefaultCharSize == 1)
  40.                     return Encoding.Default;
  41.                 else
  42.                     return new UnicodeEncoding(false, false);
  43.             }
  44.         }
  45.        
  46.         public override void Write(char c)
  47.         {
  48.             OutputDebugString(c.ToString());
  49.         }
  50.        
  51.         public override void Write(string str)
  52.         {
  53.             OutputDebugString(str);
  54.         }
  55.        
  56.         public override void Write(char[] array)
  57.         {
  58.             if (array != null)
  59.                 OutputDebugString(new string(array));
  60.         }
  61.        
  62.         public override void WriteLine(string str)
  63.         {
  64.             if (str != null)
  65.                 OutputDebugString(_consoleType + str);
  66.             else
  67.                 OutputDebugString("<null>");
  68.             OutputDebugString(new string(CoreNewLine));
  69.         }
  70.        
  71.         [DllImport(Win32Native.KERNEL32, CharSet = CharSet.Auto), SuppressUnmanagedCodeSecurityAttribute()]
  72.         private static extern void OutputDebugString(string output);
  73.     }
  74. }
  75. #endif // _DEBUG

Developer Fusion