author | sundar |
Mon, 11 Feb 2013 21:26:06 +0530 | |
changeset 16226 | 0e4f37e6cc40 |
parent 16201 | 889ddb179cdf |
child 16233 | 95d3e01c04c3 |
permissions | -rw-r--r-- |
16147 | 1 |
/* |
16151 | 2 |
* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. |
16147 | 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 |
||
26 |
package jdk.nashorn.internal.runtime; |
|
27 |
||
28 |
import java.io.PrintWriter; |
|
29 |
import java.util.logging.Level; |
|
30 |
import java.util.logging.Logger; |
|
31 |
import jdk.nashorn.internal.runtime.options.Options; |
|
32 |
||
33 |
/** |
|
34 |
* Wrapper class for Logging system. This is how you are supposed to register a logger and use it |
|
35 |
*/ |
|
36 |
||
16226
0e4f37e6cc40
8007915: Nashorn IR, codegen, parser packages and Context instance should be inaccessible to user code
sundar
parents:
16201
diff
changeset
|
37 |
public final class DebugLogger { |
16147 | 38 |
@SuppressWarnings("NonConstantLogger") |
39 |
private final Logger logger; |
|
40 |
private final boolean isEnabled; |
|
41 |
||
42 |
private int indent; |
|
43 |
||
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
44 |
private static final int INDENT_SPACE = 4; |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
45 |
|
16147 | 46 |
/** |
47 |
* Constructor |
|
48 |
* |
|
49 |
* @param loggerName name of logger - this is the unique key with which it can be identified |
|
50 |
*/ |
|
51 |
public DebugLogger(final String loggerName) { |
|
52 |
this(loggerName, null); |
|
53 |
} |
|
54 |
||
55 |
/** |
|
56 |
* Constructor |
|
57 |
* |
|
58 |
* A logger can be paired with a property, e.g. {@code --log:codegen:info} is equivalent to {@code -Dnashorn.codegen.log} |
|
59 |
* |
|
60 |
* @param loggerName name of logger - this is the unique key with which it can be identified |
|
61 |
* @param property system property activating the logger on {@code info} level |
|
62 |
*/ |
|
63 |
public DebugLogger(final String loggerName, final String property) { |
|
16155
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
64 |
if (property != null && Options.getBooleanProperty(property)) { |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
65 |
this.logger = Logging.getOrCreateLogger(loggerName, Level.INFO); |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
66 |
} else { |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
67 |
this.logger = Logging.getLogger(loggerName); |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
68 |
} |
a8ab83cbaa49
8005788: Loggers and their corresponding system properties not working correctly
lagergren
parents:
16151
diff
changeset
|
69 |
this.isEnabled = logger.getLevel() != Level.OFF; |
16147 | 70 |
} |
71 |
||
72 |
/** |
|
73 |
* Get the output writer for the logger. Loggers always default to |
|
74 |
* stderr for output as they are used mainly to output debug info |
|
75 |
* |
|
76 |
* @return print writer for log output. |
|
77 |
*/ |
|
78 |
public PrintWriter getOutputStream() { |
|
16185 | 79 |
return Context.getCurrentErr(); |
16147 | 80 |
} |
81 |
||
82 |
/** |
|
83 |
* Check if the logger is enabled |
|
84 |
* @return true if enabled |
|
85 |
*/ |
|
86 |
public boolean isEnabled() { |
|
87 |
return isEnabled; |
|
88 |
} |
|
89 |
||
90 |
/** |
|
91 |
* If you want to change the indent level of your logger, call indent with a new position. |
|
92 |
* Positions start at 0 and are increased by one for a new "tab" |
|
93 |
* |
|
94 |
* @param pos indent position |
|
95 |
*/ |
|
96 |
public void indent(final int pos) { |
|
97 |
if (isEnabled) { |
|
16201
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
98 |
indent += pos * INDENT_SPACE; |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
99 |
} |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
100 |
} |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
101 |
|
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
102 |
/** |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
103 |
* Add an indent position |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
104 |
*/ |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
105 |
public void indent() { |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
106 |
indent += INDENT_SPACE; |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
107 |
} |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
108 |
|
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
109 |
/** |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
110 |
* Unindent a position |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
111 |
*/ |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
112 |
public void unindent() { |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
113 |
indent -= INDENT_SPACE; |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
114 |
if (indent < 0) { |
889ddb179cdf
8007062: Split Lower up into Lower/Attr/FinalizeTypes. Integrate AccessSpecalizer into FinalizeTypes.
lagergren
parents:
16185
diff
changeset
|
115 |
indent = 0; |
16147 | 116 |
} |
117 |
} |
|
118 |
||
119 |
/** |
|
120 |
* Check if the logger is above of the level of detail given |
|
121 |
* @see java.util.logging.Level |
|
122 |
* |
|
123 |
* @param level logging level |
|
124 |
* @return true if level is above the given one |
|
125 |
*/ |
|
126 |
public boolean levelAbove(final Level level) { |
|
127 |
return logger.getLevel().intValue() > level.intValue(); |
|
128 |
} |
|
129 |
||
130 |
/** |
|
131 |
* Shorthand for outputting a log string as log level {@link java.util.logging.Level#FINEST} on this logger |
|
132 |
* @param str the string to log |
|
133 |
*/ |
|
134 |
public void finest(final String str) { |
|
135 |
log(str, Level.FINEST); |
|
136 |
} |
|
137 |
||
138 |
/** |
|
139 |
* Shorthand for outputting a log string as log level |
|
140 |
* {@link java.util.logging.Level#FINER} on this logger |
|
141 |
* @param str the string to log |
|
142 |
*/ |
|
143 |
public void finer(final String str) { |
|
144 |
log(str, Level.FINER); |
|
145 |
} |
|
146 |
||
147 |
/** |
|
148 |
* Shorthand for outputting a log string as log level |
|
149 |
* {@link java.util.logging.Level#FINE} on this logger |
|
150 |
* @param str the string to log |
|
151 |
*/ |
|
152 |
public void fine(final String str) { |
|
153 |
log(str, Level.FINE); |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* Shorthand for outputting a log string as log level |
|
158 |
* {@link java.util.logging.Level#CONFIG} on this logger |
|
159 |
* @param str the string to log |
|
160 |
*/ |
|
161 |
public void config(final String str) { |
|
162 |
log(str, Level.CONFIG); |
|
163 |
} |
|
164 |
||
165 |
/** |
|
166 |
* Shorthand for outputting a log string as log level |
|
167 |
* {@link java.util.logging.Level#INFO} on this logger |
|
168 |
* @param str the string to log |
|
169 |
*/ |
|
170 |
public void info(final String str) { |
|
171 |
log(str, Level.INFO); |
|
172 |
} |
|
173 |
||
174 |
/** |
|
175 |
* Shorthand for outputting a log string as log level |
|
176 |
* {@link java.util.logging.Level#WARNING} on this logger |
|
177 |
* @param str the string to log |
|
178 |
*/ |
|
179 |
public void warning(final String str) { |
|
180 |
log(str, Level.WARNING); |
|
181 |
} |
|
182 |
||
183 |
/** |
|
184 |
* Shorthand for outputting a log string as log level |
|
185 |
* {@link java.util.logging.Level#SEVERE} on this logger |
|
186 |
* @param str the string to log |
|
187 |
*/ |
|
188 |
public void severe(final String str) { |
|
189 |
log(str, Level.SEVERE); |
|
190 |
} |
|
191 |
||
192 |
/** |
|
193 |
* Output log line on this logger at a given level of verbosity |
|
194 |
* @see java.util.logging.Level |
|
195 |
* |
|
196 |
* @param str string to log |
|
197 |
* @param level minimum log level required for logging to take place |
|
198 |
*/ |
|
199 |
public void log(final String str, final Level level) { |
|
200 |
if (isEnabled) { |
|
201 |
final StringBuilder sb = new StringBuilder(); |
|
202 |
||
203 |
for (int i = 0 ; i < indent ; i++) { |
|
204 |
sb.append(' '); |
|
205 |
} |
|
206 |
sb.append(str); |
|
207 |
logger.log(level, sb.toString()); |
|
208 |
} |
|
209 |
} |
|
210 |
} |