JAL-3746 apply copyright to source
[jalview.git] / src / jalview / log / JLogger.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.log;
22
23 import java.util.HashMap;
24 import java.util.Map;
25
26 import jalview.bin.Console;
27 import jalview.util.Platform;
28
29 public abstract class JLogger implements JLoggerI
30 {
31   protected String name;
32
33   protected LogLevel level;
34
35   private Object logger = null;
36
37   private static Map<String, JLogger> registry = new HashMap<>();
38
39   // implement these abstract methods
40   protected abstract void loggerSetup();
41
42   public abstract boolean loggerExists();
43
44   protected abstract void loggerSetLevel(LogLevel level);
45
46   protected abstract void loggerLogMessage(LogLevel level, String message,
47           Throwable t);
48
49   public static LogLevel toLevel(String levelString)
50   {
51     try
52     {
53       return LogLevel.valueOf(levelString);
54     } catch (IllegalArgumentException e)
55     {
56       Console.error("Could not parse LogLevel '" + levelString + "'", e);
57       return LogLevel.INFO;
58     }
59   }
60
61   public static JLogger getLogger(Class c)
62   {
63     return getLogger(c);
64   }
65
66   public static JLogger getLogger(Class c, LogLevel loglevel)
67   {
68     return getLogger(c.getCanonicalName(), loglevel);
69   }
70
71   public static JLogger getLogger(String name)
72   {
73     return getLogger(name, LogLevel.INFO);
74   }
75
76   public static JLogger getLogger(String name, LogLevel loglevel)
77   {
78     return registry.containsKey(name) ? (JLogger) registry.get(name) : null;
79   }
80
81   protected JLogger()
82   {
83   }
84
85   protected JLogger(String name, LogLevel level)
86   {
87     this.name = name;
88     this.level = level;
89     this.loggerSetup();
90     this.registryStore();
91   }
92
93   protected void registryStore()
94   {
95     registry.put(this.name, this);
96   }
97
98   protected static boolean registryContainsKey(String name)
99   {
100     return registry.containsKey(name);
101   }
102
103   protected static JLogger registryGet(String name)
104   {
105     return registry.get(name);
106   }
107
108   public LogLevel getLevel()
109   {
110     return this.level;
111   }
112
113   public void setLevel(LogLevel level)
114   {
115     this.level = level;
116     if (loggerExists())
117       loggerSetLevel(level);
118   }
119
120   private boolean println(LogLevel loglevel, String message, Throwable t)
121   {
122     if (loglevel.compareTo(this.level) < 0)
123     {
124       return false;
125     }
126     if (!loggerExists() || Platform.isJS())
127     {
128       String logLine = String.format("%s: %s", loglevel.toString(),
129               message);
130       System.out.println(logLine);
131       if (t != null)
132       {
133         if (loglevel.compareTo(LogLevel.DEBUG) <= 0)
134           t.printStackTrace(System.err);
135         else
136           System.err.println(t.getMessage());
137       }
138       return false;
139     }
140     else
141     {
142       loggerLogMessage(loglevel, message, t);
143       return true;
144     }
145   }
146
147   public void trace(String message)
148   {
149     trace(message, null);
150   }
151
152   public void trace(String message, Throwable t)
153   {
154     println(LogLevel.TRACE, message, t);
155   }
156
157   public void debug(String message)
158   {
159     debug(message, null);
160   }
161
162   public void debug(String message, Throwable t)
163   {
164     println(LogLevel.DEBUG, message, t);
165   }
166
167   public void info(String message)
168   {
169     info(message, null);
170   }
171
172   public void info(String message, Throwable t)
173   {
174     println(LogLevel.INFO, message, t);
175   }
176
177   public void warn(String message)
178   {
179     warn(message, null);
180   }
181
182   public void warn(String message, Throwable t)
183   {
184     println(LogLevel.WARN, message, t);
185   }
186
187   public void error(String message)
188   {
189     error(message, null);
190   }
191
192   public void error(String message, Throwable t)
193   {
194     println(LogLevel.ERROR, message, t);
195   }
196
197   public void fatal(String message)
198   {
199     fatal(message, null);
200   }
201
202   public void fatal(String message, Throwable t)
203   {
204     println(LogLevel.FATAL, message, t);
205   }
206
207   public boolean isDebugEnabled()
208   {
209     return level.compareTo(LogLevel.DEBUG) <= 0;
210   }
211
212   public boolean isTraceEnabled()
213   {
214     return level.compareTo(LogLevel.TRACE) <= 0;
215   }
216 }