2 * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@efficios.com>
3 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
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.
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
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
19 package org
.lttng
.ust
.agent
.jul
;
21 import java
.io
.IOException
;
22 import java
.util
.concurrent
.atomic
.AtomicLong
;
23 import java
.util
.logging
.Handler
;
24 import java
.util
.logging
.LogRecord
;
26 import org
.lttng
.ust
.agent
.ILttngAgent
;
27 import org
.lttng
.ust
.agent
.ILttngHandler
;
30 * LTTng-UST JUL log handler.
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.
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.
39 * @author Alexandre Montplaisir
40 * @author David Goulet
42 public class LttngLogHandler
extends Handler
implements ILttngHandler
{
44 private static final String SHARED_OBJECT_NAME
= "lttng-ust-jul-jni";
46 private final ILttngAgent
<LttngLogHandler
> agent
;
48 /** Number of events logged (really sent through JNI) by this handler */
49 private final AtomicLong eventCount
= new AtomicLong(0);
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.
62 public LttngLogHandler() throws IOException
, SecurityException
{
64 /* Initialize LTTng UST tracer. */
66 System
.loadLibrary(SHARED_OBJECT_NAME
); //$NON-NLS-1$
67 } catch (UnsatisfiedLinkError e
) {
68 throw new IOException(e
);
71 /** Register to the relevant agent */
72 agent
= LttngJulAgent
.getInstance();
73 agent
.registerHandler(this);
77 public synchronized void close() {
78 agent
.unregisterHandler(this);
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.
85 * @return The number of events logged so far
88 public long getEventCount() {
89 return eventCount
.get();
97 public void publish(LogRecord record
) {
99 * Check if the current message should be logged, according to the UST
102 if (!agent
.isEventEnabled(record
.getLoggerName())) {
106 eventCount
.incrementAndGet();
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.
112 tracepoint(record
.getMessage(),
113 record
.getLoggerName(),
114 record
.getSourceClassName(),
115 record
.getSourceMethodName(),
117 record
.getLevel().intValue(),
118 record
.getThreadID());
121 /* Send tracepoint information to the JNI library */
122 private native void tracepoint(String msg
,