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