001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 * <p>
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 * <p>
009 * or (per the licensee's choosing)
010 * <p>
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.status;
015
016import ch.qos.logback.core.Context;
017import ch.qos.logback.core.CoreConstants;
018
019import java.util.ArrayList;
020import java.util.List;
021import java.util.Objects;
022import java.util.regex.Matcher;
023import java.util.regex.Pattern;
024
025public class StatusUtil {
026
027    StatusManager sm;
028
029    public StatusUtil(StatusManager sm) {
030        this.sm = sm;
031    }
032
033    public StatusUtil(Context context) {
034        this.sm = context.getStatusManager();
035    }
036
037    /**
038     * Returns true if the StatusManager associated with the context passed as
039     * parameter has one or more StatusListener instances registered. Returns false
040     * otherwise.
041     *
042     * @param context
043     * @return true if one or more StatusListeners registered, false otherwise
044     * @since 1.0.8
045     */
046    static public boolean contextHasStatusListener(Context context) {
047        StatusManager sm = context.getStatusManager();
048        if (sm == null)
049            return false;
050        List<StatusListener> listeners = sm.getCopyOfStatusListenerList();
051        if (listeners == null || listeners.size() == 0)
052            return false;
053        else
054            return true;
055    }
056
057    static public List<Status> filterStatusListByTimeThreshold(List<Status> rawList, long threshold) {
058        List<Status> filteredList = new ArrayList<Status>();
059        for (Status s : rawList) {
060            if (s.getTimestamp() >= threshold)
061                filteredList.add(s);
062        }
063        return filteredList;
064    }
065
066    public void addStatus(Status status) {
067        if (sm != null) {
068            sm.add(status);
069        }
070    }
071
072    public void addInfo(Object caller, String msg) {
073        addStatus(new InfoStatus(msg, caller));
074    }
075
076    public void addWarn(Object caller, String msg) {
077        addStatus(new WarnStatus(msg, caller));
078    }
079
080    public void addError(Object caller, String msg, Throwable t) {
081        addStatus(new ErrorStatus(msg, caller, t));
082    }
083
084    public boolean hasXMLParsingErrors(long threshold) {
085        return containsMatch(threshold, Status.ERROR, CoreConstants.XML_PARSING);
086    }
087
088    public boolean noXMLParsingErrorsOccurred(long threshold) {
089        return !hasXMLParsingErrors(threshold);
090    }
091
092    public int getHighestLevel(long threshold) {
093        List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold);
094        int maxLevel = Status.INFO;
095        for (Status s : filteredList) {
096            if (s.getLevel() > maxLevel)
097                maxLevel = s.getLevel();
098        }
099        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    public int levelCount(int level, long threshold) {
144        List<Status> filteredList = filterStatusListByTimeThreshold(sm.getCopyOfStatusList(), threshold);
145
146        int count = 0;
147        for (Status status : filteredList) {
148            if (status.getLevel() == level)
149                count++;
150        }
151        return count;
152    }
153
154    public int matchCount(String regex) {
155        int count = 0;
156        Pattern p = Pattern.compile(regex);
157        for (Status status : sm.getCopyOfStatusList()) {
158            String msg = status.getMessage();
159            Matcher matcher = p.matcher(msg);
160            if (matcher.lookingAt()) {
161                count++;
162            }
163        }
164        return count;
165    }
166
167    public boolean containsException(Class<?> exceptionType) {
168        return containsException(exceptionType, null);
169    }
170
171    public boolean containsException(Class<?> exceptionType, String msgRegex) {
172        for (Status status : sm.getCopyOfStatusList()) {
173            Throwable t = status.getThrowable();
174            while (t != null) {
175                if (t.getClass().getName().equals(exceptionType.getName())) {
176                    if (msgRegex == null) {
177                        return true;
178                    } else if (checkRegexMatch(t.getMessage(), msgRegex)) {
179                        return true;
180                    }
181                }
182                t = t.getCause();
183            }
184        }
185        return false;
186    }
187
188    private boolean checkRegexMatch(String message, String msgRegex) {
189        Pattern p = Pattern.compile(msgRegex);
190        Matcher matcher = p.matcher(message);
191        return matcher.lookingAt();
192    }
193
194
195    /**
196     * Return the time of last reset. -1 if last reset time could not be found
197     *
198     * @return time of last reset or -1
199     */
200    public long timeOfLastReset() {
201        List<Status> statusList = sm.getCopyOfStatusList();
202        if (statusList == null)
203            return -1;
204
205        int len = statusList.size();
206        for (int i = len - 1; i >= 0; i--) {
207            Status s = statusList.get(i);
208            if (CoreConstants.RESET_MSG_PREFIX.equals(s.getMessage())) {
209                return s.getTimestamp();
210            }
211        }
212        return -1;
213    }
214
215    public static String diff(Status left, Status right) {
216        StringBuilder sb = new StringBuilder();
217        if( left.getLevel() != right.getLevel()) {
218            sb.append(" left.level ").append(left.getLevel()).append(" != right.level ").append(right.getLevel());
219        }
220        if( left.getTimestamp() != right.getTimestamp()) {
221            sb.append(" left.timestamp ").append(left.getTimestamp()).append(" != right.timestamp ").append(right.getTimestamp());
222        }
223        if( !Objects.equals(left.getMessage(), right.getMessage())) {
224            sb.append(" left.message ").append(left.getMessage()).append(" != right.message ").append(right.getMessage());
225        }
226
227        return sb.toString();
228    }
229}