Commit | Line | Data |
---|---|---|
43e5396b DG |
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; | |
a15440fd DG |
24 | import java.util.logging.Level; |
25 | import java.util.HashMap; | |
43e5396b DG |
26 | |
27 | import org.lttng.ust.jul.LTTngUst; | |
28 | ||
a15440fd DG |
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 | class LTTngLogLevel { | |
38 | /* Event name on which this loglevel is applied on. */ | |
39 | private String event_name; | |
40 | /* This level is a JUL int level value. */ | |
41 | private int level; | |
42 | private int type; | |
43 | ||
44 | public LTTngLogLevel(String event_name, int level, int type) { | |
45 | this.event_name = event_name; | |
46 | this.type = type; | |
47 | this.level = level; | |
48 | } | |
49 | ||
50 | public String getName() { | |
51 | return this.event_name; | |
52 | } | |
53 | ||
54 | public int getLevel() { | |
55 | return this.level; | |
56 | } | |
57 | ||
58 | public int getType() { | |
59 | return this.type; | |
60 | } | |
61 | } | |
62 | ||
43e5396b DG |
63 | public class LTTngLogHandler extends Handler { |
64 | public LogManager logManager; | |
65 | ||
a15440fd DG |
66 | private HashMap<String, LTTngLogLevel> logLevels = |
67 | new HashMap<String, LTTngLogLevel>(); | |
68 | ||
43e5396b DG |
69 | public LTTngLogHandler(LogManager logManager) { |
70 | super(); | |
71 | ||
72 | this.logManager = logManager; | |
73 | ||
74 | /* Initialize LTTng UST tracer. */ | |
75 | LTTngUst.init(); | |
76 | } | |
77 | ||
a15440fd DG |
78 | public void setLogLevel(String event_name, int level, int type) { |
79 | LTTngLogLevel lttngLogLevel = new LTTngLogLevel(event_name, level, | |
80 | type); | |
81 | logLevels.put(event_name, lttngLogLevel); | |
82 | } | |
83 | ||
43e5396b DG |
84 | @Override |
85 | public void close() throws SecurityException {} | |
86 | ||
87 | @Override | |
88 | public void flush() {} | |
89 | ||
90 | @Override | |
91 | public void publish(LogRecord record) { | |
a15440fd DG |
92 | int fire_tp = 0, rec_log_level, ev_type, ev_log_level; |
93 | LTTngLogLevel lttngLogLevel; | |
94 | String event_name = record.getLoggerName(); | |
95 | ||
96 | lttngLogLevel = logLevels.get(event_name); | |
97 | if (lttngLogLevel != null) { | |
98 | rec_log_level = record.getLevel().intValue(); | |
99 | ev_log_level = lttngLogLevel.getLevel(); | |
100 | ev_type = lttngLogLevel.getType(); | |
101 | ||
102 | switch (ev_type) { | |
103 | case LTTngLogLevelABI.LOGLEVEL_TYPE_RANGE: | |
104 | if (ev_log_level <= rec_log_level) { | |
105 | fire_tp = 1; | |
106 | } | |
107 | break; | |
108 | case LTTngLogLevelABI.LOGLEVEL_TYPE_SINGLE: | |
109 | if (ev_log_level == rec_log_level) { | |
110 | fire_tp = 1; | |
111 | } | |
112 | break; | |
113 | case LTTngLogLevelABI.LOGLEVEL_TYPE_ALL: | |
114 | fire_tp = 1; | |
115 | break; | |
116 | } | |
117 | } else { | |
118 | /* No loglevel attached thus fire tracepoint. */ | |
119 | fire_tp = 1; | |
120 | } | |
121 | ||
122 | if (fire_tp == 0) { | |
123 | return; | |
124 | } | |
125 | ||
43e5396b DG |
126 | /* |
127 | * Specific tracepoing designed for JUL events. The source class of the | |
128 | * caller is used for the event name, the raw message is taken, the | |
129 | * loglevel of the record and the thread ID. | |
130 | */ | |
131 | LTTngUst.tracepoint(record.getMessage(), record.getLoggerName(), | |
132 | record.getSourceClassName(), record.getSourceMethodName(), | |
133 | record.getMillis(), record.getLevel().intValue(), | |
134 | record.getThreadID()); | |
135 | } | |
136 | } |