# HG changeset patch # User dfuchs # Date 1412686185 -7200 # Node ID 8203ecb092afb3dd3166d129e37eb941a6cd7a69 # Parent d280345c2cfb897c863def3ffaec7ebba002c734 8028788: Logger.enterring uses String concatenation in a loop Summary: Changed code to use StringBuilder instead. Reviewed-by: chegar, lancea diff -r d280345c2cfb -r 8203ecb092af jdk/src/java.logging/share/classes/java/util/logging/Logger.java --- a/jdk/src/java.logging/share/classes/java/util/logging/Logger.java Tue Oct 07 14:00:24 2014 +0400 +++ b/jdk/src/java.logging/share/classes/java/util/logging/Logger.java Tue Oct 07 14:49:45 2014 +0200 @@ -1376,8 +1376,12 @@ return; } if (!isLoggable(Level.FINER)) return; - for (int i = 0; i < params.length; i++) { - msg = msg + " {" + i + "}"; + if (params.length > 0) { + final StringBuilder b = new StringBuilder(msg); + for (int i = 0; i < params.length; i++) { + b.append(' ').append('{').append(i).append('}'); + } + msg = b.toString(); } logp(Level.FINER, sourceClass, sourceMethod, msg, params); } diff -r d280345c2cfb -r 8203ecb092af jdk/test/java/util/logging/Logger/entering/LoggerEnteringWithParams.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/test/java/util/logging/Logger/entering/LoggerEnteringWithParams.java Tue Oct 07 14:49:45 2014 +0200 @@ -0,0 +1,173 @@ + +/* + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +import java.io.ByteArrayOutputStream; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CopyOnWriteArrayList; +import java.util.logging.Handler; +import java.util.logging.Level; +import java.util.logging.LogRecord; +import java.util.logging.Logger; +import java.util.logging.SimpleFormatter; +import java.util.logging.StreamHandler; + +/** + * @test + * @bug 8028788 + * @summary tests that the message format string is correctly constructed + * by Logger.entering + * @run main/othervm LoggerEnteringWithParams + * @author daniel fuchs + */ +public class LoggerEnteringWithParams { + + final static Object[] data = { + "one", "two", "three", "four", "five", "six", "seven", "eight" + }; + + static final class TestHandler extends Handler { + final List events = new CopyOnWriteArrayList<>(); + final ByteArrayOutputStream bytes = new ByteArrayOutputStream(); + final StreamHandler wrapped = new StreamHandler(bytes, new SimpleFormatter()); + + @Override + public void publish(LogRecord record) { + events.add(record); + wrapped.publish(record); + wrapped.flush(); + } + @Override + public void flush() { + wrapped.flush(); + } + @Override + public void close() throws SecurityException { + wrapped.close(); + } + + @Override + public synchronized void setLevel(Level newLevel) throws SecurityException { + super.setLevel(newLevel); + wrapped.setLevel(newLevel); + } + + public void reset() { + bytes.reset(); + events.clear(); + } + + } + + public static void main(String[] args) { + Logger logger = Logger.getLogger("some.logger"); + TestHandler handler = new TestHandler(); + logger.setUseParentHandlers(false); + handler.setLevel(Level.ALL); + logger.setLevel(Level.FINEST); + logger.addHandler(handler); + + // Auto-detect the line termination used by SimpleFormatter - (CR vs CRLF) + logger.entering("test", "test"); + final String test = handler.bytes.toString(); + System.out.println(test); + final String lineEnd = test.substring(test.indexOf("ENTRY")+"ENTRY".length()); + System.out.print("Line termination is " + lineEnd.length() + " char(s) long:"); + for (int i=0; i>"); + } + if (!logged.contains(methodName)) { + throw new RuntimeException("Marker for " + methodName + + " not found." + + "\n\tgot: <<\n" + logged + "\t >>"); + } + if (!logged.contains(expected)) { + throw new RuntimeException("Marker for parameters[size=" + + i + "] not found" + + "\n\tgot: <<\n" + logged + "\t >>"); + } + if (!logged.contains(expected+lineEnd)) { + throw new RuntimeException("Marker for parameters[size=" + + i + "] has extra characters" + + "\n\tgot: <<\n" + logged + "\t >>"); + } + + LogRecord record = handler.events.remove(0); + if (!handler.events.isEmpty()) { + throw new RuntimeException("Handler has more log records: " + + handler.events.toString()); + } + if (!className.equals(record.getSourceClassName())) { + throw new RuntimeException("Unexpected class name in LogRecord." + + "\n\texpected: " + className + + "\n\tgot: " + record.getSourceClassName()); + } + if (!methodName.equals(record.getSourceMethodName())) { + throw new RuntimeException("Unexpected method name in LogRecord." + + "\n\texpected: " + methodName + + "\n\tgot: " + record.getSourceMethodName()); + } + if (!format.equals(record.getMessage())) { + throw new RuntimeException("Unexpected message format in LogRecord." + + "\n\texpected: " + format + + "\n\tgot: " + record.getMessage()); + } + if (!Arrays.deepEquals(params, record.getParameters())) { + throw new RuntimeException("Unexpected parameter array in LogRecord." + + "\n\texpected: " + Arrays.toString(params) + + "\n\tgot: " + Arrays.toString(record.getParameters())); + } + + handler.reset(); + } + } + +}