b4e1c088423a5a2e68f491b07ff9c24c528c97f7
[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.ArrayList;
26 import java.util.HashMap;
27
28 import org.lttng.ust.jul.LTTngUst;
29
30 /* Note: This is taken from the LTTng tools ABI. */
31 class LTTngLogLevelABI {
32 /* Loglevel type. */
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;
36 }
37
38 public class LTTngLogHandler extends Handler {
39 /*
40 * Indicate if the enable all event has been seen and if yes logger that we
41 * enabled should use the loglevel/type below.
42 */
43 public int logLevelUseAll = 0;
44 public ArrayList<LTTngLogLevel> logLevelsAll =
45 new ArrayList<LTTngLogLevel>();
46
47 /* Am I a root Log Handler. */
48 public int is_root = 0;
49
50 public LogManager logManager;
51
52 /* Indexed by name and corresponding LTTngEvent. */
53 private HashMap<String, LTTngEvent> eventMap =
54 new HashMap<String, LTTngEvent>();
55
56 public LTTngLogHandler(LogManager logManager) {
57 super();
58
59 this.logManager = logManager;
60
61 /* Initialize LTTng UST tracer. */
62 LTTngUst.init();
63 }
64
65 /**
66 * Add event to handler hash map if new.
67 *
68 * @return 0 if it did not exist else 1.
69 */
70 public int setEvent(LTTngEvent new_event) {
71 LTTngEvent event;
72
73 event = eventMap.get(new_event.name);
74 if (event == null) {
75 eventMap.put(new_event.name, new_event);
76 /* Did not exists. */
77 return 0;
78 } else {
79 /* Add new event loglevel to existing event. */
80 event.logLevels.addAll(new_event.logLevels);
81 /* Already exists. */
82 return 1;
83 }
84 }
85
86 @Override
87 public void close() throws SecurityException {}
88
89 @Override
90 public void flush() {}
91
92 @Override
93 public void publish(LogRecord record) {
94 int fire_tp = 0, rec_log_level, ev_type, ev_log_level;
95 LTTngEvent event;
96 LTTngLogLevel lttngLogLevel;
97 String logger_name = record.getLoggerName();
98
99 /* Get back the event if any and check for loglevel. */
100 event = eventMap.get(logger_name);
101 if (event != null) {
102 for (LTTngLogLevel ev_log : event.logLevels) {
103 /* Get record and event log level. */
104 rec_log_level = record.getLevel().intValue();
105 ev_log_level = ev_log.level;
106
107 switch (ev_log.type) {
108 case LTTngLogLevelABI.LOGLEVEL_TYPE_RANGE:
109 if (ev_log_level <= rec_log_level) {
110 fire_tp = 1;
111 }
112 break;
113 case LTTngLogLevelABI.LOGLEVEL_TYPE_SINGLE:
114 if (ev_log_level == rec_log_level) {
115 fire_tp = 1;
116 }
117 break;
118 case LTTngLogLevelABI.LOGLEVEL_TYPE_ALL:
119 fire_tp = 1;
120 break;
121 }
122
123 /*
124 * If we match, stop right now else continue to the next
125 * loglevel contained in the event.
126 */
127 if (fire_tp == 1) {
128 break;
129 }
130 }
131 } else {
132 /* No loglevel attached thus fire tracepoint. */
133 fire_tp = 1;
134 }
135
136 if (fire_tp == 0) {
137 return;
138 }
139
140 /*
141 * Specific tracepoing designed for JUL events. The source class of the
142 * caller is used for the event name, the raw message is taken, the
143 * loglevel of the record and the thread ID.
144 */
145 if (this.is_root == 1) {
146 LTTngUst.tracepointS(record.getMessage(),
147 record.getLoggerName(), record.getSourceClassName(),
148 record.getSourceMethodName(), record.getMillis(),
149 record.getLevel().intValue(), record.getThreadID());
150 } else {
151 LTTngUst.tracepointU(record.getMessage(),
152 record.getLoggerName(), record.getSourceClassName(),
153 record.getSourceMethodName(), record.getMillis(),
154 record.getLevel().intValue(), record.getThreadID());
155 }
156 }
157 }
This page took 0.032085 seconds and 3 git commands to generate.