Fix: add LTTngEvent class to fix delayed logger
[lttng-ust.git] / liblttng-ust-jul / org / lttng / ust / jul / LTTngSessiondCmd2_4.java
CommitLineData
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
19package org.lttng.ust.jul;
20
21import java.nio.ByteBuffer;
22import java.nio.ByteOrder;
a15440fd 23import java.lang.Object;
43e5396b
DG
24import java.util.logging.Logger;
25import java.util.ArrayList;
529e6def 26import java.util.HashMap;
43e5396b
DG
27import java.util.List;
28import java.util.Enumeration;
29
30public 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. */
67 CMD_DISABLE(3);
68 private int code;
69
70 private lttng_jul_command(int c) {
71 code = c;
72 }
73
74 public int getCommand() {
75 return code;
76 }
77 }
78
79 enum lttng_jul_ret_code {
80 CODE_SUCCESS_CMD(1),
81 CODE_INVALID_CMD(2),
82 CODE_UNK_LOGGER_NAME(3);
83 private int code;
84
85 private lttng_jul_ret_code(int c) {
86 code = c;
87 }
88
89 public int getCode() {
90 return code;
91 }
92 }
93
94 public class sessiond_hdr implements SessiondCommand {
95 /** ABI size of command header. */
96 public final static int SIZE = 16;
97 /** Payload size in bytes following this header. */
98 public long data_size;
99 /** Command type. */
100 public lttng_jul_command cmd;
101 /** Command version. */
102 public int cmd_version;
103
104 public void populate(byte[] data) {
105 ByteBuffer buf = ByteBuffer.wrap(data);
106 buf.order(ByteOrder.BIG_ENDIAN);
107
108 data_size = buf.getLong();
109 cmd = lttng_jul_command.values()[buf.getInt() - 1];
110 cmd_version = buf.getInt();
111 }
112 }
113
114 public class sessiond_enable_handler implements SessiondResponse, SessiondCommand {
115 private final static int SIZE = 4;
116 public String name;
a15440fd
DG
117 public int lttngLogLevel;
118 public int lttngLogLevelType;
43e5396b
DG
119
120 /** Return status code to the session daemon. */
121 public lttng_jul_ret_code code;
122
123 @Override
124 public void populate(byte[] data) {
a15440fd
DG
125 int data_offset = INT_SIZE * 2;
126
43e5396b
DG
127 ByteBuffer buf = ByteBuffer.wrap(data);
128 buf.order(ByteOrder.LITTLE_ENDIAN);
a15440fd
DG
129 lttngLogLevel = buf.getInt();
130 lttngLogLevelType = buf.getInt();
131 name = new String(data, data_offset, data.length - data_offset);
43e5396b
DG
132 }
133
134 @Override
135 public byte[] getBytes() {
136 byte data[] = new byte[SIZE];
137 ByteBuffer buf = ByteBuffer.wrap(data);
138 buf.order(ByteOrder.BIG_ENDIAN);
139 buf.putInt(code.getCode());
140 return data;
141 }
142
5b5ffa03
DG
143 /*
144 * Enable a logger meaning add our handler to it using an exiting
145 * event. If successful, the logger is added to the given enabled
146 * Loggers hashmap.
147 *
148 * @return 0 if NO logger is found else 1 if added.
149 */
150 public int enableLogger(LTTngLogHandler handler, LTTngEvent event,
151 HashMap enabledLoggers) {
152 Logger logger;
153
154 logger = handler.logManager.getLogger(event.name);
155 if (logger == null) {
156 return 0;
157 }
158
159 handler.setEvent(event);
160 logger.addHandler(handler);
161 enabledLoggers.put(event.name, logger);
162
163 return 1;
164 }
165
43e5396b
DG
166 /**
167 * Execute enable handler action which is to enable the given handler
168 * to the received name.
169 *
170 * @return Event name as a string if the event is NOT found thus was
171 * not enabled.
172 */
5b5ffa03
DG
173 public LTTngEvent execute(LTTngLogHandler handler, HashMap enabledLoggers) {
174 int ret;
43e5396b 175 Logger logger;
5b5ffa03 176 LTTngEvent event;
43e5396b
DG
177
178 if (name == null) {
179 this.code = lttng_jul_ret_code.CODE_INVALID_CMD;
180 return null;
181 }
182
183 /* Wild card to enable ALL logger. */
184 if (name.trim().equals("*")) {
185 String loggerName;
186 Enumeration loggers = handler.logManager.getLoggerNames();
5b5ffa03
DG
187
188 /*
189 * Keep the loglevel value for all events in case an event
190 * appears later on.
191 */
192 handler.logLevelUseAll = 1;
193 handler.logLevelAll = lttngLogLevel;
194 handler.logLevelTypeAll = lttngLogLevelType;
195
43e5396b
DG
196 while (loggers.hasMoreElements()) {
197 loggerName = loggers.nextElement().toString();
198 /* Somehow there is always an empty string at the end. */
199 if (loggerName == "") {
200 continue;
201 }
202
529e6def
DG
203 if (enabledLoggers.get(loggerName) != null) {
204 continue;
205 }
206
5b5ffa03
DG
207 /*
208 * Create new event object and set it in the log handler so
209 * we can process the record entry with the right
210 * attributes like the loglevels.
211 */
212 event = new LTTngEvent(loggerName, lttngLogLevel,
a15440fd 213 lttngLogLevelType);
5b5ffa03 214 enableLogger(handler, event, enabledLoggers);
43e5396b
DG
215 }
216 this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
5b5ffa03
DG
217
218 event = new LTTngEvent("*", lttngLogLevel, lttngLogLevelType);
219 return event;
43e5396b
DG
220 }
221
222 this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
5b5ffa03
DG
223
224 /*
225 * Create new event object and set it in the log handler so we can
226 * process the record entry with the right attributes like the
227 * loglevels.
228 */
229 event = new LTTngEvent(name.trim(), lttngLogLevel,
230 lttngLogLevelType);
231 ret = enableLogger(handler, event, enabledLoggers);
232 if (ret == 1) {
43e5396b 233 return null;
43e5396b 234 }
5b5ffa03 235 return event;
43e5396b
DG
236 }
237 }
238
239 public class sessiond_disable_handler implements SessiondResponse, SessiondCommand {
240 private final static int SIZE = 4;
241 public String name;
242
243 /** Return status code to the session daemon. */
244 public lttng_jul_ret_code code;
245
246 @Override
247 public void populate(byte[] data) {
248 ByteBuffer buf = ByteBuffer.wrap(data);
249 buf.order(ByteOrder.BIG_ENDIAN);
250 name = new String(data, 0, data.length);
251 }
252
253 @Override
254 public byte[] getBytes() {
255 byte data[] = new byte[SIZE];
256 ByteBuffer buf = ByteBuffer.wrap(data);
257 buf.order(ByteOrder.BIG_ENDIAN);
258 buf.putInt(code.getCode());
259 return data;
260 }
261
262 /**
263 * Execute disable handler action which is to disable the given handler
264 * to the received name.
265 */
266 public void execute(LTTngLogHandler handler) {
267 Logger logger;
268
269 if (name == null) {
270 this.code = lttng_jul_ret_code.CODE_INVALID_CMD;
271 return;
272 }
273
274 /* Wild card to disable ALL logger. */
275 if (name.trim().equals("*")) {
276 String loggerName;
277 Enumeration loggers = handler.logManager.getLoggerNames();
278 while (loggers.hasMoreElements()) {
279 loggerName = loggers.nextElement().toString();
280 /* Somehow there is always an empty string at the end. */
281 if (loggerName == "") {
282 continue;
283 }
284
285 logger = handler.logManager.getLogger(loggerName);
286 logger.removeHandler(handler);
287 }
288 this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
289 return;
290 }
291
292 logger = handler.logManager.getLogger(name.trim());
293 if (logger == null) {
294 this.code = lttng_jul_ret_code.CODE_UNK_LOGGER_NAME;
295 } else {
296 logger.removeHandler(handler);
297 this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
298 }
299 }
300 }
301
302 public class sessiond_list_logger implements SessiondResponse {
303 private final static int SIZE = 12;
304
305 private int data_size = 0;
306 private int nb_logger = 0;
307
308 List<String> logger_list = new ArrayList<String>();
309
310 /** Return status code to the session daemon. */
311 public lttng_jul_ret_code code;
312
313 @Override
314 public byte[] getBytes() {
315 byte data[] = new byte[SIZE + data_size];
316 ByteBuffer buf = ByteBuffer.wrap(data);
317 buf.order(ByteOrder.BIG_ENDIAN);
318
319 /* Returned code */
320 buf.putInt(code.getCode());
321 buf.putInt(data_size);
322 buf.putInt(nb_logger);
323
324 for (String logger: logger_list) {
325 buf.put(logger.getBytes());
326 /* NULL terminated byte after the logger name. */
327 buf.put((byte) 0x0);
328 }
329 return data;
330 }
331
332 /**
333 * Execute enable handler action which is to enable the given handler
334 * to the received name.
335 */
336 public void execute(LTTngLogHandler handler) {
337 String loggerName;
338
339 Enumeration loggers = handler.logManager.getLoggerNames();
340 while (loggers.hasMoreElements()) {
341 loggerName = loggers.nextElement().toString();
342 /* Somehow there is always an empty string at the end. */
343 if (loggerName == "") {
344 continue;
345 }
346
347 this.logger_list.add(loggerName);
348 this.nb_logger++;
349 this.data_size += loggerName.length() + 1;
350 }
351
352 this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
353 }
354 }
355}
This page took 0.036868 seconds and 4 git commands to generate.