JUL: fix enable all event for 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;
23import java.util.logging.Logger;
24import java.util.ArrayList;
529e6def 25import java.util.HashMap;
43e5396b
DG
26import java.util.List;
27import java.util.Enumeration;
28
29public interface LTTngSessiondCmd2_4 {
30 /**
31 * Maximum name length for a logger name to be send to sessiond.
32 */
33 final static int NAME_MAX = 255;
34
35 public interface SessiondResponse {
36 /**
37 * Gets a byte array of the command so that it may be streamed
38 *
39 * @return the byte array of the command
40 */
41 public byte[] getBytes();
42 }
43
44 public interface SessiondCommand {
45 /**
46 * Populate the class from a byte array
47 *
48 * @param data
49 * the byte array containing the streamed command
50 */
51 public void populate(byte[] data);
52 }
53
54 public enum lttng_jul_command {
55 /** List logger(s). */
56 CMD_LIST(1),
57 /** Enable logger by name. */
58 CMD_ENABLE(2),
59 /** Disable logger by name. */
60 CMD_DISABLE(3);
61 private int code;
62
63 private lttng_jul_command(int c) {
64 code = c;
65 }
66
67 public int getCommand() {
68 return code;
69 }
70 }
71
72 enum lttng_jul_ret_code {
73 CODE_SUCCESS_CMD(1),
74 CODE_INVALID_CMD(2),
75 CODE_UNK_LOGGER_NAME(3);
76 private int code;
77
78 private lttng_jul_ret_code(int c) {
79 code = c;
80 }
81
82 public int getCode() {
83 return code;
84 }
85 }
86
87 public class sessiond_hdr implements SessiondCommand {
88 /** ABI size of command header. */
89 public final static int SIZE = 16;
90 /** Payload size in bytes following this header. */
91 public long data_size;
92 /** Command type. */
93 public lttng_jul_command cmd;
94 /** Command version. */
95 public int cmd_version;
96
97 public void populate(byte[] data) {
98 ByteBuffer buf = ByteBuffer.wrap(data);
99 buf.order(ByteOrder.BIG_ENDIAN);
100
101 data_size = buf.getLong();
102 cmd = lttng_jul_command.values()[buf.getInt() - 1];
103 cmd_version = buf.getInt();
104 }
105 }
106
107 public class sessiond_enable_handler implements SessiondResponse, SessiondCommand {
108 private final static int SIZE = 4;
109 public String name;
110
111 /** Return status code to the session daemon. */
112 public lttng_jul_ret_code code;
113
114 @Override
115 public void populate(byte[] data) {
116 ByteBuffer buf = ByteBuffer.wrap(data);
117 buf.order(ByteOrder.LITTLE_ENDIAN);
118 name = new String(data, 0, data.length);
119 }
120
121 @Override
122 public byte[] getBytes() {
123 byte data[] = new byte[SIZE];
124 ByteBuffer buf = ByteBuffer.wrap(data);
125 buf.order(ByteOrder.BIG_ENDIAN);
126 buf.putInt(code.getCode());
127 return data;
128 }
129
130 /**
131 * Execute enable handler action which is to enable the given handler
132 * to the received name.
133 *
134 * @return Event name as a string if the event is NOT found thus was
135 * not enabled.
136 */
529e6def 137 public String execute(LTTngLogHandler handler, HashMap enabledLoggers) {
43e5396b
DG
138 Logger logger;
139
140 if (name == null) {
141 this.code = lttng_jul_ret_code.CODE_INVALID_CMD;
142 return null;
143 }
144
145 /* Wild card to enable ALL logger. */
146 if (name.trim().equals("*")) {
147 String loggerName;
148 Enumeration loggers = handler.logManager.getLoggerNames();
149 while (loggers.hasMoreElements()) {
150 loggerName = loggers.nextElement().toString();
151 /* Somehow there is always an empty string at the end. */
152 if (loggerName == "") {
153 continue;
154 }
155
529e6def
DG
156 if (enabledLoggers.get(loggerName) != null) {
157 continue;
158 }
159
43e5396b
DG
160 logger = handler.logManager.getLogger(loggerName);
161 logger.addHandler(handler);
529e6def 162 enabledLoggers.put(loggerName, logger);
43e5396b
DG
163 }
164 this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
529e6def
DG
165 /*
166 * Return the name as a new string so we can add the * event
167 * name to the event list that we need to enable for new
168 * Logger.
169 */
170 return new String(name);
43e5396b
DG
171 }
172
173 this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
174 logger = handler.logManager.getLogger(name.trim());
175 if (logger != null) {
176 logger.addHandler(handler);
529e6def 177 enabledLoggers.put(name.trim(), logger);
43e5396b
DG
178 return null;
179 } else {
180 return new String(name);
181 }
182 }
183 }
184
185 public class sessiond_disable_handler implements SessiondResponse, SessiondCommand {
186 private final static int SIZE = 4;
187 public String name;
188
189 /** Return status code to the session daemon. */
190 public lttng_jul_ret_code code;
191
192 @Override
193 public void populate(byte[] data) {
194 ByteBuffer buf = ByteBuffer.wrap(data);
195 buf.order(ByteOrder.BIG_ENDIAN);
196 name = new String(data, 0, data.length);
197 }
198
199 @Override
200 public byte[] getBytes() {
201 byte data[] = new byte[SIZE];
202 ByteBuffer buf = ByteBuffer.wrap(data);
203 buf.order(ByteOrder.BIG_ENDIAN);
204 buf.putInt(code.getCode());
205 return data;
206 }
207
208 /**
209 * Execute disable handler action which is to disable the given handler
210 * to the received name.
211 */
212 public void execute(LTTngLogHandler handler) {
213 Logger logger;
214
215 if (name == null) {
216 this.code = lttng_jul_ret_code.CODE_INVALID_CMD;
217 return;
218 }
219
220 /* Wild card to disable ALL logger. */
221 if (name.trim().equals("*")) {
222 String loggerName;
223 Enumeration loggers = handler.logManager.getLoggerNames();
224 while (loggers.hasMoreElements()) {
225 loggerName = loggers.nextElement().toString();
226 /* Somehow there is always an empty string at the end. */
227 if (loggerName == "") {
228 continue;
229 }
230
231 logger = handler.logManager.getLogger(loggerName);
232 logger.removeHandler(handler);
233 }
234 this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
235 return;
236 }
237
238 logger = handler.logManager.getLogger(name.trim());
239 if (logger == null) {
240 this.code = lttng_jul_ret_code.CODE_UNK_LOGGER_NAME;
241 } else {
242 logger.removeHandler(handler);
243 this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
244 }
245 }
246 }
247
248 public class sessiond_list_logger implements SessiondResponse {
249 private final static int SIZE = 12;
250
251 private int data_size = 0;
252 private int nb_logger = 0;
253
254 List<String> logger_list = new ArrayList<String>();
255
256 /** Return status code to the session daemon. */
257 public lttng_jul_ret_code code;
258
259 @Override
260 public byte[] getBytes() {
261 byte data[] = new byte[SIZE + data_size];
262 ByteBuffer buf = ByteBuffer.wrap(data);
263 buf.order(ByteOrder.BIG_ENDIAN);
264
265 /* Returned code */
266 buf.putInt(code.getCode());
267 buf.putInt(data_size);
268 buf.putInt(nb_logger);
269
270 for (String logger: logger_list) {
271 buf.put(logger.getBytes());
272 /* NULL terminated byte after the logger name. */
273 buf.put((byte) 0x0);
274 }
275 return data;
276 }
277
278 /**
279 * Execute enable handler action which is to enable the given handler
280 * to the received name.
281 */
282 public void execute(LTTngLogHandler handler) {
283 String loggerName;
284
285 Enumeration loggers = handler.logManager.getLoggerNames();
286 while (loggers.hasMoreElements()) {
287 loggerName = loggers.nextElement().toString();
288 /* Somehow there is always an empty string at the end. */
289 if (loggerName == "") {
290 continue;
291 }
292
293 this.logger_list.add(loggerName);
294 this.nb_logger++;
295 this.data_size += loggerName.length() + 1;
296 }
297
298 this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
299 }
300 }
301}
This page took 0.034823 seconds and 4 git commands to generate.