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