blob: 3429675d7b60829c0fa18b9e106ac1cd09b75534 [file] [log] [blame]
Brian O'Connorc67f9fa2014-08-07 18:17:46 -07001package net.floodlightcontroller.debugevent;
2
3import net.floodlightcontroller.debugevent.IDebugEventService.EventColumn;
4import net.floodlightcontroller.debugevent.IDebugEventService.EventFieldType;
5import net.floodlightcontroller.debugevent.IDebugEventService.EventType;
6import net.floodlightcontroller.debugevent.IDebugEventService.MaxEventsRegistered;
7import ch.qos.logback.classic.Level;
8import ch.qos.logback.classic.spi.ILoggingEvent;
9import ch.qos.logback.core.UnsynchronizedAppenderBase;
10
11public class DebugEventAppender<E> extends UnsynchronizedAppenderBase<E> {
12 static IDebugEventService debugEvent;
13 static IEventUpdater<WarnErrorEvent> evWarnError;
14 static Thread debugEventRegistryTask = new Thread() {
15 @Override
16 public void run() {
17 while(DebugEventAppender.debugEvent == null) {
18 try {
19 Thread.sleep(100);
20 } catch (InterruptedException e) {
21 return;
22 }
23 }
24 //safe to register debugEvent
25 registerDebugEventQueue();
26 }
27 };
28
29 @Override
30 public void start() {
31 DebugEventAppender.debugEventRegistryTask.start();
32 super.start();
33 }
34
35 public static void setDebugEventServiceImpl(IDebugEventService debugEvent) {
36 DebugEventAppender.debugEvent = debugEvent;
37 // It is now ok to register an event Q - but letting this thread go
38 // since it was called from a startUp() routine
39 }
40
41 /**
42 * The logging system calls append for every log message. This method filters
43 * out the WARN and ERROR message and adds to a debug event queue that can
44 * be accessed via cli or rest-api or gui.
45 */
46 @Override
47 protected void append(E eventObject) {
48 if (!isStarted()) {
49 return;
50 }
51 if (evWarnError != null) {
52 ILoggingEvent ev = ((ILoggingEvent) eventObject);
53 if (ev.getLevel().equals(Level.ERROR) || ev.getLevel().equals(Level.WARN)) {
54 evWarnError.updateEventWithFlush(
55 new WarnErrorEvent(ev.getFormattedMessage(), ev.getLevel(),
56 ev.getThreadName(), ev.getLoggerName()));
57 }
58 }
59 }
60
61 private static void registerDebugEventQueue() {
62 try {
63 evWarnError = debugEvent.registerEvent("net.floodlightcontroller.core",
64 "warn-error-queue",
65 "all WARN and ERROR logs",
66 EventType.ALWAYS_LOG, WarnErrorEvent.class,
67 100);
68 } catch (MaxEventsRegistered e) {
69 e.printStackTrace();
70 }
71
72 }
73
74 public static class WarnErrorEvent {
75 @EventColumn(name = "message", description = EventFieldType.STRING)
76 String message;
77
78 @EventColumn(name = "level", description = EventFieldType.OBJECT)
79 Level level;
80
81 @EventColumn(name = "threadName", description = EventFieldType.STRING)
82 String threadName;
83
84 @EventColumn(name = "logger", description = EventFieldType.OBJECT)
85 String logger;
86
87 public WarnErrorEvent(String message, Level level, String threadName,
88 String logger) {
89 this.message = message;
90 this.level = level;
91 this.threadName = threadName;
92 this.logger = logger;
93 }
94 }
95
96}