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