20926cda48128d0df8f37a0952549e1b6fe485ed
[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.io.PrintStream;
24 import java.util.Locale;
25
26 import jalview.log.JLogger;
27 import jalview.log.JLoggerI;
28 import jalview.log.JLoggerI.LogLevel;
29 import jalview.log.JLoggerLog4j;
30 import jalview.util.ChannelProperties;
31 import jalview.util.Log4j;
32 import jalview.util.Platform;
33
34 public class Console
35 {
36
37   public static JLoggerLog4j log;
38
39   public static void debug(String message, Throwable t)
40   {
41     if (Console.initLogger())
42     {
43       log.debug(message, t);
44     }
45     else
46     {
47       outPrintln(message);
48       Console.printStackTrace(t);
49     }
50
51   }
52
53   public static void info(String message)
54   {
55     if (Console.initLogger())
56     {
57       log.info(message, null);
58     }
59     else
60     {
61       outPrintln(message);
62     }
63
64   }
65
66   public static void trace(String message, Throwable t)
67   {
68     if (Console.initLogger())
69     {
70       log.trace(message, t);
71     }
72     else
73     {
74       outPrintln(message);
75       Console.printStackTrace(t);
76     }
77   }
78
79   public static void debug(String message)
80   {
81     if (Console.initLogger())
82     {
83       log.debug(message, null);
84     }
85     else
86     {
87       outPrintln(message);
88     }
89
90   }
91
92   public static void info(String message, Throwable t)
93   {
94     if (Console.initLogger())
95     {
96       log.info(message, t);
97     }
98     else
99     {
100       outPrintln(message);
101       Console.printStackTrace(t);
102     }
103
104   }
105
106   public static void warn(String message)
107   {
108     if (Console.initLogger())
109     {
110       log.warn(message, null);
111     }
112     else
113     {
114       outPrintln(message);
115     }
116
117   }
118
119   public static void trace(String message)
120   {
121     if (Console.initLogger())
122     {
123       log.trace(message, null);
124     }
125     else
126     {
127       outPrintln(message);
128     }
129   }
130
131   public static void warn(String message, Throwable t)
132   {
133     if (Console.initLogger())
134     {
135       log.warn(message, t);
136     }
137     else
138     {
139       outPrintln(message);
140       Console.printStackTrace(t);
141     }
142
143   }
144
145   public static void error(String message)
146   {
147     if (Console.initLogger())
148     {
149       log.error(message, null);
150     }
151     else
152     {
153       jalview.bin.Console.errPrintln(message);
154     }
155
156   }
157
158   public static void error(String message, Throwable t)
159   {
160     if (Console.initLogger())
161     {
162       log.error(message, t);
163     }
164     else
165     {
166       jalview.bin.Console.errPrintln(message);
167       Console.printStackTrace(t);
168     }
169
170   }
171
172   public static void fatal(String message)
173   {
174     if (Console.initLogger())
175     {
176       log.fatal(message, null);
177     }
178     else
179     {
180       jalview.bin.Console.errPrintln(message);
181     }
182
183   }
184
185   public static void fatal(String message, Throwable t)
186   {
187     if (Console.initLogger())
188     {
189       log.fatal(message, t);
190     }
191     else
192     {
193       jalview.bin.Console.errPrintln(message);
194       Console.printStackTrace(t);
195     }
196
197   }
198
199   public static boolean isDebugEnabled()
200   {
201     return log == null ? false : log.isDebugEnabled();
202   }
203
204   public static boolean isTraceEnabled()
205   {
206     return log == null ? false : log.isTraceEnabled();
207   }
208
209   public static JLogger.LogLevel getCachedLogLevel()
210   {
211     return Console.getCachedLogLevel(Cache.JALVIEWLOGLEVEL);
212   }
213
214   public static JLogger.LogLevel getCachedLogLevel(String key)
215   {
216     return getLogLevel(Cache.getDefault(key, "INFO"));
217   }
218
219   public static JLogger.LogLevel getLogLevel(String level)
220   {
221     return JLogger.toLevel(level);
222   }
223
224   public static JLogger getLogger()
225   {
226     return log;
227   }
228
229   public static boolean initLogger()
230   {
231     return initLogger(null);
232   }
233
234   public static boolean initLogger(String providedLogLevel)
235   {
236     if (log != null)
237     {
238       return true;
239     }
240     try
241     {
242       JLogger.LogLevel logLevel = JLogger.LogLevel.INFO;
243
244       if (JLogger.isLevel(providedLogLevel))
245       {
246         logLevel = Console.getLogLevel(providedLogLevel);
247       }
248       else
249       {
250         logLevel = getCachedLogLevel();
251       }
252
253       if (!Platform.isJS())
254       {
255         if (!Jalview.quiet())
256         {
257           jalview.bin.Console.errPrintln(
258                   "Setting initial log level to " + logLevel.name());
259         }
260         Log4j.init(logLevel);
261       }
262       // log output
263       // is laxis used? Does getLogger do anything without a Logger object?
264       // Logger laxis = Log4j.getLogger("org.apache.axis", myLevel);
265       JLoggerLog4j.getLogger("org.apache.axis", logLevel);
266
267       // The main application logger
268       log = JLoggerLog4j.getLogger(Cache.JALVIEW_LOGGER_NAME, logLevel);
269     } catch (NoClassDefFoundError e)
270     {
271       jalview.bin.Console
272               .errPrintln("Could not initialise the logger framework");
273       Console.printStackTrace(e);
274     }
275
276     // Test message
277     if (log != null)
278     {
279       // Logging test message should go through the logger object
280       if (log.loggerExists())
281         log.debug(Console.LOGGING_TEST_MESSAGE);
282       // Tell the user that debug is enabled
283       debug(ChannelProperties.getProperty("app_name")
284               + " Debugging Output Follows.");
285       return true;
286     }
287     else
288     {
289       return false;
290     }
291   }
292
293   public static void setLogLevel(String logLevelString)
294   {
295     for (LogLevel logLevel : JLoggerI.LogLevel.values())
296     {
297       if (logLevel.toString().toLowerCase(Locale.ROOT)
298               .equals(logLevelString.toLowerCase(Locale.ROOT)))
299       {
300         log.setLevel(logLevel);
301         if (!Platform.isJS())
302         {
303           Log4j.init(logLevel);
304         }
305         JLoggerLog4j.getLogger("org.apache.axis", logLevel);
306         break;
307       }
308     }
309   }
310
311   public static void outPrint()
312   {
313     outPrint("");
314   }
315
316   public static void outPrintln()
317   {
318     outPrintln("");
319   }
320
321   public static void outPrint(Object message)
322   {
323     outPrintMessage(message, false, false);
324   }
325
326   public static void outPrint(Object message, boolean forceStdout)
327   {
328     outPrintMessage(message, false, forceStdout);
329   }
330
331   public static void outPrintln(Object message)
332   {
333     outPrintMessage(message, true, false);
334   }
335
336   public static PrintStream outputStream(boolean forceStdout)
337   {
338     // send message to stderr if an output file to stdout is expected
339     if (!forceStdout && Jalview.getInstance() != null
340             && Jalview.getInstance().bootstrapArgs != null
341             && Jalview.getInstance().bootstrapArgs.outputToStdout())
342     {
343       return System.err;
344     }
345     else
346     {
347       return System.out;
348     }
349   }
350
351   public static void outPrintMessage(Object message, boolean newline,
352           boolean forceStdout)
353   {
354     PrintStream ps = outputStream(forceStdout);
355     if (newline)
356     {
357       ps.println(message);
358     }
359     else
360     {
361       ps.print(message);
362     }
363   }
364
365   public static void errPrint()
366   {
367     errPrint("");
368   }
369
370   public static void errPrintln()
371   {
372     errPrintln("");
373   }
374
375   public static void errPrint(Object message)
376   {
377     System.err.print(message);
378   }
379
380   public static void errPrintln(Object message)
381   {
382     System.err.println(message);
383   }
384
385   public static void debugPrintStackTrace(Throwable t)
386   {
387     if (!isDebugEnabled())
388     {
389       return;
390     }
391     // send message to stderr if output to stdout is expected
392     printStackTrace(t);
393   }
394
395   public static void printStackTrace(Throwable t)
396   {
397     // send message to stderr if output to stdout is expected
398     t.printStackTrace(System.err);
399   }
400
401   public final static String LOGGING_TEST_MESSAGE = "Logging to STDERR";
402
403 }