jeremias 2004/09/11 11:56:20
Modified: src/java/org/apache/fop/fonts/apps TTFReader.java
PFMReader.java
src/java/org/apache/fop/apps CommandLineOptions.java
Fop.java
Added: src/java/org/apache/fop/util CommandLineLogger.java
Log:
Bugzilla patch 31162:
Better command-line logging without losing the possibility to fine-tune Commons
Logging from outside.
Submitted by: Finn Bock <bckfnn.at.apache.org>
plus using the new CommandLineLogger for PFMReader and TTFReader, too.
Revision Changes Path
1.1 xml-fop/src/java/org/apache/fop/util/CommandLineLogger.java
Index: CommandLineLogger.java
===================================================================
/* Copyright 2004 The Apache Software Foundation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id: CommandLineLogger.java,v 1.1 2004/09/11 18:56:20 jeremias Exp $ */
package org.apache.fop.util;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
* This is a commons-logging logger for command line use.
*/
public class CommandLineLogger implements Log {
/** "Trace" level logging. */
public static final int LOG_LEVEL_TRACE = 1;
/** "Debug" level logging. */
public static final int LOG_LEVEL_DEBUG = 2;
/** "Info" level logging. */
public static final int LOG_LEVEL_INFO = 3;
/** "Warn" level logging. */
public static final int LOG_LEVEL_WARN = 4;
/** "Error" level logging. */
public static final int LOG_LEVEL_ERROR = 5;
/** "Fatal" level logging. */
public static final int LOG_LEVEL_FATAL = 6;
private int logLevel;
private String logName;
/**
* Construct the logger with a default log level taken from the LogFactory
* attribute "level".
* @param logName the logger name.
*/
public CommandLineLogger(String logName) {
this.logName = logName;
setLogLevel((String) LogFactory.getFactory().getAttribute("level"));
}
/**
* Set a log level for the logger.
* @param level the log level
*/
public void setLogLevel(String level) {
if ("fatal".equals(level)) {
logLevel = LOG_LEVEL_FATAL;
} else if ("error".equals(level)) {
logLevel = LOG_LEVEL_ERROR;
} else if ("warn".equals(level)) {
logLevel = LOG_LEVEL_WARN;
} else if ("info".equals(level)) {
logLevel = LOG_LEVEL_INFO;
} else if ("debug".equals(level)) {
logLevel = LOG_LEVEL_DEBUG;
} else if ("trace".equals(level)) {
logLevel = LOG_LEVEL_TRACE;
} else {
logLevel = LOG_LEVEL_INFO;
}
}
/**
* @see org.apache.commons.logging.Log#isTraceEnabled()
*/
public final boolean isTraceEnabled() {
return logLevel <= LOG_LEVEL_TRACE;
}
/**
* @see org.apache.commons.logging.Log#isDebugEnabled()
*/
public final boolean isDebugEnabled() {
return logLevel <= LOG_LEVEL_DEBUG;
}
/**
* @see org.apache.commons.logging.Log#isInfoEnabled()
*/
public final boolean isInfoEnabled() {
return logLevel <= LOG_LEVEL_INFO;
}
/**
* @see org.apache.commons.logging.Log#isWarnEnabled()
*/
public final boolean isWarnEnabled() {
return logLevel <= LOG_LEVEL_WARN;
}
/**
* @see org.apache.commons.logging.Log#isErrorEnabled()
*/
public final boolean isErrorEnabled() {
return logLevel <= LOG_LEVEL_ERROR;
}
/**
* @see org.apache.commons.logging.Log#isFatalEnabled()
*/
public final boolean isFatalEnabled() {
return logLevel <= LOG_LEVEL_FATAL;
}
/**
* @see org.apache.commons.logging.Log#trace(java.lang.Object)
*/
public final void trace(Object message) {
if (isTraceEnabled()) {
log(LOG_LEVEL_TRACE, message, null);
}
}
/**
* @see org.apache.commons.logging.Log#trace(java.lang.Object,
java.lang.Throwable)
*/
public final void trace(Object message, Throwable t) {
if (isTraceEnabled()) {
log(LOG_LEVEL_TRACE, message, t);
}
}
/**
* @see org.apache.commons.logging.Log#debug(java.lang.Object)
*/
public final void debug(Object message) {
if (isDebugEnabled()) {
log(LOG_LEVEL_DEBUG, message, null);
}
}
/**
* @see org.apache.commons.logging.Log#debug(java.lang.Object,
java.lang.Throwable)
*/
public final void debug(Object message, Throwable t) {
if (isDebugEnabled()) {
log(LOG_LEVEL_DEBUG, message, t);
}
}
/**
* @see org.apache.commons.logging.Log#info(java.lang.Object)
*/
public final void info(Object message) {
if (isInfoEnabled()) {
log(LOG_LEVEL_INFO, message, null);
}
}
/**
* @see org.apache.commons.logging.Log#info(java.lang.Object,
java.lang.Throwable)
*/
public final void info(Object message, Throwable t) {
if (isInfoEnabled()) {
log(LOG_LEVEL_INFO, message, t);
}
}
/**
* @see org.apache.commons.logging.Log#warn(java.lang.Object)
*/
public final void warn(Object message) {
if (isWarnEnabled()) {
log(LOG_LEVEL_WARN, message, null);
}
}
/**
* @see org.apache.commons.logging.Log#warn(java.lang.Object,
java.lang.Throwable)
*/
public final void warn(Object message, Throwable t) {
if (isWarnEnabled()) {
log(LOG_LEVEL_WARN, message, t);
}
}
/**
* @see org.apache.commons.logging.Log#error(java.lang.Object)
*/
public final void error(Object message) {
if (isErrorEnabled()) {
log(LOG_LEVEL_ERROR, message, null);
}
}
/**
* @see org.apache.commons.logging.Log#error(java.lang.Object,
java.lang.Throwable)
*/
public final void error(Object message, Throwable t) {
if (isErrorEnabled()) {
log(LOG_LEVEL_ERROR, message, t);
}
}
/**
* @see org.apache.commons.logging.Log#fatal(java.lang.Object)
*/
public final void fatal(Object message) {
if (isFatalEnabled()) {
log(LOG_LEVEL_FATAL, message, null);
}
}
/**
* @see org.apache.commons.logging.Log#fatal(java.lang.Object,
java.lang.Throwable)
*/
public final void fatal(Object message, Throwable t) {
if (isFatalEnabled()) {
log(LOG_LEVEL_FATAL, message, t);
}
}
/**
* Do the actual logging.
* This method assembles the message and prints it to
* and then calls <code>write()</code> to cause it to be written.</p>
*
* @param type One of the LOG_LEVEL_XXX constants defining the log level
* @param message The message itself (typically a String)
* @param t The exception whose stack trace should be logged
*/
protected void log(int type, Object message, Throwable t) {
StringBuffer buf = new StringBuffer();
// Append the message
buf.append(String.valueOf(message));
if (t != null) {
buf.append("\n");
// Append a stack trace or just the stack trace message.
if (!isDebugEnabled()) {
buf.append(t.toString());
buf.append("\n");
} else {
java.io.StringWriter sw = new java.io.StringWriter(1024);
java.io.PrintWriter pw = new java.io.PrintWriter(sw);
t.printStackTrace(pw);
pw.close();
buf.append(sw.toString());
}
}
// Print to the appropriate destination
if (type >= LOG_LEVEL_WARN) {
System.err.println(buf);
} else {
System.out.println(buf);
}
}
}
1.8 +38 -31 xml-fop/src/java/org/apache/fop/fonts/apps/TTFReader.java
Index: TTFReader.java
===================================================================
RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/fonts/apps/TTFReader.java,v
retrieving revision 1.7
retrieving revision 1.8
diff -u -r1.7 -r1.8
--- TTFReader.java 15 Aug 2004 09:57:07 -0000 1.7
+++ TTFReader.java 11 Sep 2004 18:56:20 -0000 1.8
@@ -39,6 +39,7 @@
import org.apache.fop.fonts.truetype.FontFileReader;
import org.apache.fop.fonts.truetype.TTFCmapEntry;
import org.apache.fop.fonts.truetype.TTFFile;
+import org.apache.fop.util.CommandLineLogger;
/**
* A tool which reads TTF files and generates
@@ -49,7 +50,7 @@
/**
* logging instance
*/
- protected Log log = LogFactory.getLog(TTFReader.class);
+ protected static Log log;
/**
* Parse commandline arguments. put options in the HashMap and return
@@ -62,7 +63,9 @@
List arguments = new java.util.ArrayList();
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("-")) {
- if ((i + 1) < args.length && !args[i + 1].startsWith("-")) {
+ if ("-d".equals(args[i]) || "-q".equals(args[i])) {
+ options.put(args[i], "");
+ } else if ((i + 1) < args.length && !args[i + 1].startsWith("-")) {
options.put(args[i], args[i + 1]);
i++;
} else {
@@ -82,8 +85,8 @@
"java " + TTFReader.class.getName() + " [options] fontfile.ttf
xmlfile.xml");
System.out.println();
System.out.println("where options can be:");
- System.out.println("-d <WARN|INFO|DEBUG>");
- System.out.println(" Set debug level (default: WARN).");
+ System.out.println("-d Debug mode");
+ System.out.println("-q Quiet mode");
System.out.println("-enc ansi");
System.out.println(" With this option you create a WinAnsi encoded
font.");
System.out.println(" The default is to create a CID keyed font.");
@@ -135,32 +138,27 @@
Map options = new java.util.HashMap();
String[] arguments = parseArguments(options, args);
- //Setup simple logger for this command-line application
- System.setProperty("org.apache.commons.logging.Log",
- "org.apache.commons.logging.impl.SimpleLog");
- System.setProperty("org.apache.commons.logging.simplelog.showShortLogname",
- "false");
-
+ // Enable the simple command line logging when no other logger is
+ // defined.
+ LogFactory logFactory = LogFactory.getFactory();
+ if (System.getProperty("org.apache.commons.logging.Log") == null) {
+ logFactory.setAttribute("org.apache.commons.logging.Log",
+ CommandLineLogger.class.getName());
+ }
+
//Determine log level
- String level;
if (options.get("-d") != null) {
- String lev = (String)options.get("-d");
- if ("DEBUG".equalsIgnoreCase(lev)) {
- level = "debug";
- } else if ("INFO".equalsIgnoreCase(lev)) {
- level = "info";
- } else {
- level = "warn";
- }
+ setLogLevel("debug");
+ } else if (options.get("-q") != null) {
+ setLogLevel("error");
} else {
- level = "warn";
+ setLogLevel("info");
}
- System.setProperty("org.apache.commons.logging.simplelog.defaultlog",
- level);
+ log = LogFactory.getLog(TTFReader.class);
TTFReader app = new TTFReader();
- System.out.println("TTF Reader for Apache FOP " + Fop.getVersion() + "\n");
+ log.info("TTF Reader for Apache FOP " + Fop.getVersion() + "\n");
if (options.get("-enc") != null) {
String enc = (String)options.get("-enc");
@@ -194,7 +192,7 @@
displayUsage();
} else {
try {
- System.out.println("Parsing font...");
+ log.info("Parsing font...");
TTFFile ttf = app.loadTTF(arguments[0], ttcName);
if (ttf != null) {
org.w3c.dom.Document doc = app.constructFontXML(ttf,
@@ -202,9 +200,9 @@
ttcName);
if (isCid) {
- System.out.println("Creating CID encoded metrics...");
+ log.info("Creating CID encoded metrics...");
} else {
- System.out.println("Creating WinAnsi encoded metrics...");
+ log.info("Creating WinAnsi encoded metrics...");
}
if (doc != null) {
@@ -212,18 +210,27 @@
}
if (ttf.isEmbeddable()) {
- System.out.println("This font contains no embedding license
restrictions.");
+ log.info("This font contains no embedding license
restrictions.");
} else {
- System.out.println("** Note: This font contains license
retrictions for\n"
+ log.info("** Note: This font contains license retrictions
for\n"
+ " embedding. This font shouldn't be
embedded.");
}
}
- System.out.println("\nXML font metrics file successfullly
created.");
+ log.info("");
+ log.info("XML font metrics file successfullly created.");
} catch (Exception e) {
- System.err.println("Error while building XML font metrics file.");
- e.printStackTrace(System.err);
+ log.error("Error while building XML font metrics file.", e);
System.exit(-1);
}
+ }
+ }
+
+ private static void setLogLevel(String level) {
+ // Set the evel for future loggers.
+ LogFactory.getFactory().setAttribute("level", level);
+ if (log instanceof CommandLineLogger) {
+ // Set the level for the logger creates already.
+ ((CommandLineLogger) log).setLogLevel(level);
}
}
1.7 +31 -26 xml-fop/src/java/org/apache/fop/fonts/apps/PFMReader.java
Index: PFMReader.java
===================================================================
RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/fonts/apps/PFMReader.java,v
retrieving revision 1.6
retrieving revision 1.7
diff -u -r1.6 -r1.7
--- PFMReader.java 15 Aug 2004 09:57:07 -0000 1.6
+++ PFMReader.java 11 Sep 2004 18:56:20 -0000 1.7
@@ -38,6 +38,7 @@
//FOP
import org.apache.fop.apps.Fop;
import org.apache.fop.fonts.type1.PFMFile;
+import org.apache.fop.util.CommandLineLogger;
/**
* A tool which reads PFM files from Adobe Type 1 fonts and creates
@@ -48,7 +49,7 @@
/**
* logging instance
*/
- protected Log log = LogFactory.getLog(TTFReader.class);
+ protected static Log log;
/**
* Parse commandline arguments. put options in the HashMap and return
@@ -61,7 +62,9 @@
List arguments = new java.util.ArrayList();
for (int i = 0; i < args.length; i++) {
if (args[i].startsWith("-")) {
- if ((i + 1) < args.length && !args[i + 1].startsWith("-")) {
+ if ("-d".equals(args[i]) || "-q".equals(args[i])) {
+ options.put(args[i], "");
+ } else if ((i + 1) < args.length && !args[i + 1].startsWith("-")) {
options.put(args[i], args[i + 1]);
i++;
} else {
@@ -80,8 +83,8 @@
"java " + PFMReader.class.getName() + " [options] metricfile.pfm
xmlfile.xml");
System.out.println();
System.out.println("where options can be:");
- System.out.println("-d <WARN|INFO|DEBUG>");
- System.out.println(" Set debug level (default: WARN).");
+ System.out.println("-d Debug mode");
+ System.out.println("-q Quiet mode");
System.out.println("-fn <fontname>");
System.out.println(" default is to use the fontname in the .pfm file,
but");
System.out.println(" you can override that name to make sure that the");
@@ -119,32 +122,27 @@
Map options = new java.util.HashMap();
String[] arguments = parseArguments(options, args);
- //Setup simple logger for this command-line application
- System.setProperty("org.apache.commons.logging.Log",
- "org.apache.commons.logging.impl.SimpleLog");
- System.setProperty("org.apache.commons.logging.simplelog.showShortLogname",
- "false");
+ // Enable the simple command line logging when no other logger is
+ // defined.
+ LogFactory logFactory = LogFactory.getFactory();
+ if (System.getProperty("org.apache.commons.logging.Log") == null) {
+ logFactory.setAttribute("org.apache.commons.logging.Log",
+ CommandLineLogger.class.getName());
+ }
//Determine log level
- String level;
if (options.get("-d") != null) {
- String lev = (String)options.get("-d");
- if ("DEBUG".equalsIgnoreCase(lev)) {
- level = "debug";
- } else if ("INFO".equalsIgnoreCase(lev)) {
- level = "info";
- } else {
- level = "warn";
- }
+ setLogLevel("debug");
+ } else if (options.get("-q") != null) {
+ setLogLevel("error");
} else {
- level = "warn";
+ setLogLevel("info");
}
- System.setProperty("org.apache.commons.logging.simplelog.defaultlog",
- level);
+ log = LogFactory.getLog(PFMReader.class);
PFMReader app = new PFMReader();
- System.out.println("PFM Reader for Apache FOP " + Fop.getVersion() + "\n");
+ log.info("PFM Reader for Apache FOP " + Fop.getVersion() + "\n");
if (options.get("-ef") != null) {
embFile = (String)options.get("-ef");
@@ -167,7 +165,7 @@
displayUsage();
} else {
try {
- System.out.println("Parsing font...");
+ log.info("Parsing font...");
PFMFile pfm = app.loadPFM(arguments[0]);
if (pfm != null) {
app.preview(pfm);
@@ -177,15 +175,22 @@
app.writeFontXML(doc, arguments[1]);
}
- System.out.println("\nXML font metrics file successfullly
created.");
+ log.info("XML font metrics file successfullly created.");
} catch (Exception e) {
- System.err.println("Error while building XML font metrics file");
- e.printStackTrace(System.err);
+ log.error("Error while building XML font metrics file", e);
System.exit(-1);
}
}
}
+ private static void setLogLevel(String level) {
+ // Set the evel for future loggers.
+ LogFactory.getFactory().setAttribute("level", level);
+ if (log instanceof CommandLineLogger) {
+ // Set the level for the logger creates already.
+ ((CommandLineLogger) log).setLogLevel(level);
+ }
+ }
/**
* Read a PFM file and returns it as an object.
1.30 +36 -6 xml-fop/src/java/org/apache/fop/apps/CommandLineOptions.java
Index: CommandLineOptions.java
===================================================================
RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/apps/CommandLineOptions.java,v
retrieving revision 1.29
retrieving revision 1.30
diff -u -r1.29 -r1.30
--- CommandLineOptions.java 9 Sep 2004 07:29:40 -0000 1.29
+++ CommandLineOptions.java 11 Sep 2004 18:56:20 -0000 1.30
@@ -25,6 +25,7 @@
import java.util.Vector;
import org.apache.fop.fo.Constants;
+import org.apache.fop.util.CommandLineLogger;
// commons logging
import org.apache.commons.logging.Log;
@@ -74,13 +75,27 @@
* Construct a command line option object from command line arguments
* @param args command line parameters
* @throws FOPException for general errors
- * @throws FileNotFoundException if an input file wasn't found.
+ * @throws FileNotFoundException if an input file wasn't found
+ * @throws IOException if the the configuration file could not be loaded
*/
- public CommandLineOptions(String[] args)
- throws FOPException, FileNotFoundException, IOException {
+ public CommandLineOptions(String[] args) throws FOPException, IOException {
+ LogFactory logFactory = LogFactory.getFactory();
+
+ // Enable the simple command line logging when no other logger is
+ // defined.
+ if (System.getProperty("org.apache.commons.logging.Log") == null) {
+ logFactory.setAttribute("org.apache.commons.logging.Log",
+ CommandLineLogger.class.getName());
+ setLogLevel("info");
+ }
log = LogFactory.getLog("FOP");
+ parse(args);
+ }
+
+ private void parse(String[] args)
+ throws FOPException, IOException {
boolean optionsParsed = true;
foUserAgent = new FOUserAgent();
@@ -130,6 +145,10 @@
i = i + parseLanguageOption(args, i);
} else if (args[i].equals("-s")) {
suppressLowLevelAreas = Boolean.TRUE;
+ } else if (args[i].equals("-d")) {
+ setLogLevel("debug");
+ } else if (args[i].equals("-q") || args[i].equals("--quiet")) {
+ setLogLevel("error");
} else if (args[i].equals("-fo")) {
i = i + parseFOInputOption(args, i);
} else if (args[i].equals("-xsl")) {
@@ -363,6 +382,15 @@
}
}
+ private void setLogLevel(String level) {
+ // Set the evel for future loggers.
+ LogFactory.getFactory().setAttribute("level", level);
+ if (log instanceof CommandLineLogger) {
+ // Set the level for the logger creates already.
+ ((CommandLineLogger) log).setLogLevel(level);
+ }
+ }
+
/**
* checks whether all necessary information has been given in a consistent way
*/
@@ -376,8 +404,8 @@
}
if ((outputmode == RENDER_AWT || outputmode == RENDER_PRINT) && outfile !=
null) {
- throw new FOPException("Output file may not be specified " +
- "for AWT or PRINT output");
+ throw new FOPException("Output file may not be specified "
+ + "for AWT or PRINT output");
}
if (inputmode == XSLT_INPUT) {
@@ -477,7 +505,7 @@
/**
* Create an InputHandler object based on command-line parameters
* @return a new InputHandler instance
- * @throws IllegalStateException if invalid/missing parameters
+ * @throws IllegalArgumentException if invalid/missing parameters
*/
private InputHandler createInputHandler() throws IllegalArgumentException {
switch (inputmode) {
@@ -577,7 +605,9 @@
"\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] "
+ "[-awt|-pdf|-mif|-rtf|-pcl|-ps|-txt|-at|-print] <outfile>\n"
+ " [OPTIONS] \n"
+ + " -d debug mode \n"
+ " -x dump configuration settings \n"
+ + " -q quiet mode \n"
+ " -c cfg.xml use additional configuration file cfg.xml\n"
+ " -l lang the language to use for user information \n"
+ " -s for area tree XML, down to block areas only\n"
1.20 +3 -18 xml-fop/src/java/org/apache/fop/apps/Fop.java
Index: Fop.java
===================================================================
RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/apps/Fop.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- Fop.java 24 Jul 2004 05:47:45 -0000 1.19
+++ Fop.java 11 Sep 2004 18:56:20 -0000 1.20
@@ -162,24 +162,9 @@
if (options.getOutputMode() != CommandLineOptions.RENDER_AWT) {
System.exit(0);
}
- } catch (FOPException e) {
- if (e.getMessage() == null) {
- System.err.println("Exception occured with a null error message");
- } else {
- System.err.println("" + e.getMessage());
- }
- if (options != null && options.getLogger().isDebugEnabled()) {
- e.printStackTrace();
- } else {
- System.err.println("Turn on debugging for more information");
- }
- System.exit(1);
- } catch (java.io.IOException e) {
- System.err.println("" + e.getMessage());
- if (options != null && options.getLogger().isDebugEnabled()) {
- e.printStackTrace();
- } else {
- System.err.println("Turn on debugging for more information");
+ } catch (Exception e) {
+ if (options != null) {
+ options.getLogger().error("Exception", e);
}
System.exit(1);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]