JUL: use root logger to capture events
[lttng-ust.git] / liblttng-ust-jul / org / lttng / ust / jul / LTTngSessiondCmd2_4.java
index eab7e5d9c1752dce3b35e36da48c7c67f07214af..1a65f131be23e9c5ac15c1c67e2820b6a8dfd8b4 100644 (file)
@@ -20,9 +20,9 @@ package org.lttng.ust.jul;
 
 import java.nio.ByteBuffer;
 import java.nio.ByteOrder;
+import java.lang.Object;
 import java.util.logging.Logger;
 import java.util.ArrayList;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Enumeration;
 
@@ -32,6 +32,12 @@ public interface LTTngSessiondCmd2_4 {
         */
        final static int NAME_MAX = 255;
 
+       /*
+        * Size of a primitive type int in byte. Because you know, Java can't
+        * provide that since it does not makes sense...
+        */
+       final static int INT_SIZE = 4;
+
        public interface SessiondResponse {
                /**
                 * Gets a byte array of the command so that it may be streamed
@@ -57,7 +63,10 @@ public interface LTTngSessiondCmd2_4 {
                /** Enable logger by name. */
                CMD_ENABLE(2),
                /** Disable logger by name. */
-               CMD_DISABLE(3);
+               CMD_DISABLE(3),
+               /** Registration done */
+               CMD_REG_DONE(4);
+
                private int code;
 
                private lttng_jul_command(int c) {
@@ -107,15 +116,21 @@ public interface LTTngSessiondCmd2_4 {
        public class sessiond_enable_handler implements SessiondResponse, SessiondCommand {
                private final static int SIZE = 4;
                public String name;
+               public int lttngLogLevel;
+               public int lttngLogLevelType;
 
                /** Return status code to the session daemon. */
                public lttng_jul_ret_code code;
 
                @Override
                public void populate(byte[] data) {
+                       int data_offset = INT_SIZE * 2;
+
                        ByteBuffer buf = ByteBuffer.wrap(data);
                        buf.order(ByteOrder.LITTLE_ENDIAN);
-                       name = new String(data, 0, data.length);
+                       lttngLogLevel = buf.getInt();
+                       lttngLogLevelType = buf.getInt();
+                       name = new String(data, data_offset, data.length - data_offset).trim();
                }
 
                @Override
@@ -134,57 +149,45 @@ public interface LTTngSessiondCmd2_4 {
                 * @return Event name as a string if the event is NOT found thus was
                 * not enabled.
                 */
-               public String execute(LTTngLogHandler handler, HashMap enabledLoggers) {
-                       Logger logger;
+               public void execute(LTTngLogHandler handler) {
+                       LTTngEvent event;
 
-                       if (name == null) {
+                       if (this.name == null) {
                                this.code = lttng_jul_ret_code.CODE_INVALID_CMD;
-                               return null;
+                               return;
                        }
 
-                       /* Wild card to enable ALL logger. */
-                       if (name.trim().equals("*")) {
-                               String loggerName;
-                               Enumeration loggers = handler.logManager.getLoggerNames();
-                               while (loggers.hasMoreElements()) {
-                                       loggerName = loggers.nextElement().toString();
-                                       /* Somehow there is always an empty string at the end. */
-                                       if (loggerName == "") {
-                                               continue;
-                                       }
-
-                                       if (enabledLoggers.get(loggerName) != null) {
-                                               continue;
-                                       }
-
-                                       logger = handler.logManager.getLogger(loggerName);
-                                       logger.addHandler(handler);
-                                       enabledLoggers.put(loggerName, logger);
-                               }
+                       /* Add event to the enabled events hash map. */
+                       event = handler.enabledEvents.put(this.name,
+                                       new LTTngEvent(this.name, 0, 0));
+                       if (event != null) {
+                               /* The event exists so skip updating the refcount. */
                                this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
-                               /*
-                                * Return the name as a new string so we can add the * event
-                                * name to the event list that we need to enable for new
-                                * Logger.
-                                */
-                               return new String(name);
+                               return;
                        }
 
-                       this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
-                       logger = handler.logManager.getLogger(name.trim());
-                       if (logger != null) {
-                               logger.addHandler(handler);
-                               enabledLoggers.put(name.trim(), logger);
-                               return null;
-                       } else {
-                               return new String(name);
+                       /*
+                        * Get the root logger and attach to it if it's the first enable
+                        * seen by the handler.
+                        */
+                       Logger rootLogger = handler.logManager.getLogger("");
+
+                       handler.refcount++;
+                       if (handler.refcount == 1) {
+                               /* Add handler only if it's the first enable. */
+                               rootLogger.addHandler(handler);
                        }
+
+                       this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
+                       return;
                }
        }
 
        public class sessiond_disable_handler implements SessiondResponse, SessiondCommand {
                private final static int SIZE = 4;
                public String name;
+               public int lttngLogLevel;
+               public int lttngLogLevelType;
 
                /** Return status code to the session daemon. */
                public lttng_jul_ret_code code;
@@ -192,8 +195,8 @@ public interface LTTngSessiondCmd2_4 {
                @Override
                public void populate(byte[] data) {
                        ByteBuffer buf = ByteBuffer.wrap(data);
-                       buf.order(ByteOrder.BIG_ENDIAN);
-                       name = new String(data, 0, data.length);
+                       buf.order(ByteOrder.LITTLE_ENDIAN);
+                       name = new String(data).trim();
                }
 
                @Override
@@ -210,38 +213,34 @@ public interface LTTngSessiondCmd2_4 {
                 * to the received name.
                 */
                public void execute(LTTngLogHandler handler) {
-                       Logger logger;
+                       LTTngEvent event;
 
-                       if (name == null) {
+                       if (this.name == null) {
                                this.code = lttng_jul_ret_code.CODE_INVALID_CMD;
                                return;
                        }
 
-                       /* Wild card to disable ALL logger. */
-                       if (name.trim().equals("*")) {
-                               String loggerName;
-                               Enumeration loggers = handler.logManager.getLoggerNames();
-                               while (loggers.hasMoreElements()) {
-                                       loggerName = loggers.nextElement().toString();
-                                       /* Somehow there is always an empty string at the end. */
-                                       if (loggerName == "") {
-                                               continue;
-                                       }
-
-                                       logger = handler.logManager.getLogger(loggerName);
-                                       logger.removeHandler(handler);
-                               }
-                               this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
+                       /*
+                        * Try to remove the logger name from the events map and if we
+                        * can't, just skip the refcount update since the event was never
+                        * enabled.
+                        */
+                       event = handler.enabledEvents.remove(this.name);
+                       if (event == null) {
+                               /* The event didn't exists so skip updating the refcount. */
+                               this.code = lttng_jul_ret_code.CODE_INVALID_CMD;
                                return;
                        }
 
-                       logger = handler.logManager.getLogger(name.trim());
-                       if (logger == null) {
-                               this.code = lttng_jul_ret_code.CODE_UNK_LOGGER_NAME;
-                       } else {
-                               logger.removeHandler(handler);
-                               this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
+                       Logger rootLogger = handler.logManager.getLogger("");
+
+                       handler.refcount--;
+                       if (handler.refcount == 0) {
+                               rootLogger.removeHandler(handler);
                        }
+
+                       this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD;
+                       return;
                }
        }
 
This page took 0.025871 seconds and 4 git commands to generate.