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