f6f6a396ad2701f4e146f769204e14290ba13458
[lttng-ust.git] / liblttng-ust-java-agent / java / lttng-ust-agent-common / org / lttng / ust / agent / session / LogLevelSelector.java
1 /*
2 * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@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.agent.session;
19
20 /**
21 * Log level filtering element, which is part of an {@link EventRule}.
22 *
23 * @author Alexandre Montplaisir
24 */
25 public class LogLevelSelector {
26
27 /**
28 * The type of log level filter that is enabled.
29 *
30 * Defined from lttng-tools' include/lttng/event.h.
31 */
32 public enum LogLevelType {
33 /**
34 * All log levels are enabled. This overrides the value of
35 * {@link LogLevelSelector#getLogLevel}.
36 */
37 LTTNG_EVENT_LOGLEVEL_ALL(0),
38
39 /** This log level along with all log levels of higher severity are enabled. */
40 LTTNG_EVENT_LOGLEVEL_RANGE(1),
41
42 /** Only this exact log level is enabled. */
43 LTTNG_EVENT_LOGLEVEL_SINGLE(2);
44
45 private final int value;
46
47 private LogLevelType(int value) {
48 this.value = value;
49 }
50
51 /**
52 * Get the numerical (int) value representing this log level type in the
53 * communication protocol.
54 *
55 * @return The int value
56 */
57 public int getValue() {
58 return value;
59 }
60
61 static LogLevelType fromValue(int val) {
62 switch (val) {
63 case 0:
64 return LTTNG_EVENT_LOGLEVEL_ALL;
65 case 1:
66 return LTTNG_EVENT_LOGLEVEL_RANGE;
67 case 2:
68 return LTTNG_EVENT_LOGLEVEL_SINGLE;
69 default:
70 throw new IllegalArgumentException();
71 }
72 }
73 }
74
75 private final int logLevel;
76 private final LogLevelType logLevelType;
77
78 /**
79 * Constructor using numerical values straight from the communication
80 * protocol.
81 *
82 * @param logLevel
83 * The numerical value of the log level. The exact value depends
84 * on the tracing domain, see include/lttng/event.h in the
85 * lttng-tools tree for the complete enumeration.
86 * @param logLevelType
87 * The numerical value of the log level type. It will be
88 * converted to a {@link LogLevelType} by this constructor.
89 * @throws IllegalArgumentException
90 * If the 'logLevelType' does not correspond to a valid value.
91 */
92 public LogLevelSelector(int logLevel, int logLevelType) {
93 this.logLevel = logLevel;
94 this.logLevelType = LogLevelType.fromValue(logLevelType);
95 }
96
97 /**
98 * "Manual" constructor, specifying the {@link LogLevelType} directly.
99 *
100 * @param logLevel
101 * The numerical value of the log level. The exact value depends
102 * on the tracing domain, see include/lttng/event.h in the
103 * lttng-tools tree for the complete enumeration.
104 * @param type
105 * The log level filter type.
106 */
107 public LogLevelSelector(int logLevel, LogLevelType type) {
108 this.logLevel = logLevel;
109 this.logLevelType = type;
110 }
111
112 /**
113 * Get the numerical value of the log level element. Does not apply if
114 * {@link #getLogLevelType} returns
115 * {@link LogLevelType#LTTNG_EVENT_LOGLEVEL_ALL}.
116 *
117 * @return The numerical value of the log level
118 */
119 public int getLogLevel() {
120 return logLevel;
121 }
122
123 /**
124 * Get the log level filter type.
125 *
126 * @return The log level filter type
127 */
128 public LogLevelType getLogLevelType() {
129 return logLevelType;
130 }
131
132 /**
133 * Helper method to determine if an event with the given log level should be
134 * traced when considering this filter.
135 *
136 * For example, if this filter object represents "higher severity than 5",
137 * and the log level passed in parameter is "8", it will return that it
138 * matches (higher value means higher severity).
139 *
140 * @param targetLogLevel
141 * The log level value of the event to check for
142 * @return Should this event be traced, or not
143 */
144 public boolean matches(int targetLogLevel) {
145 switch (logLevelType) {
146 case LTTNG_EVENT_LOGLEVEL_ALL:
147 return true;
148 case LTTNG_EVENT_LOGLEVEL_RANGE:
149 return (targetLogLevel >= logLevel);
150 case LTTNG_EVENT_LOGLEVEL_SINGLE:
151 return (targetLogLevel == logLevel);
152 default:
153 throw new IllegalStateException();
154 }
155 }
156
157 // ------------------------------------------------------------------------
158 // Methods from Object
159 // ------------------------------------------------------------------------
160
161 @Override
162 public int hashCode() {
163 final int prime = 31;
164 int result = 1;
165 result = prime * result + logLevel;
166 result = prime * result + ((logLevelType == null) ? 0 : logLevelType.hashCode());
167 return result;
168 }
169
170 @Override
171 public boolean equals(Object obj) {
172 if (this == obj) {
173 return true;
174 }
175 if (obj == null) {
176 return false;
177 }
178 if (getClass() != obj.getClass()) {
179 return false;
180 }
181 LogLevelSelector other = (LogLevelSelector) obj;
182
183 if (logLevel != other.logLevel) {
184 return false;
185 }
186 if (logLevelType != other.logLevelType) {
187 return false;
188 }
189 return true;
190 }
191
192 @Override
193 public String toString() {
194 if (getLogLevelType() == LogLevelType.LTTNG_EVENT_LOGLEVEL_ALL) {
195 return LogLevelType.LTTNG_EVENT_LOGLEVEL_ALL.toString();
196 }
197 return String.valueOf(getLogLevel()) + ", " + getLogLevelType().toString();
198 }
199 }
This page took 0.033271 seconds and 3 git commands to generate.