Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4368275

LOGGING APIs: Formatter's head string appears without logging

    Details

    • Subcomponent:
    • Resolved In Build:
      beta
    • CPU:
      generic
    • OS:
      generic
    • Verification:
      Verified

      Description



      Name: szR10032 Date: 09/05/2000



      The testbase_nsk/src/nsk/logging/StreamHandler/close/close007
      test detects some inconsistency between logging API implementation
      behaviour and StreamHandler class spec.
      The test shows the head-string appears in OutputStream even
      when the StreamHandler is closed without logging.
      Such behaviour is not defined clearly by the spec. The
      sole place in the spec, which explains when head string should
      be placed into OutputStream, is the StreamHandler.publish() definition:
      "If this is the first LogRecord to be written to a given OutputStream, the
      Formatter's "head" string is written to the stream before the LogRecord
      is written."
      This may imply that head-string should appear in OutputStream
      only if publish() method is invoked at least once.

      Similarly, the sole place, which explains when tail-string
      should be placed into OutputStream, is StreamHandler.close() definition:
      "The Formatter's "tail" string is written to the stream before it is closed."
      The tail-string appearing is consistent rigorously with the spec, for example,
      if close() method is not invoked tail-string does not appear
      in OutputStream.

      However, it may be that the test's behaviour is correct and head-string
      should appear every time when StreamHandler's OutputStream is created.
      But in this case I think the spec needs to be improved, for example,
      move the conditions of head-string appearing from StreamHandler.publish()
      definition to StreamHandler.StreamHandler(OutputStream, Formatter)
      definition or include the additional explanations in
      StreamHandler.close() definition.

      See log and source below.


      LOG:
      ====
      novo7% setenv CLASSPATH .:/export/ld24/java/hotspot/Solaris_JDK_1.3/lib/tools.jar:/export/home/_FOR_REMOVING/logging/classes
      novo7% javac -d . close007.java
      novo7% java -Xbootclasspath/a:/export/home/_FOR_REMOVING/logging/classes -Xbootclasspath/a:. logging.StreamHandler.close.close007
      FAILURE: (1) the log file under the StreamHandler using should not contain any info after close() invocation if publish() method is not invoked and Formatter.getTail() returns an empty string.
      novo7%



      SOURCE:
      =======
      // Ident: @(#)close007.java 1.1 00/08/24
      // Copyright 08/24/00 Sun Microsystems, Inc. All Rights Reserved

      package logging.StreamHandler.close;

      import java.util.logging.StreamHandler;
      import java.util.logging.Handler;
      import java.util.logging.Formatter;
      import java.util.logging.LogRecord;

      import java.io.PrintStream;
      import java.io.FileOutputStream;
      import java.io.FileInputStream;
      import java.io.DataInputStream;
      import java.io.OutputStream;
      import java.io.InputStream;



      /**
       * Test on
       *
       * Interface: java.util.logging.StreamHandler
       * Method: public void close()
       * Assertions: "Close the current output stream.
       * The Formatter's "tail" string is written to
       * the stream before it is closed."
       *
       *
       * Description: The test creates StreamHandler,
       * does not add this Handler to Logger and therefore does
       * not log any info through this StreamHandler, i.e.
       * StreamHandler.publish() method is not invoked and
       * Formatter's "head" string should not be written into OutputStream.
       * The used Formatter includes getHead()
       * method giving non-empty head string and the default getTail()
       * method giving empty tail string.
       * Consequently, according to StreamHandler spec the used
       * log file should not contain any info.
       *
       *
       * Comments:
       *
       *
       * Warning:
       *
       *
       * @author Serguei S. Zapreyev
       */

      public class close007 {
          static StreamHandler sh;
          static Formatter f;

          public static void main( String argv[] ) {
              System.exit(run(argv, System.out)+95); // JCK-compatible exit status
          }
          
          public static int run(String argv[],PrintStream out) {
              close007 test = new close007();

              try {
                  FileOutputStream fos = new FileOutputStream("close007F");
                  f = new close007f();
                  sh = new StreamHandler((OutputStream) fos, f);
                  sh.close();
                  fos.close();

                  FileInputStream fis = new FileInputStream("close007F");
                  DataInputStream dis = new DataInputStream((InputStream) fis);
                  String ln = null;
                  int count = 0;

                  for (;;) {
                      ln = dis.readLine();
      // out.println(ln);

                      if (ln == null) {
                          break;
                      } else {
                          count += 1;
                      }

                      if (ln.indexOf("<<<<<<< HEAD >>>>>>>") != -1) {
                          out.println("FAILURE: (1) the log file under the StreamHandler using should not contain any info after close() invocation if publish() method is not invoked and Formatter.getTail() returns an empty string.");
                          return 2; //TEST FAILED
                      }
                  }
                  if (count != 0) {
                      out.println("FAILURE: (2) the log file under the StreamHandler using should not contain any info after close() invocation if publish() method is not invoked and Formatter.getTail() returns an empty string.");
                      return 2; //TEST FAILED
                  }

                  fis.close();
              } catch (Exception ex) {
                  out.println(ex);
                  return 2; //TEST FAILED
              }

              return 0;
          }

      }

      class close007f extends Formatter {
          public java.lang.String format(LogRecord record) {
              return record.message;
          }
          public java.lang.String getHead(Handler h) {
              return "<<<<<<< HEAD >>>>>>>\n";
          }
      // public java.lang.String getTail(Handler h) {
      // return "<<<<<<< TAIL >>>>>>>\n";
      // }
      }



      ======================================================================

        Attachments

          Activity

            People

            • Assignee:
              ghamiltosunw Graham Hamilton (Inactive)
              Reporter:
              zsssunw Zss Zss (Inactive)
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:
                Imported:
                Indexed: