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