| 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.logging.Logger; |
| 26 | import java.util.Collections; |
| 27 | import java.util.HashMap; |
| 28 | import java.util.Map; |
| 29 | |
| 30 | import org.lttng.ust.jul.LTTngUst; |
| 31 | |
| 32 | class LTTngLogger { |
| 33 | /* |
| 34 | * The log handler is attached to the logger when the reference count is |
| 35 | * nonzero. Each event referring to a logger holds a reference to that |
| 36 | * logger. If down to 0, this object is removed from the handler. |
| 37 | */ |
| 38 | public int refcount; |
| 39 | public String name; |
| 40 | Logger logger; |
| 41 | |
| 42 | public LTTngLogger(String name, Logger logger) { |
| 43 | this.name = name; |
| 44 | this.refcount = 0; |
| 45 | this.logger = logger; |
| 46 | } |
| 47 | |
| 48 | public void attach(LTTngLogHandler handler) { |
| 49 | this.logger.addHandler(handler); |
| 50 | } |
| 51 | |
| 52 | public void detach(LTTngLogHandler handler) { |
| 53 | this.logger.removeHandler(handler); |
| 54 | } |
| 55 | } |
| 56 | |
| 57 | public class LTTngLogHandler extends Handler { |
| 58 | /* Am I a root Log Handler. */ |
| 59 | public int is_root = 0; |
| 60 | |
| 61 | public LogManager logManager; |
| 62 | |
| 63 | /* Logger object attached to this handler that can trigger a tracepoint. */ |
| 64 | private Map<String, LTTngLogger> loggerMap = |
| 65 | Collections.synchronizedMap(new HashMap<String, LTTngLogger>()); |
| 66 | |
| 67 | /* Constructor */ |
| 68 | public LTTngLogHandler(LogManager logManager) { |
| 69 | super(); |
| 70 | |
| 71 | this.logManager = logManager; |
| 72 | |
| 73 | /* Initialize LTTng UST tracer. */ |
| 74 | LTTngUst.init(); |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Return true if the logger is enabled and attached. Else, if not found, |
| 79 | * return false. |
| 80 | */ |
| 81 | public boolean exists(String name) { |
| 82 | if (loggerMap.get(name) != null) { |
| 83 | return true; |
| 84 | } else { |
| 85 | return false; |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Attach an event to this handler. If no logger object exists, one is |
| 91 | * created else the refcount is incremented. |
| 92 | */ |
| 93 | public void attachEvent(LTTngEvent event) { |
| 94 | Logger logger; |
| 95 | LTTngLogger lttngLogger; |
| 96 | |
| 97 | /* Does the logger actually exist. */ |
| 98 | logger = this.logManager.getLogger(event.name); |
| 99 | if (logger == null) { |
| 100 | /* Stop attach right now. */ |
| 101 | return; |
| 102 | } |
| 103 | |
| 104 | lttngLogger = loggerMap.get(event.name); |
| 105 | if (lttngLogger == null) { |
| 106 | lttngLogger = new LTTngLogger(event.name, logger); |
| 107 | |
| 108 | /* Attach the handler to the logger and add is to the map. */ |
| 109 | lttngLogger.attach(this); |
| 110 | lttngLogger.refcount = 1; |
| 111 | loggerMap.put(lttngLogger.name, lttngLogger); |
| 112 | } else { |
| 113 | lttngLogger.refcount += 1; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | /* |
| 118 | * Dettach an event from this handler. If the refcount goes down to 0, the |
| 119 | * logger object is removed thus not attached anymore. |
| 120 | */ |
| 121 | public void detachEvent(LTTngEvent event) { |
| 122 | LTTngLogger logger; |
| 123 | |
| 124 | logger = loggerMap.get(event.name); |
| 125 | if (logger != null) { |
| 126 | logger.refcount -= 1; |
| 127 | if (logger.refcount == 0) { |
| 128 | /* Dettach from handler since no more event is associated. */ |
| 129 | logger.detach(this); |
| 130 | loggerMap.remove(logger); |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | /* |
| 136 | * Cleanup this handler state meaning put it back to a vanilla state. |
| 137 | */ |
| 138 | public void clear() { |
| 139 | this.loggerMap.clear(); |
| 140 | } |
| 141 | |
| 142 | @Override |
| 143 | public void close() throws SecurityException {} |
| 144 | |
| 145 | @Override |
| 146 | public void flush() {} |
| 147 | |
| 148 | @Override |
| 149 | public void publish(LogRecord record) { |
| 150 | LTTngLogger logger; |
| 151 | |
| 152 | logger = loggerMap.get(record.getLoggerName()); |
| 153 | if (logger == null) { |
| 154 | /* Ignore and don't fire TP. */ |
| 155 | return; |
| 156 | } |
| 157 | |
| 158 | /* |
| 159 | * Specific tracepoing designed for JUL events. The source class of the |
| 160 | * caller is used for the event name, the raw message is taken, the |
| 161 | * loglevel of the record and the thread ID. |
| 162 | */ |
| 163 | if (this.is_root == 1) { |
| 164 | LTTngUst.tracepointS(record.getMessage(), |
| 165 | record.getLoggerName(), record.getSourceClassName(), |
| 166 | record.getSourceMethodName(), record.getMillis(), |
| 167 | record.getLevel().intValue(), record.getThreadID()); |
| 168 | } else { |
| 169 | LTTngUst.tracepointU(record.getMessage(), |
| 170 | record.getLoggerName(), record.getSourceClassName(), |
| 171 | record.getSourceMethodName(), record.getMillis(), |
| 172 | record.getLevel().intValue(), record.getThreadID()); |
| 173 | } |
| 174 | } |
| 175 | } |