ant-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Michael Zhou <...@alibaba.com>
Subject Re: A logger issue
Date Sat, 01 Jun 2002 10:56:49 GMT
Thanks, Conor.  The patch solved the problem!

But there's still something uncomfortable, for I still need to extend the
DefaultLogger(or AnsiColorLogger, NoBannerLogger, ..., if I need) in
order to display any Non-English characters. The CharsetPrintStream.java I
posted in last mail is a hack of PrintStream rather than a solution.

BTW, another code that print directly to System.out and System.err still works improperly.

My general idea is to refactory a few classes, so that they
output through PrintWriter rather than PrintStream.  But I have not made
a perfect work yet, because this may cause some interfaces change so the
classes implements this interface have to change either.

Do you have any good idea?

1. interface org.apache.tools.ant.BuildLogger:
   replace the lines:

     void setOutputPrintStream(PrintStream out);
     void setErrorPrintStream(PrintStream err);

   with lines:

     void setOutputStream(OutputStream out);
     void setOutputStreamForError(OutputStream err);

   This enforces the loggers should not "print" directly to the PrintStream.
   For loggers that never "print" or "println", it is sufficent.
   For example, XmlLogger can "write" to the output stream and never needs the PrintStream.
   MailLogger even doesn't need an OutputStream!

2. class org.apache.tools.ant.DefaultLogger

     protected OutputStream outStream;
     protected OutputStream errStream;
     protected PrintWriter out;
     protected PrintWriter err;

     public void setOutputStream(OutputStream outStream) {
         this.outStream = outStream;

         try {
             if (System.getProperty("ant.logger.charset") == null) {

                 // use system default charset
                 this.out = new PrintWriter(new OutputStreamWriter(outStream));
             } else {

                 // use user specified charset
                 this.out = new PrintWriter(new OutputStreamWriter(outStream, System.getProperty("ant.logger.charset")));
             }
         } catch (UnsupportedEncodingException e) {

             // if the user specified an invalid charset, use system default
             this.out = new PrintWriter(new OutputStreamWriter(outStream));
         }
     }

   This is the base class for most text-base logger, such as AnsiColorLogger, NoBannerLogger,
..., etc.
   The class accepts an OutputStream and convert it to a PrintWriter, use the user specified
charset.

2. class org.apache.tools.ant.Main and class org.apache.tools.ant.taskdefs.Ant

   rather than set the out/err with PrintStream, use the following statement instead:

     System.setOut(new CharsetPrintStream(new DemuxOutputStream(project, false)));
     System.setErr(new CharsetPrintStream(new DemuxOutputStream(project, true)));

   where CharsetPrintStream is the extension of PrintStream (attached again).

3. class org.apache.tools.ant.DemuxOutputStream

    protected void processBuffer(ByteArrayOutputStream buffer) {
        String output = null;
        if (System.getProperty("ant.logger.charset") == null) {
            buffer.toString(System.getProperty("ant.logger.charset"));
        } else {
            buffer.toString();
        }
        project.demuxOutput(output, isErrorStream);
        resetBufferInfo();
    }

   so that the bytes array is converted properly to string.

If you like, I will debug and test the preceding code and send you the patch.

-- 
Michael Zhou <zyh@alibaba.com>



--
To unsubscribe, e-mail:   <mailto:ant-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:ant-dev-help@jakarta.apache.org>


Mime
View raw message