Commit | Line | Data |
---|---|---|
501f6777 CB |
1 | /* |
2 | * Copyright (C) 2013 - David Goulet <dgoulet@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; | |
19 | ||
501f6777 | 20 | import java.io.IOException; |
bc7de6d9 | 21 | import java.lang.reflect.InvocationTargetException; |
501f6777 CB |
22 | import java.util.concurrent.Semaphore; |
23 | import java.util.concurrent.TimeUnit; | |
501f6777 | 24 | |
5bfeaeca AM |
25 | /** |
26 | * The central agent managing the JUL and Log4j handlers. | |
27 | * | |
28 | * @author David Goulet | |
29 | */ | |
501f6777 | 30 | public class LTTngAgent { |
08284556 | 31 | |
501f6777 CB |
32 | /* Domains */ |
33 | static enum Domain { | |
34 | JUL(3), LOG4J(4); | |
35 | private int value; | |
36 | ||
37 | private Domain(int value) { | |
38 | this.value = value; | |
39 | } | |
40 | ||
41 | public int value() { | |
42 | return value; | |
43 | } | |
44 | } | |
45 | ||
08284556 AM |
46 | private static final int SEM_TIMEOUT = 3; /* Seconds */ |
47 | ||
501f6777 CB |
48 | private static LogFramework julUser; |
49 | private static LogFramework julRoot; | |
50 | private static LogFramework log4jUser; | |
51 | private static LogFramework log4jRoot; | |
52 | ||
53 | /* Sessiond clients */ | |
54 | private static LTTngTCPSessiondClient julUserClient; | |
55 | private static LTTngTCPSessiondClient julRootClient; | |
56 | private static LTTngTCPSessiondClient log4jUserClient; | |
57 | private static LTTngTCPSessiondClient log4jRootClient; | |
58 | ||
59 | private static Thread sessiondThreadJULUser; | |
60 | private static Thread sessiondThreadJULRoot; | |
61 | private static Thread sessiondThreadLog4jUser; | |
62 | private static Thread sessiondThreadLog4jRoot; | |
63 | ||
64 | private boolean useJUL = false; | |
65 | private boolean useLog4j = false; | |
66 | ||
67 | /* Singleton agent object */ | |
68 | private static LTTngAgent curAgent = null; | |
69 | ||
70 | /* Indicate if this object has been initialized. */ | |
71 | private static boolean initialized = false; | |
72 | ||
73 | private static Semaphore registerSem; | |
501f6777 CB |
74 | |
75 | /* | |
76 | * Constructor is private. This is a singleton and a reference should be | |
77 | * acquired using getLTTngAgent(). | |
78 | */ | |
bc7de6d9 | 79 | private LTTngAgent() { |
501f6777 CB |
80 | initAgentJULClasses(); |
81 | ||
82 | /* Since Log4j is a 3rd party JAR, we need to check if we can load any of its classes */ | |
83 | Boolean log4jLoaded = loadLog4jClasses(); | |
84 | if (log4jLoaded) { | |
85 | initAgentLog4jClasses(); | |
86 | } | |
87 | ||
bc7de6d9 | 88 | registerSem = new Semaphore(0, true); |
501f6777 CB |
89 | } |
90 | ||
bc7de6d9 | 91 | private static Boolean loadLog4jClasses() { |
17be0b58 CB |
92 | Class<?> logging; |
93 | ||
501f6777 | 94 | try { |
9a9a2d37 | 95 | logging = loadClass("org.apache.log4j.spi.LoggingEvent"); |
501f6777 CB |
96 | } catch (ClassNotFoundException e) { |
97 | /* Log4j classes not found, no need to create the relevant objects */ | |
17be0b58 CB |
98 | return false; |
99 | } | |
100 | ||
101 | /* | |
102 | * Detect capabilities of the log4j library. We only | |
103 | * support log4j >= 1.2.15. The getTimeStamp() method | |
104 | * was introduced in log4j 1.2.15, so verify that it | |
105 | * is available. | |
106 | * | |
107 | * We can't rely on the getPackage().getImplementationVersion() | |
108 | * call that would retrieves information from the manifest file | |
109 | * found in the JAR since the manifest file shipped | |
110 | * from upstream is known to be broken in several | |
111 | * versions of the library. | |
112 | * | |
113 | * More info: | |
114 | * https://issues.apache.org/bugzilla/show_bug.cgi?id=44370 | |
115 | */ | |
116 | ||
117 | try { | |
118 | logging.getDeclaredMethod("getTimeStamp"); | |
119 | } catch (NoSuchMethodException e) { | |
0bc36e5d | 120 | System.err.println("Warning: The loaded log4j library is too old. Log4j tracing with LTTng will be disabled."); |
17be0b58 CB |
121 | return false; |
122 | } catch (NullPointerException e) { | |
123 | /* Should never happen */ | |
124 | return false; | |
125 | } catch (SecurityException e) { | |
126 | return false; | |
501f6777 CB |
127 | } |
128 | ||
17be0b58 | 129 | return true; |
501f6777 CB |
130 | } |
131 | ||
bc7de6d9 | 132 | private static Class<?> loadClass(String className) throws ClassNotFoundException { |
9a9a2d37 JG |
133 | ClassLoader loader; |
134 | Class<?> loadedClass; | |
135 | ||
136 | try { | |
137 | /* Try to load class using the current thread's context class loader */ | |
138 | loader = Thread.currentThread().getContextClassLoader(); | |
139 | loadedClass = loader.loadClass(className); | |
140 | } catch (ClassNotFoundException e) { | |
141 | /* Loading failed, try using the system class loader */ | |
142 | loader = ClassLoader.getSystemClassLoader(); | |
143 | loadedClass = loader.loadClass(className); | |
144 | } | |
145 | ||
146 | return loadedClass; | |
147 | } | |
148 | ||
501f6777 CB |
149 | private void initAgentJULClasses() { |
150 | try { | |
9a9a2d37 | 151 | Class<?> lttngJUL = loadClass("org.lttng.ust.agent.jul.LTTngJUL"); |
08284556 AM |
152 | julUser = (LogFramework) lttngJUL.getDeclaredConstructor(new Class[] { Boolean.class }).newInstance(false); |
153 | julRoot = (LogFramework) lttngJUL.getDeclaredConstructor(new Class[] { Boolean.class }).newInstance(true); | |
501f6777 CB |
154 | this.useJUL = true; |
155 | } catch (ClassNotFoundException e) { | |
156 | /* LTTng JUL classes not found, no need to create the relevant objects */ | |
157 | this.useJUL = false; | |
158 | } catch (InstantiationException e) { | |
159 | this.useJUL = false; | |
160 | } catch (NoSuchMethodException e) { | |
161 | this.useJUL = false; | |
162 | } catch (IllegalAccessException e) { | |
163 | this.useJUL = false; | |
164 | } catch (InvocationTargetException e) { | |
165 | this.useJUL = false; | |
166 | } | |
167 | } | |
168 | ||
169 | private void initAgentLog4jClasses() { | |
170 | try { | |
9a9a2d37 | 171 | Class<?> lttngLog4j = loadClass("org.lttng.ust.agent.log4j.LTTngLog4j"); |
bc7de6d9 AM |
172 | log4jUser = (LogFramework)lttngLog4j.getDeclaredConstructor(new Class[] {Boolean.class}).newInstance(false); |
173 | log4jRoot = (LogFramework)lttngLog4j.getDeclaredConstructor(new Class[] {Boolean.class}).newInstance(true); | |
501f6777 CB |
174 | this.useLog4j = true; |
175 | } catch (ClassNotFoundException e) { | |
176 | /* LTTng Log4j classes not found, no need to create the relevant objects */ | |
177 | this.useLog4j = false; | |
178 | } catch (InstantiationException e) { | |
179 | this.useLog4j = false; | |
180 | } catch (NoSuchMethodException e) { | |
181 | this.useLog4j = false; | |
182 | } catch (IllegalAccessException e) { | |
183 | this.useLog4j = false; | |
184 | } catch (InvocationTargetException e) { | |
185 | this.useLog4j = false; | |
186 | } | |
187 | } | |
188 | ||
5bfeaeca | 189 | /** |
501f6777 | 190 | * Public getter to acquire a reference to this singleton object. |
5bfeaeca AM |
191 | * |
192 | * @return The agent instance | |
193 | * @throws IOException | |
501f6777 CB |
194 | */ |
195 | public static synchronized LTTngAgent getLTTngAgent() throws IOException { | |
196 | if (curAgent == null) { | |
197 | curAgent = new LTTngAgent(); | |
198 | curAgent.init(); | |
199 | } | |
200 | ||
201 | return curAgent; | |
202 | } | |
203 | ||
bc7de6d9 AM |
204 | private synchronized void init() throws SecurityException { |
205 | if (initialized) { | |
501f6777 CB |
206 | return; |
207 | } | |
208 | ||
209 | Integer numJULThreads = 0; | |
210 | Integer numLog4jThreads = 0; | |
211 | ||
212 | if (this.useJUL) { | |
213 | numJULThreads = initJULClientThreads(); | |
214 | } | |
215 | ||
216 | if (this.useLog4j) { | |
217 | numLog4jThreads = initLog4jClientThreads(); | |
218 | } | |
219 | ||
220 | Integer numThreads = numJULThreads + numLog4jThreads; | |
221 | ||
222 | /* Wait for each registration to end. */ | |
223 | try { | |
bc7de6d9 | 224 | registerSem.tryAcquire(numThreads, |
08284556 | 225 | SEM_TIMEOUT, |
501f6777 CB |
226 | TimeUnit.SECONDS); |
227 | } catch (InterruptedException e) { | |
228 | e.printStackTrace(); | |
229 | } | |
230 | ||
bc7de6d9 | 231 | initialized = true; |
501f6777 CB |
232 | } |
233 | ||
bc7de6d9 | 234 | private synchronized static Integer initJULClientThreads() { |
501f6777 CB |
235 | Integer numThreads = 2; |
236 | ||
237 | /* Handle user session daemon if any. */ | |
bc7de6d9 AM |
238 | julUserClient = new LTTngTCPSessiondClient(Domain.JUL, |
239 | julUser, | |
240 | registerSem); | |
501f6777 CB |
241 | |
242 | String userThreadName = "LTTng UST agent JUL user thread"; | |
bc7de6d9 AM |
243 | sessiondThreadJULUser = new Thread(julUserClient, userThreadName); |
244 | sessiondThreadJULUser.setDaemon(true); | |
245 | sessiondThreadJULUser.start(); | |
501f6777 CB |
246 | |
247 | /* Handle root session daemon. */ | |
bc7de6d9 AM |
248 | julRootClient = new LTTngTCPSessiondClient(Domain.JUL, |
249 | julRoot, | |
250 | registerSem); | |
501f6777 CB |
251 | |
252 | String rootThreadName = "LTTng UST agent JUL root thread"; | |
bc7de6d9 AM |
253 | sessiondThreadJULRoot = new Thread(julRootClient, rootThreadName); |
254 | sessiondThreadJULRoot.setDaemon(true); | |
255 | sessiondThreadJULRoot.start(); | |
501f6777 CB |
256 | |
257 | return numThreads; | |
258 | } | |
259 | ||
bc7de6d9 | 260 | private synchronized static Integer initLog4jClientThreads() { |
501f6777 CB |
261 | Integer numThreads = 2; |
262 | ||
bc7de6d9 AM |
263 | log4jUserClient = new LTTngTCPSessiondClient(Domain.LOG4J, |
264 | log4jUser, | |
265 | registerSem); | |
501f6777 CB |
266 | |
267 | String userThreadName = "LTTng UST agent Log4j user thread"; | |
bc7de6d9 AM |
268 | sessiondThreadLog4jUser = new Thread(log4jUserClient, userThreadName); |
269 | sessiondThreadLog4jUser.setDaemon(true); | |
270 | sessiondThreadLog4jUser.start(); | |
501f6777 | 271 | |
bc7de6d9 AM |
272 | log4jRootClient = new LTTngTCPSessiondClient(Domain.LOG4J, |
273 | log4jRoot, | |
274 | registerSem); | |
501f6777 CB |
275 | |
276 | String rootThreadName = "LTTng UST agent Log4j root thread"; | |
bc7de6d9 AM |
277 | sessiondThreadLog4jRoot = new Thread(log4jRootClient,rootThreadName); |
278 | sessiondThreadLog4jRoot.setDaemon(true); | |
279 | sessiondThreadLog4jRoot.start(); | |
501f6777 CB |
280 | |
281 | return numThreads; | |
282 | } | |
283 | ||
5bfeaeca AM |
284 | /** |
285 | * Dispose the agent. Applications should call this once they are done | |
286 | * logging. | |
287 | */ | |
288 | public void dispose() { | |
501f6777 | 289 | if (this.useJUL) { |
bc7de6d9 AM |
290 | julUserClient.destroy(); |
291 | julRootClient.destroy(); | |
292 | julUser.reset(); | |
293 | julRoot.reset(); | |
501f6777 CB |
294 | } |
295 | ||
296 | if (this.useLog4j) { | |
bc7de6d9 AM |
297 | log4jUserClient.destroy(); |
298 | log4jRootClient.destroy(); | |
299 | log4jUser.reset(); | |
300 | log4jRoot.reset(); | |
501f6777 CB |
301 | } |
302 | ||
303 | try { | |
304 | if (this.useJUL) { | |
bc7de6d9 AM |
305 | sessiondThreadJULUser.join(); |
306 | sessiondThreadJULRoot.join(); | |
501f6777 CB |
307 | } |
308 | ||
309 | if (this.useLog4j) { | |
bc7de6d9 AM |
310 | sessiondThreadLog4jUser.join(); |
311 | sessiondThreadLog4jRoot.join(); | |
501f6777 CB |
312 | } |
313 | ||
314 | } catch (InterruptedException e) { | |
315 | e.printStackTrace(); | |
316 | } | |
317 | } | |
318 | } |