| 1 | /* |
| 2 | * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@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.agent.jul; |
| 19 | |
| 20 | import java.util.ArrayList; |
| 21 | import java.util.Collection; |
| 22 | import java.util.Collections; |
| 23 | import java.util.List; |
| 24 | import java.util.Set; |
| 25 | import java.util.TreeSet; |
| 26 | import java.util.logging.Handler; |
| 27 | import java.util.logging.LogManager; |
| 28 | import java.util.logging.Logger; |
| 29 | |
| 30 | import org.lttng.ust.agent.AbstractLttngAgent; |
| 31 | |
| 32 | /** |
| 33 | * Agent implementation for tracing from JUL loggers. |
| 34 | * |
| 35 | * @author Alexandre Montplaisir |
| 36 | */ |
| 37 | class LttngJulAgent extends AbstractLttngAgent<LttngLogHandler> { |
| 38 | |
| 39 | private static LttngJulAgent instance = null; |
| 40 | |
| 41 | private LttngJulAgent() { |
| 42 | super(Domain.JUL); |
| 43 | } |
| 44 | |
| 45 | public static synchronized LttngJulAgent getInstance() { |
| 46 | if (instance == null) { |
| 47 | instance = new LttngJulAgent(); |
| 48 | } |
| 49 | return instance; |
| 50 | } |
| 51 | |
| 52 | @Override |
| 53 | public Collection<String> listAvailableEvents() { |
| 54 | Set<String> ret = new TreeSet<String>(); |
| 55 | |
| 56 | List<String> loggersNames = Collections.list(LogManager.getLogManager().getLoggerNames()); |
| 57 | for (String name : loggersNames) { |
| 58 | /* |
| 59 | * Skip the root logger. An empty string is not a valid event name |
| 60 | * in LTTng. |
| 61 | */ |
| 62 | if (name.equals("") || name.equals("global")) { |
| 63 | continue; |
| 64 | } |
| 65 | |
| 66 | /* |
| 67 | * Check if that logger has at least one LTTng JUL handler attached. |
| 68 | */ |
| 69 | Logger logger = Logger.getLogger(name); |
| 70 | if (hasLttngHandlerAttached(logger)) { |
| 71 | ret.add(name); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | return ret; |
| 76 | } |
| 77 | |
| 78 | private static boolean hasLttngHandlerAttached(Logger logger) { |
| 79 | for (Handler handler : logger.getHandlers()) { |
| 80 | if (handler instanceof LttngLogHandler) { |
| 81 | return true; |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | /* |
| 86 | * A parent logger, if any, may be connected to an LTTng handler. In |
| 87 | * this case, we will want to include this child logger in the output, |
| 88 | * since it will be accessible by LTTng. |
| 89 | */ |
| 90 | Logger parent = logger.getParent(); |
| 91 | if (parent != null) { |
| 92 | return hasLttngHandlerAttached(parent); |
| 93 | } |
| 94 | |
| 95 | /* |
| 96 | * We have reached the root logger and have not found any LTTng handler, |
| 97 | * this event will not be accessible. |
| 98 | */ |
| 99 | return false; |
| 100 | } |
| 101 | |
| 102 | } |