Commit | Line | Data |
---|---|---|
43e5396b DG |
1 | /* |
2 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> | |
3 | * | |
4 | * | |
5 | * This library is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
7 | * as published by the Free Software Foundation. | |
8 | * | |
9 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
12 | * for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Lesser General Public License | |
15 | * along with this library; if not, write to the Free Software Foundation, | |
16 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | ||
19 | package org.lttng.ust.jul; | |
20 | ||
21 | import java.nio.ByteBuffer; | |
22 | import java.nio.ByteOrder; | |
a15440fd | 23 | import java.lang.Object; |
43e5396b DG |
24 | import java.util.logging.Logger; |
25 | import java.util.ArrayList; | |
529e6def | 26 | import java.util.HashMap; |
43e5396b DG |
27 | import java.util.List; |
28 | import java.util.Enumeration; | |
29 | ||
30 | public interface LTTngSessiondCmd2_4 { | |
31 | /** | |
32 | * Maximum name length for a logger name to be send to sessiond. | |
33 | */ | |
34 | final static int NAME_MAX = 255; | |
35 | ||
a15440fd DG |
36 | /* |
37 | * Size of a primitive type int in byte. Because you know, Java can't | |
38 | * provide that since it does not makes sense... | |
39 | */ | |
40 | final static int INT_SIZE = 4; | |
41 | ||
43e5396b DG |
42 | public interface SessiondResponse { |
43 | /** | |
44 | * Gets a byte array of the command so that it may be streamed | |
45 | * | |
46 | * @return the byte array of the command | |
47 | */ | |
48 | public byte[] getBytes(); | |
49 | } | |
50 | ||
51 | public interface SessiondCommand { | |
52 | /** | |
53 | * Populate the class from a byte array | |
54 | * | |
55 | * @param data | |
56 | * the byte array containing the streamed command | |
57 | */ | |
58 | public void populate(byte[] data); | |
59 | } | |
60 | ||
61 | public enum lttng_jul_command { | |
62 | /** List logger(s). */ | |
63 | CMD_LIST(1), | |
64 | /** Enable logger by name. */ | |
65 | CMD_ENABLE(2), | |
66 | /** Disable logger by name. */ | |
f08bb871 DG |
67 | CMD_DISABLE(3), |
68 | /** Registration done */ | |
69 | CMD_REG_DONE(4); | |
70 | ||
43e5396b DG |
71 | private int code; |
72 | ||
73 | private lttng_jul_command(int c) { | |
74 | code = c; | |
75 | } | |
76 | ||
77 | public int getCommand() { | |
78 | return code; | |
79 | } | |
80 | } | |
81 | ||
82 | enum lttng_jul_ret_code { | |
83 | CODE_SUCCESS_CMD(1), | |
84 | CODE_INVALID_CMD(2), | |
85 | CODE_UNK_LOGGER_NAME(3); | |
86 | private int code; | |
87 | ||
88 | private lttng_jul_ret_code(int c) { | |
89 | code = c; | |
90 | } | |
91 | ||
92 | public int getCode() { | |
93 | return code; | |
94 | } | |
95 | } | |
96 | ||
97 | public class sessiond_hdr implements SessiondCommand { | |
98 | /** ABI size of command header. */ | |
99 | public final static int SIZE = 16; | |
100 | /** Payload size in bytes following this header. */ | |
101 | public long data_size; | |
102 | /** Command type. */ | |
103 | public lttng_jul_command cmd; | |
104 | /** Command version. */ | |
105 | public int cmd_version; | |
106 | ||
107 | public void populate(byte[] data) { | |
108 | ByteBuffer buf = ByteBuffer.wrap(data); | |
109 | buf.order(ByteOrder.BIG_ENDIAN); | |
110 | ||
111 | data_size = buf.getLong(); | |
112 | cmd = lttng_jul_command.values()[buf.getInt() - 1]; | |
113 | cmd_version = buf.getInt(); | |
114 | } | |
115 | } | |
116 | ||
117 | public class sessiond_enable_handler implements SessiondResponse, SessiondCommand { | |
118 | private final static int SIZE = 4; | |
119 | public String name; | |
a15440fd DG |
120 | public int lttngLogLevel; |
121 | public int lttngLogLevelType; | |
43e5396b DG |
122 | |
123 | /** Return status code to the session daemon. */ | |
124 | public lttng_jul_ret_code code; | |
125 | ||
126 | @Override | |
127 | public void populate(byte[] data) { | |
a15440fd DG |
128 | int data_offset = INT_SIZE * 2; |
129 | ||
43e5396b DG |
130 | ByteBuffer buf = ByteBuffer.wrap(data); |
131 | buf.order(ByteOrder.LITTLE_ENDIAN); | |
a15440fd DG |
132 | lttngLogLevel = buf.getInt(); |
133 | lttngLogLevelType = buf.getInt(); | |
134 | name = new String(data, data_offset, data.length - data_offset); | |
43e5396b DG |
135 | } |
136 | ||
137 | @Override | |
138 | public byte[] getBytes() { | |
139 | byte data[] = new byte[SIZE]; | |
140 | ByteBuffer buf = ByteBuffer.wrap(data); | |
141 | buf.order(ByteOrder.BIG_ENDIAN); | |
142 | buf.putInt(code.getCode()); | |
143 | return data; | |
144 | } | |
145 | ||
5b5ffa03 DG |
146 | /* |
147 | * Enable a logger meaning add our handler to it using an exiting | |
148 | * event. If successful, the logger is added to the given enabled | |
149 | * Loggers hashmap. | |
150 | * | |
151 | * @return 0 if NO logger is found else 1 if added. | |
152 | */ | |
153 | public int enableLogger(LTTngLogHandler handler, LTTngEvent event, | |
154 | HashMap enabledLoggers) { | |
3c3a0129 | 155 | int ret; |
5b5ffa03 DG |
156 | Logger logger; |
157 | ||
158 | logger = handler.logManager.getLogger(event.name); | |
159 | if (logger == null) { | |
160 | return 0; | |
161 | } | |
162 | ||
3c3a0129 DG |
163 | ret = handler.setEvent(event); |
164 | if (ret == 0) { | |
165 | /* Newly created event, add the handler. */ | |
166 | logger.addHandler(handler); | |
167 | enabledLoggers.put(event.name, logger); | |
168 | } | |
5b5ffa03 DG |
169 | |
170 | return 1; | |
171 | } | |
172 | ||
43e5396b DG |
173 | /** |
174 | * Execute enable handler action which is to enable the given handler | |
175 | * to the received name. | |
176 | * | |
177 | * @return Event name as a string if the event is NOT found thus was | |
178 | * not enabled. | |
179 | */ | |
5b5ffa03 DG |
180 | public LTTngEvent execute(LTTngLogHandler handler, HashMap enabledLoggers) { |
181 | int ret; | |
43e5396b | 182 | Logger logger; |
3c3a0129 | 183 | LTTngEvent event = null; |
43e5396b DG |
184 | |
185 | if (name == null) { | |
186 | this.code = lttng_jul_ret_code.CODE_INVALID_CMD; | |
187 | return null; | |
188 | } | |
189 | ||
190 | /* Wild card to enable ALL logger. */ | |
191 | if (name.trim().equals("*")) { | |
192 | String loggerName; | |
193 | Enumeration loggers = handler.logManager.getLoggerNames(); | |
5b5ffa03 DG |
194 | |
195 | /* | |
196 | * Keep the loglevel value for all events in case an event | |
197 | * appears later on. | |
198 | */ | |
3c3a0129 DG |
199 | if (lttngLogLevel != -1) { |
200 | handler.logLevelUseAll = 1; | |
201 | handler.logLevelsAll.add(new LTTngLogLevel(lttngLogLevel, | |
202 | lttngLogLevelType)); | |
203 | } | |
5b5ffa03 | 204 | |
43e5396b DG |
205 | while (loggers.hasMoreElements()) { |
206 | loggerName = loggers.nextElement().toString(); | |
207 | /* Somehow there is always an empty string at the end. */ | |
208 | if (loggerName == "") { | |
209 | continue; | |
210 | } | |
211 | ||
5b5ffa03 DG |
212 | /* |
213 | * Create new event object and set it in the log handler so | |
214 | * we can process the record entry with the right | |
215 | * attributes like the loglevels. | |
216 | */ | |
3c3a0129 DG |
217 | event = new LTTngEvent(loggerName, 0, 0); |
218 | /* Clean up loglevel and merge the the ones from all events. */ | |
219 | event.logLevels.clear(); | |
220 | event.logLevels.addAll(handler.logLevelsAll); | |
5b5ffa03 | 221 | enableLogger(handler, event, enabledLoggers); |
43e5396b DG |
222 | } |
223 | this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD; | |
5b5ffa03 | 224 | |
3c3a0129 DG |
225 | /* |
226 | * Only return an event if this is a newly created event | |
227 | * meaning the loglevel is valid. | |
228 | */ | |
229 | if (lttngLogLevel != -1) { | |
230 | event = new LTTngEvent("*", lttngLogLevel, lttngLogLevelType); | |
231 | } | |
5b5ffa03 | 232 | return event; |
43e5396b DG |
233 | } |
234 | ||
235 | this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD; | |
5b5ffa03 DG |
236 | |
237 | /* | |
238 | * Create new event object and set it in the log handler so we can | |
239 | * process the record entry with the right attributes like the | |
240 | * loglevels. | |
241 | */ | |
242 | event = new LTTngEvent(name.trim(), lttngLogLevel, | |
243 | lttngLogLevelType); | |
244 | ret = enableLogger(handler, event, enabledLoggers); | |
245 | if (ret == 1) { | |
43e5396b | 246 | return null; |
43e5396b | 247 | } |
5b5ffa03 | 248 | return event; |
43e5396b DG |
249 | } |
250 | } | |
251 | ||
252 | public class sessiond_disable_handler implements SessiondResponse, SessiondCommand { | |
253 | private final static int SIZE = 4; | |
254 | public String name; | |
255 | ||
256 | /** Return status code to the session daemon. */ | |
257 | public lttng_jul_ret_code code; | |
258 | ||
259 | @Override | |
260 | public void populate(byte[] data) { | |
261 | ByteBuffer buf = ByteBuffer.wrap(data); | |
262 | buf.order(ByteOrder.BIG_ENDIAN); | |
263 | name = new String(data, 0, data.length); | |
264 | } | |
265 | ||
266 | @Override | |
267 | public byte[] getBytes() { | |
268 | byte data[] = new byte[SIZE]; | |
269 | ByteBuffer buf = ByteBuffer.wrap(data); | |
270 | buf.order(ByteOrder.BIG_ENDIAN); | |
271 | buf.putInt(code.getCode()); | |
272 | return data; | |
273 | } | |
274 | ||
275 | /** | |
276 | * Execute disable handler action which is to disable the given handler | |
277 | * to the received name. | |
278 | */ | |
279 | public void execute(LTTngLogHandler handler) { | |
280 | Logger logger; | |
281 | ||
282 | if (name == null) { | |
283 | this.code = lttng_jul_ret_code.CODE_INVALID_CMD; | |
284 | return; | |
285 | } | |
286 | ||
287 | /* Wild card to disable ALL logger. */ | |
288 | if (name.trim().equals("*")) { | |
289 | String loggerName; | |
290 | Enumeration loggers = handler.logManager.getLoggerNames(); | |
291 | while (loggers.hasMoreElements()) { | |
292 | loggerName = loggers.nextElement().toString(); | |
293 | /* Somehow there is always an empty string at the end. */ | |
294 | if (loggerName == "") { | |
295 | continue; | |
296 | } | |
297 | ||
298 | logger = handler.logManager.getLogger(loggerName); | |
299 | logger.removeHandler(handler); | |
300 | } | |
301 | this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD; | |
302 | return; | |
303 | } | |
304 | ||
305 | logger = handler.logManager.getLogger(name.trim()); | |
306 | if (logger == null) { | |
307 | this.code = lttng_jul_ret_code.CODE_UNK_LOGGER_NAME; | |
308 | } else { | |
309 | logger.removeHandler(handler); | |
310 | this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD; | |
311 | } | |
312 | } | |
313 | } | |
314 | ||
315 | public class sessiond_list_logger implements SessiondResponse { | |
316 | private final static int SIZE = 12; | |
317 | ||
318 | private int data_size = 0; | |
319 | private int nb_logger = 0; | |
320 | ||
321 | List<String> logger_list = new ArrayList<String>(); | |
322 | ||
323 | /** Return status code to the session daemon. */ | |
324 | public lttng_jul_ret_code code; | |
325 | ||
326 | @Override | |
327 | public byte[] getBytes() { | |
328 | byte data[] = new byte[SIZE + data_size]; | |
329 | ByteBuffer buf = ByteBuffer.wrap(data); | |
330 | buf.order(ByteOrder.BIG_ENDIAN); | |
331 | ||
332 | /* Returned code */ | |
333 | buf.putInt(code.getCode()); | |
334 | buf.putInt(data_size); | |
335 | buf.putInt(nb_logger); | |
336 | ||
337 | for (String logger: logger_list) { | |
338 | buf.put(logger.getBytes()); | |
339 | /* NULL terminated byte after the logger name. */ | |
340 | buf.put((byte) 0x0); | |
341 | } | |
342 | return data; | |
343 | } | |
344 | ||
345 | /** | |
346 | * Execute enable handler action which is to enable the given handler | |
347 | * to the received name. | |
348 | */ | |
349 | public void execute(LTTngLogHandler handler) { | |
350 | String loggerName; | |
351 | ||
352 | Enumeration loggers = handler.logManager.getLoggerNames(); | |
353 | while (loggers.hasMoreElements()) { | |
354 | loggerName = loggers.nextElement().toString(); | |
355 | /* Somehow there is always an empty string at the end. */ | |
356 | if (loggerName == "") { | |
357 | continue; | |
358 | } | |
359 | ||
360 | this.logger_list.add(loggerName); | |
361 | this.nb_logger++; | |
362 | this.data_size += loggerName.length() + 1; | |
363 | } | |
364 | ||
365 | this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD; | |
366 | } | |
367 | } | |
368 | } |