KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > genimen > djeneric > util > DjLogPrintStream


1 /*
2  * Copyright (c) 2001-2005 by Genimen BV (www.genimen.com) All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, is permitted
5  * provided that the following conditions are met:
6  * - Redistributions of source code must retain the above copyright notice, this list of conditions
7  * and the following disclaimer.
8  * - Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * - All advertising materials mentioning features or use of this software must display the
12  * following acknowledgment: "This product includes Djeneric."
13  * - Products derived from this software may not be called "Djeneric" nor may
14  * "Djeneric" appear in their names without prior written permission of Genimen BV.
15  * - Redistributions of any form whatsoever must retain the following acknowledgment: "This
16  * product includes Djeneric."
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL GENIMEN BV, DJENERIC.ORG,
22  * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
28  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */

30 package com.genimen.djeneric.util;
31
32 import java.io.FileOutputStream JavaDoc;
33 import java.io.IOException JavaDoc;
34 import java.io.OutputStream JavaDoc;
35 import java.io.PrintStream JavaDoc;
36 import java.io.PrintWriter JavaDoc;
37 import java.text.SimpleDateFormat JavaDoc;
38 import java.util.Date JavaDoc;
39
40 public class DjLogPrintStream extends PrintStream JavaDoc
41 {
42   FileOutputStream JavaDoc _fos;
43   String JavaDoc _banner;
44
45   public static void logStdOut(String JavaDoc banner, String JavaDoc fileName, boolean autoFlush, boolean writeHeader) throws Exception JavaDoc
46   {
47     DjLogPrintStream newStdOut = new DjLogPrintStream(banner, fileName, System.out, autoFlush, writeHeader);
48     System.setOut(newStdOut);
49   }
50
51   public static void logStdErr(String JavaDoc banner, String JavaDoc fileName, boolean autoFlush, boolean writeHeader) throws Exception JavaDoc
52   {
53     DjLogPrintStream newStdErr = new DjLogPrintStream(banner, fileName, System.err, autoFlush, writeHeader);
54     System.setErr(newStdErr);
55   }
56
57   public static void logAll(String JavaDoc banner, String JavaDoc fileName, boolean autoFlush, boolean writeHeader) throws Exception JavaDoc
58   {
59     DjLogPrintStream newStdOut = new DjLogPrintStream(banner, fileName, System.out, autoFlush, writeHeader);
60     FileOutputStream JavaDoc fos = newStdOut.getFileOutputStream();
61     DjLogPrintStream newStdErr = new DjLogPrintStream(banner, fos, System.err, autoFlush, false);
62
63     System.setOut(newStdOut);
64     System.setErr(newStdErr);
65   }
66
67   public static void logAll(String JavaDoc banner, String JavaDoc fileName) throws Exception JavaDoc
68   {
69     logAll(banner, fileName, true, true);
70   }
71
72   private DjLogPrintStream(String JavaDoc banner, String JavaDoc logFileName, OutputStream JavaDoc out, boolean autoFlush, boolean writeHeader)
73       throws Exception JavaDoc
74   {
75     super(out, autoFlush);
76     _banner = banner;
77
78     _fos = new FileOutputStream JavaDoc(logFileName, true);
79     if (writeHeader) writeHeader();
80   }
81
82   private DjLogPrintStream(String JavaDoc banner, FileOutputStream JavaDoc fos, OutputStream JavaDoc out, boolean autoFlush, boolean writeHeader)
83       throws Exception JavaDoc
84   {
85     super(out, autoFlush);
86
87     _banner = banner;
88     _fos = fos;
89     if (writeHeader) writeHeader();
90   }
91
92   private String JavaDoc getDivLine(int len)
93   {
94     StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
95     for (int i = 0; i < len; i++)
96       sb.append("-");
97     return sb.toString();
98   }
99
100   private void writeHeader() throws Exception JavaDoc
101   {
102     SimpleDateFormat JavaDoc sf = new SimpleDateFormat JavaDoc("dd-MM-yyyy HH:mm:ss");
103
104     Date JavaDoc date = new Date JavaDoc(System.currentTimeMillis());
105     PrintWriter JavaDoc pw = new PrintWriter JavaDoc(_fos);
106     pw.println();
107
108     int len = 38;
109
110     if (_banner != null && _banner.trim().length() > 0)
111     {
112       len = Math.max(len, _banner.length());
113     }
114
115     String JavaDoc dl = getDivLine(len);
116     pw.println(dl);
117     if (_banner != null && _banner.trim().length() > 0) pw.println(_banner);
118     printProperty(pw, "java.runtime.name");
119     printProperty(pw, "java.runtime.version");
120     printProperty(pw, "java.vm.name");
121     printProperty(pw, "java.vm.version");
122     printProperty(pw, "java.vm.vendor");
123     printProperty(pw, "java.class.version");
124     printProperty(pw, "java.home");
125     printProperty(pw, "java.version");
126     printProperty(pw, "file.encoding");
127     pw.println("Logging started at " + sf.format(date));
128     pw.println(dl);
129
130     pw.flush();
131   }
132
133   protected void printProperty(PrintWriter JavaDoc pw, String JavaDoc property)
134   {
135     pw.println(property + "=" + System.getProperty(property));
136   }
137
138   public FileOutputStream JavaDoc getFileOutputStream()
139   {
140     return _fos;
141   }
142
143   public void flush()
144   {
145     super.flush();
146     try
147     {
148       _fos.flush();
149     }
150     catch (IOException JavaDoc iox)
151     {
152       // ignore to avoid recursive problems when trying to print this error
153
}
154   }
155
156   public void close()
157   {
158     super.close();
159
160     try
161     {
162       _fos.close();
163     }
164     catch (IOException JavaDoc iox)
165     {
166       // ignore to avoid recursive problems when trying to print this error
167
}
168
169   }
170
171   public void write(int b)
172   {
173     super.write(b);
174
175     try
176     {
177       _fos.write(b);
178     }
179     catch (IOException JavaDoc iox)
180     {
181       // ignore to avoid recursive problems when trying to print this error
182
}
183   }
184
185   public void write(byte buf[], int off, int len)
186   {
187     super.write(buf, off, len);
188
189     try
190     {
191       _fos.write(buf, off, len);
192     }
193     catch (IOException JavaDoc iox)
194     {
195       // ignore to avoid recursive problems when trying to print this error
196
}
197   }
198 }
Popular Tags