1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v2.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.core.status;
15  
16  import ch.qos.logback.core.Context;
17  import ch.qos.logback.core.CoreConstants;
18  
19  import java.util.ArrayList;
20  import java.util.List;
21  import java.util.Objects;
22  import java.util.regex.Matcher;
23  import java.util.regex.Pattern;
24  
25  public class StatusUtil {
26  
27      StatusManager sm;
28  
29      public StatusUtil(StatusManager sm) {
30          this.sm = sm;
31      }
32  
33      public StatusUtil(Context context) {
34          this.sm = context.getStatusManager();
35      }
36  
37      /**
38       * Returns true if the StatusManager associated with the context passed as
39       * parameter has one or more StatusListener instances registered. Returns false
40       * otherwise.
41       *
42       * @param context
43       * @return true if one or more StatusListeners registered, false otherwise
44       * @since 1.0.8
45       */
46      static public boolean contextHasStatusListener(Context context) {
47          StatusManager sm = context.getStatusManager();
48          if (sm == null)
49              return false;
50          List<StatusListener> listeners = sm.getCopyOfStatusListenerList();
51          if (listeners == null || listeners.size() == 0)
52              return false;
53          else
54              return true;
55      }
56  
57      static public List<Status> filterStatusListByTimeThreshold(List<Status> rawList, long threshold) {
58          List<Status> filteredList = new ArrayList<Status>();
59          for (Status s : rawList) {
60              if (s.getTimestamp() >= threshold)
61                  filteredList.add(s);
62          }
63          return filteredList;
64      }
65  
66      public void addStatus(Status status) {
67          if (sm != null) {
68              sm.add(status);
69          }
70      }
71  
72      public void addInfo(Object caller, String msg) {
73          addStatus(new InfoStatus(msg, caller));
74      }
75  
76      public void addWarn(Object caller, String msg) {
77          addStatus(new WarnStatus(msg, caller));
78      }
79  
80      public void addError(Object caller, String msg, Throwable t) {
81          addStatus(new ErrorStatus(msg, caller, t));
82      }
83  
84      public boolean hasXMLParsingErrors(long threshold) {
85          return containsMatch(threshold, Status.ERROR, CoreConstants.XML_PARSING);
86      }
87  
88      public boolean noXMLParsingErrorsOccurred(long threshold) {
89          return !hasXMLParsingErrors(threshold);
90      }
91  
92      public int getHighestLevel(long threshold) {
93          List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold);
94          int maxLevel = Status.INFO;
95          for (Status s : filteredList) {
96              if (s.getLevel() > maxLevel)
97                  maxLevel = s.getLevel();
98          }
99          return maxLevel;
100     }
101 
102     public boolean isErrorFree(long threshold) {
103         return getHighestLevel(threshold) < Status.ERROR;
104     }
105 
106     public boolean isWarningOrErrorFree(long threshold) {
107         return Status.WARN > getHighestLevel(threshold);
108     }
109 
110     public boolean containsMatch(long threshold, int level, String regex) {
111         List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold);
112         Pattern p = Pattern.compile(regex);
113 
114         for (Status status : filteredList) {
115             if (level != status.getLevel()) {
116                 continue;
117             }
118             String msg = status.getMessage();
119             Matcher matcher = p.matcher(msg);
120             if (matcher.lookingAt()) {
121                 return true;
122             }
123         }
124         return false;
125     }
126 
127     public boolean containsMatch(int level, String regex) {
128         return containsMatch(0, level, regex);
129     }
130 
131     public boolean containsMatch(String regex) {
132         Pattern p = Pattern.compile(regex);
133         for (Status status : sm.getCopyOfStatusList()) {
134             String msg = status.getMessage();
135             Matcher matcher = p.matcher(msg);
136             if (matcher.lookingAt()) {
137                 return true;
138             }
139         }
140         return false;
141     }
142 
143 
144     public int levelCount(int level, long threshold) {
145         List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold);
146 
147         int count = 0;
148         for (Status status : filteredList) {
149             if (status.getLevel() == level)
150                 count++;
151         }
152         return count;
153     }
154 
155     public int matchCount(String regex) {
156         int count = 0;
157         Pattern p = Pattern.compile(regex);
158         for (Status status : sm.getCopyOfStatusList()) {
159             String msg = status.getMessage();
160             Matcher matcher = p.matcher(msg);
161             if (matcher.lookingAt()) {
162                 count++;
163             }
164         }
165         return count;
166     }
167 
168     public boolean containsException(Class<?> exceptionType) {
169         return containsException(exceptionType, null);
170     }
171 
172     public boolean containsException(Class<?> exceptionType, String msgRegex) {
173         for (Status status : sm.getCopyOfStatusList()) {
174             Throwable t = status.getThrowable();
175             while (t != null) {
176                 if (t.getClass().getName().equals(exceptionType.getName())) {
177                     if (msgRegex == null) {
178                         return true;
179                     } else if (checkRegexMatch(t.getMessage(), msgRegex)) {
180                         return true;
181                     }
182                 }
183                 t = t.getCause();
184             }
185         }
186         return false;
187     }
188 
189     private boolean checkRegexMatch(String message, String msgRegex) {
190         Pattern p = Pattern.compile(msgRegex);
191         Matcher matcher = p.matcher(message);
192         return matcher.lookingAt();
193     }
194 
195 
196     /**
197      * Return the time of last reset. -1 if last reset time could not be found
198      *
199      * @return time of last reset or -1
200      */
201     public long timeOfLastReset() {
202         List<Status> statusList = sm.getCopyOfStatusList();
203         if (statusList == null)
204             return -1;
205 
206         int len = statusList.size();
207         for (int i = len - 1; i >= 0; i--) {
208             Status s = statusList.get(i);
209             if (CoreConstants.RESET_MSG_PREFIX.equals(s.getMessage())) {
210                 return s.getTimestamp();
211             }
212         }
213         return -1;
214     }
215 
216     public static String diff(Status left, Status right) {
217         StringBuilder sb = new StringBuilder();
218         if( left.getLevel() != right.getLevel()) {
219             sb.append(" left.level ").append(left.getLevel()).append(" != right.level ").append(right.getLevel());
220         }
221         if( left.getTimestamp() != right.getTimestamp()) {
222             sb.append(" left.timestamp ").append(left.getTimestamp()).append(" != right.timestamp ").append(right.getTimestamp());
223         }
224         if( !Objects.equals(left.getMessage(), right.getMessage())) {
225             sb.append(" left.message ").append(left.getMessage()).append(" != right.message ").append(right.getMessage());
226         }
227 
228         return sb.toString();
229     }
230 }