Commit | Line | Data |
---|---|---|
d60dfbe4 AM |
1 | /* |
2 | * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com> | |
3 | * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com> | |
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.io.IOException; | |
22 | import java.util.concurrent.atomic.AtomicLong; | |
23 | import java.util.logging.Handler; | |
24 | import java.util.logging.LogRecord; | |
25 | ||
26 | import org.lttng.ust.agent.ILttngAgent; | |
27 | import org.lttng.ust.agent.ILttngHandler; | |
28 | ||
29 | /** | |
30 | * LTTng-UST JUL log handler. | |
31 | * | |
32 | * Applications can attach this handler to their | |
33 | * {@link java.util.logging.Logger} to have it generate UST events from logging | |
34 | * events received through the logger. | |
35 | * | |
36 | * It sends its events to UST via the JNI library "liblttng-ust-jul-jni.so". | |
37 | * Make sure this library is available before using this handler. | |
38 | * | |
39 | * @author Alexandre Montplaisir | |
40 | * @author David Goulet | |
41 | */ | |
42 | public class LttngLogHandler extends Handler implements ILttngHandler { | |
43 | ||
44 | private static final String SHARED_OBJECT_NAME = "lttng-ust-jul-jni"; | |
45 | ||
46 | private final ILttngAgent<LttngLogHandler> agent; | |
47 | ||
48 | /** Number of events logged (really sent through JNI) by this handler */ | |
49 | private final AtomicLong eventCount = new AtomicLong(0); | |
50 | ||
51 | /** | |
52 | * Constructor | |
53 | * | |
54 | * @throws IOException | |
55 | * This handler requires the lttng-ust-jul-jni.so native | |
56 | * library, through which it will send the trace events. This | |
57 | * exception is throw is this library cannot be found. | |
58 | * @throws SecurityException | |
59 | * We will forward any SecurityExcepion that may be thrown when | |
60 | * trying to load the JNI library. | |
61 | */ | |
62 | public LttngLogHandler() throws IOException, SecurityException { | |
63 | super(); | |
64 | /* Initialize LTTng UST tracer. */ | |
65 | try { | |
66 | System.loadLibrary(SHARED_OBJECT_NAME); //$NON-NLS-1$ | |
67 | } catch (UnsatisfiedLinkError e) { | |
68 | throw new IOException(e); | |
69 | } | |
70 | ||
71 | /** Register to the relevant agent */ | |
72 | agent = LttngJulAgent.getInstance(); | |
73 | agent.registerHandler(this); | |
74 | } | |
75 | ||
76 | @Override | |
77 | public synchronized void close() { | |
78 | agent.unregisterHandler(this); | |
79 | } | |
80 | ||
81 | /** | |
82 | * Get the number of events logged by this handler so far. This means the | |
83 | * number of events actually sent through JNI to UST. | |
84 | * | |
85 | * @return The number of events logged so far | |
86 | */ | |
87 | @Override | |
88 | public long getEventCount() { | |
89 | return eventCount.get(); | |
90 | } | |
91 | ||
92 | @Override | |
93 | public void flush() { | |
94 | } | |
95 | ||
96 | @Override | |
97 | public void publish(LogRecord record) { | |
98 | /* | |
99 | * Check if the current message should be logged, according to the UST | |
100 | * session settings. | |
101 | */ | |
102 | if (!agent.isEventEnabled(record.getLoggerName())) { | |
103 | return; | |
104 | } | |
105 | ||
106 | eventCount.incrementAndGet(); | |
107 | /* | |
108 | * Specific tracepoint designed for JUL events. The source class of the | |
109 | * caller is used for the event name, the raw message is taken, the | |
110 | * loglevel of the record and the thread ID. | |
111 | */ | |
112 | tracepoint(record.getMessage(), | |
113 | record.getLoggerName(), | |
114 | record.getSourceClassName(), | |
115 | record.getSourceMethodName(), | |
116 | record.getMillis(), | |
117 | record.getLevel().intValue(), | |
118 | record.getThreadID()); | |
119 | } | |
120 | ||
121 | /* Send tracepoint information to the JNI library */ | |
122 | private native void tracepoint(String msg, | |
123 | String logger_name, | |
124 | String class_name, | |
125 | String method_name, | |
126 | long millis, | |
127 | int log_level, | |
128 | int thread_id); | |
129 | } |