| 1 | /* |
| 2 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or modify it |
| 5 | * under the terms of the GNU Lesser General Public License, version 2.1 only, |
| 6 | * as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This library is distributed in the hope that it will be useful, but WITHOUT |
| 9 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 10 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 11 | * for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU Lesser General Public License |
| 14 | * along with this library; if not, write to the Free Software Foundation, |
| 15 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
| 16 | */ |
| 17 | |
| 18 | package org.lttng.ust.jul; |
| 19 | |
| 20 | import java.lang.String; |
| 21 | import java.util.logging.Handler; |
| 22 | import java.util.logging.LogRecord; |
| 23 | import java.util.logging.LogManager; |
| 24 | import java.util.logging.Level; |
| 25 | import java.util.HashMap; |
| 26 | |
| 27 | import org.lttng.ust.jul.LTTngUst; |
| 28 | |
| 29 | /* Note: This is taken from the LTTng tools ABI. */ |
| 30 | class LTTngLogLevelABI { |
| 31 | /* Loglevel type. */ |
| 32 | public static final int LOGLEVEL_TYPE_ALL = 0; |
| 33 | public static final int LOGLEVEL_TYPE_RANGE = 1; |
| 34 | public static final int LOGLEVEL_TYPE_SINGLE = 2; |
| 35 | } |
| 36 | |
| 37 | public class LTTngLogHandler extends Handler { |
| 38 | /* |
| 39 | * Indicate if the enable all event has been seen and if yes logger that we |
| 40 | * enabled should use the loglevel/type below. |
| 41 | */ |
| 42 | public int logLevelUseAll = 0; |
| 43 | public int logLevelAll = 0; |
| 44 | public int logLevelTypeAll; |
| 45 | |
| 46 | /* Am I a root Log Handler. */ |
| 47 | public int is_root = 0; |
| 48 | |
| 49 | public LogManager logManager; |
| 50 | |
| 51 | /* Indexed by name and corresponding LTTngEvent. */ |
| 52 | private HashMap<String, LTTngEvent> eventMap = |
| 53 | new HashMap<String, LTTngEvent>(); |
| 54 | |
| 55 | public LTTngLogHandler(LogManager logManager) { |
| 56 | super(); |
| 57 | |
| 58 | this.logManager = logManager; |
| 59 | |
| 60 | /* Initialize LTTng UST tracer. */ |
| 61 | LTTngUst.init(); |
| 62 | } |
| 63 | |
| 64 | public void setEvent(LTTngEvent event) { |
| 65 | eventMap.put(event.name, event); |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void close() throws SecurityException {} |
| 70 | |
| 71 | @Override |
| 72 | public void flush() {} |
| 73 | |
| 74 | @Override |
| 75 | public void publish(LogRecord record) { |
| 76 | int fire_tp = 0, rec_log_level, ev_type, ev_log_level; |
| 77 | LTTngEvent event; |
| 78 | LTTngLogLevel lttngLogLevel; |
| 79 | String logger_name = record.getLoggerName(); |
| 80 | |
| 81 | /* Get back the event if any and check for loglevel. */ |
| 82 | event = eventMap.get(logger_name); |
| 83 | if (event != null) { |
| 84 | rec_log_level = record.getLevel().intValue(); |
| 85 | ev_log_level = event.logLevel.level; |
| 86 | ev_type = event.logLevel.type; |
| 87 | |
| 88 | switch (ev_type) { |
| 89 | case LTTngLogLevelABI.LOGLEVEL_TYPE_RANGE: |
| 90 | if (ev_log_level <= rec_log_level) { |
| 91 | fire_tp = 1; |
| 92 | } |
| 93 | break; |
| 94 | case LTTngLogLevelABI.LOGLEVEL_TYPE_SINGLE: |
| 95 | if (ev_log_level == rec_log_level) { |
| 96 | fire_tp = 1; |
| 97 | } |
| 98 | break; |
| 99 | case LTTngLogLevelABI.LOGLEVEL_TYPE_ALL: |
| 100 | fire_tp = 1; |
| 101 | break; |
| 102 | } |
| 103 | } else { |
| 104 | /* No loglevel attached thus fire tracepoint. */ |
| 105 | fire_tp = 1; |
| 106 | } |
| 107 | |
| 108 | if (fire_tp == 0) { |
| 109 | return; |
| 110 | } |
| 111 | |
| 112 | /* |
| 113 | * Specific tracepoing designed for JUL events. The source class of the |
| 114 | * caller is used for the event name, the raw message is taken, the |
| 115 | * loglevel of the record and the thread ID. |
| 116 | */ |
| 117 | if (this.is_root == 1) { |
| 118 | LTTngUst.tracepointS(record.getMessage(), |
| 119 | record.getLoggerName(), record.getSourceClassName(), |
| 120 | record.getSourceMethodName(), record.getMillis(), |
| 121 | record.getLevel().intValue(), record.getThreadID()); |
| 122 | } else { |
| 123 | LTTngUst.tracepointU(record.getMessage(), |
| 124 | record.getLoggerName(), record.getSourceClassName(), |
| 125 | record.getSourceMethodName(), record.getMillis(), |
| 126 | record.getLevel().intValue(), record.getThreadID()); |
| 127 | } |
| 128 | } |
| 129 | } |