Fix: handle registration done command
[lttng-ust.git] / liblttng-ust-jul / org / lttng / ust / jul / LTTngSessiondCmd2_4.java
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;
23 import java.lang.Object;
24 import java.util.logging.Logger;
25 import java.util.ArrayList;
26 import java.util.HashMap;
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
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
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. */
67 CMD_DISABLE(3),
68 /** Registration done */
69 CMD_REG_DONE(4);
70
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;
120 public int lttngLogLevel;
121 public int lttngLogLevelType;
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) {
128 int data_offset = INT_SIZE * 2;
129
130 ByteBuffer buf = ByteBuffer.wrap(data);
131 buf.order(ByteOrder.LITTLE_ENDIAN);
132 lttngLogLevel = buf.getInt();
133 lttngLogLevelType = buf.getInt();
134 name = new String(data, data_offset, data.length - data_offset);
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
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) {
155 Logger logger;
156
157 logger = handler.logManager.getLogger(event.name);
158 if (logger == null) {
159 return 0;
160 }
161
162 handler.setEvent(event);
163 logger.addHandler(handler);
164 enabledLoggers.put(event.name, logger);
165
166 return 1;
167 }
168
169 /**
170 * Execute enable handler action which is to enable the given handler
171 * to the received name.
172 *
173 * @return Event name as a string if the event is NOT found thus was
174 * not enabled.
175 */
176 public LTTngEvent execute(LTTngLogHandler handler, HashMap enabledLoggers) {
177 int ret;
178 Logger logger;
179 LTTngEvent event;
180
181 if (name == null) {
182 this.code = lttng_jul_ret_code.CODE_INVALID_CMD;
183 return null;
184 }
185
186 /* Wild card to enable ALL logger. */
187 if (name.trim().equals("*")) {
188 String loggerName;
189 Enumeration loggers = handler.logManager.getLoggerNames();
190
191 /*
192 * Keep the loglevel value for all events in case an event
193 * appears later on.
194 */
195 handler.logLevelUseAll = 1;
196 handler.logLevelAll = lttngLogLevel;
197 handler.logLevelTypeAll = lttngLogLevelType;
198
199 while (loggers.hasMoreElements()) {
200 loggerName = loggers.nextElement().toString();
201 /* Somehow there is always an empty string at the end. */
202 if (loggerName == "") {
203 continue;
204 }
205
206 if (enabledLoggers.get(loggerName) != null) {
207 continue;
208 }
209
210 /*
211 * Create new event object and set it in the log handler so
212 * we can process the record entry with the right
213 * attributes like the loglevels.
214 */
215 event = new LTTngEvent(loggerName, lttngLogLevel,
216 lttngLogLevelType);
217 enableLogger(handler, event, enabledLoggers);
218 }
219 this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
220
221 event = new LTTngEvent("*", lttngLogLevel, lttngLogLevelType);
222 return event;
223 }
224
225 this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
226
227 /*
228 * Create new event object and set it in the log handler so we can
229 * process the record entry with the right attributes like the
230 * loglevels.
231 */
232 event = new LTTngEvent(name.trim(), lttngLogLevel,
233 lttngLogLevelType);
234 ret = enableLogger(handler, event, enabledLoggers);
235 if (ret == 1) {
236 return null;
237 }
238 return event;
239 }
240 }
241
242 public class sessiond_disable_handler implements SessiondResponse, SessiondCommand {
243 private final static int SIZE = 4;
244 public String name;
245
246 /** Return status code to the session daemon. */
247 public lttng_jul_ret_code code;
248
249 @Override
250 public void populate(byte[] data) {
251 ByteBuffer buf = ByteBuffer.wrap(data);
252 buf.order(ByteOrder.BIG_ENDIAN);
253 name = new String(data, 0, data.length);
254 }
255
256 @Override
257 public byte[] getBytes() {
258 byte data[] = new byte[SIZE];
259 ByteBuffer buf = ByteBuffer.wrap(data);
260 buf.order(ByteOrder.BIG_ENDIAN);
261 buf.putInt(code.getCode());
262 return data;
263 }
264
265 /**
266 * Execute disable handler action which is to disable the given handler
267 * to the received name.
268 */
269 public void execute(LTTngLogHandler handler) {
270 Logger logger;
271
272 if (name == null) {
273 this.code = lttng_jul_ret_code.CODE_INVALID_CMD;
274 return;
275 }
276
277 /* Wild card to disable ALL logger. */
278 if (name.trim().equals("*")) {
279 String loggerName;
280 Enumeration loggers = handler.logManager.getLoggerNames();
281 while (loggers.hasMoreElements()) {
282 loggerName = loggers.nextElement().toString();
283 /* Somehow there is always an empty string at the end. */
284 if (loggerName == "") {
285 continue;
286 }
287
288 logger = handler.logManager.getLogger(loggerName);
289 logger.removeHandler(handler);
290 }
291 this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
292 return;
293 }
294
295 logger = handler.logManager.getLogger(name.trim());
296 if (logger == null) {
297 this.code = lttng_jul_ret_code.CODE_UNK_LOGGER_NAME;
298 } else {
299 logger.removeHandler(handler);
300 this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
301 }
302 }
303 }
304
305 public class sessiond_list_logger implements SessiondResponse {
306 private final static int SIZE = 12;
307
308 private int data_size = 0;
309 private int nb_logger = 0;
310
311 List<String> logger_list = new ArrayList<String>();
312
313 /** Return status code to the session daemon. */
314 public lttng_jul_ret_code code;
315
316 @Override
317 public byte[] getBytes() {
318 byte data[] = new byte[SIZE + data_size];
319 ByteBuffer buf = ByteBuffer.wrap(data);
320 buf.order(ByteOrder.BIG_ENDIAN);
321
322 /* Returned code */
323 buf.putInt(code.getCode());
324 buf.putInt(data_size);
325 buf.putInt(nb_logger);
326
327 for (String logger: logger_list) {
328 buf.put(logger.getBytes());
329 /* NULL terminated byte after the logger name. */
330 buf.put((byte) 0x0);
331 }
332 return data;
333 }
334
335 /**
336 * Execute enable handler action which is to enable the given handler
337 * to the received name.
338 */
339 public void execute(LTTngLogHandler handler) {
340 String loggerName;
341
342 Enumeration loggers = handler.logManager.getLoggerNames();
343 while (loggers.hasMoreElements()) {
344 loggerName = loggers.nextElement().toString();
345 /* Somehow there is always an empty string at the end. */
346 if (loggerName == "") {
347 continue;
348 }
349
350 this.logger_list.add(loggerName);
351 this.nb_logger++;
352 this.data_size += loggerName.length() + 1;
353 }
354
355 this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
356 }
357 }
358 }
This page took 0.038012 seconds and 4 git commands to generate.