KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jbet > Logger


1 /*
2  * JBET - Java Binary Enhancement Tool
3  * Copyright (c) 2003 Networks Associates Technology, Inc.
4  *
5  * This software was developed under DARPA/SPAWAR contract
6  * N66001-00-C-8602 "SPMA" as part of the
7  * DARPA OASIS research program.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  * notice, this list of conditions and the following disclaimer in the
16  * documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
19  * 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 THE AUTHOR OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  */

30
31 package jbet;
32 import java.io.*;
33 import java.util.*;
34 import java.lang.reflect.Field JavaDoc;
35
36 /**
37  * Log file handler.
38  *
39  * $Id: Logger.java,v 1.10 2003/09/09 17:31:54 areisse Exp $
40  *
41  * @author Larry D'Anna
42  * @version 0.1
43  * @since JDK 1.1.8
44  */

45
46 public class Logger {
47
48     static final int ALL = 0; // output all messages sent to this Logger
49

50     // Message severity levels / Logger message filter levels
51
static final int DEBUG = 10;
52     static final int INFO = 20;
53     static final int WARN = 30;
54     static final int ERROR = 40;
55     static final int FATAL = 50;
56
57     public static int str2level(String JavaDoc s) {
58     if (s.equalsIgnoreCase("all"))
59         return ALL;
60     if (s.equalsIgnoreCase("debug"))
61         return DEBUG;
62     if (s.equalsIgnoreCase("info"))
63         return INFO;
64     if (s.equalsIgnoreCase("warn"))
65         return WARN;
66     if (s.equalsIgnoreCase("error"))
67         return ERROR;
68     if (s.equalsIgnoreCase("fatal"))
69         return FATAL;
70     return -1;
71     }
72
73     public static String JavaDoc level2str (int s)
74     {
75     if (s >= FATAL)
76         return "fatal";
77     else if (s >= ERROR)
78         return "error";
79     else if (s >= WARN)
80         return "warn";
81     else if (s >= INFO)
82         return "info";
83     else if (s >= DEBUG)
84         return "debug";
85     else
86         return "all";
87     }
88
89     PrintStream out; // Output sink assocated with this Logger
90
int loglevel; // Min. level of messages added to the log
91
HashSet facilities; // facilities to show all messages from
92

93     // Constructor
94

95     /**
96      * Output redirect and filter level init
97      * @see Jbet initialization code.
98      */

99     Logger(PrintStream out, int level) {
100     this.out = out;
101     loglevel = level;
102     facilities = new HashSet();
103     }
104
105     /**
106      * Jbet -{v,V} or Jbet -L {"all","debug",...}
107      * @see Jbet.parseArgs
108      */

109     public void setLevel(int l) {
110     loglevel = l;
111     }
112
113     public void addFacility (String JavaDoc f) {
114     facilities.add (f);
115     }
116     
117     /**
118      * Using the Jbet R option replaces the Logger's output with System.err
119      * @see Jbet.parseArgs
120      */

121     public void setOut(PrintStream out) {
122     this.out = out;
123     }
124
125     public void outline(String JavaDoc s, int lev, String JavaDoc facility, String JavaDoc cmd) {
126     if (filter (lev, facility)) {
127         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ();
128
129         if (cmd != null) {
130         sb.append (cmd);
131         sb.append (":");
132         }
133         if (facility != null) {
134         sb.append (facility);
135         sb.append (":");
136         }
137         sb.append (" ");
138         sb.append (s);
139         out.println(sb.toString());
140     }
141     }
142
143     static String JavaDoc class2facil(String JavaDoc cname) {
144
145     Class JavaDoc cr;
146     try {
147         cr = Class.forName (cname);
148         Field JavaDoc f = cr.getField ("JbetLogFacility");
149         return f.get (null).toString();
150     }
151     catch (Exception JavaDoc e) {
152
153         int i = cname.indexOf("$");
154         if (i != -1)
155         return class2facil (cname.substring(0,i));
156         else
157         return null;
158     }
159     }
160
161     static String JavaDoc getfacil() {
162     StackTraceElement JavaDoc [] st = new Exception JavaDoc().getStackTrace();
163     String JavaDoc cname;
164     for (int i = 0; i < st.length; i++) {
165         cname = st[i].getClassName();
166         if (!cname.equals("jbet.Logger") &&
167         !cname.startsWith("jbet.Logger$") &&
168         !cname.startsWith("java.") &&
169         !cname.equals("jbet.LineWriter") &&
170         !cname.startsWith("jbet.LineWriter$")) {
171
172         String JavaDoc fac = class2facil (cname);
173         if (fac != null)
174             return fac;
175         }
176     }
177     return null;
178     }
179
180     static String JavaDoc getcmd() {
181     StackTraceElement JavaDoc [] st = new Exception JavaDoc().getStackTrace();
182     String JavaDoc cname;
183
184     for (int i = 0; i < st.length; i++) {
185         cname = st[i].getClassName();
186
187         Class JavaDoc cr;
188
189         try {
190         cr = Class.forName (cname);
191         } catch (Exception JavaDoc e) {
192         continue;
193         }
194         if (Command.class.isAssignableFrom (cr)) {
195         if (cname.startsWith ("jbet.cmd.")) {
196             i = cname.indexOf("$", 9);
197             if (i != -1)
198             return cname.substring(9,i-9);
199             else
200             return cname.substring (9);
201         }
202         else
203             return cname;
204         }
205     }
206
207     return "<none>";
208     }
209
210     public LineWriter getStream(final int level) {
211     return new LineWriter() {
212         String JavaDoc currfacility;
213         String JavaDoc currcmd;
214
215         protected void doflush() {}
216         protected void doclose() {}
217         protected void outline(String JavaDoc s) {
218             Logger.this.outline(s,level,currfacility,currcmd);
219         }
220         protected void dowrite(char[]buf, int off, int len) {
221             if (startofline) {
222             currfacility = Logger.getfacil();
223             currcmd = Logger.getcmd();
224             }
225
226             super.dowrite(buf, off, len);
227         }
228         };
229     }
230
231     private boolean filter(int level, String JavaDoc fac) {
232     return level >= loglevel || facilities.contains (fac);
233     }
234 }
235                 
236             
237         
238
239
240
241
Popular Tags