GlueGen: src/java/com/jogamp/gluegen/Logging.java Source File

GlueGen v2.6.0 GlueGen, Native Binding Generator for Java™ (public API).

Logging.java
Go to the documentation of this file.
1/**
2 * Copyright 2010 JogAmp Community. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without modification, are
5 * permitted provided that the following conditions are met:
6 *
7 * 1. Redistributions of source code must retain the above copyright notice, this list of
8 * conditions and the following disclaimer.
9 *
10 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11 * of conditions and the following disclaimer in the documentation and/or other materials
12 * provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY JogAmp Community ``AS IS'' AND ANY EXPRESS OR IMPLIED
15 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JogAmp Community OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
19 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
20 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
21 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23 *
24 * The views and conclusions contained in the software and documentation are those of the
25 * authors and should not be interpreted as representing official policies, either expressed
26 * or implied, of JogAmp Community.
27 */
28
29/*
30 * Created on Wednesday, March 31 2010 13:30
31 */
32package com.jogamp.gluegen;
33
34import java.util.HashMap;
35import java.util.Map;
36import java.util.logging.ConsoleHandler;
37import java.util.logging.Formatter;
38import java.util.logging.Handler;
39import java.util.logging.Level;
40import java.util.logging.LogRecord;
41import java.util.logging.Logger;
42
43import jogamp.common.Debug;
44
45import com.jogamp.common.util.PropertyAccess;
46import com.jogamp.gluegen.cgram.types.AliasedSymbol;
47import com.jogamp.gluegen.cgram.types.Type;
48
49/**
50 *
51 * @author Michael Bien, et.al.
52 */
53 public class Logging {
54 public static final boolean DEBUG = Debug.debug("Logging");
55
56 /**
57 * An interface for {@link Logger}.
58 */
59 public static interface LoggerIf {
60 /**
61 * See {@link Logger#info(String)}
62 */
63 void info(final String msg);
64 /**
65 * See {@link Logger#info(String)}
66 */
67 void info(final ASTLocusTag loc, final String msg);
68
69 /**
70 * See {@link Logger#warning(String)}
71 */
72 void warning(final String msg);
73 /**
74 * See {@link Logger#warning(String)}
75 */
76 void warning(final ASTLocusTag loc, final String msg);
77
78 /**
79 * Calls {@link #log(Level, String)} w/ {@link Level#FINE}.
80 */
81 void debug(final String msg);
82 /**
83 * Calls {@link #log(Level, ASTLocusTag, String)} w/ {@link Level#FINE}.
84 */
85 void debug(final ASTLocusTag loc, final String msg);
86
87 /**
88 * See {@link Logger#log(Level, String)}
89 */
90 void log(final Level level, final String msg);
91 /**
92 * See {@link Logger#log(Level, String, Object)}
93 */
94 void log(final Level level, final String msg, final Object param);
95 /**
96 * See {@link Logger#log(Level, String, Object[])}
97 */
98 void log(final Level level, final String msg, final Object ... params);
99
100 /**
101 * See {@link Logger#log(Level, String)}
102 */
103 void log(final Level level, final ASTLocusTag loc, final String msg);
104 /**
105 * See {@link Logger#log(Level, String, Object)}
106 */
107 void log(final Level level, final ASTLocusTag loc, final String msg, final Object param);
108 /**
109 * See {@link Logger#log(Level, String, Object[])}
110 */
111 void log(final Level level, final ASTLocusTag loc, final String msg, final Object ... params);
112
113 /**
114 * See {@link Logger#setLevel(Level)}
115 */
116 void setLevel(final Level newLevel) throws SecurityException;
117 /**
118 * See {@link Handler#setLevel(Level)}
119 */
120 void setLevelOfAllHandler(final Level newLevel) throws SecurityException;
121 /**
122 * See {@link Logger#getLevel()}
123 */
124 Level getLevel();
125 /**
126 * See {@link Logger#isLoggable(Level)}
127 */
128 boolean isLoggable(Level level);
129 /**
130 * See {@link Logger#getName()}
131 */
132 String getName();
133 /**
134 * See {@link Logger#getHandlers()}
135 */
136 Handler[] getHandlers();
137 /**
138 * See {@link LogRecord#getSourceClassName()}
139 */
140 String getSourceClassName();
141 }
142 /* pp */ static class FQNLogger implements LoggerIf {
143 public final Logger impl;
144 public final PlainLogConsoleHandler handler;
145 /* pp */ FQNLogger(final String fqnClassName, final String simpleClassName, final Level level) {
146 this.impl = Logger.getLogger(fqnClassName);
147 this.handler = new PlainLogConsoleHandler(new PlainLogFormatter(simpleClassName), Level.ALL);
148 this.impl.setUseParentHandlers(false);
149 this.impl.setLevel(level);
150 this.impl.addHandler(this.handler);
151 this.impl.log(Level.INFO, "Logging.new: "+impl.getName()+": level "+level+
152 ": obj 0x"+Integer.toHexString(impl.hashCode()));
153 }
154 @Override
155 public void info(final String msg) {
156 impl.info(msg);
157 }
158 @Override
159 public void info(final ASTLocusTag loc, final String msg) {
160 handler.plf.setASTLocusTag(loc);
161 try {
162 impl.info(msg);
163 } finally {
164 handler.plf.setASTLocusTag(null);
165 }
166 }
167
168 @Override
169 public void warning(final String msg) {
170 impl.warning(msg);
171 }
172 @Override
173 public void warning(final ASTLocusTag loc, final String msg) {
174 handler.plf.setASTLocusTag(loc);
175 try {
176 impl.warning(msg);
177 } finally {
178 handler.plf.setASTLocusTag(null);
179 }
180 }
181
182 @Override
183 public void debug(final String msg) {
184 log(Level.FINE, msg);
185 }
186 @Override
187 public void debug(final ASTLocusTag loc, final String msg) {
188 log(Level.FINE, loc, msg);
189 }
190
191 @Override
192 public void log(final Level level, final String msg) {
193 impl.log(level, msg);
194 }
195 @Override
196 public void log(final Level level, final String msg, final Object param) {
197 impl.log(level, msg, param);
198 }
199 @Override
200 public void log(final Level level, final String msg, final Object ... params) {
201 impl.log(level, msg, params);
202 }
203
204 @Override
205 public void log(final Level level, final ASTLocusTag loc, final String msg) {
206 handler.plf.setASTLocusTag(loc);
207 try {
208 impl.log(level, msg);
209 } finally {
210 handler.plf.setASTLocusTag(null);
211 }
212 }
213 @Override
214 public void log(final Level level, final ASTLocusTag loc, final String msg, final Object param) {
215 handler.plf.setASTLocusTag(loc);
216 try {
217 impl.log(level, msg, param);
218 } finally {
219 handler.plf.setASTLocusTag(null);
220 }
221 }
222 @Override
223 public void log(final Level level, final ASTLocusTag loc, final String msg, final Object ... params) {
224 handler.plf.setASTLocusTag(loc);
225 try {
226 impl.log(level, msg, params);
227 } finally {
228 handler.plf.setASTLocusTag(null);
229 }
230 }
231
232 @Override
233 public void setLevel(final Level newLevel) throws SecurityException {
234 impl.setLevel(newLevel);
235 }
236 @Override
237 public void setLevelOfAllHandler(final Level newLevel) throws SecurityException {
238 final Handler[] hs = getHandlers();
239 for(final Handler h:hs) {
240 h.setLevel(newLevel);
241 }
242 }
243 @Override
244 public Level getLevel() {
245 return impl.getLevel();
246 }
247 @Override
248 public boolean isLoggable(final Level level) {
249 return impl.isLoggable(level);
250 }
251 @Override
252 public String getName() {
253 return impl.getName();
254 }
255 @Override
256 public synchronized Handler[] getHandlers() {
257 return impl.getHandlers();
258 }
259 @Override
260 public String getSourceClassName() {
261 return handler.plf.simpleClassName;
262 }
263 }
264 static class PlainLogConsoleHandler extends ConsoleHandler {
265 final PlainLogFormatter plf;
266 PlainLogConsoleHandler(final PlainLogFormatter plf, final Level level) {
267 this.plf = plf;
268 setFormatter(plf);
269 setLevel(level);
270 }
271 @Override
272 public java.util.logging.Formatter getFormatter() {
273 return plf;
274 }
275 }
276 static class PlainLogFormatter extends Formatter {
277 final String simpleClassName;
278 ASTLocusTag astLocus;
279 PlainLogFormatter(final String simpleClassName) {
280 this.simpleClassName = simpleClassName;
281 }
282 public void setASTLocusTag(final ASTLocusTag loc) { astLocus = loc; }
283 @Override
284 public String format(final LogRecord record) {
285 // Replace [Type, JavaType] -> its debug string!
286 final Object[] params = record.getParameters();
287 if( null != params ) {
288 for(int i=params.length-1; 0<=i; i--) {
289 final Object o = params[i];
290 if( o instanceof Type ) {
291 params[i] = ((Type)o).getDebugString();
292 } else if( o instanceof JavaType ) {
293 params[i] = ((JavaType)o).getDebugString();
294 } else if( o instanceof AliasedSymbol ) {
295 params[i] = ((AliasedSymbol)o).getAliasedString();
296 }
297 }
298 }
299 final StringBuilder sb = new StringBuilder(256);
300 if( null != astLocus ) {
301 astLocus.toString(sb, getCanonicalName(record.getLevel()), GlueGen.debug()).append(": ");
302 }
303 if( GlueGen.debug() ) {
304 sb.append(simpleClassName).append(": ");
305 }
306 sb.append(formatMessage(record)).append("\n");
307 return sb.toString();
308 }
309 }
310
311 private final static Map<String, LoggerIf> loggers;
312 private final static FQNLogger rootPackageLogger;
313 static {
314 loggers = new HashMap<String, LoggerIf>();
315 final String packageName = Logging.class.getPackage().getName();
316 final String property = PropertyAccess.getProperty(packageName+".level", true);
317 Level level;
318 if(property != null) {
319 level = Level.parse(property);
320 } else {
321 if( DEBUG || GlueGen.debug() ) {
322 level = Level.ALL;
323 } else {
324 level = Level.WARNING;
325 }
326 }
327 final String simpleClassName = Logging.class.getSimpleName();
328 final String fqnClassName = packageName+"."+simpleClassName;
329 rootPackageLogger = new FQNLogger(fqnClassName, simpleClassName, level);
330 loggers.put(fqnClassName, rootPackageLogger);
331 }
332
333 /** provokes static initialization */
334 static void init() { }
335
336 public static String getCanonicalName(final Level level) {
337 if( Level.CONFIG == level ) {
338 return "config";
339 } else if( Level.FINER == level ) {
340 return "verbose";
341 } else if( Level.FINE == level ) {
342 return "debug";
343 } else if( Level.INFO == level ) {
344 return "info";
345 } else if( Level.WARNING == level ) {
346 return "warning";
347 } else if( Level.SEVERE == level ) {
348 return "error";
349 } else {
350 return level.getName().toLowerCase();
351 }
352 }
353
354 /** Returns the <i>root package logger</i>. */
355 public static LoggerIf getLogger() {
356 return rootPackageLogger;
357 }
358 /** Returns the demanded logger, while aligning its log-level to the root logger's level. */
359 public static synchronized LoggerIf getLogger(final Class<?> clazz) {
360 return getLogger(clazz.getPackage().getName(), clazz.getSimpleName());
361 }
362
363 /** Returns the demanded logger, while aligning its log-level to the root logger's level. */
364 public static synchronized LoggerIf getLogger(final String packageName, final String simpleClassName) {
365 final String fqnClassName = packageName+"."+simpleClassName;
366 LoggerIf res = loggers.get(fqnClassName);
367 if( null == res ) {
368 res = new FQNLogger(fqnClassName, simpleClassName, rootPackageLogger.getLevel());
369 loggers.put(fqnClassName, res);
370 }
371 return res;
372 }
373 /** Align log-level of given logger to the root logger's level. */
374 public static void alignLevel(final LoggerIf l) {
375 alignLevel(l, rootPackageLogger.getLevel());
376 }
377 /** Align log-level of given logger and all its handlers to the given level. */
378 public static void alignLevel(final LoggerIf l, final Level level) {
379 l.setLevel(level);
380 l.setLevelOfAllHandler(level);
381 }
382}
com.jogamp.gluegen.ASTLocusTag
An AST location tag.
Definition: ASTLocusTag.java:33
com.jogamp.gluegen.Logging
Definition: Logging.java:53
com.jogamp.gluegen.Logging.getLogger
static synchronized LoggerIf getLogger(final Class<?> clazz)
Returns the demanded logger, while aligning its log-level to the root logger's level.
Definition: Logging.java:359
com.jogamp.gluegen.Logging.alignLevel
static void alignLevel(final LoggerIf l)
Align log-level of given logger to the root logger's level.
Definition: Logging.java:374
com.jogamp.gluegen.Logging.getLogger
static LoggerIf getLogger()
Returns the root package logger.
Definition: Logging.java:355
com.jogamp.gluegen.Logging.alignLevel
static void alignLevel(final LoggerIf l, final Level level)
Align log-level of given logger and all its handlers to the given level.
Definition: Logging.java:378
com.jogamp.gluegen.Logging.DEBUG
static final boolean DEBUG
Definition: Logging.java:54
com.jogamp.gluegen.Logging.getCanonicalName
static String getCanonicalName(final Level level)
Definition: Logging.java:336
com.jogamp.gluegen.Logging.getLogger
static synchronized LoggerIf getLogger(final String packageName, final String simpleClassName)
Returns the demanded logger, while aligning its log-level to the root logger's level.
Definition: Logging.java:364
com.jogamp.gluegen.Logging.LoggerIf
An interface for Logger.
Definition: Logging.java:59
com.jogamp.gluegen.Logging.LoggerIf.getName
String getName()
See Logger#getName().
com.jogamp.gluegen.Logging.LoggerIf.info
void info(final String msg)
See Logger#info(String).
com.jogamp.gluegen.Logging.LoggerIf.isLoggable
boolean isLoggable(Level level)
See Logger#isLoggable(Level).
com.jogamp.gluegen.Logging.LoggerIf.log
void log(final Level level, final String msg, final Object ... params)
See Logger#log(Level, String, Object[]).
com.jogamp.gluegen.Logging.LoggerIf.getHandlers
Handler[] getHandlers()
See Logger#getHandlers().
com.jogamp.gluegen.Logging.LoggerIf.setLevel
void setLevel(final Level newLevel)
See Logger#setLevel(Level).
com.jogamp.gluegen.Logging.LoggerIf.log
void log(final Level level, final ASTLocusTag loc, final String msg)
See Logger#log(Level, String).
com.jogamp.gluegen.Logging.LoggerIf.log
void log(final Level level, final ASTLocusTag loc, final String msg, final Object param)
See Logger#log(Level, String, Object).
com.jogamp.gluegen.Logging.LoggerIf.getLevel
Level getLevel()
See Logger#getLevel().
com.jogamp.gluegen.Logging.LoggerIf.warning
void warning(final ASTLocusTag loc, final String msg)
See Logger#warning(String).
com.jogamp.gluegen.Logging.LoggerIf.debug
void debug(final ASTLocusTag loc, final String msg)
Calls log(Level, ASTLocusTag, String) w/ Level#FINE.
com.jogamp.gluegen.Logging.LoggerIf.log
void log(final Level level, final ASTLocusTag loc, final String msg, final Object ... params)
See Logger#log(Level, String, Object[]).
com.jogamp.gluegen.Logging.LoggerIf.warning
void warning(final String msg)
See Logger#warning(String).
com.jogamp.gluegen.Logging.LoggerIf.debug
void debug(final String msg)
Calls log(Level, String) w/ Level#FINE.
com.jogamp.gluegen.Logging.LoggerIf.log
void log(final Level level, final String msg, final Object param)
See Logger#log(Level, String, Object).
com.jogamp.gluegen.Logging.LoggerIf.log
void log(final Level level, final String msg)
See Logger#log(Level, String).
com.jogamp.gluegen.Logging.LoggerIf.setLevelOfAllHandler
void setLevelOfAllHandler(final Level newLevel)
See Handler#setLevel(Level).
com.jogamp.gluegen.Logging.LoggerIf.getSourceClassName
String getSourceClassName()
See LogRecord#getSourceClassName().
com.jogamp.gluegen.Logging.LoggerIf.info
void info(final ASTLocusTag loc, final String msg)
See Logger#info(String).