JAL-629 bootstrap args and properties. Remember index of args for 'previous structure...
[jalview.git] / src / jalview / bin / Console.java
1 /*
2  * Jalview - A Sequence Alignment Editor and Viewer ($$Version-Rel$$)
3  * Copyright (C) $$Year-Rel$$ The Jalview Authors
4  * 
5  * This file is part of Jalview.
6  * 
7  * Jalview is free software: you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License 
9  * as published by the Free Software Foundation, either version 3
10  * of the License, or (at your option) any later version.
11  *  
12  * Jalview is distributed in the hope that it will be useful, but 
13  * WITHOUT ANY WARRANTY; without even the implied warranty 
14  * of MERCHANTABILITY or FITNESS FOR A PARTICULAR 
15  * PURPOSE.  See the GNU General Public License for more details.
16  * 
17  * You should have received a copy of the GNU General Public License
18  * along with Jalview.  If not, see <http://www.gnu.org/licenses/>.
19  * The Jalview Authors are detailed in the 'AUTHORS' file.
20  */
21 package jalview.bin;
22
23 import java.util.Locale;
24
25 import jalview.log.JLogger;
26 import jalview.log.JLoggerI;
27 import jalview.log.JLoggerI.LogLevel;
28 import jalview.log.JLoggerLog4j;
29 import jalview.util.ChannelProperties;
30 import jalview.util.Log4j;
31 import jalview.util.Platform;
32
33 public class Console
34 {
35
36   public static JLoggerLog4j log;
37
38   public static void debug(String message, Throwable t)
39   {
40     if (Console.initLogger())
41     {
42       log.debug(message, t);
43     }
44     else
45     {
46       System.out.println(message);
47       t.printStackTrace();
48     }
49
50   }
51
52   public static void info(String message)
53   {
54     if (Console.initLogger())
55     {
56       log.info(message, null);
57     }
58     else
59     {
60       System.out.println(message);
61     }
62
63   }
64
65   public static void trace(String message, Throwable t)
66   {
67     if (Console.initLogger())
68     {
69       log.trace(message, t);
70     }
71     else
72     {
73       System.out.println(message);
74       t.printStackTrace();
75     }
76   }
77
78   public static void debug(String message)
79   {
80     if (Console.initLogger())
81     {
82       log.debug(message, null);
83     }
84     else
85     {
86       System.out.println(message);
87     }
88
89   }
90
91   public static void info(String message, Throwable t)
92   {
93     if (Console.initLogger())
94     {
95       log.info(message, t);
96     }
97     else
98     {
99       System.out.println(message);
100       t.printStackTrace();
101     }
102
103   }
104
105   public static void warn(String message)
106   {
107     if (Console.initLogger())
108     {
109       log.warn(message, null);
110     }
111     else
112     {
113       System.out.println(message);
114     }
115
116   }
117
118   public static void trace(String message)
119   {
120     if (Console.initLogger())
121     {
122       log.trace(message, null);
123     }
124     else
125     {
126       System.out.println(message);
127     }
128   }
129
130   public static void warn(String message, Throwable t)
131   {
132     if (Console.initLogger())
133     {
134       log.warn(message, t);
135     }
136     else
137     {
138       System.out.println(message);
139       t.printStackTrace();
140     }
141
142   }
143
144   public static void error(String message)
145   {
146     if (Console.initLogger())
147     {
148       log.error(message, null);
149     }
150     else
151     {
152       System.err.println(message);
153     }
154
155   }
156
157   public static void error(String message, Throwable t)
158   {
159     if (Console.initLogger())
160     {
161       log.error(message, t);
162     }
163     else
164     {
165       System.err.println(message);
166       t.printStackTrace(System.err);
167     }
168
169   }
170
171   public static void fatal(String message)
172   {
173     if (Console.initLogger())
174     {
175       log.fatal(message, null);
176     }
177     else
178     {
179       System.err.println(message);
180     }
181
182   }
183
184   public static void fatal(String message, Throwable t)
185   {
186     if (Console.initLogger())
187     {
188       log.fatal(message, t);
189     }
190     else
191     {
192       System.err.println(message);
193       t.printStackTrace(System.err);
194     }
195
196   }
197
198   public static boolean isDebugEnabled()
199   {
200     return log == null ? false : log.isDebugEnabled();
201   }
202
203   public static boolean isTraceEnabled()
204   {
205     return log == null ? false : log.isTraceEnabled();
206   }
207
208   public static JLogger.LogLevel getCachedLogLevel()
209   {
210     return Console.getCachedLogLevel(Cache.JALVIEWLOGLEVEL);
211   }
212
213   public static JLogger.LogLevel getCachedLogLevel(String key)
214   {
215     return getLogLevel(Cache.getDefault(key, "INFO"));
216   }
217
218   public static JLogger.LogLevel getLogLevel(String level)
219   {
220     return JLogger.toLevel(level);
221   }
222
223   public static boolean initLogger()
224   {
225     return initLogger(null);
226   }
227
228   public static boolean initLogger(String providedLogLevel)
229   {
230     if (log != null)
231     {
232       return true;
233     }
234     try
235     {
236       JLogger.LogLevel logLevel = JLogger.LogLevel.INFO;
237
238       if (JLogger.isLevel(providedLogLevel))
239         logLevel = Console.getLogLevel(providedLogLevel);
240       else
241         logLevel = getCachedLogLevel();
242
243       if (!Platform.isJS())
244       {
245         System.err
246                 .println("Setting initial log level to " + logLevel.name());
247         Log4j.init(logLevel);
248       }
249       // log output
250       // is laxis used? Does getLogger do anything without a Logger object?
251       // Logger laxis = Log4j.getLogger("org.apache.axis", myLevel);
252       JLoggerLog4j.getLogger("org.apache.axis", logLevel);
253
254       // The main application logger
255       log = JLoggerLog4j.getLogger(Cache.JALVIEW_LOGGER_NAME, logLevel);
256     } catch (NoClassDefFoundError e)
257     {
258       System.err.println("Could not initialise the logger framework");
259       e.printStackTrace();
260     }
261
262     // Test message
263     if (log != null)
264     {
265       // Logging test message should go through the logger object
266       if (log.loggerExists())
267         log.debug(Console.LOGGING_TEST_MESSAGE);
268       // Tell the user that debug is enabled
269       debug(ChannelProperties.getProperty("app_name")
270               + " Debugging Output Follows.");
271       return true;
272     }
273     else
274     {
275       return false;
276     }
277   }
278
279   public static void setLogLevel(String logLevelString)
280   {
281     for (LogLevel logLevel : JLoggerI.LogLevel.values())
282     {
283       if (logLevel.toString().toLowerCase(Locale.ROOT)
284               .equals(logLevelString.toLowerCase(Locale.ROOT)))
285       {
286         log.setLevel(logLevel);
287         if (!Platform.isJS())
288         {
289           Log4j.init(logLevel);
290         }
291         JLoggerLog4j.getLogger("org.apache.axis", logLevel);
292         break;
293       }
294     }
295   }
296
297   public final static String LOGGING_TEST_MESSAGE = "Logging to STDERR";
298
299 }