f61677d0b3b270e96a07fef07a817ef73f00c33c
[lttng-ust.git] / liblttng-ust-jul / org / lttng / ust / 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.jul;
19
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.HashMap;
26
27 import org.lttng.ust.jul.LTTngUst;
28
29 /* Note: This is taken from the LTTng tools ABI. */
30 class LTTngLogLevelABI {
31 /* Loglevel type. */
32 public static final int LOGLEVEL_TYPE_ALL = 0;
33 public static final int LOGLEVEL_TYPE_RANGE = 1;
34 public static final int LOGLEVEL_TYPE_SINGLE = 2;
35 }
36
37 public class LTTngLogHandler extends Handler {
38 /*
39 * Indicate if the enable all event has been seen and if yes logger that we
40 * enabled should use the loglevel/type below.
41 */
42 public int logLevelUseAll = 0;
43 public int logLevelAll = 0;
44 public int logLevelTypeAll;
45
46 public LogManager logManager;
47
48 /* Indexed by name and corresponding LTTngEvent. */
49 private HashMap<String, LTTngEvent> eventMap =
50 new HashMap<String, LTTngEvent>();
51
52 public LTTngLogHandler(LogManager logManager) {
53 super();
54
55 this.logManager = logManager;
56
57 /* Initialize LTTng UST tracer. */
58 LTTngUst.init();
59 }
60
61 public void setEvent(LTTngEvent event) {
62 eventMap.put(event.name, event);
63 }
64
65 @Override
66 public void close() throws SecurityException {}
67
68 @Override
69 public void flush() {}
70
71 @Override
72 public void publish(LogRecord record) {
73 int fire_tp = 0, rec_log_level, ev_type, ev_log_level;
74 LTTngEvent event;
75 LTTngLogLevel lttngLogLevel;
76 String logger_name = record.getLoggerName();
77
78 /* Get back the event if any and check for loglevel. */
79 event = eventMap.get(logger_name);
80 if (event != null) {
81 rec_log_level = record.getLevel().intValue();
82 ev_log_level = event.logLevel.level;
83 ev_type = event.logLevel.type;
84
85 switch (ev_type) {
86 case LTTngLogLevelABI.LOGLEVEL_TYPE_RANGE:
87 if (ev_log_level <= rec_log_level) {
88 fire_tp = 1;
89 }
90 break;
91 case LTTngLogLevelABI.LOGLEVEL_TYPE_SINGLE:
92 if (ev_log_level == rec_log_level) {
93 fire_tp = 1;
94 }
95 break;
96 case LTTngLogLevelABI.LOGLEVEL_TYPE_ALL:
97 fire_tp = 1;
98 break;
99 }
100 } else {
101 /* No loglevel attached thus fire tracepoint. */
102 fire_tp = 1;
103 }
104
105 if (fire_tp == 0) {
106 return;
107 }
108
109 /*
110 * Specific tracepoing designed for JUL events. The source class of the
111 * caller is used for the event name, the raw message is taken, the
112 * loglevel of the record and the thread ID.
113 */
114 LTTngUst.tracepoint(record.getMessage(), record.getLoggerName(),
115 record.getSourceClassName(), record.getSourceMethodName(),
116 record.getMillis(), record.getLevel().intValue(),
117 record.getThreadID());
118 }
119 }
This page took 0.031028 seconds and 3 git commands to generate.