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