1 | package org.etsi.common.logging; |
---|
2 | |
---|
3 | |
---|
4 | import org.etsi.common.MiscTools; |
---|
5 | |
---|
6 | public class LoggingInterface { |
---|
7 | |
---|
8 | public enum LogLevel { |
---|
9 | //ORDER MATTERS |
---|
10 | ERROR, |
---|
11 | WARNING, |
---|
12 | INFORMATION, |
---|
13 | FIXME, |
---|
14 | DEBUG; |
---|
15 | } |
---|
16 | |
---|
17 | public enum MessageClass { |
---|
18 | //ORDER MATTERS |
---|
19 | UNIVERSAL ("Universal", 0), |
---|
20 | GENERAL ("General", 1), |
---|
21 | NAMING ("Naming Conventions", 2), |
---|
22 | DOCUMENTATION ("Code Documentation", 3), |
---|
23 | LOGGING ("Log Statements", 4), |
---|
24 | STRUCTURE ("Structure of Data", 5), |
---|
25 | STYLE ("Code Style", 6), |
---|
26 | MODULARIZATION ("Test Suite Modularization", 7); |
---|
27 | |
---|
28 | private final String description; |
---|
29 | private final int id; |
---|
30 | private int occurenceCount = 0; |
---|
31 | |
---|
32 | MessageClass (String description, int id){ |
---|
33 | this.description = description; |
---|
34 | this.id = id; |
---|
35 | } |
---|
36 | |
---|
37 | public String getDescription() { |
---|
38 | return description; |
---|
39 | } |
---|
40 | |
---|
41 | public int getId() { |
---|
42 | return id; |
---|
43 | } |
---|
44 | |
---|
45 | public void addOccurence() { |
---|
46 | this.occurenceCount++; |
---|
47 | } |
---|
48 | |
---|
49 | public int getOccurenceCount() { |
---|
50 | return occurenceCount; |
---|
51 | } |
---|
52 | } |
---|
53 | |
---|
54 | private String logSourceName = ""; |
---|
55 | private LogLevel maximumLogLevel = LogLevel.INFORMATION; |
---|
56 | private LogLevel defaultLogLevel = LogLevel.INFORMATION; |
---|
57 | private LoggingConfiguration configuration = new LoggingConfiguration(); |
---|
58 | |
---|
59 | public LoggingInterface(LoggingConfiguration logConfiguration) { |
---|
60 | this.setConfiguration(logConfiguration); |
---|
61 | } |
---|
62 | |
---|
63 | public LoggingInterface(String logSourceName, LoggingConfiguration logConfiguration){ |
---|
64 | this(logConfiguration); |
---|
65 | this.setLogSourceName(logSourceName); |
---|
66 | } |
---|
67 | public LoggingInterface(String logSourceName, LoggingConfiguration logConfiguration, LogLevel maximumLogLevel){ |
---|
68 | this(logSourceName, logConfiguration); |
---|
69 | this.setMaximumLogLevel(maximumLogLevel); |
---|
70 | } |
---|
71 | |
---|
72 | private LogMessage createLogMessage(LogLevel l, int startLine, int endLine, MessageClass messageClass, String message, String details){ |
---|
73 | LogMessage logMessage = new LogMessage(); |
---|
74 | logMessage.setPrefix(this.getConfiguration().getLogOutputPrefix()); |
---|
75 | |
---|
76 | if (this.getConfiguration().isShowFullPath()) { |
---|
77 | logMessage.setFilename(this.getLogSourceName()); |
---|
78 | } else { |
---|
79 | if (this.getConfiguration().isShowFilename()) { |
---|
80 | logMessage.setFilename(MiscTools.getShortFilename(this |
---|
81 | .getLogSourceName())); |
---|
82 | } |
---|
83 | } |
---|
84 | logMessage.setStartLine(startLine); |
---|
85 | logMessage.setEndLine(endLine); |
---|
86 | logMessage.setLogLevel(l); |
---|
87 | |
---|
88 | messageClass.addOccurence(); |
---|
89 | |
---|
90 | if (this.getConfiguration().isShowMessageClass()) { |
---|
91 | logMessage.setMessageClass(messageClass); |
---|
92 | } else { |
---|
93 | logMessage.setMessageClass(null); |
---|
94 | } |
---|
95 | |
---|
96 | logMessage.setMessage(message); |
---|
97 | |
---|
98 | if (this.getConfiguration().isShowDetails() && details != null) { |
---|
99 | logMessage.setDetails(details); |
---|
100 | } else { |
---|
101 | logMessage.setDetails(null); |
---|
102 | } |
---|
103 | |
---|
104 | return logMessage; |
---|
105 | } |
---|
106 | |
---|
107 | /* private String composeLogMessage(LogLevel l, int startLine, int endLine, MessageClass messageClass, String message, String details){ |
---|
108 | String composedLogMessage = ""; |
---|
109 | |
---|
110 | composedLogMessage += this.getConfiguration().getLogOutputPrefix(); |
---|
111 | |
---|
112 | if (this.getConfiguration().isShowFullPath()) { |
---|
113 | composedLogMessage += this.getLogSourceName() + ": "; |
---|
114 | } else { |
---|
115 | if (this.getConfiguration().isShowFilename()) { |
---|
116 | composedLogMessage += MiscTools.getShortFilename(this |
---|
117 | .getLogSourceName()) |
---|
118 | + ": "; |
---|
119 | } |
---|
120 | } |
---|
121 | |
---|
122 | if (startLine >= endLine) { |
---|
123 | composedLogMessage += startLine + ": "; |
---|
124 | } else { |
---|
125 | composedLogMessage += startLine + "-" + endLine + ": "; |
---|
126 | } |
---|
127 | |
---|
128 | composedLogMessage += l + ": "; |
---|
129 | |
---|
130 | if (this.getConfiguration().isShowMessageClass()) { |
---|
131 | composedLogMessage += messageClass.getDescription() + ": "; |
---|
132 | } |
---|
133 | |
---|
134 | composedLogMessage += message; |
---|
135 | |
---|
136 | if (this.getConfiguration().isShowDetails() && details != null) { |
---|
137 | composedLogMessage += "(" + details + ")"; |
---|
138 | } |
---|
139 | |
---|
140 | return composedLogMessage; |
---|
141 | } |
---|
142 | */ |
---|
143 | |
---|
144 | public void logMessage(LogLevel l, int startLine, int endLine, MessageClass messageClass, String message, String details){ |
---|
145 | if (showLogLevel(l)){ |
---|
146 | LogMessage composedLogMessage = createLogMessage(l, startLine, endLine, messageClass, message, details); |
---|
147 | outputLog(composedLogMessage); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | public void logMessage(LogLevel l, int startLine, int endLine, MessageClass messageClass, String message){ |
---|
152 | if (showLogLevel(l)){ |
---|
153 | LogMessage composedLogMessage = createLogMessage(l, startLine, endLine, messageClass, message, null); |
---|
154 | outputLog(composedLogMessage); |
---|
155 | } |
---|
156 | } |
---|
157 | |
---|
158 | public void logMessage(int startLine, int endLine, MessageClass messageClass, String message, String details){ |
---|
159 | if (showLogLevel(this.getDefaultLogLevel())){ |
---|
160 | LogMessage composedLogMessage = createLogMessage(this.getDefaultLogLevel(), startLine, endLine, messageClass, message, details); |
---|
161 | outputLog(composedLogMessage); |
---|
162 | } |
---|
163 | } |
---|
164 | |
---|
165 | |
---|
166 | public void logMessage(int startLine, int endLine, MessageClass messageClass, String message){ |
---|
167 | if (showLogLevel(this.getDefaultLogLevel())){ |
---|
168 | LogMessage composedLogMessage = createLogMessage(this.getDefaultLogLevel(), startLine, endLine, messageClass, message, null); |
---|
169 | outputLog(composedLogMessage); |
---|
170 | } |
---|
171 | } |
---|
172 | |
---|
173 | |
---|
174 | // private void outputLog(String composedLogMessage){ |
---|
175 | // System.out.println(composedLogMessage); |
---|
176 | // } |
---|
177 | |
---|
178 | private void outputLog(LogMessage composedLogMessage){ |
---|
179 | System.out.println(composedLogMessage.toString()); |
---|
180 | } |
---|
181 | |
---|
182 | public void logWarning(int startLine, int endLine, MessageClass messageClass, String message){ |
---|
183 | if (showLogLevel(LogLevel.WARNING)){ |
---|
184 | LogMessage composedLogMessage = createLogMessage(LogLevel.WARNING, startLine, endLine, messageClass, message, null); |
---|
185 | outputLog(composedLogMessage); |
---|
186 | } |
---|
187 | } |
---|
188 | |
---|
189 | public void logWarning(int startLine, int endLine, MessageClass messageClass, String message, String details){ |
---|
190 | if (showLogLevel(LogLevel.WARNING)){ |
---|
191 | LogMessage composedLogMessage = createLogMessage(LogLevel.WARNING, startLine, endLine, messageClass, message, details); |
---|
192 | outputLog(composedLogMessage); |
---|
193 | } |
---|
194 | |
---|
195 | } |
---|
196 | |
---|
197 | public void logDebug(int startLine, int endLine, MessageClass messageClass, String message){ |
---|
198 | if (showLogLevel(LogLevel.DEBUG)){ |
---|
199 | LogMessage composedLogMessage = createLogMessage(LogLevel.DEBUG, startLine, endLine, messageClass, message, null); |
---|
200 | outputLog(composedLogMessage); |
---|
201 | } |
---|
202 | |
---|
203 | } |
---|
204 | |
---|
205 | public void logDebug(int startLine, int endLine, MessageClass messageClass, String message, String details){ |
---|
206 | if (showLogLevel(LogLevel.DEBUG)){ |
---|
207 | LogMessage composedLogMessage = createLogMessage(LogLevel.DEBUG, startLine, endLine, messageClass, message, details); |
---|
208 | outputLog(composedLogMessage); |
---|
209 | } |
---|
210 | |
---|
211 | } |
---|
212 | |
---|
213 | public void logFix(int startLine, int endLine, MessageClass messageClass, String message){ |
---|
214 | if (showLogLevel(LogLevel.FIXME)){ |
---|
215 | LogMessage composedLogMessage = createLogMessage(LogLevel.FIXME, startLine, endLine, messageClass, message, null); |
---|
216 | outputLog(composedLogMessage); |
---|
217 | } |
---|
218 | |
---|
219 | } |
---|
220 | |
---|
221 | public void logFix(int startLine, int endLine, MessageClass messageClass, String message, String details){ |
---|
222 | if (showLogLevel(LogLevel.FIXME)){ |
---|
223 | LogMessage composedLogMessage = createLogMessage(LogLevel.FIXME, startLine, endLine, messageClass, message, details); |
---|
224 | outputLog(composedLogMessage); |
---|
225 | } |
---|
226 | |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | public void logError(int startLine, int endLine, MessageClass messageClass, String message){ |
---|
231 | if (showLogLevel(LogLevel.ERROR)){ |
---|
232 | LogMessage composedLogMessage = createLogMessage(LogLevel.ERROR, startLine, endLine, messageClass, message, null); |
---|
233 | outputLog(composedLogMessage); |
---|
234 | } |
---|
235 | } |
---|
236 | |
---|
237 | public void logInformation(int startLine, int endLine, MessageClass messageClass, String message){ |
---|
238 | if (showLogLevel(LogLevel.INFORMATION)){ |
---|
239 | LogMessage composedLogMessage = createLogMessage(LogLevel.INFORMATION, startLine, endLine, messageClass, message, null); |
---|
240 | outputLog(composedLogMessage); |
---|
241 | } |
---|
242 | } |
---|
243 | |
---|
244 | public void logInformation(int startLine, int endLine, MessageClass messageClass, String message, String detail){ |
---|
245 | if (showLogLevel(LogLevel.INFORMATION)){ |
---|
246 | LogMessage composedLogMessage = createLogMessage(LogLevel.INFORMATION, startLine, endLine, messageClass, message, detail); |
---|
247 | outputLog(composedLogMessage); |
---|
248 | } |
---|
249 | } |
---|
250 | |
---|
251 | |
---|
252 | public String getLogSourceName() { |
---|
253 | return logSourceName; |
---|
254 | } |
---|
255 | |
---|
256 | |
---|
257 | public void setMaximumLogLevel(LogLevel maximumLogLevel) { |
---|
258 | this.maximumLogLevel = maximumLogLevel; |
---|
259 | } |
---|
260 | |
---|
261 | public LogLevel getMaximumLogLevel() { |
---|
262 | return maximumLogLevel; |
---|
263 | } |
---|
264 | |
---|
265 | public boolean showLogLevel(LogLevel logLevel) { |
---|
266 | boolean show = false; |
---|
267 | if (logLevel.ordinal() <= this.getMaximumLogLevel().ordinal()){ |
---|
268 | show = true; |
---|
269 | } |
---|
270 | return show; |
---|
271 | } |
---|
272 | |
---|
273 | public void setConfiguration(LoggingConfiguration configuration) { |
---|
274 | this.configuration = configuration; |
---|
275 | } |
---|
276 | public LoggingConfiguration getConfiguration() { |
---|
277 | return configuration; |
---|
278 | } |
---|
279 | public void setDefaultLogLevel(LogLevel defaultLogLevel) { |
---|
280 | this.defaultLogLevel = defaultLogLevel; |
---|
281 | } |
---|
282 | public LogLevel getDefaultLogLevel() { |
---|
283 | return defaultLogLevel; |
---|
284 | } |
---|
285 | public void setLogSourceName(String logSourceName) { |
---|
286 | this.logSourceName = logSourceName; |
---|
287 | } |
---|
288 | |
---|
289 | |
---|
290 | |
---|
291 | } |
---|