JAL-3032 adds Java 8 functionality (1/2)
[jalview.git] / srcjar2 / org / apache / log4j / Level.java
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  * 
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  * 
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17
18 // Contributors:  Kitching Simon <Simon.Kitching@orange.ch>
19 //                Nicholas Wolff
20
21 package org.apache.log4j;
22
23 import java.io.IOException;
24 import java.io.ObjectInputStream;
25 import java.io.ObjectOutputStream;
26 import java.io.ObjectStreamException;
27 import java.io.Serializable;
28
29 /**
30  * Defines the minimum set of levels recognized by the system, that is <code>OFF</code>, <code>FATAL</code>, <code>ERROR</code>,
31  * <code>WARN</code>, <code>INFO</code>, <code>DEBUG</code> and <code>ALL</code>.
32  * 
33  * <p>
34  * The <code>Level</code> class may be subclassed to define a larger level set.
35  * </p>
36  * 
37  * @author Ceki G&uuml;lc&uuml;
38  */
39 public class Level extends Priority implements Serializable {
40
41     private static final String ALL_NAME = "ALL";
42
43     private static final String TRACE_NAME = "TRACE";
44
45     private static final String DEBUG_NAME = "DEBUG";
46
47     private static final String INFO_NAME = "INFO";
48
49     private static final String WARN_NAME = "WARN";
50
51     private static final String ERROR_NAME = "ERROR";
52
53     private static final String FATAL_NAME = "FATAL";
54
55     private static final String OFF_NAME = "OFF";
56
57     /**
58      * TRACE level integer value.
59      * 
60      * @since 1.2.12
61      */
62     public static final int TRACE_INT = 5000;
63
64     /**
65      * The <code>OFF</code> has the highest possible rank and is intended to turn off logging.
66      */
67     final static public Level OFF = new Level(OFF_INT, OFF_NAME, 0);
68
69     /**
70      * The <code>FATAL</code> level designates very severe error events that will presumably lead the application to abort.
71      */
72     final static public Level FATAL = new Level(FATAL_INT, FATAL_NAME, 0);
73
74     /**
75      * The <code>ERROR</code> level designates error events that might still allow the application to continue running.
76      */
77     final static public Level ERROR = new Level(ERROR_INT, ERROR_NAME, 3);
78
79     /**
80      * The <code>WARN</code> level designates potentially harmful situations.
81      */
82     final static public Level WARN = new Level(WARN_INT, WARN_NAME, 4);
83
84     /**
85      * The <code>INFO</code> level designates informational messages that highlight the progress of the application at coarse-grained level.
86      */
87     final static public Level INFO = new Level(INFO_INT, INFO_NAME, 6);
88
89     /**
90      * The <code>DEBUG</code> Level designates fine-grained informational events that are most useful to debug an application.
91      */
92     final static public Level DEBUG = new Level(DEBUG_INT, DEBUG_NAME, 7);
93
94     /**
95      * The <code>TRACE</code> Level designates finer-grained informational events than the <code>DEBUG</code level.
96      * 
97      * @since 1.2.12
98      */
99     public static final Level TRACE = new Level(TRACE_INT, TRACE_NAME, 7);
100
101     /**
102      * The <code>ALL</code> has the lowest possible rank and is intended to turn on all logging.
103      */
104     final static public Level ALL = new Level(ALL_INT, ALL_NAME, 7);
105
106     /**
107      * Serialization version id.
108      */
109     static final long serialVersionUID = 3491141966387921974L;
110
111     /**
112      * Instantiate a Level object.
113      */
114     protected Level(int level, String levelStr, int syslogEquivalent) {
115         super(level, levelStr, syslogEquivalent);
116     }
117
118     /**
119      * Convert the string passed as argument to a level. If the conversion fails, then this method returns {@link #DEBUG}.
120      */
121     public static Level toLevel(String sArg) {
122         return toLevel(sArg, Level.DEBUG);
123     }
124
125     /**
126      * Convert an integer passed as argument to a level. If the conversion fails, then this method returns {@link #DEBUG}.
127      */
128     public static Level toLevel(int val) {
129         return toLevel(val, Level.DEBUG);
130     }
131
132     /**
133      * Convert an integer passed as argument to a level. If the conversion fails, then this method returns the specified default.
134      */
135     public static Level toLevel(int val, Level defaultLevel) {
136         switch (val) {
137         case ALL_INT:
138             return ALL;
139         case DEBUG_INT:
140             return Level.DEBUG;
141         case INFO_INT:
142             return Level.INFO;
143         case WARN_INT:
144             return Level.WARN;
145         case ERROR_INT:
146             return Level.ERROR;
147         case FATAL_INT:
148             return Level.FATAL;
149         case OFF_INT:
150             return OFF;
151         case TRACE_INT:
152             return Level.TRACE;
153         default:
154             return defaultLevel;
155         }
156     }
157
158     /**
159      * Convert the string passed as argument to a level. If the conversion fails, then this method returns the value of
160      * <code>defaultLevel</code>.
161      */
162     public static Level toLevel(String sArg, Level defaultLevel) {
163         if (sArg == null) {
164             return defaultLevel;
165         }
166         String s = sArg.toUpperCase();
167
168         if (s.equals(ALL_NAME)) {
169             return Level.ALL;
170         }
171         if (s.equals(DEBUG_NAME)) {
172             return Level.DEBUG;
173         }
174         if (s.equals(INFO_NAME)) {
175             return Level.INFO;
176         }
177         if (s.equals(WARN_NAME)) {
178             return Level.WARN;
179         }
180         if (s.equals(ERROR_NAME)) {
181             return Level.ERROR;
182         }
183         if (s.equals(FATAL_NAME)) {
184             return Level.FATAL;
185         }
186         if (s.equals(OFF_NAME)) {
187             return Level.OFF;
188         }
189         if (s.equals(TRACE_NAME)) {
190             return Level.TRACE;
191         }
192         //
193         // For Turkish i problem, see bug 40937
194         //
195         if (s.equals("\u0130NFO")) {
196             return Level.INFO;
197         }
198         return defaultLevel;
199     }
200
201     /**
202      * Custom deserialization of Level.
203      * 
204      * @param s
205      *            serialization stream.
206      * @throws IOException
207      *             if IO exception.
208      * @throws ClassNotFoundException
209      *             if class not found.
210      */
211     private void readObject(final ObjectInputStream s) throws IOException, ClassNotFoundException {
212         s.defaultReadObject();
213         level = s.readInt();
214         syslogEquivalent = s.readInt();
215         levelStr = s.readUTF();
216         if (levelStr == null) {
217             levelStr = "";
218         }
219     }
220
221     /**
222      * Serialize level.
223      * 
224      * @param s
225      *            serialization stream.
226      * @throws IOException
227      *             if exception during serialization.
228      */
229     private void writeObject(final ObjectOutputStream s) throws IOException {
230         s.defaultWriteObject();
231         s.writeInt(level);
232         s.writeInt(syslogEquivalent);
233         s.writeUTF(levelStr);
234     }
235
236     /**
237      * Resolved deserialized level to one of the stock instances. May be overriden in classes derived from Level.
238      * 
239      * @return resolved object.
240      * @throws ObjectStreamException
241      *             if exception during resolution.
242      */
243     private Object readResolve() throws ObjectStreamException {
244         //
245         // if the deserizalized object is exactly an instance of Level
246         //
247         if (getClass() == Level.class) {
248             return toLevel(level);
249         }
250         //
251         // extension of Level can't substitute stock item
252         //
253         return this;
254     }
255
256 }