From 3c3a0129d36e41b4c007ab008f603813efbb66ac Mon Sep 17 00:00:00 2001 From: David Goulet Date: Thu, 27 Feb 2014 14:51:55 -0500 Subject: [PATCH] Fix: JUL support multiple loglevels Fixes #744 Signed-off-by: David Goulet Signed-off-by: Mathieu Desnoyers --- .../org/lttng/ust/jul/LTTngEvent.java | 11 +++- .../org/lttng/ust/jul/LTTngLogHandler.java | 66 +++++++++++++------ .../lttng/ust/jul/LTTngSessiondCmd2_4.java | 38 +++++++---- .../lttng/ust/jul/LTTngTCPSessiondClient.java | 7 +- 4 files changed, 82 insertions(+), 40 deletions(-) diff --git a/liblttng-ust-jul/org/lttng/ust/jul/LTTngEvent.java b/liblttng-ust-jul/org/lttng/ust/jul/LTTngEvent.java index a519f449..6d81922e 100644 --- a/liblttng-ust-jul/org/lttng/ust/jul/LTTngEvent.java +++ b/liblttng-ust-jul/org/lttng/ust/jul/LTTngEvent.java @@ -17,6 +17,7 @@ package org.lttng.ust.jul; +import java.util.ArrayList; import java.lang.String; import org.lttng.ust.jul.LTTngUst; @@ -26,7 +27,7 @@ class LTTngLogLevel { public int level; public int type; - public LTTngLogLevel(String event_name, int level, int type) { + public LTTngLogLevel(int level, int type) { this.type = type; this.level = level; } @@ -35,10 +36,14 @@ class LTTngLogLevel { public class LTTngEvent { /* Name of the event. */ public String name; - public LTTngLogLevel logLevel; + public ArrayList logLevels = new ArrayList(); + + public void addLogLevel(int loglevel, int loglevel_type) { + this.logLevels.add(new LTTngLogLevel(loglevel, loglevel_type)); + } public LTTngEvent(String name, int loglevel, int loglevel_type) { this.name = name; - this.logLevel = new LTTngLogLevel(name, loglevel, loglevel_type); + this.addLogLevel(loglevel, loglevel_type); } } diff --git a/liblttng-ust-jul/org/lttng/ust/jul/LTTngLogHandler.java b/liblttng-ust-jul/org/lttng/ust/jul/LTTngLogHandler.java index 1ad11712..b4e1c088 100644 --- a/liblttng-ust-jul/org/lttng/ust/jul/LTTngLogHandler.java +++ b/liblttng-ust-jul/org/lttng/ust/jul/LTTngLogHandler.java @@ -22,6 +22,7 @@ import java.util.logging.Handler; import java.util.logging.LogRecord; import java.util.logging.LogManager; import java.util.logging.Level; +import java.util.ArrayList; import java.util.HashMap; import org.lttng.ust.jul.LTTngUst; @@ -40,8 +41,8 @@ public class LTTngLogHandler extends Handler { * enabled should use the loglevel/type below. */ public int logLevelUseAll = 0; - public int logLevelAll = 0; - public int logLevelTypeAll; + public ArrayList logLevelsAll = + new ArrayList(); /* Am I a root Log Handler. */ public int is_root = 0; @@ -61,8 +62,25 @@ public class LTTngLogHandler extends Handler { LTTngUst.init(); } - public void setEvent(LTTngEvent event) { - eventMap.put(event.name, event); + /** + * Add event to handler hash map if new. + * + * @return 0 if it did not exist else 1. + */ + public int setEvent(LTTngEvent new_event) { + LTTngEvent event; + + event = eventMap.get(new_event.name); + if (event == null) { + eventMap.put(new_event.name, new_event); + /* Did not exists. */ + return 0; + } else { + /* Add new event loglevel to existing event. */ + event.logLevels.addAll(new_event.logLevels); + /* Already exists. */ + return 1; + } } @Override @@ -81,24 +99,34 @@ public class LTTngLogHandler extends Handler { /* Get back the event if any and check for loglevel. */ event = eventMap.get(logger_name); if (event != null) { - rec_log_level = record.getLevel().intValue(); - ev_log_level = event.logLevel.level; - ev_type = event.logLevel.type; - - switch (ev_type) { - case LTTngLogLevelABI.LOGLEVEL_TYPE_RANGE: - if (ev_log_level <= rec_log_level) { + for (LTTngLogLevel ev_log : event.logLevels) { + /* Get record and event log level. */ + rec_log_level = record.getLevel().intValue(); + ev_log_level = ev_log.level; + + switch (ev_log.type) { + case LTTngLogLevelABI.LOGLEVEL_TYPE_RANGE: + if (ev_log_level <= rec_log_level) { + fire_tp = 1; + } + break; + case LTTngLogLevelABI.LOGLEVEL_TYPE_SINGLE: + if (ev_log_level == rec_log_level) { + fire_tp = 1; + } + break; + case LTTngLogLevelABI.LOGLEVEL_TYPE_ALL: fire_tp = 1; + break; } - break; - case LTTngLogLevelABI.LOGLEVEL_TYPE_SINGLE: - if (ev_log_level == rec_log_level) { - fire_tp = 1; + + /* + * If we match, stop right now else continue to the next + * loglevel contained in the event. + */ + if (fire_tp == 1) { + break; } - break; - case LTTngLogLevelABI.LOGLEVEL_TYPE_ALL: - fire_tp = 1; - break; } } else { /* No loglevel attached thus fire tracepoint. */ diff --git a/liblttng-ust-jul/org/lttng/ust/jul/LTTngSessiondCmd2_4.java b/liblttng-ust-jul/org/lttng/ust/jul/LTTngSessiondCmd2_4.java index 4cb5a731..9b544be5 100644 --- a/liblttng-ust-jul/org/lttng/ust/jul/LTTngSessiondCmd2_4.java +++ b/liblttng-ust-jul/org/lttng/ust/jul/LTTngSessiondCmd2_4.java @@ -152,6 +152,7 @@ public interface LTTngSessiondCmd2_4 { */ public int enableLogger(LTTngLogHandler handler, LTTngEvent event, HashMap enabledLoggers) { + int ret; Logger logger; logger = handler.logManager.getLogger(event.name); @@ -159,9 +160,12 @@ public interface LTTngSessiondCmd2_4 { return 0; } - handler.setEvent(event); - logger.addHandler(handler); - enabledLoggers.put(event.name, logger); + ret = handler.setEvent(event); + if (ret == 0) { + /* Newly created event, add the handler. */ + logger.addHandler(handler); + enabledLoggers.put(event.name, logger); + } return 1; } @@ -176,7 +180,7 @@ public interface LTTngSessiondCmd2_4 { public LTTngEvent execute(LTTngLogHandler handler, HashMap enabledLoggers) { int ret; Logger logger; - LTTngEvent event; + LTTngEvent event = null; if (name == null) { this.code = lttng_jul_ret_code.CODE_INVALID_CMD; @@ -192,9 +196,11 @@ public interface LTTngSessiondCmd2_4 { * Keep the loglevel value for all events in case an event * appears later on. */ - handler.logLevelUseAll = 1; - handler.logLevelAll = lttngLogLevel; - handler.logLevelTypeAll = lttngLogLevelType; + if (lttngLogLevel != -1) { + handler.logLevelUseAll = 1; + handler.logLevelsAll.add(new LTTngLogLevel(lttngLogLevel, + lttngLogLevelType)); + } while (loggers.hasMoreElements()) { loggerName = loggers.nextElement().toString(); @@ -203,22 +209,26 @@ public interface LTTngSessiondCmd2_4 { continue; } - if (enabledLoggers.get(loggerName) != null) { - continue; - } - /* * Create new event object and set it in the log handler so * we can process the record entry with the right * attributes like the loglevels. */ - event = new LTTngEvent(loggerName, lttngLogLevel, - lttngLogLevelType); + event = new LTTngEvent(loggerName, 0, 0); + /* Clean up loglevel and merge the the ones from all events. */ + event.logLevels.clear(); + event.logLevels.addAll(handler.logLevelsAll); enableLogger(handler, event, enabledLoggers); } this.code = lttng_jul_ret_code.CODE_SUCCESS_CMD; - event = new LTTngEvent("*", lttngLogLevel, lttngLogLevelType); + /* + * Only return an event if this is a newly created event + * meaning the loglevel is valid. + */ + if (lttngLogLevel != -1) { + event = new LTTngEvent("*", lttngLogLevel, lttngLogLevelType); + } return event; } diff --git a/liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java b/liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java index 594e735a..f3ac80dc 100644 --- a/liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java +++ b/liblttng-ust-jul/org/lttng/ust/jul/LTTngTCPSessiondClient.java @@ -133,8 +133,7 @@ public class LTTngTCPSessiondClient { */ if (handler.logLevelUseAll == 1) { it.remove(); - event.logLevel.level = handler.logLevelAll; - event.logLevel.type = handler.logLevelTypeAll; + event.logLevels.addAll(handler.logLevelsAll); modifiedEvents.add(event); } @@ -144,8 +143,8 @@ public class LTTngTCPSessiondClient { */ if (event.name.equals("*")) { enableCmd.name = event.name; - enableCmd.lttngLogLevel = event.logLevel.level; - enableCmd.lttngLogLevelType = event.logLevel.type; + /* Tell the command NOT to add the loglevel. */ + enableCmd.lttngLogLevel = -1; /* * The return value is irrelevant since the * event is * always kept in the set. -- 2.34.1