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
.logging
.Logger
;
26 import java
.util
.Collections
;
27 import java
.util
.HashMap
;
30 import org
.lttng
.ust
.jul
.LTTngUst
;
34 * The log handler is attached to the logger when the reference count is
35 * nonzero. Each event referring to a logger holds a reference to that
36 * logger. If down to 0, this object is removed from the handler.
42 public LTTngLogger(String name
, Logger logger
) {
48 public void attach(LTTngLogHandler handler
) {
49 this.logger
.addHandler(handler
);
52 public void detach(LTTngLogHandler handler
) {
53 this.logger
.removeHandler(handler
);
57 public class LTTngLogHandler
extends Handler
{
58 /* Am I a root Log Handler. */
59 public int is_root
= 0;
61 public LogManager logManager
;
63 /* Logger object attached to this handler that can trigger a tracepoint. */
64 private Map
<String
, LTTngLogger
> loggerMap
=
65 Collections
.synchronizedMap(new HashMap
<String
, LTTngLogger
>());
68 public LTTngLogHandler(LogManager logManager
) {
71 this.logManager
= logManager
;
73 /* Initialize LTTng UST tracer. */
78 * Return true if the logger is enabled and attached. Else, if not found,
81 public boolean exists(String name
) {
82 if (loggerMap
.get(name
) != null) {
90 * Attach an event to this handler. If no logger object exists, one is
91 * created else the refcount is incremented.
93 public void attachEvent(LTTngEvent event
) {
95 LTTngLogger lttngLogger
;
97 /* Does the logger actually exist. */
98 logger
= this.logManager
.getLogger(event
.name
);
100 /* Stop attach right now. */
104 lttngLogger
= loggerMap
.get(event
.name
);
105 if (lttngLogger
== null) {
106 lttngLogger
= new LTTngLogger(event
.name
, logger
);
108 /* Attach the handler to the logger and add is to the map. */
109 lttngLogger
.attach(this);
110 lttngLogger
.refcount
= 1;
111 loggerMap
.put(lttngLogger
.name
, lttngLogger
);
113 lttngLogger
.refcount
+= 1;
118 * Dettach an event from this handler. If the refcount goes down to 0, the
119 * logger object is removed thus not attached anymore.
121 public void detachEvent(LTTngEvent event
) {
124 logger
= loggerMap
.get(event
.name
);
125 if (logger
!= null) {
126 logger
.refcount
-= 1;
127 if (logger
.refcount
== 0) {
128 /* Dettach from handler since no more event is associated. */
130 loggerMap
.remove(logger
);
136 * Cleanup this handler state meaning put it back to a vanilla state.
138 public void clear() {
139 this.loggerMap
.clear();
143 public void close() throws SecurityException
{}
146 public void flush() {}
149 public void publish(LogRecord record
) {
152 logger
= loggerMap
.get(record
.getLoggerName());
153 if (logger
== null) {
154 /* Ignore and don't fire TP. */
159 * Specific tracepoing designed for JUL events. The source class of the
160 * caller is used for the event name, the raw message is taken, the
161 * loglevel of the record and the thread ID.
163 if (this.is_root
== 1) {
164 LTTngUst
.tracepointS(record
.getMessage(),
165 record
.getLoggerName(), record
.getSourceClassName(),
166 record
.getSourceMethodName(), record
.getMillis(),
167 record
.getLevel().intValue(), record
.getThreadID());
169 LTTngUst
.tracepointU(record
.getMessage(),
170 record
.getLoggerName(), record
.getSourceClassName(),
171 record
.getSourceMethodName(), record
.getMillis(),
172 record
.getLevel().intValue(), record
.getThreadID());
This page took 0.032604 seconds and 4 git commands to generate.