Commit | Line | Data |
---|---|---|
501f6777 CB |
1 | /* |
2 | * Copyright (C) 2014 - Christian Babeux <christian.babeux@efficios.com> | |
3 | * | |
4 | * | |
5 | * This library is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License, version 2.1 only, | |
7 | * as published by the Free Software Foundation. | |
8 | * | |
9 | * This library is distributed in the hope that it will be useful, but WITHOUT | |
10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License | |
12 | * for more details. | |
13 | * | |
14 | * You should have received a copy of the GNU Lesser General Public License | |
15 | * along with this library; if not, write to the Free Software Foundation, | |
16 | * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | |
17 | */ | |
18 | ||
19 | package org.lttng.ust.agent.jul; | |
20 | ||
21 | import java.util.Enumeration; | |
22 | import java.util.Iterator; | |
23 | import java.util.Vector; | |
501f6777 CB |
24 | import java.util.logging.LogManager; |
25 | import java.util.logging.Logger; | |
26 | ||
27 | import org.lttng.ust.agent.LogFrameworkSkeleton; | |
28 | ||
5bfeaeca AM |
29 | /** |
30 | * JUL logging framework | |
31 | * | |
32 | * @author Christian Babeux | |
33 | */ | |
501f6777 CB |
34 | public class LTTngJUL extends LogFrameworkSkeleton { |
35 | ||
36 | private LTTngLogHandler handler; | |
37 | private Boolean attached; | |
38 | ||
5bfeaeca AM |
39 | /** |
40 | * Constructor | |
41 | * | |
42 | * @param isRoot | |
43 | * If this logger is a root logger or not. | |
44 | */ | |
501f6777 CB |
45 | public LTTngJUL(Boolean isRoot) { |
46 | super(); | |
47 | this.handler = new LTTngLogHandler(isRoot); | |
48 | this.attached = false; | |
49 | } | |
50 | ||
51 | @Override | |
52 | public Boolean enableLogger(String name) { | |
53 | if(!super.enableLogger(name)) { | |
54 | return false; | |
55 | } | |
56 | ||
57 | /* The first enable of any event triggers the attachment to the root logger */ | |
58 | if (getEventCount() == 1 && !this.attached) { | |
59 | attachToRootLogger(); | |
60 | } | |
61 | ||
62 | return true; | |
63 | } | |
64 | ||
65 | @Override | |
66 | public Boolean disableLogger(String name) { | |
67 | if(!super.disableLogger(name)) { | |
68 | return false; | |
69 | } | |
70 | ||
71 | /* Detach from the root logger when the event count reach zero */ | |
72 | if (getEventCount() == 0 && this.attached) { | |
73 | detachFromRootLogger(); | |
74 | } | |
75 | ||
76 | return true; | |
77 | } | |
78 | ||
79 | @Override | |
80 | public Iterator<String> listLoggers() { | |
81 | Vector<String> logs = new Vector<String>(); | |
82 | for (Enumeration<String> loggers = LogManager.getLogManager().getLoggerNames(); loggers.hasMoreElements(); ) { | |
83 | String name = loggers.nextElement(); | |
84 | /* Skip the root logger */ | |
85 | if (name.equals("")) { | |
86 | continue; | |
87 | } | |
88 | ||
89 | logs.add(name); | |
90 | } | |
91 | ||
92 | return logs.iterator(); | |
93 | } | |
94 | ||
95 | @Override | |
96 | public Boolean isRoot() { | |
97 | return handler.isRoot(); | |
98 | } | |
99 | ||
100 | @Override | |
101 | public void reset() { | |
102 | super.reset(); | |
103 | detachFromRootLogger(); | |
104 | } | |
105 | ||
106 | private void attachToRootLogger() { | |
107 | if (this.attached) { | |
108 | return; | |
109 | } | |
110 | ||
111 | Logger rootLogger = LogManager.getLogManager().getLogger(""); | |
112 | rootLogger.addHandler(this.handler); | |
113 | this.attached = true; | |
114 | } | |
115 | ||
116 | private void detachFromRootLogger() { | |
117 | if (!this.attached) { | |
118 | return; | |
119 | } | |
120 | ||
121 | Logger rootLogger = LogManager.getLogManager().getLogger(""); | |
122 | rootLogger.removeHandler(this.handler); | |
123 | this.attached = false; | |
124 | } | |
125 | } |