diff options
author | Michael Bien <[email protected]> | 2010-03-31 16:08:54 +0200 |
---|---|---|
committer | Michael Bien <[email protected]> | 2010-03-31 16:08:54 +0200 |
commit | 2fe517b9a2e1a680b50c7e1273897495800c5350 (patch) | |
tree | c39ed5cbb48d49a34f024437e5dbfe76ff29c447 | |
parent | f76e3bb1cff43d35b6884c17418ea20cd4ed5e22 (diff) |
implemented better logging using java.util.logging.
-rw-r--r-- | src/java/com/sun/gluegen/CMethodBindingEmitter.java | 17 | ||||
-rw-r--r-- | src/java/com/sun/gluegen/GlueGen.java | 4 | ||||
-rw-r--r-- | src/java/com/sun/gluegen/JavaEmitter.java | 44 | ||||
-rw-r--r-- | src/java/com/sun/gluegen/Logging.java | 85 | ||||
-rw-r--r-- | src/java/com/sun/gluegen/pcpp/PCPP.java | 112 |
5 files changed, 176 insertions, 86 deletions
diff --git a/src/java/com/sun/gluegen/CMethodBindingEmitter.java b/src/java/com/sun/gluegen/CMethodBindingEmitter.java index 022df39..ef5d0e9 100644 --- a/src/java/com/sun/gluegen/CMethodBindingEmitter.java +++ b/src/java/com/sun/gluegen/CMethodBindingEmitter.java @@ -44,12 +44,15 @@ import java.io.*; import java.text.MessageFormat; import com.sun.gluegen.cgram.types.*; +import java.util.logging.Logger; + +import static java.util.logging.Level.*; /** Emits the C-side component of the Java<->C JNI binding. */ -public class CMethodBindingEmitter extends FunctionEmitter -{ - protected static final CommentEmitter defaultCommentEmitter = - new DefaultCommentEmitter(); +public class CMethodBindingEmitter extends FunctionEmitter { + + protected static final Logger LOG = Logger.getLogger(CMethodBindingEmitter.class.getPackage().getName()); + protected static final CommentEmitter defaultCommentEmitter = new DefaultCommentEmitter(); protected static final String arrayResLength = "_array_res_length"; protected static final String arrayRes = "_array_res"; @@ -1006,13 +1009,13 @@ public class CMethodBindingEmitter extends FunctionEmitter } } writer.print("sizeof(" + cReturnType.getName() + ")"); - System.err.println( - "WARNING: No capacity specified for java.nio.Buffer return " + + LOG.warning( + "No capacity specified for java.nio.Buffer return " + "value for function \"" + binding + "\";" + " assuming size of equivalent C return type (sizeof(" + cReturnType.getName() + ")): " + binding); /** throw new RuntimeException( - "WARNING: No capacity specified for java.nio.Buffer return " + + "No capacity specified for java.nio.Buffer return " + "value for function \"" + binding + "\";" + " C return type is " + cReturnType.getName() + ": " + binding); */ } diff --git a/src/java/com/sun/gluegen/GlueGen.java b/src/java/com/sun/gluegen/GlueGen.java index 8d2144a..aac5319 100644 --- a/src/java/com/sun/gluegen/GlueGen.java +++ b/src/java/com/sun/gluegen/GlueGen.java @@ -51,6 +51,10 @@ import static java.lang.System.*; /** Glue code generator for C functions and data structures. */ public class GlueGen implements GlueEmitterControls { + static{ + Logging.init(); + } + private List<String> forcedStructNames = new ArrayList<String>(); private PCPP preprocessor; diff --git a/src/java/com/sun/gluegen/JavaEmitter.java b/src/java/com/sun/gluegen/JavaEmitter.java index 25aeb16..4e154d7 100644 --- a/src/java/com/sun/gluegen/JavaEmitter.java +++ b/src/java/com/sun/gluegen/JavaEmitter.java @@ -39,11 +39,20 @@ package com.sun.gluegen; +import java.beans.PropertyChangeEvent; import java.io.*; import java.util.*; import java.text.MessageFormat; import com.sun.gluegen.cgram.types.*; +import java.beans.PropertyChangeListener; +import java.util.logging.ConsoleHandler; +import java.util.logging.Filter; +import java.util.logging.LogManager; +import java.util.logging.LogRecord; +import java.util.logging.Logger; +import javax.swing.plaf.basic.BasicComboBoxUI.PropertyChangeHandler; +import static java.util.logging.Level.*; // PROBLEMS: // - what if something returns 'const int *'? Could we @@ -89,6 +98,8 @@ public class JavaEmitter implements GlueEmitter { private MachineDescription machDesc32; private MachineDescription machDesc64; + protected final static Logger LOG = Logger.getLogger(JavaEmitter.class.getPackage().getName()); + public void readConfigurationFile(String filename) throws Exception { cfg = createConfig(); cfg.read(filename); @@ -406,16 +417,10 @@ public class JavaEmitter implements GlueEmitter { ArrayList<FunctionSymbol> funcsToBind = new ArrayList<FunctionSymbol>(funcsToBindSet); // sort functions to make them easier to find in native code - Collections.sort( - funcsToBind, - new Comparator<FunctionSymbol>() { - public int compare(FunctionSymbol o1, FunctionSymbol o2) { - return o1.getName().compareTo(o2.getName()); - } - @Override - public boolean equals(Object obj) { - return obj.getClass() == this.getClass(); - } + Collections.sort(funcsToBind, new Comparator<FunctionSymbol>() { + public int compare(FunctionSymbol o1, FunctionSymbol o2) { + return o1.getName().compareTo(o2.getName()); + } }); // Bind all the C funcs to Java methods @@ -787,7 +792,7 @@ public class JavaEmitter implements GlueEmitter { } if (name == null) { - System.err.println("WARNING: skipping emission of unnamed struct \"" + structType + "\""); + LOG.log(WARNING, "skipping emission of unnamed struct \"{0}\"", structType); return; } @@ -851,7 +856,6 @@ public class JavaEmitter implements GlueEmitter { String structClassPkg = cfg.packageForStruct(name); PrintWriter writer = null; - PrintWriter cWriter = null; try { writer = openFile( cfg.javaOutputDir() + File.separator + @@ -863,9 +867,9 @@ public class JavaEmitter implements GlueEmitter { if (cfg.nativeOutputUsesJavaHierarchy()) { nRoot += File.separator + CodeGenUtils.packageAsPath(cfg.packageName()); } - cWriter = openFile(nRoot + File.separator + containingTypeName + "_JNI.c"); - CodeGenUtils.emitAutogeneratedWarning(cWriter, this); - emitCHeader(cWriter, containingTypeName); + PrintWriter newCWriter = openFile(nRoot + File.separator + containingTypeName + "_JNI.c"); + CodeGenUtils.emitAutogeneratedWarning(newCWriter, this); + emitCHeader(newCWriter, containingTypeName); } } catch(Exception e) { throw new RuntimeException("Unable to open files for emission of struct class", e); @@ -1095,7 +1099,6 @@ public class JavaEmitter implements GlueEmitter { } catch (Exception e) { System.err.println("Error occurred while creating accessor for field \"" + field.getName() + "\" in type \"" + name + "\""); - e.printStackTrace(); throw(e); } if (externalJavaType.isPrimitive()) { @@ -1152,8 +1155,7 @@ public class JavaEmitter implements GlueEmitter { } } else { // FIXME - System.err.println("WARNING: Complicated fields (field \"" + field + "\" of type \"" + name + - "\") not implemented yet"); + LOG.log(WARNING, "Complicated fields (field \"{0}\" of type \"{1}\") not implemented yet", new Object[]{field, name}); // throw new RuntimeException("Complicated fields (field \"" + field + "\" of type \"" + t + // "\") not implemented yet"); } @@ -1240,7 +1242,7 @@ public class JavaEmitter implements GlueEmitter { // t is<type>**, targetType is <type>*, we need to get <type> Type bottomType = targetType.asPointer().getTargetType(); - System.out.println("INFO: Opaque Type: "+t+", targetType: "+targetType+", bottomType: "+bottomType+" is ptr-ptr"); + LOG.log(INFO, "Opaque Type: {0}, targetType: {1}, bottomType: {2} is ptr-ptr", new Object[]{t, targetType, bottomType}); } } } @@ -1333,7 +1335,7 @@ public class JavaEmitter implements GlueEmitter { } else { // t is<type>[][], targetType is <type>[], we need to get <type> bottomType = targetType.asArray().getElementType(); - System.out.println("WARNING: typeToJavaType(ptr-ptr): "+t+", targetType: "+targetType+", bottomType: "+bottomType+" -> Unhandled!"); + LOG.log(WARNING, "typeToJavaType(ptr-ptr): {0}, targetType: {1}, bottomType: {2} -> Unhandled!", new Object[]{t, targetType, bottomType}); } // Warning: The below code is not backed up by an implementation, @@ -1569,7 +1571,7 @@ public class JavaEmitter implements GlueEmitter { */ protected void emitCustomJavaCode(PrintWriter writer, String className) throws Exception { List<String> code = cfg.customJavaCodeForClass(className); - if (code.size() == 0) + if (code.isEmpty()) return; writer.println(); diff --git a/src/java/com/sun/gluegen/Logging.java b/src/java/com/sun/gluegen/Logging.java new file mode 100644 index 0000000..b38118c --- /dev/null +++ b/src/java/com/sun/gluegen/Logging.java @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2010, Michael Bien + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of JogAmp nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL Michael Bien BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * Created on Wednesday, March 31 2010 13:30 + */ +package com.sun.gluegen; + +import java.util.logging.ConsoleHandler; +import java.util.logging.Formatter; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; + +/** + * + * @author Michael Bien + */ +public class Logging { + + static void init() { + + String pakage = Logging.class.getPackage().getName(); + String property = System.getProperty(pakage+".level"); + Level level; + if(property != null) { + level = Level.parse(property); + }else{ + level = Level.WARNING; + } + + ConsoleHandler handler = new ConsoleHandler() { + @Override + public java.util.logging.Formatter getFormatter() { + return new PlainLogFormatter(); + } + }; + handler.setFormatter(new PlainLogFormatter()); + handler.setLevel(level); + + Logger rootPackageLogger = Logger.getLogger(pakage); + rootPackageLogger.setUseParentHandlers(false); + rootPackageLogger.setLevel(level); + rootPackageLogger.addHandler(handler); + } + + /** + * This log formatter needs usually one line per log record. + * @author Michael Bien + */ + private static class PlainLogFormatter extends Formatter { + + //@Override + public String format(LogRecord record) { + StringBuilder sb = new StringBuilder(128); + sb.append("[").append(record.getLevel()).append(' ').append(record.getSourceClassName()).append("]: "); + sb.append(formatMessage(record)).append("\n"); + return sb.toString(); + } + } +} diff --git a/src/java/com/sun/gluegen/pcpp/PCPP.java b/src/java/com/sun/gluegen/pcpp/PCPP.java index 386ad8c..26c4f27 100644 --- a/src/java/com/sun/gluegen/pcpp/PCPP.java +++ b/src/java/com/sun/gluegen/pcpp/PCPP.java @@ -56,6 +56,8 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.logging.Logger; +import static java.util.logging.Level.*; /** A minimal pseudo-C-preprocessor designed in particular to preserve #define statements defining constants so they can be observed by a @@ -63,6 +65,8 @@ import java.util.Set; public class PCPP { + private static final Logger LOG = Logger.getLogger(PCPP.class.getPackage().getName()); + private static final boolean disableDebugPrint = true; /** Map containing the results of #define statements. We must @@ -207,7 +211,7 @@ public class PCPP { private final List<String> values; private final List<String> params; - public Macro(List<String> params, List<String> values) { + Macro(List<String> params, List<String> values) { this.values = values; this.params = params; } @@ -431,15 +435,15 @@ public class PCPP { if (enabled()) { String oldDef = defineMap.remove(name); if (oldDef == null) { - System.err.println("WARNING: ignoring redundant \"#undef " + - name + "\", at \"" + filename() + "\" line " + lineNumber() + - ": \"" + name + "\" was not previously defined"); + LOG.log(WARNING, "ignoring redundant \"#undef {0}\", at \"{1}\" line {2}: \"{3}\" was not previously defined", + new Object[]{name, filename(), lineNumber(), name}); } else { // System.err.println("UNDEFINED: '" + name + "' (line " + lineNumber() + " file " + filename() + ")"); } nonConstantDefines.remove(name); + } else { + LOG.log(WARNING, "FAILED TO UNDEFINE: ''{0}'' (line {1} file {2})", new Object[]{name, lineNumber(), filename()}); } - else System.err.println("FAILED TO UNDEFINE: '" + name + "' (line " + lineNumber() + " file " + filename() + ")"); } private void handleDefine() throws IOException { @@ -489,8 +493,7 @@ public class PCPP { String value = ""; String oldDef = defineMap.put(name, value); if (oldDef != null && !oldDef.equals(value)) { - System.err.println("WARNING: \"" + name + "\" redefined from \"" + - oldDef + "\" to \"\""); + LOG.log(WARNING, "\"{0}\" redefined from \"{1}\" to \"\"", new Object[]{name, oldDef}); } // We don't want to emit the define, because it would serve no purpose // and cause GlueGen errors (confuse the GnuCParser) @@ -505,8 +508,7 @@ public class PCPP { // Put it in the #define map String oldDef = defineMap.put(name, value); if (oldDef != null && !oldDef.equals(value)) { - System.err.println("WARNING: \"" + name + "\" redefined from \"" + - oldDef + "\" to \"" + value + "\""); + LOG.log(WARNING, "\"{0}\" redefined from \"{1}\" to \"{2}\"", new Object[]{name, oldDef, value}); } debugPrint(true, "#define " + name + " ["+oldDef+" ] -> "+value + " CONST"); //System.out.println("//---DEFINED: " + name + " to \"" + value + "\""); @@ -551,8 +553,7 @@ public class PCPP { Macro macro = new Macro(params, values); Macro oldDef = macroMap.put(name, macro); if (oldDef != null) { - System.err.println("WARNING: \"" + name + "\" redefined from \"" + - oldDef + "\" to \"" + macro + "\""); + LOG.log(WARNING, "\"{0}\" redefined from \"{1}\" to \"{2}\"", new Object[]{name, oldDef, macro}); } emitDefine = false; @@ -602,8 +603,7 @@ public class PCPP { String oldDef = defineMap.put(name, value); if (oldDef != null && !oldDef.equals(value)) { - System.err.println("WARNING: \"" + name + "\" redefined from \"" + - oldDef + "\" to \"" + value + "\""); + LOG.log(WARNING, "\"{0}\" redefined from \"{1}\" to \"{2}\"", new Object[]{name, oldDef, value}); } debugPrint(true, "#define " + name + " ["+oldDef+" ] -> "+value + " CONST"); // System.out.println("#define " + name +" "+value + " CONST EXPRESSION"); @@ -919,12 +919,12 @@ public class PCPP { } else if (t == '<') { // Components of path name are coming in as separate tokens; // concatenate them - StringBuffer buf = new StringBuffer(); + StringBuilder buf = new StringBuilder(); while ((t = nextToken()) != '>' && (t != StreamTokenizer.TT_EOF)) { buf.append(curTokenAsString()); } if (t == StreamTokenizer.TT_EOF) { - System.err.println("WARNING: unexpected EOF while processing #include directive"); + LOG.warning("unexpected EOF while processing #include directive"); } filename = buf.toString(); } @@ -937,7 +937,7 @@ public class PCPP { String fullname = findFile(filename); //System.out.println("ACTIVE BLOCK, LOADING " + filename); if (fullname == null) { - System.err.println("WARNING: unable to find #include file \"" + filename + "\""); + LOG.log(WARNING, "unable to find #include file \"{0}\"", filename); return; } // Process this file in-line @@ -957,18 +957,18 @@ public class PCPP { private List<Boolean> enabledBits = new ArrayList<Boolean>(); private static int debugPrintIndentLevel = 0; - private void debugPrint(boolean onlyPrintIfEnabled, String msg) { + + private void debugPrint(boolean onlyPrintIfEnabled, String msg) { if (disableDebugPrint) { return; } - if (!onlyPrintIfEnabled || (onlyPrintIfEnabled && enabled())) - { - for (int i = debugPrintIndentLevel; --i >0; ) { - System.out.print(" "); - } - System.out.println(msg + " (line " + lineNumber() + " file " + filename() + ")"); + if (!onlyPrintIfEnabled || (onlyPrintIfEnabled && enabled())) { + for (int i = debugPrintIndentLevel; --i > 0;) { + System.out.print(" "); } + System.out.println(msg + " (line " + lineNumber() + " file " + filename() + ")"); + } } private void pushEnableBit(boolean enabled) { @@ -978,8 +978,8 @@ public class PCPP { } private void popEnableBit() { - if (enabledBits.size() == 0) { - System.err.println("WARNING: mismatched #ifdef/endif pairs"); + if (enabledBits.isEmpty()) { + LOG.warning("mismatched #ifdef/endif pairs"); return; } enabledBits.remove(enabledBits.size() - 1); @@ -988,7 +988,7 @@ public class PCPP { } private boolean enabled() { - return (enabledBits.size() == 0 || enabledBits.get(enabledBits.size() - 1)); + return (enabledBits.isEmpty() || enabledBits.get(enabledBits.size() - 1)); } private void print(String s) { @@ -1036,46 +1036,42 @@ public class PCPP { System.exit(1); } - public static void main(String[] args) { - try { - Reader reader = null; - String filename = null; + public static void main(String[] args) throws IOException { + Reader reader = null; + String filename = null; - if (args.length == 0) { - usage(); - } + if (args.length == 0) { + usage(); + } - List<String> includePaths = new ArrayList<String>(); - for (int i = 0; i < args.length; i++) { - if (i < args.length - 1) { - String arg = args[i]; - if (arg.startsWith("-I")) { - String[] paths = arg.substring(2).split(System.getProperty("path.separator")); - for (int j = 0; j < paths.length; j++) { - includePaths.add(paths[j]); - } - } else { - usage(); + List<String> includePaths = new ArrayList<String>(); + for (int i = 0; i < args.length; i++) { + if (i < args.length - 1) { + String arg = args[i]; + if (arg.startsWith("-I")) { + String[] paths = arg.substring(2).split(System.getProperty("path.separator")); + for (int j = 0; j < paths.length; j++) { + includePaths.add(paths[j]); } } else { - String arg = args[i]; - if (arg.equals("-")) { - reader = new InputStreamReader(System.in); - filename = "standard input"; - } else { - if (arg.startsWith("-")) { - usage(); - } - filename = arg; - reader = new BufferedReader(new FileReader(filename)); + usage(); + } + } else { + String arg = args[i]; + if (arg.equals("-")) { + reader = new InputStreamReader(System.in); + filename = "standard input"; + } else { + if (arg.startsWith("-")) { + usage(); } + filename = arg; + reader = new BufferedReader(new FileReader(filename)); } } - - new PCPP(includePaths).run(reader, filename); - } catch (IOException e) { - e.printStackTrace(); } + + new PCPP(includePaths).run(reader, filename); } } |