2 * Copyright (C) 2015 - EfficiOS Inc., Alexandre Montplaisir <alexmonthy@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.agent.session;
21 * Log level filtering element, which is part of an {@link EventRule}.
23 * @author Alexandre Montplaisir
25 public class LogLevelSelector {
28 * The type of log level filter that is enabled.
30 * Defined from lttng-tools' include/lttng/event.h.
32 public enum LogLevelType {
34 * All log levels are enabled. This overrides the value of
35 * {@link LogLevelSelector#getLogLevel}.
37 LTTNG_EVENT_LOGLEVEL_ALL(0),
39 /** This log level along with all log levels of higher severity are enabled. */
40 LTTNG_EVENT_LOGLEVEL_RANGE(1),
42 /** Only this exact log level is enabled. */
43 LTTNG_EVENT_LOGLEVEL_SINGLE(2);
45 private final int value;
47 private LogLevelType(int value) {
52 * Get the numerical (int) value representing this log level type in the
53 * communication protocol.
55 * @return The int value
57 public int getValue() {
61 static LogLevelType fromValue(int val) {
64 return LTTNG_EVENT_LOGLEVEL_ALL;
66 return LTTNG_EVENT_LOGLEVEL_RANGE;
68 return LTTNG_EVENT_LOGLEVEL_SINGLE;
70 throw new IllegalArgumentException();
75 private final int logLevel;
76 private final LogLevelType logLevelType;
79 * Constructor using numerical values straight from the communication
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.
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.
92 public LogLevelSelector(int logLevel, int logLevelType) {
93 this.logLevel = logLevel;
94 this.logLevelType = LogLevelType.fromValue(logLevelType);
98 * "Manual" constructor, specifying the {@link LogLevelType} directly.
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.
105 * The log level filter type.
107 public LogLevelSelector(int logLevel, LogLevelType type) {
108 this.logLevel = logLevel;
109 this.logLevelType = type;
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}.
117 * @return The numerical value of the log level
119 public int getLogLevel() {
124 * Get the log level filter type.
126 * @return The log level filter type
128 public LogLevelType getLogLevelType() {
133 * Helper method to determine if an event with the given log level should be
134 * traced when considering this filter.
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).
140 * @param targetLogLevel
141 * The log level value of the event to check for
142 * @return Should this event be traced, or not
144 public boolean matches(int targetLogLevel) {
145 switch (logLevelType) {
146 case LTTNG_EVENT_LOGLEVEL_ALL:
148 case LTTNG_EVENT_LOGLEVEL_RANGE:
149 return (targetLogLevel >= logLevel);
150 case LTTNG_EVENT_LOGLEVEL_SINGLE:
151 return (targetLogLevel == logLevel);
153 throw new IllegalStateException();
157 // ------------------------------------------------------------------------
158 // Methods from Object
159 // ------------------------------------------------------------------------
162 public int hashCode() {
163 final int prime = 31;
165 result = prime * result + logLevel;
166 result = prime * result + ((logLevelType == null) ? 0 : logLevelType.hashCode());
171 public boolean equals(Object obj) {
178 if (getClass() != obj.getClass()) {
181 LogLevelSelector other = (LogLevelSelector) obj;
183 if (logLevel != other.logLevel) {
186 if (logLevelType != other.logLevelType) {
193 public String toString() {
194 if (getLogLevelType() == LogLevelType.LTTNG_EVENT_LOGLEVEL_ALL) {
195 return LogLevelType.LTTNG_EVENT_LOGLEVEL_ALL.toString();
197 return String.valueOf(getLogLevel()) + ", " + getLogLevelType().toString();