32package com.jogamp.gluegen;
34import java.util.HashMap;
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;
43import jogamp.common.Debug;
45import com.jogamp.common.util.PropertyAccess;
46import com.jogamp.gluegen.cgram.types.AliasedSymbol;
47import com.jogamp.gluegen.cgram.types.Type;
54 public static final boolean DEBUG = Debug.debug(
"Logging");
63 void info(
final String msg);
90 void log(
final Level level,
final String msg);
94 void log(
final Level level,
final String msg,
final Object param);
98 void log(
final Level level,
final String msg,
final Object ... params);
107 void log(
final Level level,
final ASTLocusTag loc,
final String msg,
final Object param);
111 void log(
final Level level,
final ASTLocusTag loc,
final String msg,
final Object ... params);
116 void setLevel(
final Level newLevel)
throws SecurityException;
142 static class FQNLogger
implements LoggerIf {
143 public final Logger impl;
144 public final PlainLogConsoleHandler handler;
145 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()));
155 public void info(
final String msg) {
159 public void info(
final ASTLocusTag loc,
final String msg) {
160 handler.plf.setASTLocusTag(loc);
164 handler.plf.setASTLocusTag(
null);
169 public void warning(
final String msg) {
173 public void warning(
final ASTLocusTag loc,
final String msg) {
174 handler.plf.setASTLocusTag(loc);
178 handler.plf.setASTLocusTag(
null);
183 public void debug(
final String msg) {
184 log(Level.FINE, msg);
187 public void debug(
final ASTLocusTag loc,
final String msg) {
188 log(Level.FINE, loc, msg);
192 public void log(
final Level level,
final String msg) {
193 impl.log(level, msg);
196 public void log(
final Level level,
final String msg,
final Object param) {
197 impl.log(level, msg, param);
200 public void log(
final Level level,
final String msg,
final Object ... params) {
201 impl.log(level, msg, params);
205 public void log(
final Level level,
final ASTLocusTag loc,
final String msg) {
206 handler.plf.setASTLocusTag(loc);
208 impl.log(level, msg);
210 handler.plf.setASTLocusTag(
null);
214 public void log(
final Level level,
final ASTLocusTag loc,
final String msg,
final Object param) {
215 handler.plf.setASTLocusTag(loc);
217 impl.log(level, msg, param);
219 handler.plf.setASTLocusTag(
null);
223 public void log(
final Level level,
final ASTLocusTag loc,
final String msg,
final Object ... params) {
224 handler.plf.setASTLocusTag(loc);
226 impl.log(level, msg, params);
228 handler.plf.setASTLocusTag(
null);
233 public void setLevel(
final Level newLevel)
throws SecurityException {
234 impl.setLevel(newLevel);
237 public void setLevelOfAllHandler(
final Level newLevel)
throws SecurityException {
238 final Handler[] hs = getHandlers();
239 for(
final Handler h:hs) {
240 h.setLevel(newLevel);
244 public Level getLevel() {
245 return impl.getLevel();
248 public boolean isLoggable(
final Level level) {
249 return impl.isLoggable(level);
252 public String getName() {
253 return impl.getName();
256 public synchronized Handler[] getHandlers() {
257 return impl.getHandlers();
260 public String getSourceClassName() {
261 return handler.plf.simpleClassName;
264 static class PlainLogConsoleHandler
extends ConsoleHandler {
265 final PlainLogFormatter plf;
266 PlainLogConsoleHandler(
final PlainLogFormatter plf,
final Level level) {
272 public java.util.logging.Formatter getFormatter() {
276 static class PlainLogFormatter
extends Formatter {
277 final String simpleClassName;
278 ASTLocusTag astLocus;
279 PlainLogFormatter(
final String simpleClassName) {
280 this.simpleClassName = simpleClassName;
282 public void setASTLocusTag(
final ASTLocusTag loc) { astLocus = loc; }
284 public String format(
final LogRecord record) {
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();
299 final StringBuilder sb =
new StringBuilder(256);
300 if(
null != astLocus ) {
301 astLocus.toString(sb,
getCanonicalName(record.getLevel()), GlueGen.debug()).append(
": ");
303 if( GlueGen.debug() ) {
304 sb.append(simpleClassName).append(
": ");
306 sb.append(formatMessage(record)).append(
"\n");
307 return sb.toString();
311 private final static Map<String, LoggerIf> loggers;
312 private final static FQNLogger rootPackageLogger;
314 loggers =
new HashMap<String, LoggerIf>();
315 final String packageName = Logging.class.getPackage().getName();
316 final String
property = PropertyAccess.getProperty(packageName+
".level",
true);
318 if(property !=
null) {
319 level = Level.parse(property);
321 if(
DEBUG || GlueGen.debug() ) {
324 level = Level.WARNING;
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);
334 static void init() { }
337 if( Level.CONFIG == level ) {
339 }
else if( Level.FINER == level ) {
341 }
else if( Level.FINE == level ) {
343 }
else if( Level.INFO == level ) {
345 }
else if( Level.WARNING == level ) {
347 }
else if( Level.SEVERE == level ) {
350 return level.getName().toLowerCase();
356 return rootPackageLogger;
360 return getLogger(clazz.getPackage().getName(), clazz.getSimpleName());
364 public static synchronized LoggerIf getLogger(
final String packageName,
final String simpleClassName) {
365 final String fqnClassName = packageName+
"."+simpleClassName;
366 LoggerIf res = loggers.get(fqnClassName);
368 res =
new FQNLogger(fqnClassName, simpleClassName, rootPackageLogger.getLevel());
369 loggers.put(fqnClassName, res);
static synchronized LoggerIf getLogger(final Class<?> clazz)
Returns the demanded logger, while aligning its log-level to the root logger's level.
static void alignLevel(final LoggerIf l)
Align log-level of given logger to the root logger's level.
static LoggerIf getLogger()
Returns the root package logger.
static void alignLevel(final LoggerIf l, final Level level)
Align log-level of given logger and all its handlers to the given level.
static final boolean DEBUG
static String getCanonicalName(final Level level)
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.
String getName()
See Logger#getName().
void info(final String msg)
See Logger#info(String).
boolean isLoggable(Level level)
See Logger#isLoggable(Level).
void log(final Level level, final String msg, final Object ... params)
See Logger#log(Level, String, Object[]).
Handler[] getHandlers()
See Logger#getHandlers().
void setLevel(final Level newLevel)
See Logger#setLevel(Level).
void log(final Level level, final ASTLocusTag loc, final String msg)
See Logger#log(Level, String).
void log(final Level level, final ASTLocusTag loc, final String msg, final Object param)
See Logger#log(Level, String, Object).
Level getLevel()
See Logger#getLevel().
void warning(final ASTLocusTag loc, final String msg)
See Logger#warning(String).
void debug(final ASTLocusTag loc, final String msg)
Calls log(Level, ASTLocusTag, String) w/ Level#FINE.
void log(final Level level, final ASTLocusTag loc, final String msg, final Object ... params)
See Logger#log(Level, String, Object[]).
void warning(final String msg)
See Logger#warning(String).
void debug(final String msg)
Calls log(Level, String) w/ Level#FINE.
void log(final Level level, final String msg, final Object param)
See Logger#log(Level, String, Object).
void log(final Level level, final String msg)
See Logger#log(Level, String).
void setLevelOfAllHandler(final Level newLevel)
See Handler#setLevel(Level).
String getSourceClassName()
See LogRecord#getSourceClassName().
void info(final ASTLocusTag loc, final String msg)
See Logger#info(String).