2 * Copyright (C) 2013 - David Goulet <dgoulet@efficios.com>
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.
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
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
18 package org
.lttng
.ust
.jul
;
20 import java
.lang
.String
;
21 import java
.util
.logging
.Handler
;
22 import java
.util
.logging
.LogRecord
;
23 import java
.util
.logging
.LogManager
;
24 import java
.util
.logging
.Level
;
25 import java
.util
.ArrayList
;
26 import java
.util
.HashMap
;
28 import org
.lttng
.ust
.jul
.LTTngUst
;
30 /* Note: This is taken from the LTTng tools ABI. */
31 class LTTngLogLevelABI
{
33 public static final int LOGLEVEL_TYPE_ALL
= 0;
34 public static final int LOGLEVEL_TYPE_RANGE
= 1;
35 public static final int LOGLEVEL_TYPE_SINGLE
= 2;
38 public class LTTngLogHandler
extends Handler
{
40 * Indicate if the enable all event has been seen and if yes logger that we
41 * enabled should use the loglevel/type below.
43 public int logLevelUseAll
= 0;
44 public ArrayList
<LTTngLogLevel
> logLevelsAll
=
45 new ArrayList
<LTTngLogLevel
>();
47 /* Am I a root Log Handler. */
48 public int is_root
= 0;
50 public LogManager logManager
;
52 /* Indexed by name and corresponding LTTngEvent. */
53 private HashMap
<String
, LTTngEvent
> eventMap
=
54 new HashMap
<String
, LTTngEvent
>();
56 public LTTngLogHandler(LogManager logManager
) {
59 this.logManager
= logManager
;
61 /* Initialize LTTng UST tracer. */
66 * Add event to handler hash map if new.
68 * @return 0 if it did not exist else 1.
70 public int setEvent(LTTngEvent new_event
) {
73 event
= eventMap
.get(new_event
.name
);
75 eventMap
.put(new_event
.name
, new_event
);
79 /* Add new event loglevel to existing event. */
80 event
.logLevels
.addAll(new_event
.logLevels
);
87 * Cleanup this handler state meaning put it back to a vanilla state.
90 this.eventMap
.clear();
91 this.logLevelsAll
.clear();
95 public void close() throws SecurityException
{}
98 public void flush() {}
101 public void publish(LogRecord record
) {
102 int fire_tp
= 0, rec_log_level
, ev_type
, ev_log_level
;
104 LTTngLogLevel lttngLogLevel
;
105 String logger_name
= record
.getLoggerName();
107 /* Get back the event if any and check for loglevel. */
108 event
= eventMap
.get(logger_name
);
110 for (LTTngLogLevel ev_log
: event
.logLevels
) {
111 /* Get record and event log level. */
112 rec_log_level
= record
.getLevel().intValue();
113 ev_log_level
= ev_log
.level
;
115 switch (ev_log
.type
) {
116 case LTTngLogLevelABI
.LOGLEVEL_TYPE_RANGE
:
117 if (ev_log_level
<= rec_log_level
) {
121 case LTTngLogLevelABI
.LOGLEVEL_TYPE_SINGLE
:
122 if (ev_log_level
== rec_log_level
) {
126 case LTTngLogLevelABI
.LOGLEVEL_TYPE_ALL
:
132 * If we match, stop right now else continue to the next
133 * loglevel contained in the event.
140 /* No loglevel attached thus fire tracepoint. */
149 * Specific tracepoing designed for JUL events. The source class of the
150 * caller is used for the event name, the raw message is taken, the
151 * loglevel of the record and the thread ID.
153 if (this.is_root
== 1) {
154 LTTngUst
.tracepointS(record
.getMessage(),
155 record
.getLoggerName(), record
.getSourceClassName(),
156 record
.getSourceMethodName(), record
.getMillis(),
157 record
.getLevel().intValue(), record
.getThreadID());
159 LTTngUst
.tracepointU(record
.getMessage(),
160 record
.getLoggerName(), record
.getSourceClassName(),
161 record
.getSourceMethodName(), record
.getMillis(),
162 record
.getLevel().intValue(), record
.getThreadID());