JAL-3746 apply copyright to source
[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 jalview.log.JLogger;
24 import jalview.log.JLoggerI.LogLevel;
25 import jalview.log.JLoggerLog4j;
26 import jalview.util.ChannelProperties;
27 import jalview.util.Log4j;
28 import jalview.util.Platform;
29
30 public class Console
31 {
32
33   public static JLoggerLog4j log;
34
35   public static void debug(String message, Throwable t)
36   {
37     if (Console.initLogger())
38     {
39       log.debug(message, t);
40     }
41     else
42     {
43       System.out.println(message);
44       t.printStackTrace();
45     }
46
47   }
48
49   public static void info(String message)
50   {
51     if (Console.initLogger())
52     {
53       log.info(message, null);
54     }
55     else
56     {
57       System.out.println(message);
58     }
59
60   }
61
62   public static void trace(String message, Throwable t)
63   {
64     if (Console.initLogger())
65     {
66       log.trace(message, t);
67     }
68     else
69     {
70       System.out.println(message);
71       t.printStackTrace();
72     }
73   }
74
75   public static void debug(String message)
76   {
77     if (Console.initLogger())
78     {
79       log.debug(message, null);
80     }
81     else
82     {
83       System.out.println(message);
84     }
85
86   }
87
88   public static void info(String message, Throwable t)
89   {
90     if (Console.initLogger())
91     {
92       log.info(message, t);
93     }
94     else
95     {
96       System.out.println(message);
97       t.printStackTrace();
98     }
99
100   }
101
102   public static void warn(String message)
103   {
104     if (Console.initLogger())
105     {
106       log.warn(message, null);
107     }
108     else
109     {
110       System.out.println(message);
111     }
112
113   }
114
115   public static void trace(String message)
116   {
117     if (Console.initLogger())
118     {
119       log.trace(message, null);
120     }
121     else
122     {
123       System.out.println(message);
124     }
125   }
126
127   public static void warn(String message, Throwable t)
128   {
129     if (Console.initLogger())
130     {
131       log.warn(message, t);
132     }
133     else
134     {
135       System.out.println(message);
136       t.printStackTrace();
137     }
138
139   }
140
141   public static void error(String message)
142   {
143     if (Console.initLogger())
144     {
145       log.error(message, null);
146     }
147     else
148     {
149       System.err.println(message);
150     }
151
152   }
153
154   public static void error(String message, Throwable t)
155   {
156     if (Console.initLogger())
157     {
158       log.error(message, t);
159     }
160     else
161     {
162       System.err.println(message);
163       t.printStackTrace(System.err);
164     }
165
166   }
167
168   public static void fatal(String message)
169   {
170     if (Console.initLogger())
171     {
172       log.fatal(message, null);
173     }
174     else
175     {
176       System.err.println(message);
177     }
178
179   }
180
181   public static void fatal(String message, Throwable t)
182   {
183     if (Console.initLogger())
184     {
185       log.fatal(message, t);
186     }
187     else
188     {
189       System.err.println(message);
190       t.printStackTrace(System.err);
191     }
192
193   }
194
195   public static boolean isDebugEnabled()
196   {
197     return log == null ? false : log.isDebugEnabled();
198   }
199
200   public static boolean isTraceEnabled()
201   {
202     return log == null ? false : log.isTraceEnabled();
203   }
204
205   public static JLogger.LogLevel getCachedLogLevel()
206   {
207     return Console.getCachedLogLevel(Cache.JALVIEWLOGLEVEL);
208   }
209
210   public static JLogger.LogLevel getCachedLogLevel(String key)
211   {
212     return JLogger.toLevel(Cache.getDefault(key, "INFO"));
213   }
214
215   public static boolean initLogger()
216   {
217     if (log != null)
218     {
219       return true;
220     }
221     try
222     {
223       JLogger.LogLevel cachedLevel = getCachedLogLevel();
224       if (!Platform.isJS())
225       {
226         Log4j.init(cachedLevel);
227       }
228       // log output
229       // is laxis used? Does getLogger do anything without a Logger object?
230       // Logger laxis = Log4j.getLogger("org.apache.axis", myLevel);
231       JLoggerLog4j.getLogger("org.apache.axis", cachedLevel);
232
233       // The main application logger
234       log = JLoggerLog4j.getLogger(Cache.JALVIEW_LOGGER_NAME, cachedLevel);
235     } catch (NoClassDefFoundError e)
236     {
237       System.err.println("Could not initialise the logger framework");
238       e.printStackTrace();
239     }
240
241     // Test message
242     if (log != null)
243     {
244       // Logging test message should got through the logger object
245       if (log.loggerExists())
246         log.debug(Console.LOGGING_TEST_MESSAGE);
247       // Tell the user that debug is enabled
248       debug(ChannelProperties.getProperty("app_name")
249               + " Debugging Output Follows.");
250       return true;
251     }
252     else
253     {
254       return false;
255     }
256   }
257
258   public final static String LOGGING_TEST_MESSAGE = "Logging to STDERR";
259
260 }