KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > corba > runtime > TheLogger


1 // ====================================================================
2
//
3
// ECM: The Extensible Container Model
4
// Copyright (C) 2004 THALES
5
// Contact: openccm-ecm@objectweb.org
6
//
7
// This library is free software; you can redistribute it and/or
8
// modify it under the terms of the GNU Lesser General Public
9
// License as published by the Free Software Foundation; either
10
// version 2.1 of the License, or any later version.
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20
// USA
21
//
22
// Initial developer(s): Mathieu Vadet.
23
// Initial Funding: IST COACH European project (IST-2001-34445)
24
// http://www.ist-coach.org
25
//
26
// ====================================================================
27

28
29
30 package org.objectweb.corba.runtime;
31
32 /**
33  ** <p>The <tt>TheLogger</tt> class provides very basic log features. Messages are log to the
34  ** standard error output. By default, no message is displayed. To turn on the <tt>LOG</tt>
35  ** or <tt>DEBUG</tt> levels, the <tt>log</tt> or <tt>debug</tt> Java system property must be
36  ** set with a value of "true". Note that turning on the <tt>DEBUG</tt> level also turns on
37  ** the <tt>LOG</tt> level. Finally, the <tt>runtime.id</tt> property may also be set to display
38  ** a process specific information.</p>
39  **
40  ** The format of the displayed message is the following:<br>
41  ** LEVEL:runtime.id:[classname/methodname] message<br>
42  **
43  **/

44 public class TheLogger
45 {
46     static public int FATAL = 0;
47     static public int ERROR = 1;
48     static public int LOG = 2;
49     static public int DEBUG = 3;
50
51     static private boolean _debug_on;
52     static private boolean _log_on;
53     static private String JavaDoc _runtime_name;
54
55     // NOTE:
56
//static private java.io.FileWriter _log_file;
57

58     static {
59         // default init
60
_log_on = false;
61         _debug_on = false;
62
63         // check if log is on
64
String JavaDoc flag = System.getProperty("log");
65         if ((flag!=null) && (flag.equals("true"))) {
66             _log_on = true;
67         }
68
69         // check if debug is on (in that case, log is also activated)
70
flag = System.getProperty("debug");
71         if ((flag!=null) && (flag.equals("true"))) {
72             _debug_on = true;
73             _log_on = true;
74         }
75
76         // obtain runtime name from properties
77
//_runtime_name = System.getProperty("runtime.id");
78

79         // create log file
80
/*
81         try {
82             String fname = "logger.out";
83             _log_file = new java.io.FileWriter(fname);
84         } catch (Exception ex) {
85             // ignore
86         }
87         */

88     }
89
90     static public String JavaDoc
91     getIndentation(int length)
92     {
93         char[] cindent = new char[length];
94         for (int i=0;i<cindent.length;i++) {
95             cindent[i] = ' ';
96         }
97
98         return new String JavaDoc(cindent);
99     }
100
101     static public boolean
102     debugOn()
103     {
104         return _debug_on;
105     }
106
107     static public boolean
108     logOn()
109     {
110         return _log_on;
111     }
112
113
114     static public String JavaDoc
115     getRuntimeName()
116     {
117         return System.getProperty("runtime.id");
118     }
119
120     static public void
121     log(int level, String JavaDoc clazz, String JavaDoc meth, String JavaDoc msg)
122     {
123         if (level==ERROR)
124             error(clazz, meth, msg);
125         else if (level==LOG)
126             log(clazz, meth, msg);
127         else if (level==DEBUG)
128             debug(clazz, meth, msg);
129     }
130
131     static public void
132     log(int level, String JavaDoc clazz, String JavaDoc meth, String JavaDoc msg, Exception JavaDoc ex)
133     {
134         if (level==ERROR)
135             error(clazz, meth, msg, ex);
136         else if (level==LOG)
137             log(clazz, meth, msg, ex);
138         else if (level==DEBUG)
139             debug(clazz, meth, msg);
140     }
141
142     static public void
143     debug(String JavaDoc clazz, String JavaDoc meth, String JavaDoc msg)
144     {
145         if (!debugOn())
146             return ;
147
148         String JavaDoc rmsg = "DEBUG:"+getRuntimeName()+":["+clazz+"/"+meth+"] "+msg;
149         System.err.println(rmsg);
150
151         //Thread.currentThread().dumpStack();
152

153         /*
154         if (_log_file!=null) {
155             try { _log_file.write(rmsg+"\n"); } catch (Exception exc) { }
156         }
157         */

158     }
159
160     static public void
161     debug(String JavaDoc clazz, String JavaDoc meth, String JavaDoc msg, Throwable JavaDoc ex)
162     {
163         if (!debugOn())
164             return ;
165
166         String JavaDoc rmsg = "DEBUG:"+getRuntimeName()+":["+clazz+"/"+meth+"] ";
167         String JavaDoc indent = getIndentation(rmsg.length());
168
169         System.err.println(rmsg+msg);
170         System.err.println(indent+ex.getMessage());
171
172         ex.printStackTrace();
173         /*
174         if (_log_file!=null) {
175             try {
176                 _log_file.write(rmsg+msg+"\n");
177                 _log_file.write(indent+ex.getMessage()+"\n");
178             } catch (Exception exc) {
179             }
180         }
181         */

182     }
183
184     static public void
185     log(String JavaDoc clazz, String JavaDoc meth, String JavaDoc msg)
186     {
187         if (!logOn())
188             return ;
189
190         String JavaDoc rmsg = "LOG:"+getRuntimeName()+":["+clazz+"/"+meth+"] "+msg;
191         System.err.println(rmsg);
192
193         //Thread.currentThread().dumpStack();
194

195         /*
196         if (_log_file!=null) {
197             try { _log_file.write(rmsg+"\n"); } catch (Exception exc) { }
198         }
199         */

200     }
201
202     static public void
203     log(String JavaDoc clazz, String JavaDoc meth, String JavaDoc msg, Throwable JavaDoc ex)
204     {
205         if (!logOn())
206             return ;
207
208         String JavaDoc rmsg = "LOG:"+getRuntimeName()+":["+clazz+"/"+meth+"] ";
209         String JavaDoc indent = getIndentation(rmsg.length());
210
211         System.err.println(rmsg+msg);
212         System.err.println(indent+ex.getMessage());
213
214         ex.printStackTrace();
215         /*
216         if (_log_file!=null) {
217             try {
218                 _log_file.write(rmsg+msg+"\n");
219                 _log_file.write(indent+ex.getMessage()+"\n");
220             } catch (Exception exc) {
221             }
222         }
223         */

224     }
225
226     static public void
227     error(String JavaDoc clazz, String JavaDoc meth, String JavaDoc msg)
228     {
229         String JavaDoc rmsg = "ERROR:"+getRuntimeName()+":["+clazz+"/"+meth+"] "+msg;
230         System.err.println(rmsg);
231
232         // try { _log_file.write(rmsg+"\n"); } catch (Exception exc) { }
233

234         //Thread.currentThread().dumpStack();
235
throw new Error JavaDoc();
236     }
237
238     static public void
239     error(String JavaDoc clazz, String JavaDoc meth, String JavaDoc msg, Throwable JavaDoc ex)
240     {
241         String JavaDoc rmsg = "ERROR:"+getRuntimeName()+":["+clazz+"/"+meth+"] ";
242         String JavaDoc indent = getIndentation(rmsg.length());
243
244         System.err.println(rmsg+msg);
245         System.err.println(indent+ex.getMessage());
246
247         /*
248         if (_log_file!=null) {
249             try {
250                 _log_file.write(rmsg+msg+"\n");
251                 _log_file.write(indent+ex.getMessage()+"\n");
252             } catch (Exception exc) {
253             }
254         }
255         */

256
257         ex.printStackTrace();
258         throw new Error JavaDoc();
259     }
260 }
261
Popular Tags