Class |
Line # |
Actions |
|||
---|---|---|---|---|---|
ErrorLog | 26 | 37 | 28 |
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.util; | |
22 | ||
23 | import java.lang.reflect.InvocationTargetException; | |
24 | import java.lang.reflect.Method; | |
25 | ||
26 | public class ErrorLog | |
27 | { | |
28 | private static boolean hasConsole = true; | |
29 | ||
30 | private static Class console = null; | |
31 | ||
32 | private static Method initLogger = null; | |
33 | ||
34 | private static Method errPrintln = null; | |
35 | ||
36 | private static Method outPrintln = null; | |
37 | ||
38 | private static String prefix = null; | |
39 | ||
40 | private static boolean quiet = false; | |
41 | ||
42 | 0 | public static void setHasConsole(boolean b) |
43 | { | |
44 | 0 | hasConsole = b; |
45 | } | |
46 | ||
47 | 0 | public static void setQuiet(boolean b) |
48 | { | |
49 | 0 | quiet = b; |
50 | } | |
51 | ||
52 | 0 | public static void setPrefix(String s) |
53 | { | |
54 | 0 | prefix = s; |
55 | } | |
56 | ||
57 | 14 | public static void outPrintln(String message) |
58 | { | |
59 | 14 | println(message, false); |
60 | } | |
61 | ||
62 | 224 | public static void errPrintln(String message) |
63 | { | |
64 | 224 | println(message, true); |
65 | } | |
66 | ||
67 | 238 | public static void println(String message, boolean err) |
68 | { | |
69 | 238 | println(message, err, true); |
70 | } | |
71 | ||
72 | 238 | public static void println(String message0, boolean err, |
73 | boolean thisHasConsole) | |
74 | { | |
75 | 238 | if (!err && quiet) |
76 | { | |
77 | 0 | return; |
78 | } | |
79 | 238 | String message = prefix == null ? message0 : prefix + message0; |
80 | 238 | if (thisHasConsole && hasConsole) |
81 | { | |
82 | 27 | try |
83 | { | |
84 | 27 | if (console == null) |
85 | { | |
86 | 27 | Class console = Class.forName("jalview.bin.Console"); |
87 | } | |
88 | 27 | if (console == null) |
89 | { | |
90 | 27 | hasConsole = false; |
91 | } | |
92 | else | |
93 | { | |
94 | 0 | if (initLogger == null && console != null) |
95 | { | |
96 | 0 | initLogger = console.getMethod("initLogger"); |
97 | } | |
98 | 0 | hasConsole = console == null || initLogger == null |
99 | || (Boolean) initLogger.invoke(null); | |
100 | 0 | if (hasConsole && console != null) |
101 | { | |
102 | 0 | if (err) |
103 | { | |
104 | 0 | if (errPrintln == null) |
105 | { | |
106 | 0 | errPrintln = console.getMethod("errPrintln", String.class); |
107 | } | |
108 | 0 | errPrintln.invoke(null, message); |
109 | } | |
110 | else | |
111 | { | |
112 | 0 | if (outPrintln == null) |
113 | { | |
114 | 0 | outPrintln = console.getMethod("outPrintln", String.class); |
115 | } | |
116 | 0 | outPrintln.invoke(null, message); |
117 | } | |
118 | } | |
119 | } | |
120 | } catch (ClassNotFoundException | NoSuchMethodException e) | |
121 | { | |
122 | 0 | hasConsole = false; |
123 | 0 | System.err.println( |
124 | "jalview.util.ErrorLog has no jalview.bin.Console.initLogger(). Using System.err and System.out."); | |
125 | } catch (IllegalAccessException | IllegalArgumentException | |
126 | | InvocationTargetException e) | |
127 | { | |
128 | 0 | hasConsole = false; |
129 | 0 | System.err.println( |
130 | "jalview.util.ErrorLog had a problem calling a method of jalview.bin.Console. Using System.err and System.out."); | |
131 | } catch (Exception e) | |
132 | { | |
133 | 0 | e.printStackTrace(); |
134 | } catch (NoClassDefFoundError t) | |
135 | { | |
136 | 0 | hasConsole = false; |
137 | 0 | System.err.println( |
138 | "jalview.util.ErrorLog has no jalview.bin.Console. Using System.err and System.out."); | |
139 | } | |
140 | } | |
141 | 238 | if (!(thisHasConsole && hasConsole)) |
142 | { | |
143 | 238 | if (err) |
144 | { | |
145 | 224 | System.err.println(message); |
146 | } | |
147 | else | |
148 | { | |
149 | 14 | System.out.println(message); |
150 | ||
151 | } | |
152 | } | |
153 | } | |
154 | } |