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