Clover icon

Coverage Report

  1. Project Clover database Thu Dec 4 2025 14:43:25 GMT
  2. Package jalview.bin

File Console.java

 

Coverage histogram

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

Code metrics

50
122
40
1
477
378
73
0.6
3.05
40
1.83

Classes

Class Line # Actions
Console 36 122 73
0.4669811446.7%
 

Contributing tests

This file is covered by 699 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=null;
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  138 toggle public static boolean setLogFile(String filename, boolean append)
68    {
69  138 boolean usingLogfile = false;
70  138 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  138 return usingLogfile;
103    }
104   
 
105  22 toggle public static void debug(String message, Throwable t)
106    {
107  22 if (Console.initLogger())
108    {
109  22 log.debug(message, t);
110    }
111    else
112    {
113  0 outPrintln(message);
114  0 Console.printStackTrace(t);
115    }
116    }
117   
 
118  34846 toggle public static void info(String message)
119    {
120  34846 if (Console.initLogger())
121    {
122  34846 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  48784 toggle public static void debug(String message)
145    {
146  48780 if (Console.initLogger())
147    {
148  48780 log.debug(message, null);
149    }
150    else
151    {
152  0 outPrintln(message);
153    }
154   
155    }
156   
 
157  0 toggle public static void info(String message, Throwable t)
158    {
159  0 if (Console.initLogger())
160    {
161  0 log.info(message, t);
162    }
163    else
164    {
165  0 outPrintln(message);
166  0 Console.printStackTrace(t);
167    }
168   
169    }
170   
 
171  100 toggle public static void warn(String message)
172    {
173  100 if (Console.initLogger())
174    {
175  100 log.warn(message, null);
176    }
177    else
178    {
179  0 outPrintln(message);
180    }
181   
182    }
183   
 
184  2632 toggle public static void trace(String message)
185    {
186  2632 if (Console.initLogger())
187    {
188  2632 log.trace(message, null);
189    }
190    else
191    {
192  0 outPrintln(message);
193    }
194    }
195   
 
196  0 toggle public static void warn(String message, Throwable t)
197    {
198  0 if (Console.initLogger())
199    {
200  0 log.warn(message, t);
201    }
202    else
203    {
204  0 outPrintln(message);
205  0 Console.printStackTrace(t);
206    }
207   
208    }
209   
 
210  36 toggle public static void error(String message)
211    {
212  36 if (Console.initLogger())
213    {
214  36 log.error(message, null);
215    }
216    else
217    {
218  0 jalview.bin.Console.errPrintln(message);
219    }
220   
221    }
222   
 
223  3 toggle public static void error(String message, Throwable t)
224    {
225  3 if (Console.initLogger())
226    {
227  3 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  197 toggle public static boolean isDebugEnabled()
265    {
266  197 return log == null ? false : log.isDebugEnabled();
267    }
268   
 
269  88 toggle public static boolean isTraceEnabled()
270    {
271  88 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  68 toggle public static JLogger getLogger()
290    {
291  68 return log;
292    }
293   
 
294  86915 toggle public static boolean initLogger()
295    {
296  86915 String level = null;
297  86915 try {
298  86915 level = System.getProperty("jalview.loglevel");
299  86912 if (level!=null) {
300  0 level = JLogger.LogLevel.valueOf(level).toString();
301    }
302    } catch (Exception x)
303    {
304  0 Console.errPrintln("Ignoring value of jalview.loglevel '"+level+"'");
305    }
306  86914 return initLogger(level);
307    }
308   
 
309  87051 toggle public static boolean initLogger(String providedLogLevel)
310    {
311  87053 if (log != null)
312    {
313  86998 return true;
314    }
315  55 try
316    {
317  55 JLogger.LogLevel logLevel = JLogger.LogLevel.INFO;
318   
319  55 if (providedLogLevel != null && JLogger.isLevel(providedLogLevel))
320    {
321  6 logLevel = Console.getLogLevel(providedLogLevel);
322    }
323    else
324    {
325  49 logLevel = getCachedLogLevel();
326    }
327   
328  55 JLoggerLog4j.setLogfile(logfile);
329   
330    // log output
331    // is laxis used? Does getLogger do anything without a Logger object?
332    // Logger laxis = Log4j.getLogger("org.apache.axis", myLevel);
333  55 JLoggerLog4j.getLogger("org.apache.axis", logLevel);
334   
335    // The main application logger
336  55 log = JLoggerLog4j.getLogger(Cache.JALVIEW_LOGGER_NAME, logLevel);
337  55 log.info("Logging initialised at level: "+logLevel);
338    } catch (NoClassDefFoundError e)
339    {
340  0 jalview.bin.Console
341    .errPrintln("Could not initialise the logger framework");
342  0 Console.printStackTrace(e);
343    }
344   
345    // Test message
346  55 if (log != null)
347    {
348    // Logging test message should go through the logger object
349  55 if (log.loggerExists())
350  55 log.debug(Console.LOGGING_TEST_MESSAGE);
351    // Tell the user that debug is enabled
352  55 debug(ChannelProperties.getProperty("app_name")
353    + " Debugging Output Follows.");
354  55 return true;
355    }
356    else
357    {
358  0 return false;
359    }
360    }
361   
 
362  0 toggle public static void setLogLevel(String logLevelString)
363    {
364  0 LogLevel l = null;
365  0 try
366    {
367  0 l = LogLevel.valueOf(logLevelString);
368    } catch (IllegalArgumentException | NullPointerException e1)
369    {
370  0 Console.debug("Invalid log level '" + logLevelString + "'");
371  0 return;
372    }
373  0 setLogLevel(l);
374    }
375   
 
376  0 toggle public static void setLogLevel(LogLevel l)
377    {
378  0 if (l != null)
379    {
380  0 log.setLevel(l);
381  0 JLoggerLog4j.getLogger("org.apache.axis", l);
382    }
383    }
384   
 
385  0 toggle public static void outPrint()
386    {
387  0 outPrint("");
388    }
389   
 
390  0 toggle public static void outPrintln()
391    {
392  0 outPrintln("");
393    }
394   
 
395  138 toggle public static void outPrint(Object message)
396    {
397  138 outPrintMessage(message, false, false);
398    }
399   
 
400  0 toggle public static void outPrint(Object message, boolean forceStdout)
401    {
402  0 outPrintMessage(message, false, forceStdout);
403    }
404   
 
405  575 toggle public static void outPrintln(Object message)
406    {
407  575 outPrintMessage(message, true, false);
408    }
409   
 
410  713 toggle public static PrintStream outputStream(boolean forceStdout)
411    {
412    // send message to stderr if an output file to stdout is expected
413  713 if (!forceStdout && Jalview.getInstance() != null
414    && Jalview.getInstance().getBootstrapArgs() != null
415    && Jalview.getInstance().getBootstrapArgs().outputToStdout())
416    {
417  16 return err;
418    }
419    else
420    {
421  697 return out;
422    }
423    }
424   
 
425  713 toggle public static void outPrintMessage(Object message, boolean newline,
426    boolean forceStdout)
427    {
428  713 PrintStream ps = outputStream(forceStdout);
429  713 if (newline)
430    {
431  575 ps.println(message);
432    }
433    else
434    {
435  138 ps.print(message);
436    }
437    }
438   
 
439  0 toggle public static void errPrint()
440    {
441  0 errPrint("");
442    }
443   
 
444  0 toggle public static void errPrintln()
445    {
446  0 errPrintln("");
447    }
448   
 
449  0 toggle public static void errPrint(Object message)
450    {
451  0 err.print(message);
452    }
453   
 
454  922 toggle public static void errPrintln(Object message)
455    {
456  922 err.println(message);
457    }
458   
 
459  0 toggle public static void debugPrintStackTrace(Throwable t)
460    {
461  0 if (!isDebugEnabled())
462    {
463  0 return;
464    }
465    // send message to stderr if output to stdout is expected
466  0 printStackTrace(t);
467    }
468   
 
469  2 toggle public static void printStackTrace(Throwable t)
470    {
471    // send message to stderr if output to stdout is expected
472  2 t.printStackTrace(err);
473    }
474   
475    public final static String LOGGING_TEST_MESSAGE = "Logging to STDERR";
476   
477    }