1/*
2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package jdk.nashorn.internal.runtime.options;
27
28import java.util.Collections;
29import java.util.HashMap;
30import java.util.Locale;
31import java.util.Map;
32import java.util.Map.Entry;
33import java.util.logging.Level;
34
35/**
36 * Class that collects logging options like --log=compiler:finest,fields,recompile:fine into
37 * a map form that can be used to instantiate loggers in the Global object on demand
38 */
39public class LoggingOption extends KeyValueOption {
40
41    /**
42     * Logging info. Basically a logger name maps to this,
43     * which is a tuple of log level and the "is quiet" flag,
44     * which is a special log level used to collect RuntimeEvents
45     * only, but not output anything
46     */
47    public static class LoggerInfo {
48        private final Level level;
49        private final boolean isQuiet;
50
51        LoggerInfo(final Level level, final boolean isQuiet) {
52            this.level = level;
53            this.isQuiet = isQuiet;
54        }
55
56        /**
57         * Get the log level
58         * @return log level
59         */
60        public Level getLevel() {
61            return level;
62        }
63
64        /**
65         * Get the quiet flag
66         * @return true if quiet flag is set
67         */
68        public boolean isQuiet() {
69            return isQuiet;
70        }
71    }
72
73    private final Map<String, LoggerInfo> loggers = new HashMap<>();
74
75    LoggingOption(final String value) {
76        super(value);
77        initialize(getValues());
78    }
79
80    /**
81     * Return the logger info collected from this command line option
82     *
83     * @return map of logger name to logger info
84     */
85    public Map<String, LoggerInfo> getLoggers() {
86        return Collections.unmodifiableMap(loggers);
87    }
88
89    /**
90     * Initialization function that is called to instantiate the logging system. It takes
91     * logger names (keys) and logging labels respectively
92     *
93     * @param map a map where the key is a logger name and the value a logging level
94     * @throws IllegalArgumentException if level or names cannot be parsed
95     */
96    private void initialize(final Map<String, String> logMap) throws IllegalArgumentException {
97        try {
98            for (final Entry<String, String> entry : logMap.entrySet()) {
99                Level level;
100                final String name        = lastPart(entry.getKey());
101                final String levelString = entry.getValue().toUpperCase(Locale.ENGLISH);
102                final boolean isQuiet;
103
104                if ("".equals(levelString)) {
105                    level = Level.INFO;
106                    isQuiet = false;
107                } else if ("QUIET".equals(levelString)) {
108                    level = Level.INFO;
109                    isQuiet = true;
110                } else {
111                    level = Level.parse(levelString);
112                    isQuiet = false;
113                }
114
115                loggers.put(name, new LoggerInfo(level, isQuiet));
116            }
117        } catch (final IllegalArgumentException | SecurityException e) {
118            throw e;
119        }
120    }
121
122
123
124    private static String lastPart(final String packageName) {
125        final String[] parts = packageName.split("\\.");
126        if (parts.length == 0) {
127            return packageName;
128        }
129        return parts[parts.length - 1];
130    }
131
132
133}
134