2 * SPDX-License-Identifier: LGPL-2.1-only
4 * Copyright (C) 2015 EfficiOS Inc.
5 * Copyright (C) 2015 Alexandre Montplaisir <alexmonthy@efficios.com>
8 package org.lttng.ust.agent.session;
11 * Log level filtering element, which is part of an {@link EventRule}.
13 * @author Alexandre Montplaisir
15 public class LogLevelSelector {
18 * The type of log level filter that is enabled.
20 * Defined from lttng-tools' include/lttng/event.h.
22 public enum LogLevelType {
24 * All log levels are enabled. This overrides the value of
25 * {@link LogLevelSelector#getLogLevel}.
27 LTTNG_EVENT_LOGLEVEL_ALL(0),
29 /** This log level along with all log levels of higher severity are enabled. */
30 LTTNG_EVENT_LOGLEVEL_RANGE(1),
32 /** Only this exact log level is enabled. */
33 LTTNG_EVENT_LOGLEVEL_SINGLE(2);
35 private final int value;
37 private LogLevelType(int value) {
42 * Get the numerical (int) value representing this log level type in the
43 * communication protocol.
45 * @return The int value
47 public int getValue() {
51 static LogLevelType fromValue(int val) {
54 return LTTNG_EVENT_LOGLEVEL_ALL;
56 return LTTNG_EVENT_LOGLEVEL_RANGE;
58 return LTTNG_EVENT_LOGLEVEL_SINGLE;
60 throw new IllegalArgumentException();
65 private final int logLevel;
66 private final LogLevelType logLevelType;
69 * Constructor using numerical values straight from the communication
73 * The numerical value of the log level. The exact value depends
74 * on the tracing domain, see include/lttng/event.h in the
75 * lttng-tools tree for the complete enumeration.
77 * The numerical value of the log level type. It will be
78 * converted to a {@link LogLevelType} by this constructor.
79 * @throws IllegalArgumentException
80 * If the 'logLevelType' does not correspond to a valid value.
82 public LogLevelSelector(int logLevel, int logLevelType) {
83 this.logLevel = logLevel;
84 this.logLevelType = LogLevelType.fromValue(logLevelType);
88 * "Manual" constructor, specifying the {@link LogLevelType} directly.
91 * The numerical value of the log level. The exact value depends
92 * on the tracing domain, see include/lttng/event.h in the
93 * lttng-tools tree for the complete enumeration.
95 * The log level filter type.
97 public LogLevelSelector(int logLevel, LogLevelType type) {
98 this.logLevel = logLevel;
99 this.logLevelType = type;
103 * Get the numerical value of the log level element. Does not apply if
104 * {@link #getLogLevelType} returns
105 * {@link LogLevelType#LTTNG_EVENT_LOGLEVEL_ALL}.
107 * @return The numerical value of the log level
109 public int getLogLevel() {
114 * Get the log level filter type.
116 * @return The log level filter type
118 public LogLevelType getLogLevelType() {
123 * Helper method to determine if an event with the given log level should be
124 * traced when considering this filter.
126 * For example, if this filter object represents "higher severity than 5",
127 * and the log level passed in parameter is "8", it will return that it
128 * matches (higher value means higher severity).
130 * @param targetLogLevel
131 * The log level value of the event to check for
132 * @return Should this event be traced, or not
134 public boolean matches(int targetLogLevel) {
135 switch (logLevelType) {
136 case LTTNG_EVENT_LOGLEVEL_ALL:
138 case LTTNG_EVENT_LOGLEVEL_RANGE:
139 return (targetLogLevel >= logLevel);
140 case LTTNG_EVENT_LOGLEVEL_SINGLE:
141 return (targetLogLevel == logLevel);
143 throw new IllegalStateException();
147 // ------------------------------------------------------------------------
148 // Methods from Object
149 // ------------------------------------------------------------------------
152 public int hashCode() {
153 final int prime = 31;
155 result = prime * result + logLevel;
156 result = prime * result + ((logLevelType == null) ? 0 : logLevelType.hashCode());
161 public boolean equals(Object obj) {
168 if (getClass() != obj.getClass()) {
171 LogLevelSelector other = (LogLevelSelector) obj;
173 if (logLevel != other.logLevel) {
176 if (logLevelType != other.logLevelType) {
183 public String toString() {
184 if (getLogLevelType() == LogLevelType.LTTNG_EVENT_LOGLEVEL_ALL) {
185 return LogLevelType.LTTNG_EVENT_LOGLEVEL_ALL.toString();
187 return String.valueOf(getLogLevel()) + ", " + getLogLevelType().toString();