Fix: cleanup JUL agent state on sessiond disconnect
[lttng-ust.git] / liblttng-ust-jul / org / lttng / ust / jul / LTTngLogHandler.java
1 /*
2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
3 *
4 * This library is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU Lesser General Public License, version 2.1 only,
6 * as published by the Free Software Foundation.
7 *
8 * This library is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
11 * for more details.
12 *
13 * You should have received a copy of the GNU Lesser General Public License
14 * along with this library; if not, write to the Free Software Foundation,
15 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
16 */
17
18 package org.lttng.ust.jul;
19
20 import java.lang.String;
21 import java.util.logging.Handler;
22 import java.util.logging.LogRecord;
23 import java.util.logging.LogManager;
24 import java.util.logging.Level;
25 import java.util.ArrayList;
26 import java.util.HashMap;
27
28 import org.lttng.ust.jul.LTTngUst;
29
30 /* Note: This is taken from the LTTng tools ABI. */
31 class LTTngLogLevelABI {
32 /* Loglevel type. */
33 public static final int LOGLEVEL_TYPE_ALL = 0;
34 public static final int LOGLEVEL_TYPE_RANGE = 1;
35 public static final int LOGLEVEL_TYPE_SINGLE = 2;
36 }
37
38 public class LTTngLogHandler extends Handler {
39 /*
40 * Indicate if the enable all event has been seen and if yes logger that we
41 * enabled should use the loglevel/type below.
42 */
43 public int logLevelUseAll = 0;
44 public ArrayList<LTTngLogLevel> logLevelsAll =
45 new ArrayList<LTTngLogLevel>();
46
47 /* Am I a root Log Handler. */
48 public int is_root = 0;
49
50 public LogManager logManager;
51
52 /* Indexed by name and corresponding LTTngEvent. */
53 private HashMap<String, LTTngEvent> eventMap =
54 new HashMap<String, LTTngEvent>();
55
56 public LTTngLogHandler(LogManager logManager) {
57 super();
58
59 this.logManager = logManager;
60
61 /* Initialize LTTng UST tracer. */
62 LTTngUst.init();
63 }
64
65 /**
66 * Add event to handler hash map if new.
67 *
68 * @return 0 if it did not exist else 1.
69 */
70 public int setEvent(LTTngEvent new_event) {
71 LTTngEvent event;
72
73 event = eventMap.get(new_event.name);
74 if (event == null) {
75 eventMap.put(new_event.name, new_event);
76 /* Did not exists. */
77 return 0;
78 } else {
79 /* Add new event loglevel to existing event. */
80 event.logLevels.addAll(new_event.logLevels);
81 /* Already exists. */
82 return 1;
83 }
84 }
85
86 /*
87 * Cleanup this handler state meaning put it back to a vanilla state.
88 */
89 public void clear() {
90 this.eventMap.clear();
91 this.logLevelsAll.clear();
92 }
93
94 @Override
95 public void close() throws SecurityException {}
96
97 @Override
98 public void flush() {}
99
100 @Override
101 public void publish(LogRecord record) {
102 int fire_tp = 0, rec_log_level, ev_type, ev_log_level;
103 LTTngEvent event;
104 LTTngLogLevel lttngLogLevel;
105 String logger_name = record.getLoggerName();
106
107 /* Get back the event if any and check for loglevel. */
108 event = eventMap.get(logger_name);
109 if (event != null) {
110 for (LTTngLogLevel ev_log : event.logLevels) {
111 /* Get record and event log level. */
112 rec_log_level = record.getLevel().intValue();
113 ev_log_level = ev_log.level;
114
115 switch (ev_log.type) {
116 case LTTngLogLevelABI.LOGLEVEL_TYPE_RANGE:
117 if (ev_log_level <= rec_log_level) {
118 fire_tp = 1;
119 }
120 break;
121 case LTTngLogLevelABI.LOGLEVEL_TYPE_SINGLE:
122 if (ev_log_level == rec_log_level) {
123 fire_tp = 1;
124 }
125 break;
126 case LTTngLogLevelABI.LOGLEVEL_TYPE_ALL:
127 fire_tp = 1;
128 break;
129 }
130
131 /*
132 * If we match, stop right now else continue to the next
133 * loglevel contained in the event.
134 */
135 if (fire_tp == 1) {
136 break;
137 }
138 }
139 } else {
140 /* No loglevel attached thus fire tracepoint. */
141 fire_tp = 1;
142 }
143
144 if (fire_tp == 0) {
145 return;
146 }
147
148 /*
149 * Specific tracepoing designed for JUL events. The source class of the
150 * caller is used for the event name, the raw message is taken, the
151 * loglevel of the record and the thread ID.
152 */
153 if (this.is_root == 1) {
154 LTTngUst.tracepointS(record.getMessage(),
155 record.getLoggerName(), record.getSourceClassName(),
156 record.getSourceMethodName(), record.getMillis(),
157 record.getLevel().intValue(), record.getThreadID());
158 } else {
159 LTTngUst.tracepointU(record.getMessage(),
160 record.getLoggerName(), record.getSourceClassName(),
161 record.getSourceMethodName(), record.getMillis(),
162 record.getLevel().intValue(), record.getThreadID());
163 }
164 }
165 }
This page took 0.035225 seconds and 4 git commands to generate.