? src/java/org/apache/fop/util/CommandLineLogger.java
Index: src/java/org/apache/fop/apps/CommandLineOptions.java
===================================================================
RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/apps/CommandLineOptions.java,v
retrieving revision 1.29
diff -u -r1.29 CommandLineOptions.java
--- src/java/org/apache/fop/apps/CommandLineOptions.java	9 Sep 2004 07:29:40 -0000	1.29
+++ src/java/org/apache/fop/apps/CommandLineOptions.java	10 Sep 2004 13:05:10 -0000
@@ -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;
@@ -63,6 +64,8 @@
     private int inputmode = NOT_SET;
     /* output mode */
     private int outputmode = NOT_SET;
+    /* log output level */
+    private String level = null;
 
     private FOUserAgent foUserAgent;
     
@@ -76,11 +79,22 @@
      * @throws FOPException for general errors
      * @throws FileNotFoundException if an input file wasn't found.
      */
-    public CommandLineOptions(String[] args)
-            throws FOPException, FileNotFoundException, IOException {
+    public CommandLineOptions() {
+        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");
-        
+    }
+    
+    public void parse(String[] args) 
+            throws FOPException, FileNotFoundException, IOException {
         boolean optionsParsed = true;
         
         foUserAgent = new FOUserAgent();
@@ -130,6 +144,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 +381,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
      */
@@ -577,7 +604,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"
Index: src/java/org/apache/fop/apps/Fop.java
===================================================================
RCS file: /home/cvs/xml-fop/src/java/org/apache/fop/apps/Fop.java,v
retrieving revision 1.19
diff -u -r1.19 Fop.java
--- src/java/org/apache/fop/apps/Fop.java	24 Jul 2004 05:47:45 -0000	1.19
+++ src/java/org/apache/fop/apps/Fop.java	10 Sep 2004 13:05:10 -0000
@@ -138,7 +138,8 @@
         BufferedOutputStream bos = null;
 
         try {
-            options = new CommandLineOptions(args);
+            options = new CommandLineOptions();
+            options.parse(args);
             foUserAgent = options.getFOUserAgent();
             
             Fop fop = new Fop(options.getRenderer(), foUserAgent);
@@ -162,24 +163,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);
         }
--- src/java/org/apache/fop/util/CommandLineLogger.java
+++ src/java/org/apache/fop/util/CommandLineLogger.java	2004-09-10 15:00:50.546875000 +0200
@@ -0,0 +1,205 @@
+/* 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$ */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.
+     */
+    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;
+        }
+    }
+    
+    public final boolean isTraceEnabled() {
+        return logLevel <= LOG_LEVEL_TRACE;
+    }
+
+    public final boolean isDebugEnabled() {
+        return logLevel <= LOG_LEVEL_DEBUG;
+    }
+
+    public final boolean isInfoEnabled() {
+        return logLevel <= LOG_LEVEL_INFO;
+    }
+    
+    public final boolean isWarnEnabled() {
+        return logLevel <= LOG_LEVEL_WARN;
+    }
+
+    public final boolean isErrorEnabled() {
+        return logLevel <= LOG_LEVEL_ERROR;
+    }
+
+    public final boolean isFatalEnabled() {
+        return logLevel <= LOG_LEVEL_FATAL;
+    }
+    
+    public final void trace(Object message) {
+        if (isTraceEnabled()) {
+            log(LOG_LEVEL_TRACE, message, null);
+        }
+    }
+
+    public final void trace(Object message, Throwable t) {
+        if (isTraceEnabled()) {
+            log(LOG_LEVEL_TRACE, message, t);
+        }
+    }
+
+    public final void debug(Object message) {
+        if (isDebugEnabled()) {
+            log(LOG_LEVEL_DEBUG, message, null);
+        }
+    }
+
+    public final void debug(Object message, Throwable t) {
+        if (isDebugEnabled()) {
+            log(LOG_LEVEL_DEBUG, message, t);
+        }
+    }
+
+    public final void info(Object message) {
+        if (isInfoEnabled()) {
+            log(LOG_LEVEL_INFO, message, null);
+        }
+    }
+
+    public final void info(Object message, Throwable t) {
+        if (isInfoEnabled()) {
+            log(LOG_LEVEL_INFO, message, t);
+        }
+    }
+
+    
+    public final void warn(Object message) {
+        if (isWarnEnabled()) {
+            log(LOG_LEVEL_WARN, message, null);
+        }
+    }
+
+    public final void warn(Object message, Throwable t) {
+        if (isWarnEnabled()) {
+            log(LOG_LEVEL_WARN, message, t);
+        }
+    }
+
+    public final void error(Object message) {
+        if (isErrorEnabled()) {
+            log(LOG_LEVEL_ERROR, message, null);
+        }
+    }
+
+    public final void error(Object message, Throwable t) {
+        if (isErrorEnabled()) {
+            log(LOG_LEVEL_ERROR, message, t);
+        }
+    }
+
+    public final void fatal(Object message) {
+        if (isFatalEnabled()) {
+            log(LOG_LEVEL_FATAL, message, null);
+        }
+    }
+
+    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));
+        buf.append("\n");
+        if (t != null) {
+            // 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);
+        }
+
+    }
+}
