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; | |
24 | ||
25 | import java.util.logging.LogManager; | |
26 | import java.util.logging.Logger; | |
27 | ||
28 | import org.lttng.ust.agent.LogFrameworkSkeleton; | |
29 | ||
30 | public class LTTngJUL extends LogFrameworkSkeleton { | |
31 | ||
32 | private LTTngLogHandler handler; | |
33 | private Boolean attached; | |
34 | ||
35 | public LTTngJUL(Boolean isRoot) { | |
36 | super(); | |
37 | this.handler = new LTTngLogHandler(isRoot); | |
38 | this.attached = false; | |
39 | } | |
40 | ||
41 | @Override | |
42 | public Boolean enableLogger(String name) { | |
43 | if(!super.enableLogger(name)) { | |
44 | return false; | |
45 | } | |
46 | ||
47 | /* The first enable of any event triggers the attachment to the root logger */ | |
48 | if (getEventCount() == 1 && !this.attached) { | |
49 | attachToRootLogger(); | |
50 | } | |
51 | ||
52 | return true; | |
53 | } | |
54 | ||
55 | @Override | |
56 | public Boolean disableLogger(String name) { | |
57 | if(!super.disableLogger(name)) { | |
58 | return false; | |
59 | } | |
60 | ||
61 | /* Detach from the root logger when the event count reach zero */ | |
62 | if (getEventCount() == 0 && this.attached) { | |
63 | detachFromRootLogger(); | |
64 | } | |
65 | ||
66 | return true; | |
67 | } | |
68 | ||
69 | @Override | |
70 | public Iterator<String> listLoggers() { | |
71 | Vector<String> logs = new Vector<String>(); | |
72 | for (Enumeration<String> loggers = LogManager.getLogManager().getLoggerNames(); loggers.hasMoreElements(); ) { | |
73 | String name = loggers.nextElement(); | |
74 | /* Skip the root logger */ | |
75 | if (name.equals("")) { | |
76 | continue; | |
77 | } | |
78 | ||
79 | logs.add(name); | |
80 | } | |
81 | ||
82 | return logs.iterator(); | |
83 | } | |
84 | ||
85 | @Override | |
86 | public Boolean isRoot() { | |
87 | return handler.isRoot(); | |
88 | } | |
89 | ||
90 | @Override | |
91 | public void reset() { | |
92 | super.reset(); | |
93 | detachFromRootLogger(); | |
94 | } | |
95 | ||
96 | private void attachToRootLogger() { | |
97 | if (this.attached) { | |
98 | return; | |
99 | } | |
100 | ||
101 | Logger rootLogger = LogManager.getLogManager().getLogger(""); | |
102 | rootLogger.addHandler(this.handler); | |
103 | this.attached = true; | |
104 | } | |
105 | ||
106 | private void detachFromRootLogger() { | |
107 | if (!this.attached) { | |
108 | return; | |
109 | } | |
110 | ||
111 | Logger rootLogger = LogManager.getLogManager().getLogger(""); | |
112 | rootLogger.removeHandler(this.handler); | |
113 | this.attached = false; | |
114 | } | |
115 | } |