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