Fix: Ensure the Java JUL messages are correctly formatted
[lttng-ust.git] / liblttng-ust-java-agent / java / org / lttng / ust / agent / jul / LTTngLogHandler.java
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.jul;
19
20 import java.lang.String;
21
22 import java.util.logging.Formatter;
23 import java.util.logging.Handler;
24 import java.util.logging.LogRecord;
25
26 class LTTngLogHandler extends Handler {
27
28 /**
29 * Dummy Formatter object, so we can use its
30 * {@link Formatter#formatMessage(LogRecord)} method.
31 */
32 private static final Formatter FORMATTER = new Formatter() {
33 @Override
34 public String format(LogRecord record) {
35 throw new UnsupportedOperationException();
36 }
37 };
38
39 private final Boolean isRoot;
40
41 public LTTngLogHandler(Boolean isRoot) {
42 super();
43 this.isRoot = isRoot;
44 /* Initialize LTTng UST tracer. */
45 try {
46 System.loadLibrary("lttng-ust-jul-jni"); //$NON-NLS-1$
47 } catch (SecurityException e) {
48 e.printStackTrace();
49 } catch (UnsatisfiedLinkError e) {
50 e.printStackTrace();
51 } catch (NullPointerException e) {
52 /* Should never happen */
53 e.printStackTrace();
54 }
55 }
56
57 public Boolean isRoot() {
58 return this.isRoot;
59 }
60
61 @Override
62 public void close() throws SecurityException {}
63
64 @Override
65 public void flush() {}
66
67 @Override
68 public void publish(LogRecord record) {
69 String formattedMessage = FORMATTER.formatMessage(record);
70
71 /*
72 * Specific tracepoint designed for JUL events. The source class of the
73 * caller is used for the event name, the raw message is taken, the
74 * loglevel of the record and the thread ID.
75 */
76 if (this.isRoot) {
77 tracepointS(formattedMessage,
78 record.getLoggerName(), record.getSourceClassName(),
79 record.getSourceMethodName(), record.getMillis(),
80 record.getLevel().intValue(), record.getThreadID());
81 } else {
82 tracepointU(formattedMessage,
83 record.getLoggerName(), record.getSourceClassName(),
84 record.getSourceMethodName(), record.getMillis(),
85 record.getLevel().intValue(), record.getThreadID());
86 }
87 }
88
89 /* Use for a user session daemon. */
90 private native void tracepointU(String msg,
91 String logger_name,
92 String class_name,
93 String method_name,
94 long millis,
95 int log_level,
96 int thread_id);
97
98 /* Use for a root session daemon. */
99 private native void tracepointS(String msg,
100 String logger_name,
101 String class_name,
102 String method_name,
103 long millis,
104 int log_level,
105 int thread_id);
106 }
This page took 0.034326 seconds and 4 git commands to generate.