KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > exceptions > ExceptionLogger


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.exceptions;
21 import java.text.MessageFormat JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.HashSet JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.logging.Level JavaDoc;
26 import java.util.logging.LogRecord JavaDoc;
27 import org.openide.util.NbBundle;
28
29 /**
30  *
31  * @author jindra
32  */

33 public class ExceptionLogger {
34     
35     /** Creates a new instance of ExceptionLogger */
36     // private static PrintStream logStream = null;
37
// private static PrintStream errorStream = null;
38
private static final String JavaDoc NAME = NbBundle.getMessage(ExceptionLogger.class, "ExceptionLoggerName");
39     private static Collector coll = null;
40     private static final MessageFormat JavaDoc EXC_HEADER = new MessageFormat JavaDoc(NbBundle.getMessage(ExceptionLogger.class,
41             "ExceptionMessage"));
42     private static Set JavaDoc<Throwable JavaDoc> causes = new HashSet JavaDoc<Throwable JavaDoc>();
43     
44     public ExceptionLogger() {
45         
46     }
47     
48     public static void logError(LogRecord JavaDoc rec){
49         logError(rec.getLevel(), rec.getThrown());
50     }
51     
52     /**
53      * logs messages
54      * @param s message to log
55      */

56     public static void logError(Level JavaDoc severity, Throwable JavaDoc throwable){
57         // getErrorStream().println(EXC_HEADER.format(new Object[] { title, new Date() }));//error stream
58
// t.printStackTrace(getErrorStream());
59
LogRecord JavaDoc log = new LogRecord JavaDoc(convertSeverity(severity), EXC_HEADER.format(new Object JavaDoc[] { severity.toString(), new Date JavaDoc() }));
60         log.setThrown(convert(throwable));
61         log.setLoggerName(NAME);
62         getColl().write(log);//collector
63
}
64     
65     /**
66      * t could be of a different class, but I won't be able to recreate this class after sending it
67      * this function convert the exact class to a Throwable class type
68      */

69     private static Level JavaDoc convertSeverity(Level JavaDoc severity){
70         if ((severity.equals(Level.SEVERE))||(severity.equals(Level.WARNING))||(severity.equals(Level.INFO))||
71                 (severity.equals(Level.FINE))||(severity.equals(Level.FINER))||(severity.equals(Level.FINEST))||
72                 (severity.equals(Level.CONFIG))) return severity;
73         else return Level.WARNING;
74     }
75     
76     
77     private static Throwable JavaDoc convertRecursivly(Throwable JavaDoc throwable){
78         if (throwable==null){
79             return throwable;
80         }
81         causes.add(throwable);
82         Throwable JavaDoc tNew = new Throwable JavaDoc(throwable.toString());
83         tNew.setStackTrace(throwable.getStackTrace());
84         Throwable JavaDoc cause = throwable.getCause();
85         if ((cause != null)&&(!causes.contains(cause))){
86             tNew.initCause(convertRecursivly(cause));
87         }
88         return tNew;
89     }
90     
91     /* synchronized, because I need to work with causes list and other call could
92      * clear it during recursive work
93      */

94     public static synchronized Throwable JavaDoc convert(Throwable JavaDoc throwable){
95         causes.clear();
96         return convertRecursivly(throwable);
97     }
98     
99     public static void log(LogRecord JavaDoc rec){
100         rec.setLoggerName(NAME);
101         getColl().write(rec);
102     }
103     
104     public static void log(Level JavaDoc severity, String JavaDoc str){
105         // getLogStream().println(s); // print to std log stream
106
LogRecord JavaDoc log = new LogRecord JavaDoc(severity, str);
107         log.setLoggerName(NAME);
108         getColl().write(log); //collector
109
}
110     
111     // private static PrintStream getLogStream(){
112
// if (logStream == null)logStream = System.err;// PrintStream pw = TopLogging.getLogOutputStream(); should use TopLogging
113
// return logStream;
114
// }
115

116     // private static PrintStream getErrorStream(){
117
// if (errorStream == null){
118
// errorStream = System.err;//
119
// }
120
// return errorStream;
121
// }
122

123     private static synchronized Collector getColl(){
124         if (coll == null){
125             coll = Collector.getDefault();
126         }
127         return coll;
128     }
129     
130 }
131
Popular Tags