View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.maven.slf4j;
20  
21  import java.util.concurrent.atomic.AtomicReference;
22  
23  import org.apache.maven.logging.api.LogLevelRecorder;
24  
25  /**
26   * Responsible for keeping state of whether the threshold of the --fail-on-severity flag has been hit.
27   */
28  public class DefaultLogLevelRecorder implements LogLevelRecorder {
29      private Level maxAllowed;
30      private final AtomicReference<Level> maxReached = new AtomicReference<>(Level.DEBUG);
31  
32      @Override
33      public boolean hasReachedMaxLevel() {
34          return maxReached.get().ordinal() > maxAllowed.ordinal();
35      }
36  
37      @Override
38      public Level getMaxLevelReached() {
39          return maxReached.get();
40      }
41  
42      @Override
43      public Level getMaxLevelAllowed() {
44          return maxAllowed;
45      }
46  
47      @Override
48      public void setMaxLevelAllowed(Level level) {
49          this.maxAllowed = level;
50      }
51  
52      @Override
53      public void reset() {
54          this.maxAllowed = null;
55          this.maxReached.set(Level.DEBUG);
56      }
57  
58      public void record(org.slf4j.event.Level logLevel) {
59          Level level =
60                  switch (logLevel) {
61                      case TRACE, DEBUG -> Level.DEBUG;
62                      case INFO -> Level.INFO;
63                      case WARN -> Level.WARN;
64                      case ERROR -> Level.ERROR;
65                  };
66          while (true) {
67              Level r = maxReached.get();
68              if (level.ordinal() > r.ordinal()) {
69                  if (!maxReached.compareAndSet(r, level)) {
70                      continue;
71                  }
72              }
73              break;
74          }
75      }
76  
77      public boolean metThreshold() {
78          return maxAllowed != null && maxReached.get().ordinal() >= maxAllowed.ordinal();
79      }
80  }