*** empty log message ***
[IRC.git] / Robust / Transactions / jcarderdstm2version / src / com / enea / jcarder / util / logging / Logger.java
1 /*
2  * JCarder -- cards Java programs to keep threads disentangled
3  *
4  * Copyright (C) 2006-2007 Enea AB
5  * Copyright (C) 2007 Ulrik Svensson
6  * Copyright (C) 2007 Joel Rosdahl
7  *
8  * This program is made available under the GNU GPL version 2, with a special
9  * exception for linking with JUnit. See the accompanying file LICENSE.txt for
10  * details.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.
15  */
16
17 package com.enea.jcarder.util.logging;
18
19 import java.util.ArrayList;
20 import java.util.Collection;
21
22 /**
23  * A simple logging framework.
24  *
25  * We use our own simple Logging framework for several reasons:
26  *  - To avoid interfering with logging from the user application. Even if we
27  * were using Log4J instead of java.util.Logging.*, JCarder might interfere with
28  * for example Log4J system properties.
29  *  - The default java.util.logging.LogManager is reset by a shutdown hook and
30  * there is no fixed order in which the shutdown hooks are executed.
31  *  - Minimizing the usage of the Java standard library improves performance and
32  * minimizes the risk of deadlock if the standard library is instrumented by
33  * JCarder.
34  */
35 public final class Logger {
36     public static enum Level {
37         SEVERE,
38         WARNING,
39         INFO,
40         CONFIG,
41         FINE,
42         FINER,
43         FINEST;
44
45         /**
46          * Parse a string and return the corresponding log level.
47          *
48          * @param string The string to parse.
49          * @return
50          */
51         public static Level fromString(String string) {
52             for (Level level : values()) {
53                 if (string.equalsIgnoreCase(level.toString())) {
54                     return level;
55                 }
56             }
57             return null;
58         }
59
60         public static String getEnumeration() {
61             StringBuffer sb = new StringBuffer();
62             boolean first = true;
63             for (Level level : values()) {
64                 if (first) {
65                     first = false;
66                 } else {
67                     sb.append(", ");
68                 }
69                 sb.append(level.toString());
70             }
71             return sb.toString();
72         }
73     }
74
75     final Collection<Handler> mHandlers;
76     final Level mLevel;
77
78     /**
79      * Constructor setting default value of log level.
80      *
81      * The default is to log everything, i.e., log level FINEST.
82      *
83      * @param handlers Log handlers. null means no handlers.
84      */
85     public Logger(Collection<Handler> handlers) {
86         this(handlers, Level.FINEST);
87     }
88
89     /**
90      * Constructor.
91      *
92      * @param handlers Log handlers. null means no handlers.
93      * @param logLevel Log level.
94      */
95     public Logger(Collection<Handler> handlers, Level logLevel) {
96         mLevel = logLevel;
97         mHandlers = new ArrayList<Handler>();
98         if (handlers != null) {
99             // Create a copy of the provided collection instead of sharing
100             // it, in order to make sure that mHandlers is immutable and
101             // thread safe.
102             mHandlers.addAll(handlers);
103         }
104     }
105
106     /**
107      * Check whether a message with a certain level would be logged.
108      *
109      * @param level The level.
110      * @return True if the message would be logged, otherwise false.
111      */
112     public boolean isLoggable(Level level) {
113         return level.compareTo(mLevel) <= 0;
114     }
115
116     /**
117      * Log a message with level SEVERE.
118      *
119      * @param message The message.
120      */
121     public void severe(String message) {
122         publishLog(Level.SEVERE, message);
123     }
124
125
126     /**
127      * Log a message with level WARNING.
128      *
129      * @param message The message.
130      */
131     public void warning(String message) {
132         publishLog(Level.WARNING, message);
133     }
134
135
136     /**
137      * Log a message with level INFO.
138      *
139      * @param message The message.
140      */
141     public void info(String message) {
142         publishLog(Level.INFO, message);
143     }
144
145
146     /**
147      * Log a message with level CONFIG.
148      *
149      * @param message The message.
150      */
151     public void config(String message) {
152         publishLog(Level.CONFIG, message);
153     }
154
155
156     /**
157      * Log a message with level FINE.
158      *
159      * @param message The message.
160      */
161     public void fine(String message) {
162         publishLog(Level.FINE, message);
163     }
164
165
166     /**
167      * Log a message with level FINER.
168      *
169      * @param message The message.
170      */
171     public void finer(String message) {
172         publishLog(Level.FINER, message);
173     }
174
175     /**
176      * Log a message with level FINEST.
177      *
178      * @param message The message.
179      */
180     public void finest(String message) {
181         publishLog(Level.FINEST, message);
182     }
183
184     private void publishLog(Level level, String message) {
185         if (isLoggable(level)) {
186             for (Handler handler : mHandlers) {
187                 handler.publish(level, message);
188             }
189         }
190     }
191 }