New test.
[mono.git] / mcs / class / System / System.Diagnostics / TextWriterTraceListener.cs
index fe362d68520f251c3c1ab6d03da50518508c9042..3a570ea11773eb3a8d81448785157aba65738fff 100644 (file)
-//\r
-// System.Diagnostics.TextWriterTraceListener.cs\r
-//\r
-// Author:\r
-//             John R. Hicks (angryjohn69@nc.rr.com)\r
-//\r
-// (C) 2001\r
-//\r
-namespace System.Diagnostics\r
-{\r
-       using System;\r
-       using System.IO;\r
-       using System.Text;\r
-       \r
-       /// <summary>\r
-       /// Directs tracing or debugging output to a <see cref="System.IO.TextWriter">\r
-       /// TextWriter</see> or to a <see cref="System.IO.Stream">Stream</see>,\r
-       /// such as <see cref="System.Console.Out">Console.Out</see> or\r
-       /// <see cref="System.IO.FileStream">FileStream</see>.\r
-       /// </summary>\r
-       public class TextWriterTraceListener : TraceListener\r
-       {       \r
-               \r
-               private TextWriter writer;\r
-               \r
-               /// <summary>\r
-               /// Initializes a new instance of the <see cref="TextWriterTraceListener">\r
-               /// TextWriterTraceListener</see> class with <see cref="System.IO.TextWriter">\r
-               /// TextWriter</see> as the output recipient.\r
-               /// </summary>\r
-               public TextWriterTraceListener() : base("TextWriter")\r
-               {\r
-                       writer = Console.Out;\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Initializes a new instance of the <see cref="TextWriterTraceListener">\r
-               /// TextWriterTraceListener</see> class, using the stream as the output\r
-               /// recipient of the debugging and tracing output.\r
-               /// </summary>\r
-               /// <param name="stream">\r
-               /// A <see cref="System.IO.Stream">Stream</see> that represents the stream the\r
-               /// <see cref="TextWriterTraceListener">TextWriterTraceListener</see> writes to.\r
-               /// </param>\r
-               /// <exception cref="System.ArgumentNullException">\r
-               /// The stream is a null reference.\r
-               /// </exception>\r
-               public TextWriterTraceListener(Stream stream) : base("")\r
-               {\r
-                       if(stream == null)\r
-                               throw new ArgumentNullException("Stream is null");\r
-                       \r
-                       writer = new StreamWriter(stream);\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Initializes a new instance of the <see cref="TextWriterTraceListener">\r
-               /// TextWriterTraceListener</see> class, using the file as the recipient\r
-               /// of the debugging and tracing output.\r
-               /// </summary>\r
-               /// <param name="fileName">\r
-               /// The name of the file the <see cref="TextWriterTraceListener">\r
-               /// TextWriterTraceListener</see> writes to.\r
-               /// </param>\r
-               /// <exception cref="System.ArgumentNullException">\r
-               /// The fileName is null.\r
-               /// </exception>\r
-               public TextWriterTraceListener(string fileName) : base("")\r
-               {\r
-                       if(fileName == null)\r
-                               throw new ArgumentNullException("Filename is null");\r
-                       \r
-                       FileStream fileStream = new FileStream(fileName, FileMode.Create);\r
-                       writer = new StreamWriter(fileStream);\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Initializes a new instance of the <see cref="TextWriterTraceListener">\r
-               /// TextWriterTraceListener</see> class using the specified writer as the\r
-               /// recipient of the tracing or debugging output.\r
-               /// </summary>\r
-               /// <param name="writer">\r
-               /// A <see cref="System.IO.TextWriter">TextWriter</see> that receives output\r
-               /// from the <see cref="TextWriterTraceListener">TextWriterTraceListener</see>.\r
-               /// </param>\r
-               /// <exception cref="System.ArgumentNullException">\r
-               /// The writer is a null reference\r
-               /// </exception>\r
-               public TextWriterTraceListener(TextWriter writer) : base("")\r
-               {\r
-                       if(writer == null)\r
-                               throw new ArgumentNullException("Given TextWriter is null");\r
-                       this.writer = writer;\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Initializes a new instance of the <see cref="TextWriterTraceListener">\r
-               /// TextWriterTraceListener</see> class with the specified name, using the\r
-               /// stream as the recipient of the tracing or debugging output.\r
-               /// </summary>\r
-               /// <param name="stream">\r
-               /// A <see cref="System.IO.Stream">Stream</see> that represents the stream the\r
-               /// <see cref="TextWriterTraceListener">TextWriterTraceListener</see>\r
-               /// writes to.\r
-               /// </param>\r
-               /// <param name="name">\r
-               /// The name of the new instance\r
-               /// </param>\r
-               /// <exception cref="System.ArgumentNullException">\r
-               /// The stream is a null reference\r
-               /// </exception>\r
-               public TextWriterTraceListener(Stream stream, string name) : base(name)\r
-               {\r
-                       if(stream == null)\r
-                               throw new ArgumentNullException("Supplied Stream is null");\r
-                       writer = new StreamWriter(stream);\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Initializes a new instance of the <see cref="TextWriterTraceListener">\r
-               /// TextWriterTraceListener</see> class with the specified name, using the\r
-               /// file as the recipient of the tracing or debugging output.\r
-               /// </summary>\r
-               /// <param name="fileName">\r
-               /// The name of the file the <see cref="TextWriterTraceListener">\r
-               /// TextWriterTraceListener</see> writes to.\r
-               /// </param>\r
-               /// <param name="name">\r
-               /// The name of the new instance\r
-               /// </param>\r
-               /// <exception cref="System.ArgumentNullException">\r
-               /// The file is a null reference.\r
-               /// </exception>\r
-               public TextWriterTraceListener(string fileName, string name) : base(name)\r
-               {\r
-                       if(fileName == null)\r
-                               throw new ArgumentNullException("Supplied file name is null");\r
-                       FileStream fileStream = new FileStream(fileName, FileMode.Create);\r
-                       writer = new StreamWriter(fileStream);\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Initializes a new instance of the <see cref="TextWriterTraceListener">\r
-               /// TextWriterTraceListener</see> class with the specified name, using\r
-               /// the specified writer as the recipient of the tracing or debugging output.\r
-               /// </summary>\r
-               /// <param name="writer">\r
-               /// A <see cref="System.IO.TextWriter">TextWriter</see> that receives the output\r
-               /// from the <see cref="TextWriterTraceListener">TextWriterTraceListener</see>.\r
-               /// </param>\r
-               /// <param name="name">\r
-               /// The name of the new instance.\r
-               /// </param>\r
-               /// <exception cref="System.ArgumentNullException">\r
-               /// The writer is a null reference.\r
-               /// </exception>\r
-               public TextWriterTraceListener(TextWriter writer, string name) : base(name)\r
-               {\r
-                       if(writer == null)\r
-                               throw new ArgumentNullException("The supplied writer is null");\r
-                       this.writer = writer;\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Gets or sets the writer that receives the debugging or tracing output.\r
-               /// </summary>\r
-               /// <value>\r
-               /// A <see cref="System.IO.TextWriter">TextWriter</see> that represents the writer\r
-               /// that receives the tracing or debugging output.\r
-               /// </value>\r
-               public TextWriter Writer\r
-               {\r
-                       get\r
-                       {\r
-                               return writer;\r
-                       }\r
-                       set\r
-                       {\r
-                               writer = value;\r
-                       }\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Closes the <see cref="System.IO.Writer">Writer</see> so that it no longer\r
-               /// receives tracing or debugging output.\r
-               /// </summary>\r
-               public override void Close()\r
-               {\r
-                       writer.Close();\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Flushes the output buffer for the <see cref="System.IO.Writer">Writer</see>.\r
-               /// </summary>\r
-               public override void Flush()\r
-               {\r
-                       writer.Flush();\r
-               }\r
-               \r
-               /// <summary>\r
-               /// Writes a message to this instance's <see cref="System.IO.Writer">Writer</see>.\r
-               /// </summary>\r
-               /// <param name="message">\r
-               /// A message to write.\r
-               /// </param>\r
-               public override void Write(string message)\r
-               {\r
-                       Console.Error.WriteLine("We should be getting output here");\r
-                       writer.Write(message);\r
-                       \r
-               }\r
-               \r
-               /// <summary>\r
-               /// Writes a message to this instance's <see cref="System.IO.Writer">Writer</see>\r
-               /// followed by a line terminator.\r
-               /// </summary>\r
-               /// <param name="message">\r
-               /// A message to write.\r
-               /// </param>\r
-               public override void WriteLine(string message)\r
-               {\r
-                       Console.Error.WriteLine("We should be getting output here, too");       \r
-                       writer.WriteLine(message);\r
-                       \r
-               }\r
-       }\r
-}\r
+//
+// System.Diagnostics.TextWriterTraceListener.cs
+//
+// Comments from John R. Hicks <angryjohn69@nc.rr.com> original implementation 
+// can be found at: /mcs/docs/apidocs/xml/en/System.Diagnostics
+//
+// Authors:
+//   Jonathan Pryor (jonpryor@vt.edu)
+//
+// (C) 2002 Jonathan Pryor
+//
+
+//
+// Permission is hereby granted, free of charge, to any person obtaining
+// a copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to
+// permit persons to whom the Software is furnished to do so, subject to
+// the following conditions:
+// 
+// The above copyright notice and this permission notice shall be
+// included in all copies or substantial portions of the Software.
+// 
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+//
+
+using System;
+using System.IO;
+using System.Diagnostics;
+
+namespace System.Diagnostics {
+
+       public class TextWriterTraceListener : TraceListener {
+
+               private TextWriter writer;
+
+               public TextWriterTraceListener () : base ("TextWriter")
+               {
+               }
+
+               public TextWriterTraceListener (Stream stream)
+                       : this (stream, "")
+               {
+               }
+
+               public TextWriterTraceListener (string fileName)
+                       : this (fileName, "")
+               {
+               }
+
+               public TextWriterTraceListener (TextWriter writer)
+                       : this (writer, "")
+               {
+               }
+
+               public TextWriterTraceListener (Stream stream, string name) 
+                       : base (name != null ? name : "")
+               {
+                       if (stream == null) 
+                               throw new ArgumentNullException ("stream");
+                       writer = new StreamWriter (stream);
+               }
+
+               public TextWriterTraceListener (string fileName, string name) 
+                       : base (name != null ? name : "")
+               {
+                       if (fileName == null)
+                               throw new ArgumentNullException ("fileName");
+                       writer = File.AppendText (fileName);
+               }
+
+               public TextWriterTraceListener (TextWriter writer, string name) 
+                       : base (name != null ? name : "")
+               {
+                       if (writer == null)
+                               throw new ArgumentNullException ("writer");
+                       this.writer = writer;
+               }
+
+               public TextWriter Writer {
+                       get {return writer;}
+                       set {writer = value;}
+               }
+
+               public override void Close ()
+               {
+                       if (writer != null) {
+                               writer.Flush ();
+                               writer.Close ();
+                               writer = null;
+                       }
+               }
+
+               protected override void Dispose (bool disposing)
+               {
+                       if (disposing)
+                               Close ();
+
+                       base.Dispose (disposing);
+               }
+
+               public override void Flush ()
+               {
+                       if (writer != null)
+                               writer.Flush ();
+               }
+
+               public override void Write (string message)
+               {
+                       if (writer != null) {
+                               if (NeedIndent)
+                                       WriteIndent ();
+                               writer.Write (message);
+                       }
+               }
+
+               public override void WriteLine (string message)
+               {
+                       if (writer != null) {
+                               if (NeedIndent)
+                                       WriteIndent ();
+                               writer.WriteLine (message);
+                               NeedIndent = true;
+                       }
+               }
+       }
+}
+