001// Copyright (c) 2001 Hursh Jain (http://www.mollypages.org) 
002// The Molly framework is freely distributable under the terms of an
003// MIT-style license. For details, see the molly pages web site at:
004// http://www.mollypages.org/. Use, modify, have fun !
005
006package fc.io;
007
008import java.io.*;
009import java.util.*;
010
011import fc.util.*;
012
013/**
014A default implementation of {@link Log} that provides logging to a
015PrintStream destination and is intended for sending normal application
016logs to <code>System.{out, error}</code>
017<p>
018Messages written to output destinations depend upon the flushing policy of
019the output destination. Destinations that use <tt>System.{err,out}</tt>
020will flush messages after every log method invocation.
021<p>
022This class should be sufficient for all logging tasks. However custom
023loggers that subclass {@link Log} can also be written if really desired.
024
025@author hursh jain
026*/
027public class SystemLog extends Log
028{
029final PrintStream out;
030
031//or level names
032private final static String level_name_sep = " \t";
033
034/** 
035Creates a new SystemLog with a default destination of <tt>System.err</tt>
036and a default level of {@link Log#DEFAULT_LEVEL}
037
038@param  name    name of this log (any arbitrary string)
039*/
040public SystemLog(String name)
041  {
042  this(name, System.err, DEFAULT_LEVEL);
043  }
044
045/** 
046Creates a new SystemLog with the specified destination and a 
047default level of {@link Log#DEFAULT_LEVEL}
048
049@param  name    name of this log (any arbitrary string)
050@param  out     the output destination
051*/
052public SystemLog(String name, PrintStream out)
053  {
054  this(name, out, DEFAULT_LEVEL);
055  }
056
057/** 
058Creates a new SystemLog. The newly created log will automatically add
059itself to the list of all logs maintained by {@link Log} and a subsequent
060call to {@link Log#get(String)} with the same name will return this log.
061
062@param  name    name of this log (any arbitrary string)
063@param  out     the output destination
064@param  loglevel  the logging level
065*/
066public SystemLog(String name, PrintStream out, LogLevel loglevel)
067  {
068  super(name, loglevel);  
069  assert out != null;
070  this.out = out;
071  addLog(this);
072  }
073
074/**
075Closes the log by flushing the destination PrintStream.
076<code>close()</code> is <b>not</b> called on the PrintStream
077since it would make PrintStreams like <code>System.out</code> 
078unusable by other code.
079*/
080public void close()
081  {
082  out.flush();  
083  }
084
085public void log(LogLevel level, Object str1) 
086  { 
087  synchronized(out)
088    {
089    if (printLevelName) {
090      out.print(level.desc);
091      out.print(level_name_sep);
092      }
093
094    if (printTimestamp) {
095      out.print(getTS());
096      out.print(" ");
097      }   
098      
099    out.println(str1);
100    }
101  }
102    
103    
104public void log(LogLevel level, Object str1, Object str2) 
105  {
106  synchronized(out)
107    {
108    if (printLevelName) {
109      out.print(level.desc);
110      out.print(level_name_sep);
111      }
112
113    if (printTimestamp) {
114      out.print(getTS());
115      out.print(" ");
116      }   
117
118    out.print(str1);
119    out.print(" ");
120    out.println(str2);
121    }
122  }
123  
124public void log(LogLevel level, Object str1, Object str2, Object str3) 
125  { 
126  synchronized(out)
127    {
128    if (printLevelName) {
129      out.print(level.desc);
130      out.print(level_name_sep);
131      }
132
133    if (printTimestamp) {
134      out.print(getTS());
135      out.print(" ");
136      }   
137
138    out.print(str1);
139    out.print(" ");
140    out.print(str2);
141    out.println(str3);
142    }
143  }
144    
145public void log(LogLevel level, Object str1, Object str2, 
146             Object str3, Object str4) 
147  {
148  synchronized(out)
149    {
150    if (printLevelName) {
151      out.print(level.desc);
152      out.print(level_name_sep);
153      }
154
155    if (printTimestamp) {
156      out.print(getTS());
157      out.print(" ");
158      }   
159
160    out.print(str1);
161    out.print(" ");
162    out.print(str2);
163    out.print(str3);
164    out.println(str4);  
165    }
166  }
167
168public void log(LogLevel level, Object str1, Object str2, 
169             Object str3, Object str4,
170             Object str5) 
171  { 
172  synchronized(out)
173    {
174    if (printLevelName) {
175      out.print(level.desc);
176      out.print(level_name_sep);
177      }
178
179    if (printTimestamp) {
180      out.print(getTS());
181      out.print(" ");
182      }   
183
184    out.print(str1);
185    out.print(" ");
186    out.print(str2);
187    out.print(str3);
188    out.print(str4);  
189    out.println(str5);  
190    }
191  }
192
193public void log(LogLevel level, Object str1, Object str2, 
194             Object str3, Object str4,
195             Object str5, Object str6) 
196  {
197  synchronized(out)
198    {
199    if (printLevelName) {
200      out.print(level.desc);
201      out.print(level_name_sep);
202      }
203
204    if (printTimestamp) {
205      out.print(getTS());
206      out.print(" ");
207      }   
208
209    out.print(str1);
210    out.print(" ");
211    out.print(str2);
212    out.print(str3);
213    out.print(str4);  
214    out.print(str5);  
215    out.println(str6);  
216    }
217  }
218
219public void log(LogLevel level, Object str1, Object str2, 
220             Object str3, Object str4,
221             Object str5, Object str6,
222             Object str7) 
223  {
224  synchronized(out)
225    {
226    if (printLevelName) {
227      out.print(level.desc);
228      out.print(level_name_sep);
229      }
230
231    if (printTimestamp) {
232      out.print(getTS());
233      out.print(" ");
234      }   
235
236    out.print(str1);
237    out.print(" ");
238    out.print(str2);
239    out.print(str3);
240    out.print(str4);  
241    out.print(str5);  
242    out.print(str6);  
243    out.println(str7);  
244    }
245  }
246
247public void log(LogLevel level, Object str1, Object str2, 
248             Object str3, Object str4,
249             Object str5, Object str6,
250             Object str7, Object str8) 
251  {
252  synchronized(out)
253    {
254    if (printLevelName) {
255      out.print(level.desc);
256      out.print(level_name_sep);
257      }
258
259    if (printTimestamp) {
260      out.print(getTS());
261      out.print(" ");
262      }   
263
264    out.print(str1);
265    out.print(" ");
266    out.print(str2);
267    out.print(str3);
268    out.print(str4);  
269    out.print(str5);  
270    out.print(str6);  
271    out.print(str7);  
272    out.println(str8);  
273    }
274  }
275
276public void log(LogLevel level, Object str1, Object str2, 
277             Object str3, Object str4,
278             Object str5, Object str6,
279             Object str7, Object str8, 
280             Object... args) 
281  {
282  synchronized(out)
283    {
284    if (printLevelName) {
285      out.print(level.desc);
286      out.print(level_name_sep);
287      }
288
289    if (printTimestamp) {
290      out.print(getTS());
291      out.print(" ");
292      }   
293
294    out.print(str1);
295    out.print(" ");
296    out.print(str2);
297    out.print(str3);
298    out.print(str4);  
299    out.print(str5);  
300    out.print(str6);  
301    out.print(str7);  
302    out.print(str8);
303    
304    final int len = args.length;
305    for (int i = 0; i < len; i++) {
306      out.print(args[i]);
307      }
308    
309    out.println();
310    }
311  }
312
313
314public static void main(String args[]) throws Exception
315  {
316  SystemLog l = new SystemLog("stdout2");
317  l.printTimestamp(true);
318  l.printRelativeTimestamp(true);
319  System.out.println("printing using new log with timestamps=true");
320  l.log(INFO, "abc");
321  l.bug("some debug message -- should not print");  
322  l.error("error message");
323  l.warn("warn message ", "hah ", "34324 ", "foo ", "bar ", "baz");
324  l.info("info message: ", "hello", "world");
325  l.setLevel(DEBUG);
326  l.bug("some debug message -- should print");  
327  
328  l.printRelativeTimestamp(false);
329  System.out.println("sleeping for 2 seconds.....");
330  Thread.currentThread().sleep(2000);
331  System.out.println("setting level to 'inFO'");
332  l.setLevel("inFO");
333  System.out.println("level set to: " + l.currentLevel);
334  
335  l.logSystemInfo();
336  System.out.println("setting level to 'inFOO'");
337  l.setLevel("inFOO");
338  System.out.println("level set to: " + l.currentLevel);
339
340  SystemLog stdout = Log.getDefault();
341  System.out.println("printing using Default Log");
342  stdout.log(SystemLog.WARN, "some warning");
343  
344  SystemLog l2 = new SystemLog("stdout3");
345  Log.setLevelForAll("std", Log.ERROR);
346  System.out.println("Setting all logs to level: error");
347  System.out.println("l1=" + l);
348  System.out.println("l2=" + l2);
349  }
350}