src/jdk/nashorn/internal/runtime/DebugLogger.java

Mon, 11 Feb 2013 21:26:06 +0530

author
sundar
date
Mon, 11 Feb 2013 21:26:06 +0530
changeset 82
abea4ba28901
parent 57
59970b70ebb5
child 89
43e32b36153c
permissions
-rw-r--r--

8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
Reviewed-by: lagergren, jlaskey, attila

jlaskey@3 1 /*
jlaskey@7 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
jlaskey@3 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
jlaskey@3 4 *
jlaskey@3 5 * This code is free software; you can redistribute it and/or modify it
jlaskey@3 6 * under the terms of the GNU General Public License version 2 only, as
jlaskey@3 7 * published by the Free Software Foundation. Oracle designates this
jlaskey@3 8 * particular file as subject to the "Classpath" exception as provided
jlaskey@3 9 * by Oracle in the LICENSE file that accompanied this code.
jlaskey@3 10 *
jlaskey@3 11 * This code is distributed in the hope that it will be useful, but WITHOUT
jlaskey@3 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
jlaskey@3 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
jlaskey@3 14 * version 2 for more details (a copy is included in the LICENSE file that
jlaskey@3 15 * accompanied this code).
jlaskey@3 16 *
jlaskey@3 17 * You should have received a copy of the GNU General Public License version
jlaskey@3 18 * 2 along with this work; if not, write to the Free Software Foundation,
jlaskey@3 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
jlaskey@3 20 *
jlaskey@3 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
jlaskey@3 22 * or visit www.oracle.com if you need additional information or have any
jlaskey@3 23 * questions.
jlaskey@3 24 */
jlaskey@3 25
jlaskey@3 26 package jdk.nashorn.internal.runtime;
jlaskey@3 27
jlaskey@3 28 import java.io.PrintWriter;
jlaskey@3 29 import java.util.logging.Level;
jlaskey@3 30 import java.util.logging.Logger;
jlaskey@3 31 import jdk.nashorn.internal.runtime.options.Options;
jlaskey@3 32
jlaskey@3 33 /**
jlaskey@3 34 * Wrapper class for Logging system. This is how you are supposed to register a logger and use it
jlaskey@3 35 */
jlaskey@3 36
sundar@82 37 public final class DebugLogger {
jlaskey@3 38 @SuppressWarnings("NonConstantLogger")
jlaskey@3 39 private final Logger logger;
jlaskey@3 40 private final boolean isEnabled;
jlaskey@3 41
jlaskey@3 42 private int indent;
jlaskey@3 43
lagergren@57 44 private static final int INDENT_SPACE = 4;
lagergren@57 45
jlaskey@3 46 /**
jlaskey@3 47 * Constructor
jlaskey@3 48 *
jlaskey@3 49 * @param loggerName name of logger - this is the unique key with which it can be identified
jlaskey@3 50 */
jlaskey@3 51 public DebugLogger(final String loggerName) {
jlaskey@3 52 this(loggerName, null);
jlaskey@3 53 }
jlaskey@3 54
jlaskey@3 55 /**
jlaskey@3 56 * Constructor
jlaskey@3 57 *
jlaskey@3 58 * A logger can be paired with a property, e.g. {@code --log:codegen:info} is equivalent to {@code -Dnashorn.codegen.log}
jlaskey@3 59 *
jlaskey@3 60 * @param loggerName name of logger - this is the unique key with which it can be identified
jlaskey@3 61 * @param property system property activating the logger on {@code info} level
jlaskey@3 62 */
jlaskey@3 63 public DebugLogger(final String loggerName, final String property) {
lagergren@11 64 if (property != null && Options.getBooleanProperty(property)) {
lagergren@11 65 this.logger = Logging.getOrCreateLogger(loggerName, Level.INFO);
lagergren@11 66 } else {
lagergren@11 67 this.logger = Logging.getLogger(loggerName);
lagergren@11 68 }
lagergren@11 69 this.isEnabled = logger.getLevel() != Level.OFF;
jlaskey@3 70 }
jlaskey@3 71
jlaskey@3 72 /**
jlaskey@3 73 * Get the output writer for the logger. Loggers always default to
jlaskey@3 74 * stderr for output as they are used mainly to output debug info
jlaskey@3 75 *
jlaskey@3 76 * @return print writer for log output.
jlaskey@3 77 */
jlaskey@3 78 public PrintWriter getOutputStream() {
sundar@41 79 return Context.getCurrentErr();
jlaskey@3 80 }
jlaskey@3 81
jlaskey@3 82 /**
jlaskey@3 83 * Check if the logger is enabled
jlaskey@3 84 * @return true if enabled
jlaskey@3 85 */
jlaskey@3 86 public boolean isEnabled() {
jlaskey@3 87 return isEnabled;
jlaskey@3 88 }
jlaskey@3 89
jlaskey@3 90 /**
jlaskey@3 91 * If you want to change the indent level of your logger, call indent with a new position.
jlaskey@3 92 * Positions start at 0 and are increased by one for a new "tab"
jlaskey@3 93 *
jlaskey@3 94 * @param pos indent position
jlaskey@3 95 */
jlaskey@3 96 public void indent(final int pos) {
jlaskey@3 97 if (isEnabled) {
lagergren@57 98 indent += pos * INDENT_SPACE;
lagergren@57 99 }
lagergren@57 100 }
lagergren@57 101
lagergren@57 102 /**
lagergren@57 103 * Add an indent position
lagergren@57 104 */
lagergren@57 105 public void indent() {
lagergren@57 106 indent += INDENT_SPACE;
lagergren@57 107 }
lagergren@57 108
lagergren@57 109 /**
lagergren@57 110 * Unindent a position
lagergren@57 111 */
lagergren@57 112 public void unindent() {
lagergren@57 113 indent -= INDENT_SPACE;
lagergren@57 114 if (indent < 0) {
lagergren@57 115 indent = 0;
jlaskey@3 116 }
jlaskey@3 117 }
jlaskey@3 118
jlaskey@3 119 /**
jlaskey@3 120 * Check if the logger is above of the level of detail given
jlaskey@3 121 * @see java.util.logging.Level
jlaskey@3 122 *
jlaskey@3 123 * @param level logging level
jlaskey@3 124 * @return true if level is above the given one
jlaskey@3 125 */
jlaskey@3 126 public boolean levelAbove(final Level level) {
jlaskey@3 127 return logger.getLevel().intValue() > level.intValue();
jlaskey@3 128 }
jlaskey@3 129
jlaskey@3 130 /**
jlaskey@3 131 * Shorthand for outputting a log string as log level {@link java.util.logging.Level#FINEST} on this logger
jlaskey@3 132 * @param str the string to log
jlaskey@3 133 */
jlaskey@3 134 public void finest(final String str) {
jlaskey@3 135 log(str, Level.FINEST);
jlaskey@3 136 }
jlaskey@3 137
jlaskey@3 138 /**
jlaskey@3 139 * Shorthand for outputting a log string as log level
jlaskey@3 140 * {@link java.util.logging.Level#FINER} on this logger
jlaskey@3 141 * @param str the string to log
jlaskey@3 142 */
jlaskey@3 143 public void finer(final String str) {
jlaskey@3 144 log(str, Level.FINER);
jlaskey@3 145 }
jlaskey@3 146
jlaskey@3 147 /**
jlaskey@3 148 * Shorthand for outputting a log string as log level
jlaskey@3 149 * {@link java.util.logging.Level#FINE} on this logger
jlaskey@3 150 * @param str the string to log
jlaskey@3 151 */
jlaskey@3 152 public void fine(final String str) {
jlaskey@3 153 log(str, Level.FINE);
jlaskey@3 154 }
jlaskey@3 155
jlaskey@3 156 /**
jlaskey@3 157 * Shorthand for outputting a log string as log level
jlaskey@3 158 * {@link java.util.logging.Level#CONFIG} on this logger
jlaskey@3 159 * @param str the string to log
jlaskey@3 160 */
jlaskey@3 161 public void config(final String str) {
jlaskey@3 162 log(str, Level.CONFIG);
jlaskey@3 163 }
jlaskey@3 164
jlaskey@3 165 /**
jlaskey@3 166 * Shorthand for outputting a log string as log level
jlaskey@3 167 * {@link java.util.logging.Level#INFO} on this logger
jlaskey@3 168 * @param str the string to log
jlaskey@3 169 */
jlaskey@3 170 public void info(final String str) {
jlaskey@3 171 log(str, Level.INFO);
jlaskey@3 172 }
jlaskey@3 173
jlaskey@3 174 /**
jlaskey@3 175 * Shorthand for outputting a log string as log level
jlaskey@3 176 * {@link java.util.logging.Level#WARNING} on this logger
jlaskey@3 177 * @param str the string to log
jlaskey@3 178 */
jlaskey@3 179 public void warning(final String str) {
jlaskey@3 180 log(str, Level.WARNING);
jlaskey@3 181 }
jlaskey@3 182
jlaskey@3 183 /**
jlaskey@3 184 * Shorthand for outputting a log string as log level
jlaskey@3 185 * {@link java.util.logging.Level#SEVERE} on this logger
jlaskey@3 186 * @param str the string to log
jlaskey@3 187 */
jlaskey@3 188 public void severe(final String str) {
jlaskey@3 189 log(str, Level.SEVERE);
jlaskey@3 190 }
jlaskey@3 191
jlaskey@3 192 /**
jlaskey@3 193 * Output log line on this logger at a given level of verbosity
jlaskey@3 194 * @see java.util.logging.Level
jlaskey@3 195 *
jlaskey@3 196 * @param str string to log
jlaskey@3 197 * @param level minimum log level required for logging to take place
jlaskey@3 198 */
jlaskey@3 199 public void log(final String str, final Level level) {
jlaskey@3 200 if (isEnabled) {
jlaskey@3 201 final StringBuilder sb = new StringBuilder();
jlaskey@3 202
jlaskey@3 203 for (int i = 0 ; i < indent ; i++) {
jlaskey@3 204 sb.append(' ');
jlaskey@3 205 }
jlaskey@3 206 sb.append(str);
jlaskey@3 207 logger.log(level, sb.toString());
jlaskey@3 208 }
jlaskey@3 209 }
jlaskey@3 210 }

mercurial