Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/gui/JspmLogWriter.java


1   /*-----------------------------------------------------------------------------------------------------*/
2   /*                                                                                                     */
3   /*  Copyright (C)                                                                                      */ 
4   /*                                                                                                     */
5   /*  This program is free software; you can redistribute it and/or modify it under the terms of the GNU */
6   /*  General Public License as published by the Free Software Foundation; either version 2 of the       */
7   /*  License, or (at your option) any later version.                                                    */
8   /*                                                                                                     */
9   /*  This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;          */
10  /*  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR                   */
11  /*  PURPOSE. See the GNU General Public License for more details.                                      */
12  /*                                                                                                     */
13  /*  You should have received a copy of the GNU General Public License along with this program; if      */
14  /*  not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA           */
15  /*  02111-1307 USA                                                                                     */
16  /*                                                                                                     */
17  /*-----------------------------------------------------------------------------------------------------*/
18  /*                                                                                                     */
19  /* Author:  Steve Randall (strand012001@yahoo.com)                                                     */
20  /* Version: 0.0.1                                                                                      */
21  /* Date:    05/09/2001                                                                                 */
22  /*                                                                                                     */
23  /*-----------------------------------------------------------------------------------------------------*/
24  
25  package com.gui;
26  
27  import java.io.*;
28  import java.util.*;
29  import java.text.*;
30  
31  /**
32   * JSM log writer.
33   * 
34   * @author Steve Randall (strand012001@yahoo.com)
35   * @version 0.0.1
36   * @date 10/09/2001
37   */
38  public class JspmLogWriter implements Serializable
39  {
40    private int debugLevel = 0;
41    private PrintWriter out = null;
42    private PrintStream pos = null;
43  
44    public JspmLogWriter(String fileName, int debugLevel) {
45      this.debugLevel = debugLevel;
46      
47      try {
48        
49        out = new PrintWriter(new FileOutputStream(fileName));
50        
51      } catch (FileNotFoundException e) {
52        ; // Ignore
53      }
54    }
55    
56    public JspmLogWriter(PrintStream ps, int debugLevel) {
57      this.debugLevel = debugLevel;
58      pos = ps;      
59    }
60    
61    public JspmLogWriter(PrintWriter pw, int debugLevel) {
62      this.debugLevel = debugLevel;
63      out = pw;      
64    }
65    
66    /**
67     * Returns the severity as a string.
68     *
69     * @param <code>severity</code> Severity code (0-6).
70     * @return <code>string</code> Severity name.
71     */
72    private String sev2char(int severity) {
73      switch(severity) {
74      case 5: return("D");
75      case 4: return("D");
76      case 3: return("M");
77      case 2: return("M");
78      case 1: return("W");
79      case 0: return("F");
80      }
81      return("D");
82    }
83    
84    private String adjust(int in, int len) {
85      if(len == 3 && in < 100)
86        return "0"+in;
87      else if(len == 3 && in < 10)
88        return "00"+in;
89      else if(len == 2 && in < 10)
90        return "0"+in;
91      else
92        return ""+in;
93    }
94    
95    /**
96     * Logs a message to a the previously defined logfile. Each entry has the format:<P>
97     * <blockquotes>
98     *  Date/Time - Class - [Severity] - Function - Message
99     * </blockquotes> <p>
100    * The severities are as follows: F: Fatal [0], E: Error [1], W: Warning[2], I: Info[3], D: Debug[4]. An
101    * entry is only written, if the supplied severity is > then the debug level. The debuglevel can be set
102    * using the function setDebugLevel. In case the debugFile is not set the error message will be
103    * written to stderr.<p>
104    *
105    * The following line shows an example:<p>
106    *
107    * 12/3/00 4:11 PM - TNGEM  - [W] - CalendarCheckTimeCould not check calendar: ErrorCode: -811 Date is on but time is off
108    *
109    * @param <code>severity</code> Severity code (0-4).
110    * @param <code>class</code> class name.
111    * @param <code>function</code> function name.
112    * @param <code>message</code> message text.
113    */
114   public void logMessage(int severity, String clas, String function, String message) {
115     
116     if(severity <= debugLevel) {
117       Date d = new Date();
118       Calendar rightNow = Calendar.getInstance();        
119       if(pos != null) {
120         
121   System.err.println(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(d)+
122          " - ["+sev2char(severity)+"] - "+clas+"."+function+": "+message);
123         
124       } else {
125         
126   out.println(rightNow.get(Calendar.DATE)+"/"+rightNow.get(Calendar.MONTH)+"/"+rightNow.get(Calendar.YEAR)+" "+
127         adjust(rightNow.get(Calendar.HOUR), 2)+":"+
128         adjust(rightNow.get(Calendar.MINUTE), 2)+":"+
129         adjust(rightNow.get(Calendar.SECOND), 2)+"."+
130         adjust(rightNow.get(Calendar.MILLISECOND), 3)+
131         " - ["+sev2char(severity)+"] - "+clas+"."+function+": "+message);
132   out.flush();
133       }
134     }
135   }
136   
137   /**
138    * Logs a message to a the previously defined logfile. Each entry has the format:<P>
139    * <blockquotes>
140    *  Date/Time - Class - [Severity] - Function - Message
141    * </blockquotes> <p>
142    * The severities are as follows: F: Fatal [0], E: Error [1], W: Warning[2], I: Info[3], D: Debug[4]. An
143    * entry is only written, if the supplied severity is > then the debug level. The debuglevel can be set
144    * using the function setDebugLevel. In case the debugFile is not set the error message will be
145    * written to stderr.<p>
146    *
147    * The following line shows an example:<p>
148    *
149    * 12/3/00 4:11 PM - TNGEM  - [W] - CalendarCheckTimeCould not check calendar: ErrorCode: -811 Date is on but time is off
150    *
151    * @param <code>severity</code> Severity code (0-4).
152    * @param <code>class</code> class name.
153    * @param <code>function</code> function name.
154    * @param <code>message</code> message text.
155    */
156   public void log(int severity, String clas, String function, String message) {
157     
158     if(severity <= debugLevel) {
159       Date d = new Date();
160       Calendar rightNow = Calendar.getInstance();        
161       if(pos != null) {
162   
163   System.err.println(DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.LONG).format(d)+
164          " - ["+sev2char(severity)+"] - "+clas+"."+function+": "+message);
165   
166       } else {
167   
168   out.println(rightNow.get(Calendar.DATE)+"/"+rightNow.get(Calendar.MONTH)+"/"+rightNow.get(Calendar.YEAR)+" "+
169         adjust(rightNow.get(Calendar.HOUR), 2)+":"+
170         adjust(rightNow.get(Calendar.MINUTE), 2)+":"+
171         adjust(rightNow.get(Calendar.SECOND), 2)+"."+
172         adjust(rightNow.get(Calendar.MILLISECOND), 3)+
173         " - ["+sev2char(severity)+"] - "+clas+"."+function+": "+message);
174   out.flush();
175       }
176     }
177   }
178 }
179 
180 
181 
182 
183 
184 
185 
186 
187 
188 
189 
190 
191 
192 
193 
194 
195 
196 
197 
198 
199 
200 
201