1 package org.opentrafficsim.core.logger;
2
3 import java.util.Set;
4
5 import org.opentrafficsim.base.logger.CategoryLogger;
6 import org.opentrafficsim.base.logger.LogCategory;
7 import org.pmw.tinylog.Level;
8 import org.pmw.tinylog.LogEntryForwarder;
9 import org.pmw.tinylog.writers.Writer;
10
11 import nl.tudelft.simulation.dsol.simulators.SimulatorInterface;
12
13
14
15
16
17
18
19
20
21
22 @SuppressWarnings({ "checkstyle:finalclass", "checkstyle:needbraces" })
23 public class SimLogger extends CategoryLogger
24 {
25
26 private static SimulatorInterface<?, ?, ?> simulator = null;
27
28
29 private static DelegateSimLogger delegateSimLogger = new DelegateSimLogger();
30
31
32 private SimLogger()
33 {
34
35 }
36
37 static
38 {
39 create();
40 }
41
42
43
44
45
46
47 protected static void create()
48 {
49
50 }
51
52
53
54
55
56 public static final void setSimulator(final SimulatorInterface<?, ?, ?> newSimulator)
57 {
58 simulator = newSimulator;
59 }
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 public static void setAllLogMessageFormat(final String newMessageFormat)
78 {
79 CategoryLogger.setAllLogMessageFormat(newMessageFormat);
80 }
81
82
83
84
85
86 public static void setAllLogLevel(final Level newLevel)
87 {
88 CategoryLogger.setAllLogLevel(newLevel);
89 }
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108 public static void setLogMessageFormat(final Writer writer, final String newMessageFormat)
109 {
110 CategoryLogger.setLogMessageFormat(writer, newMessageFormat);
111 }
112
113
114
115
116
117
118 public static void setLogLevel(final Writer writer, final Level newLevel)
119 {
120 CategoryLogger.setLogLevel(writer, newLevel);
121 }
122
123
124
125
126
127 public static void addLogCategory(final LogCategory logCategory)
128 {
129 CategoryLogger.addLogCategory(logCategory);
130 }
131
132
133
134
135
136 public static void removeLogCategory(final LogCategory logCategory)
137 {
138 CategoryLogger.removeLogCategory(logCategory);
139 }
140
141
142
143
144
145 public static void setLogCategories(final LogCategory... newLogCategories)
146 {
147 CategoryLogger.setLogCategories(newLogCategories);
148 }
149
150
151
152
153
154
155
156 public static DelegateLogger always()
157 {
158 return delegateLogger;
159 }
160
161
162
163
164
165
166
167 public static DelegateLogger filter(final LogCategory logCategory)
168 {
169 if (categories.contains(LogCategory.ALL))
170 return delegateSimLogger;
171 if (categories.contains(logCategory))
172 return delegateSimLogger;
173 return noLogger;
174 }
175
176
177
178
179
180
181
182 public static DelegateLogger filter(final LogCategory... logCategories)
183 {
184 if (categories.contains(LogCategory.ALL))
185 return delegateSimLogger;
186 for (LogCategory logCategory : logCategories)
187 {
188 if (categories.contains(logCategory))
189 return delegateSimLogger;
190 }
191 return noLogger;
192 }
193
194
195
196
197
198
199
200 public static DelegateLogger filter(final Set<LogCategory> logCategories)
201 {
202 if (categories.contains(LogCategory.ALL))
203 return delegateSimLogger;
204 for (LogCategory logCategory : logCategories)
205 {
206 if (categories.contains(logCategory))
207 return delegateSimLogger;
208 }
209 return noLogger;
210 }
211
212
213
214
215
216
217 static String insertSimTime(final String message)
218 {
219 if (simulator == null)
220 return message;
221 return simulator.getSimulatorTime() + " - " + message;
222 }
223
224
225
226
227
228
229
230
231
232
233 public static class DelegateSimLogger extends DelegateLogger
234 {
235
236
237
238 public DelegateSimLogger()
239 {
240 super(true);
241 }
242
243
244
245
246 @Override
247 public void trace(final Object object)
248 {
249 LogEntryForwarder.forward(1, Level.TRACE, insertSimTime(object.toString()));
250 }
251
252
253 @Override
254 public void trace(final String message)
255 {
256 LogEntryForwarder.forward(1, Level.TRACE, insertSimTime(message));
257 }
258
259
260 @Override
261 public void trace(final String message, final Object... arguments)
262 {
263 LogEntryForwarder.forward(1, Level.TRACE, insertSimTime(message), arguments);
264 }
265
266
267 @Override
268 public void trace(final Throwable exception)
269 {
270 LogEntryForwarder.forward(1, Level.TRACE, exception, insertSimTime(exception.getMessage()));
271 }
272
273
274 @Override
275 public void trace(final Throwable exception, final String message)
276 {
277 LogEntryForwarder.forward(1, Level.TRACE, exception, insertSimTime(message));
278 }
279
280
281 @Override
282 public void trace(final Throwable exception, final String message, final Object... arguments)
283 {
284 LogEntryForwarder.forward(1, Level.TRACE, exception, insertSimTime(message), arguments);
285 }
286
287
288
289
290 @Override
291 public void debug(final Object object)
292 {
293 LogEntryForwarder.forward(1, Level.DEBUG, insertSimTime(object.toString()));
294 }
295
296
297 @Override
298 public void debug(final String message)
299 {
300 LogEntryForwarder.forward(1, Level.DEBUG, insertSimTime(message));
301 }
302
303
304 @Override
305 public void debug(final String message, final Object... arguments)
306 {
307 LogEntryForwarder.forward(1, Level.DEBUG, insertSimTime(message), arguments);
308 }
309
310
311 @Override
312 public void debug(final Throwable exception)
313 {
314 LogEntryForwarder.forward(1, Level.DEBUG, exception, insertSimTime(exception.getMessage()));
315 }
316
317
318 @Override
319 public void debug(final Throwable exception, final String message)
320 {
321 LogEntryForwarder.forward(1, Level.DEBUG, exception, insertSimTime(message));
322 }
323
324
325 @Override
326 public void debug(final Throwable exception, final String message, final Object... arguments)
327 {
328 LogEntryForwarder.forward(1, Level.DEBUG, exception, insertSimTime(message), arguments);
329 }
330
331
332
333
334 @Override
335 public void info(final Object object)
336 {
337 LogEntryForwarder.forward(1, Level.INFO, insertSimTime(object.toString()));
338 }
339
340
341 @Override
342 public void info(final String message)
343 {
344 LogEntryForwarder.forward(1, Level.INFO, insertSimTime(message));
345 }
346
347
348 @Override
349 public void info(final String message, final Object... arguments)
350 {
351 LogEntryForwarder.forward(1, Level.INFO, insertSimTime(message), arguments);
352 }
353
354
355 @Override
356 public void info(final Throwable exception)
357 {
358 LogEntryForwarder.forward(1, Level.INFO, exception, insertSimTime(exception.getMessage()));
359 }
360
361
362 @Override
363 public void info(final Throwable exception, final String message)
364 {
365 LogEntryForwarder.forward(1, Level.INFO, exception, insertSimTime(message));
366 }
367
368
369 @Override
370 public void info(final Throwable exception, final String message, final Object... arguments)
371 {
372 LogEntryForwarder.forward(1, Level.INFO, exception, insertSimTime(message), arguments);
373 }
374
375
376
377
378 @Override
379 public void warn(final Object object)
380 {
381 LogEntryForwarder.forward(1, Level.WARNING, insertSimTime(object.toString()));
382 }
383
384
385 @Override
386 public void warn(final String message)
387 {
388 LogEntryForwarder.forward(1, Level.WARNING, insertSimTime(message));
389 }
390
391
392 @Override
393 public void warn(final String message, final Object... arguments)
394 {
395 LogEntryForwarder.forward(1, Level.WARNING, insertSimTime(message), arguments);
396 }
397
398
399 @Override
400 public void warn(final Throwable exception)
401 {
402 LogEntryForwarder.forward(1, Level.WARNING, exception, insertSimTime(exception.getMessage()));
403 }
404
405
406 @Override
407 public void warn(final Throwable exception, final String message)
408 {
409 LogEntryForwarder.forward(1, Level.WARNING, exception, insertSimTime(message));
410 }
411
412
413 @Override
414 public void warn(final Throwable exception, final String message, final Object... arguments)
415 {
416 LogEntryForwarder.forward(1, Level.WARNING, exception, insertSimTime(message), arguments);
417 }
418
419
420
421
422 @Override
423 public void error(final Object object)
424 {
425 LogEntryForwarder.forward(1, Level.ERROR, insertSimTime(object.toString()));
426 }
427
428
429 @Override
430 public void error(final String message)
431 {
432 LogEntryForwarder.forward(1, Level.ERROR, insertSimTime(message));
433 }
434
435
436 @Override
437 public void error(final String message, final Object... arguments)
438 {
439 LogEntryForwarder.forward(1, Level.ERROR, insertSimTime(message), arguments);
440 }
441
442
443 @Override
444 public void error(final Throwable exception)
445 {
446 LogEntryForwarder.forward(1, Level.ERROR, exception, insertSimTime(exception.getMessage()));
447 }
448
449
450 @Override
451 public void error(final Throwable exception, final String message)
452 {
453 LogEntryForwarder.forward(1, Level.ERROR, exception, insertSimTime(message));
454 }
455
456
457 @Override
458 public void error(final Throwable exception, final String message, final Object... arguments)
459 {
460 LogEntryForwarder.forward(1, Level.ERROR, exception, insertSimTime(message), arguments);
461 }
462
463 }
464 }