moving to core/
[debian/openrocket] / core / src / net / sf / openrocket / logging / LogLevel.java
1 package net.sf.openrocket.logging;
2
3 /**
4  * The logging level.  The natural order of the LogLevel orders the levels
5  * from highest priority to lowest priority.  Comparisons of the relative levels
6  * should be performed using the methods {@link #atLeast(LogLevel)},
7  * {@link #moreThan(LogLevel)} and {@link #compareTo(LogLevel)}.
8  * <p>
9  * A description of the level can be obtained using {@link #toString()}.
10  * 
11  * @author Sampo Niskanen <sampo.niskanen@iki.fi>
12  */
13 public enum LogLevel {
14         /**
15          * Level for indicating a bug or error condition noticed in the software or JRE.
16          * No ERROR level events _should_ occur while running the program.
17          */
18         ERROR,
19
20         /** 
21          * Level for indicating error conditions or atypical events that can occur during
22          * normal operation (errors while loading files, weird computation results etc).
23          */
24         WARN,
25
26         /** 
27          * Level for logging user actions (adding and modifying components, running
28          * simulations etc).  A user action should be logged as soon as possible on this
29          * level.  The level is separate so that additional INFO messages won't purge
30          * user actions from a bounded log buffer.
31          */
32         USER,
33
34         /**
35          * Level for indicating general level actions the software is performing and
36          * other notable events during execution (dialogs shown, simulations run etc).
37          */
38         INFO,
39
40         /**
41          * Level for indicating mid-results, outcomes of methods and other debugging 
42          * information.  The data logged should be of value when analyzing error
43          * conditions and what has caused them.  Places that are called repeatedly
44          * during e.g. flight simulation should use the VBOSE level instead.
45          */
46         DEBUG,
47
48         /**
49          * Level of verbose debug logging to be used in areas which are called repeatedly,
50          * such as computational methods used in simulations.  This level is separated to
51          * allow filtering out the verbose logs generated during simulations, DnD etc.
52          * from the normal debug logs.
53          */
54         VBOSE;
55         
56         /** The log level with highest priority */
57         public static final LogLevel HIGHEST;
58         /** The log level with lowest priority */
59         public static final LogLevel LOWEST;
60         /** The maximum length of a level textual description */
61         public static final int LENGTH;
62         
63         static {
64                 int length = 0;
65                 for (LogLevel l : LogLevel.values()) {
66                         length = Math.max(length, l.toString().length());
67                 }
68                 LENGTH = length;
69                 
70                 LogLevel[] values = LogLevel.values();
71                 HIGHEST = values[0];
72                 LOWEST = values[values.length - 1];
73         }
74         
75         /**
76          * Return true if this log level is of a priority at least that of
77          * <code>level</code>.
78          */
79         public boolean atLeast(LogLevel level) {
80                 return this.compareTo(level) <= 0;
81         }
82         
83         /**
84          * Return true if this log level is of a priority greater than that of
85          * <code>level</code>.
86          */
87         public boolean moreThan(LogLevel level) {
88                 return this.compareTo(level) < 0;
89         }
90         
91         
92         /**
93          * Return a log level corresponding to a string.  The string is case-insensitive.  If the
94          * string is case-insensitively equal to "all", then the lowest logging level is returned.
95          * 
96          * @param value                 the string name of a log level, or "all"
97          * @param defaultLevel  the value to return if the string doesn't correspond to any log level or is null
98          * @return                              the corresponding log level, of defaultLevel.
99          */
100         public static LogLevel fromString(String value, LogLevel defaultLevel) {
101                 
102                 // Normalize the string
103                 if (value == null) {
104                         return defaultLevel;
105                 }
106                 value = value.toUpperCase().trim();
107                 
108                 // Find the correct level
109                 LogLevel level = defaultLevel;
110                 if (value.equals("ALL")) {
111                         LogLevel[] values = LogLevel.values();
112                         level = values[values.length - 1];
113                 } else {
114                         try {
115                                 level = LogLevel.valueOf(value);
116                         } catch (Exception e) {
117                                 // Ignore
118                         }
119                 }
120                 return level;
121         }
122         
123 }