| 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 | 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; |
| 117 | public int lttngLogLevel; |
| 118 | public int lttngLogLevelType; |
| 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) { |
| 125 | int data_offset = INT_SIZE * 2; |
| 126 | |
| 127 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 128 | buf.order(ByteOrder.LITTLE_ENDIAN); |
| 129 | lttngLogLevel = buf.getInt(); |
| 130 | lttngLogLevelType = buf.getInt(); |
| 131 | name = new String(data, data_offset, data.length - data_offset); |
| 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 | |
| 143 | /** |
| 144 | * Execute enable handler action which is to enable the given handler |
| 145 | * to the received name. |
| 146 | * |
| 147 | * @return Event name as a string if the event is NOT found thus was |
| 148 | * not enabled. |
| 149 | */ |
| 150 | public String execute(LTTngLogHandler handler, HashMap enabledLoggers) { |
| 151 | Logger logger; |
| 152 | |
| 153 | if (name == null) { |
| 154 | this.code = lttng_jul_ret_code.CODE_INVALID_CMD; |
| 155 | return null; |
| 156 | } |
| 157 | |
| 158 | /* Wild card to enable ALL logger. */ |
| 159 | if (name.trim().equals("*")) { |
| 160 | String loggerName; |
| 161 | Enumeration loggers = handler.logManager.getLoggerNames(); |
| 162 | while (loggers.hasMoreElements()) { |
| 163 | loggerName = loggers.nextElement().toString(); |
| 164 | /* Somehow there is always an empty string at the end. */ |
| 165 | if (loggerName == "") { |
| 166 | continue; |
| 167 | } |
| 168 | |
| 169 | if (enabledLoggers.get(loggerName) != null) { |
| 170 | continue; |
| 171 | } |
| 172 | |
| 173 | logger = handler.logManager.getLogger(loggerName); |
| 174 | handler.setLogLevel(loggerName, lttngLogLevel, |
| 175 | lttngLogLevelType); |
| 176 | logger.addHandler(handler); |
| 177 | enabledLoggers.put(loggerName, logger); |
| 178 | } |
| 179 | this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD; |
| 180 | /* |
| 181 | * Return the name as a new string so we can add the * event |
| 182 | * name to the event list that we need to enable for new |
| 183 | * Logger. |
| 184 | */ |
| 185 | return new String(name); |
| 186 | } |
| 187 | |
| 188 | this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD; |
| 189 | logger = handler.logManager.getLogger(name.trim()); |
| 190 | if (logger != null) { |
| 191 | handler.setLogLevel(name.trim(), lttngLogLevel, |
| 192 | lttngLogLevelType); |
| 193 | logger.addHandler(handler); |
| 194 | enabledLoggers.put(name.trim(), logger); |
| 195 | return null; |
| 196 | } else { |
| 197 | return new String(name); |
| 198 | } |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | public class sessiond_disable_handler implements SessiondResponse, SessiondCommand { |
| 203 | private final static int SIZE = 4; |
| 204 | public String name; |
| 205 | |
| 206 | /** Return status code to the session daemon. */ |
| 207 | public lttng_jul_ret_code code; |
| 208 | |
| 209 | @Override |
| 210 | public void populate(byte[] data) { |
| 211 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 212 | buf.order(ByteOrder.BIG_ENDIAN); |
| 213 | name = new String(data, 0, data.length); |
| 214 | } |
| 215 | |
| 216 | @Override |
| 217 | public byte[] getBytes() { |
| 218 | byte data[] = new byte[SIZE]; |
| 219 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 220 | buf.order(ByteOrder.BIG_ENDIAN); |
| 221 | buf.putInt(code.getCode()); |
| 222 | return data; |
| 223 | } |
| 224 | |
| 225 | /** |
| 226 | * Execute disable handler action which is to disable the given handler |
| 227 | * to the received name. |
| 228 | */ |
| 229 | public void execute(LTTngLogHandler handler) { |
| 230 | Logger logger; |
| 231 | |
| 232 | if (name == null) { |
| 233 | this.code = lttng_jul_ret_code.CODE_INVALID_CMD; |
| 234 | return; |
| 235 | } |
| 236 | |
| 237 | /* Wild card to disable ALL logger. */ |
| 238 | if (name.trim().equals("*")) { |
| 239 | String loggerName; |
| 240 | Enumeration loggers = handler.logManager.getLoggerNames(); |
| 241 | while (loggers.hasMoreElements()) { |
| 242 | loggerName = loggers.nextElement().toString(); |
| 243 | /* Somehow there is always an empty string at the end. */ |
| 244 | if (loggerName == "") { |
| 245 | continue; |
| 246 | } |
| 247 | |
| 248 | logger = handler.logManager.getLogger(loggerName); |
| 249 | logger.removeHandler(handler); |
| 250 | } |
| 251 | this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD; |
| 252 | return; |
| 253 | } |
| 254 | |
| 255 | logger = handler.logManager.getLogger(name.trim()); |
| 256 | if (logger == null) { |
| 257 | this.code = lttng_jul_ret_code.CODE_UNK_LOGGER_NAME; |
| 258 | } else { |
| 259 | logger.removeHandler(handler); |
| 260 | this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD; |
| 261 | } |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | public class sessiond_list_logger implements SessiondResponse { |
| 266 | private final static int SIZE = 12; |
| 267 | |
| 268 | private int data_size = 0; |
| 269 | private int nb_logger = 0; |
| 270 | |
| 271 | List<String> logger_list = new ArrayList<String>(); |
| 272 | |
| 273 | /** Return status code to the session daemon. */ |
| 274 | public lttng_jul_ret_code code; |
| 275 | |
| 276 | @Override |
| 277 | public byte[] getBytes() { |
| 278 | byte data[] = new byte[SIZE + data_size]; |
| 279 | ByteBuffer buf = ByteBuffer.wrap(data); |
| 280 | buf.order(ByteOrder.BIG_ENDIAN); |
| 281 | |
| 282 | /* Returned code */ |
| 283 | buf.putInt(code.getCode()); |
| 284 | buf.putInt(data_size); |
| 285 | buf.putInt(nb_logger); |
| 286 | |
| 287 | for (String logger: logger_list) { |
| 288 | buf.put(logger.getBytes()); |
| 289 | /* NULL terminated byte after the logger name. */ |
| 290 | buf.put((byte) 0x0); |
| 291 | } |
| 292 | return data; |
| 293 | } |
| 294 | |
| 295 | /** |
| 296 | * Execute enable handler action which is to enable the given handler |
| 297 | * to the received name. |
| 298 | */ |
| 299 | public void execute(LTTngLogHandler handler) { |
| 300 | String loggerName; |
| 301 | |
| 302 | Enumeration loggers = handler.logManager.getLoggerNames(); |
| 303 | while (loggers.hasMoreElements()) { |
| 304 | loggerName = loggers.nextElement().toString(); |
| 305 | /* Somehow there is always an empty string at the end. */ |
| 306 | if (loggerName == "") { |
| 307 | continue; |
| 308 | } |
| 309 | |
| 310 | this.logger_list.add(loggerName); |
| 311 | this.nb_logger++; |
| 312 | this.data_size += loggerName.length() + 1; |
| 313 | } |
| 314 | |
| 315 | this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD; |
| 316 | } |
| 317 | } |
| 318 | } |