2005-06-05 Peter Bartok <pbartok@novell.com>
[mono.git] / mcs / nant / src / Util / Log.cs
1 // NAnt - A .NET build tool\r
2 // Copyright (C) 2001 Gerry Shaw\r
3 //\r
4 // This program is free software; you can redistribute it and/or modify\r
5 // it under the terms of the GNU General Public License as published by\r
6 // the Free Software Foundation; either version 2 of the License, or\r
7 // (at your option) any later version.\r
8 //\r
9 // This program is distributed in the hope that it will be useful,\r
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
12 // GNU General Public License for more details.\r
13 //\r
14 // You should have received a copy of the GNU General Public License\r
15 // along with this program; if not, write to the Free Software\r
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
17 //\r
18 // Gerry Shaw (gerry_shaw@yahoo.com)\r
19 \r
20 namespace SourceForge.NAnt {\r
21 \r
22     using System;\r
23     using System.Collections;\r
24     using System.IO;\r
25     using System.Text;\r
26 \r
27     public class TextWriterCollection : ArrayList {\r
28     }\r
29 \r
30     public class Log {\r
31 \r
32         static bool _autoFlush  = false;\r
33         static int _indentLevel = 0;\r
34         static int _indentSize  = 4;\r
35 \r
36         static TextWriterCollection _listeners;\r
37 \r
38         protected Log() {\r
39         }\r
40 \r
41         ~Log() {\r
42             // make sure we release any open file handles\r
43             Close();\r
44         }\r
45 \r
46         public static bool AutoFlush {\r
47             get { return _autoFlush; }\r
48             set { _autoFlush = value; }\r
49         }\r
50 \r
51         public static int IndentLevel {\r
52             get { return _indentLevel; }\r
53             set { _indentLevel = value; }\r
54         }\r
55 \r
56         public static int IndentSize {\r
57             get { return _indentSize; }\r
58             set { _indentSize = value; }\r
59         }\r
60 \r
61         public static TextWriterCollection Listeners {\r
62             get {\r
63                 if (_listeners == null) {\r
64                     _listeners = new TextWriterCollection();\r
65                     _listeners.Add(Console.Out);\r
66                 }\r
67                 return _listeners;\r
68             }\r
69         }\r
70 \r
71         public static void Close() {\r
72             foreach (TextWriter writer in Listeners) {\r
73                 // never close the Console.Out writer\r
74                 if (writer != Console.Out) {\r
75                     writer.Close();\r
76                 }\r
77             }\r
78         }\r
79 \r
80         public static void Flush() {\r
81             foreach (TextWriter writer in Listeners) {\r
82                 writer.Flush();\r
83             }\r
84         }\r
85 \r
86         public static void Indent() {\r
87             IndentLevel++;\r
88         }\r
89 \r
90         public static void Unindent() {\r
91             if (IndentLevel <= 0) {\r
92                 throw new InvalidOperationException("IndentLevel must be greater than zero before calling Unindent()");\r
93             }\r
94             IndentLevel--;\r
95         }\r
96 \r
97         /// <summary>\r
98         /// Flag to indicate next string will start on a new line so that it can be indented.\r
99         /// </summary>\r
100         private static bool _newline = true;\r
101 \r
102         private static void PreprocessValue(ref string value) {\r
103             // if we are starting a new line then first indent the string\r
104             if (_newline) {\r
105                 if (IndentLevel > 0) {\r
106                     StringBuilder sb = new StringBuilder(value);\r
107                     sb.Insert(0, " ", IndentLevel * IndentSize);\r
108                     value = sb.ToString();\r
109                 }\r
110                 _newline = false;\r
111             }\r
112         }\r
113 \r
114         public static void Write(string value) {\r
115             PreprocessValue(ref value);\r
116             foreach (TextWriter writer in Listeners) {\r
117                 writer.Write(value);\r
118             }\r
119 \r
120             if (AutoFlush) {\r
121                 foreach (TextWriter writer in Listeners) {\r
122                     writer.Flush();\r
123                 }\r
124             }\r
125         }\r
126 \r
127         public static void WriteLine() {\r
128             WriteLine(String.Empty);\r
129         }\r
130 \r
131         public static void WriteLine(string value) {\r
132             PreprocessValue(ref value);\r
133             foreach (TextWriter writer in Listeners) {\r
134                 writer.WriteLine(value);\r
135             }\r
136 \r
137             if (AutoFlush) {\r
138                 foreach (TextWriter writer in Listeners) {\r
139                     writer.Flush();\r
140                 }\r
141             }\r
142 \r
143             // make sure we indent the next line\r
144             _newline = true;\r
145         }\r
146 \r
147         public static void Write(string format, params object[] arg) {\r
148             Write(String.Format(format, arg));\r
149         }\r
150 \r
151         public static void WriteLine(string format, params object[] arg) {\r
152             WriteLine(String.Format(format, arg));\r
153         }\r
154     }\r
155 }