Clover icon

Coverage Report

  1. Project Clover database Mon Sep 2 2024 17:57:51 BST
  2. Package jalview.bin

File Console.java

 

Coverage histogram

../../img/srcFileCovDistChart5.png
43% of files have more coverage

Code metrics

48
115
40
1
466
367
71
0.62
2.88
40
1.77

Classes

Class Line # Actions
Console 36 115 71
0.482758648.3%
 

Contributing tests

This file is covered by 489 tests. .

Source view

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