Move to kernel style SPDX license identifiers
[lttng-ust.git] / liblttng-ust-java-agent / java / lttng-ust-agent-common / org / lttng / ust / agent / session / LogLevelSelector.java
1 /*
2 * SPDX-License-Identifier: LGPL-2.1-only
3 *
4 * Copyright (C) 2015 EfficiOS Inc.
5 * Copyright (C) 2015 Alexandre Montplaisir <alexmonthy@efficios.com>
6 */
7
8 package org.lttng.ust.agent.session;
9
10 /**
11 * Log level filtering element, which is part of an {@link EventRule}.
12 *
13 * @author Alexandre Montplaisir
14 */
15 public class LogLevelSelector {
16
17 /**
18 * The type of log level filter that is enabled.
19 *
20 * Defined from lttng-tools' include/lttng/event.h.
21 */
22 public enum LogLevelType {
23 /**
24 * All log levels are enabled. This overrides the value of
25 * {@link LogLevelSelector#getLogLevel}.
26 */
27 LTTNG_EVENT_LOGLEVEL_ALL(0),
28
29 /** This log level along with all log levels of higher severity are enabled. */
30 LTTNG_EVENT_LOGLEVEL_RANGE(1),
31
32 /** Only this exact log level is enabled. */
33 LTTNG_EVENT_LOGLEVEL_SINGLE(2);
34
35 private final int value;
36
37 private LogLevelType(int value) {
38 this.value = value;
39 }
40
41 /**
42 * Get the numerical (int) value representing this log level type in the
43 * communication protocol.
44 *
45 * @return The int value
46 */
47 public int getValue() {
48 return value;
49 }
50
51 static LogLevelType fromValue(int val) {
52 switch (val) {
53 case 0:
54 return LTTNG_EVENT_LOGLEVEL_ALL;
55 case 1:
56 return LTTNG_EVENT_LOGLEVEL_RANGE;
57 case 2:
58 return LTTNG_EVENT_LOGLEVEL_SINGLE;
59 default:
60 throw new IllegalArgumentException();
61 }
62 }
63 }
64
65 private final int logLevel;
66 private final LogLevelType logLevelType;
67
68 /**
69 * Constructor using numerical values straight from the communication
70 * protocol.
71 *
72 * @param logLevel
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.
76 * @param logLevelType
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.
81 */
82 public LogLevelSelector(int logLevel, int logLevelType) {
83 this.logLevel = logLevel;
84 this.logLevelType = LogLevelType.fromValue(logLevelType);
85 }
86
87 /**
88 * "Manual" constructor, specifying the {@link LogLevelType} directly.
89 *
90 * @param logLevel
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.
94 * @param type
95 * The log level filter type.
96 */
97 public LogLevelSelector(int logLevel, LogLevelType type) {
98 this.logLevel = logLevel;
99 this.logLevelType = type;
100 }
101
102 /**
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}.
106 *
107 * @return The numerical value of the log level
108 */
109 public int getLogLevel() {
110 return logLevel;
111 }
112
113 /**
114 * Get the log level filter type.
115 *
116 * @return The log level filter type
117 */
118 public LogLevelType getLogLevelType() {
119 return logLevelType;
120 }
121
122 /**
123 * Helper method to determine if an event with the given log level should be
124 * traced when considering this filter.
125 *
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).
129 *
130 * @param targetLogLevel
131 * The log level value of the event to check for
132 * @return Should this event be traced, or not
133 */
134 public boolean matches(int targetLogLevel) {
135 switch (logLevelType) {
136 case LTTNG_EVENT_LOGLEVEL_ALL:
137 return true;
138 case LTTNG_EVENT_LOGLEVEL_RANGE:
139 return (targetLogLevel >= logLevel);
140 case LTTNG_EVENT_LOGLEVEL_SINGLE:
141 return (targetLogLevel == logLevel);
142 default:
143 throw new IllegalStateException();
144 }
145 }
146
147 // ------------------------------------------------------------------------
148 // Methods from Object
149 // ------------------------------------------------------------------------
150
151 @Override
152 public int hashCode() {
153 final int prime = 31;
154 int result = 1;
155 result = prime * result + logLevel;
156 result = prime * result + ((logLevelType == null) ? 0 : logLevelType.hashCode());
157 return result;
158 }
159
160 @Override
161 public boolean equals(Object obj) {
162 if (this == obj) {
163 return true;
164 }
165 if (obj == null) {
166 return false;
167 }
168 if (getClass() != obj.getClass()) {
169 return false;
170 }
171 LogLevelSelector other = (LogLevelSelector) obj;
172
173 if (logLevel != other.logLevel) {
174 return false;
175 }
176 if (logLevelType != other.logLevelType) {
177 return false;
178 }
179 return true;
180 }
181
182 @Override
183 public String toString() {
184 if (getLogLevelType() == LogLevelType.LTTNG_EVENT_LOGLEVEL_ALL) {
185 return LogLevelType.LTTNG_EVENT_LOGLEVEL_ALL.toString();
186 }
187 return String.valueOf(getLogLevel()) + ", " + getLogLevelType().toString();
188 }
189 }
This page took 0.032243 seconds and 4 git commands to generate.