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