001/*
002 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.
008 *
009 * This code is distributed in the hope that it will be useful, but WITHOUT
010 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
012 * version 2 for more details (a copy is included in the LICENSE file that
013 * accompanied this code).
014 *
015 * You should have received a copy of the GNU General Public License version
016 * 2 along with this work; if not, write to the Free Software Foundation,
017 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
018 *
019 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
020 * or visit www.oracle.com if you need additional information or have any
021 * questions.
022 */
023package com.oracle.graal.debug;
024
025import java.util.*;
026import java.util.regex.*;
027
028import com.oracle.graal.debug.internal.*;
029
030/**
031 * Implements the filter specified by the {@link GraalDebugConfig#Dump},
032 * {@link GraalDebugConfig#Log}, {@link GraalDebugConfig#Meter} and {@link GraalDebugConfig#Time}
033 * options.
034 * <p>
035 * These options enable the associated debug facility if their filter matches the
036 * {@linkplain DebugScope#getQualifiedName() name} of the {@linkplain Debug#currentScope() current
037 * scope}. For the {@link GraalDebugConfig#Dump} and {@link GraalDebugConfig#Log} options, the log
038 * or dump level is set. The {@link GraalDebugConfig#Meter} and {@link GraalDebugConfig#Time}
039 * options don't have a level, for them {@code level = 0} means disabled and a {@code level > 0}
040 * means enabled.
041 * <p>
042 * A filter is a list of comma-separated terms of the form {@code <pattern>[:<level>]}.
043 * {@code <pattern>} is interpreted as a glob pattern if it contains a "*" or "?" character.
044 * Otherwise, it is interpreted as a substring. If {@code <pattern>} is empty, it matches every
045 * scope. If {@code :<level>} is omitted, it defaults to {@link Debug#DEFAULT_LOG_LEVEL}. The term
046 * {@code ~<pattern>} is a shorthand for {@code <pattern>:0} to disable a debug facility for a
047 * pattern.
048 * <p>
049 * The resulting log level of a scope is determined by the <em>last</em> matching term. If no term
050 * matches, the log level is 0 (disabled). A filter with no terms matches every scope with a log
051 * level of {@link Debug#DEFAULT_LOG_LEVEL}.
052 *
053 * <h2>Examples of filters</h2>
054 *
055 * <ul>
056 * <li>(empty string)<br>
057 * Matches any scope with log level {@link Debug#DEFAULT_LOG_LEVEL}.
058 *
059 * <li> {@code :1}<br>
060 * Matches any scope with log level 1.
061 *
062 * <li> {@code *}<br>
063 * Matches any scope with log level {@link Debug#DEFAULT_LOG_LEVEL}.
064 *
065 * <li> {@code CodeGen,CodeInstall}<br>
066 * Matches scopes containing "CodeGen" or "CodeInstall", both with log level
067 * {@link Debug#DEFAULT_LOG_LEVEL}.
068 *
069 * <li> {@code CodeGen:2,CodeInstall:1}<br>
070 * Matches scopes containing "CodeGen" with log level 2, or "CodeInstall" with log level 1.
071 *
072 * <li> {@code :1,Dead:2}<br>
073 * Matches scopes containing "Dead" with log level 2, and all other scopes with log level 1.
074 *
075 * <li> {@code :1,Dead:0}<br>
076 * Matches all scopes with log level 1, except those containing "Dead".
077 *
078 * <li> {@code Code*}<br>
079 * Matches scopes starting with "Code" with log level {@link Debug#DEFAULT_LOG_LEVEL}.
080 *
081 * <li> {@code Code,~Dead}<br>
082 * Matches scopes containing "Code" but not "Dead", with log level {@link Debug#DEFAULT_LOG_LEVEL}.
083 * </ul>
084 */
085final class DebugFilter {
086
087    public static DebugFilter parse(String spec) {
088        if (spec == null) {
089            return null;
090        }
091        return new DebugFilter(spec.split(","));
092    }
093
094    private final Term[] terms;
095
096    private DebugFilter(String[] terms) {
097        if (terms.length == 0) {
098            this.terms = null;
099        } else {
100            this.terms = new Term[terms.length];
101            for (int i = 0; i < terms.length; i++) {
102                String t = terms[i];
103                int idx = t.indexOf(':');
104
105                String pattern;
106                int level;
107                if (idx < 0) {
108                    if (t.startsWith("~")) {
109                        pattern = t.substring(1);
110                        level = 0;
111                    } else {
112                        pattern = t;
113                        level = Debug.DEFAULT_LOG_LEVEL;
114                    }
115                } else {
116                    pattern = t.substring(0, idx);
117                    if (idx + 1 < t.length()) {
118                        level = Integer.parseInt(t.substring(idx + 1));
119                    } else {
120                        level = Debug.DEFAULT_LOG_LEVEL;
121                    }
122                }
123
124                this.terms[i] = new Term(pattern, level);
125            }
126        }
127    }
128
129    /**
130     * Check whether a given input is matched by this filter, and determine the log level.
131     */
132    public int matchLevel(String input) {
133        if (terms == null) {
134            return Debug.DEFAULT_LOG_LEVEL;
135        } else {
136            int level = 0;
137            for (Term t : terms) {
138                if (t.matches(input)) {
139                    level = t.level;
140                }
141            }
142            return level;
143        }
144    }
145
146    @Override
147    public String toString() {
148        StringBuilder buf = new StringBuilder("DebugFilter");
149        if (terms != null) {
150            buf.append(Arrays.toString(terms));
151        } else {
152            buf.append("[]");
153        }
154        return buf.toString();
155    }
156
157    private static class Term {
158
159        private final Pattern pattern;
160        public final int level;
161
162        public Term(String filter, int level) {
163            this.level = level;
164            if (filter.isEmpty()) {
165                this.pattern = null;
166            } else if (filter.contains("*") || filter.contains("?")) {
167                this.pattern = Pattern.compile(MethodFilter.createGlobString(filter));
168            } else {
169                this.pattern = Pattern.compile(".*" + MethodFilter.createGlobString(filter) + ".*");
170            }
171        }
172
173        /**
174         * Determines if a given input is matched by this filter.
175         */
176        public boolean matches(String input) {
177            return pattern == null || pattern.matcher(input).matches();
178        }
179
180        @Override
181        public String toString() {
182            return (pattern == null ? ".*" : pattern.toString()) + ":" + level;
183        }
184    }
185}