You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by mi...@apache.org on 2016/11/08 10:16:13 UTC
[23/31] logging-log4j2 git commit: LOG4J2-1671 make
EqualsIgnoreCaseReplacementConverter garbage-free
LOG4J2-1671 make EqualsIgnoreCaseReplacementConverter garbage-free
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/9ca35761
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/9ca35761
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/9ca35761
Branch: refs/heads/LOG4J2-1661
Commit: 9ca35761c2f83f54278a88a881415f9a3ed56187
Parents: 69e7981
Author: rpopma <rp...@apache.org>
Authored: Sun Nov 6 23:07:48 2016 +0900
Committer: rpopma <rp...@apache.org>
Committed: Sun Nov 6 23:07:48 2016 +0900
----------------------------------------------------------------------
.../pattern/EqualsBaseReplacementConverter.java | 97 ++++++++++++++++++++
.../EqualsIgnoreCaseReplacementConverter.java | 49 ++++------
.../pattern/EqualsReplacementConverter.java | 53 +----------
3 files changed, 119 insertions(+), 80 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9ca35761/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsBaseReplacementConverter.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsBaseReplacementConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsBaseReplacementConverter.java
new file mode 100644
index 0000000..61cb266
--- /dev/null
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsBaseReplacementConverter.java
@@ -0,0 +1,97 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+ */
+package org.apache.logging.log4j.core.pattern;
+
+import java.util.List;
+
+import org.apache.logging.log4j.core.LogEvent;
+
+/**
+ * Equals pattern converter.
+ */
+public abstract class EqualsBaseReplacementConverter extends LogEventPatternConverter {
+ private final List<PatternFormatter> formatters;
+ private final List<PatternFormatter> substitutionFormatters;
+ private final String substitution;
+ private final String testString;
+
+ /**
+ * Construct the converter.
+ *
+ * @param name converter name
+ * @param style converter style
+ * @param formatters The PatternFormatters to generate the text to manipulate.
+ * @param testString The test string.
+ * @param substitution The substitution string.
+ * @param parser The PatternParser.
+ */
+ protected EqualsBaseReplacementConverter(final String name, final String style,
+ final List<PatternFormatter> formatters, final String testString,
+ final String substitution, final PatternParser parser) {
+ super(name, style);
+ this.testString = testString;
+ this.substitution = substitution;
+ this.formatters = formatters;
+
+ // check if substitution needs to be parsed
+ substitutionFormatters = substitution.contains("%") ? parser.parse(substitution) : null;
+ }
+
+ /**
+ * {@inheritDoc}
+ */
+ @Override
+ public void format(final LogEvent event, final StringBuilder toAppendTo) {
+ final int initialSize = toAppendTo.length();
+ for (int i = 0; i < formatters.size(); i++) {
+ final PatternFormatter formatter = formatters.get(i);
+ formatter.format(event, toAppendTo);
+ }
+ if (equals(testString, toAppendTo, initialSize, toAppendTo.length() - initialSize)) {
+ toAppendTo.setLength(initialSize);
+ parseSubstitution(event, toAppendTo);
+ }
+ }
+
+ /**
+ * Returns true if the specified String equals the specified section of the specified StringBuilder.
+ *
+ * @param str the String to compare
+ * @param buff the StringBuilder to compare a section of
+ * @param from start index in the StringBuilder
+ * @param len length of the section in the StringBuilder
+ * @return true if equal, false otherwise
+ */
+ protected abstract boolean equals(String str, StringBuilder buff, int from, int len);
+
+ /**
+ * Adds the parsed substitution text to the specified buffer.
+ *
+ * @param event the current log event
+ * @param substitutionBuffer the StringBuilder to append the parsed substitution text to
+ */
+ void parseSubstitution(final LogEvent event, final StringBuilder substitutionBuffer) {
+ if (substitutionFormatters != null) {
+ for (int i = 0; i < substitutionFormatters.size(); i++) {
+ final PatternFormatter formatter = substitutionFormatters.get(i);
+ formatter.format(event, substitutionBuffer);
+ }
+ } else {
+ substitutionBuffer.append(substitution);
+ }
+ }
+}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9ca35761/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsIgnoreCaseReplacementConverter.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsIgnoreCaseReplacementConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsIgnoreCaseReplacementConverter.java
index 1265108..6f46c03 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsIgnoreCaseReplacementConverter.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsIgnoreCaseReplacementConverter.java
@@ -18,7 +18,6 @@ package org.apache.logging.log4j.core.pattern;
import java.util.List;
-import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.layout.PatternLayout;
@@ -28,7 +27,7 @@ import org.apache.logging.log4j.core.layout.PatternLayout;
*/
@Plugin(name = "equalsIgnoreCase", category = PatternConverter.CATEGORY)
@ConverterKeys({ "equalsIgnoreCase" })
-public final class EqualsIgnoreCaseReplacementConverter extends LogEventPatternConverter {
+public final class EqualsIgnoreCaseReplacementConverter extends EqualsBaseReplacementConverter {
/**
* Gets an instance of the class.
@@ -59,43 +58,31 @@ public final class EqualsIgnoreCaseReplacementConverter extends LogEventPatternC
final String p = options[1];
final PatternParser parser = PatternLayout.createPatternParser(config);
final List<PatternFormatter> formatters = parser.parse(options[0]);
- return new EqualsIgnoreCaseReplacementConverter(formatters, p, options[2]);
+ return new EqualsIgnoreCaseReplacementConverter(formatters, p, options[2], parser);
}
- private final List<PatternFormatter> formatters;
-
- private final String substitution;
-
- private final String testString;
-
/**
* Construct the converter.
- *
- * @param formatters
- * The PatternFormatters to generate the text to manipulate.
- * @param testString
- * The test string.
- * @param substitution
- * The substitution string.
+ *
+ * @param formatters The PatternFormatters to generate the text to manipulate.
+ * @param testString The test string.
+ * @param substitution The substitution string.
+ * @param parser The PatternParser.
*/
private EqualsIgnoreCaseReplacementConverter(final List<PatternFormatter> formatters, final String testString,
- final String substitution) {
- super("equals", "equals");
- this.testString = testString;
- this.substitution = substitution;
- this.formatters = formatters;
+ final String substitution, final PatternParser parser) {
+ super("equalsIgnoreCase", "equalsIgnoreCase", formatters, testString, substitution, parser);
}
- /**
- * {@inheritDoc}
- */
- @Override
- public void format(final LogEvent event, final StringBuilder toAppendTo) {
- final StringBuilder buf = new StringBuilder();
- for (final PatternFormatter formatter : formatters) {
- formatter.format(event, buf);
+ protected boolean equals(String str, StringBuilder buff, int from, int len) {
+ if (str.length() == len) {
+ for (int i = 0; i < len; i++) {
+ if (Character.toLowerCase(str.charAt(i)) != Character.toLowerCase(buff.charAt(i + from))) {
+ return false;
+ }
+ }
+ return true;
}
- final String string = buf.toString();
- toAppendTo.append(testString.equalsIgnoreCase(string) ? substitution : string);
+ return false;
}
}
http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/9ca35761/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsReplacementConverter.java
----------------------------------------------------------------------
diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsReplacementConverter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsReplacementConverter.java
index 7b50d25..4ccb0c5 100644
--- a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsReplacementConverter.java
+++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/EqualsReplacementConverter.java
@@ -18,7 +18,6 @@ package org.apache.logging.log4j.core.pattern;
import java.util.List;
-import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.layout.PatternLayout;
@@ -28,7 +27,7 @@ import org.apache.logging.log4j.core.layout.PatternLayout;
*/
@Plugin(name = "equals", category = PatternConverter.CATEGORY)
@ConverterKeys({ "equals" })
-public final class EqualsReplacementConverter extends LogEventPatternConverter {
+public final class EqualsReplacementConverter extends EqualsBaseReplacementConverter {
/**
* Gets an instance of the class.
@@ -60,47 +59,20 @@ public final class EqualsReplacementConverter extends LogEventPatternConverter {
return new EqualsReplacementConverter(formatters, p, options[2], parser);
}
- private final List<PatternFormatter> formatters;
- private final List<PatternFormatter> substitutionFormatters;
- private final String substitution;
- private final String testString;
-
/**
* Construct the converter.
*
* @param formatters The PatternFormatters to generate the text to manipulate.
* @param testString The test string.
* @param substitution The substitution string.
- * @param parser The PatternParser.
+ * @param parser The PatternParser.
*/
private EqualsReplacementConverter(final List<PatternFormatter> formatters, final String testString,
final String substitution, final PatternParser parser) {
- super("equals", "equals");
- this.testString = testString;
- this.substitution = substitution;
- this.formatters = formatters;
-
- // check if substitution needs to be parsed
- substitutionFormatters = substitution.contains("%") ? parser.parse(substitution) : null;
- }
-
- /**
- * {@inheritDoc}
- */
- @Override
- public void format(final LogEvent event, final StringBuilder toAppendTo) {
- final int initialSize = toAppendTo.length();
- for (int i = 0; i < formatters.size(); i++) {
- final PatternFormatter formatter = formatters.get(i);
- formatter.format(event, toAppendTo);
- }
- if (equals(testString, toAppendTo, initialSize, toAppendTo.length() - initialSize)) {
- toAppendTo.setLength(initialSize);
- parseSubstitution(event, toAppendTo);
- }
+ super("equals", "equals", formatters, testString, substitution, parser);
}
- private static boolean equals(String str, StringBuilder buff, int from, int len) {
+ protected boolean equals(String str, StringBuilder buff, int from, int len) {
if (str.length() == len) {
for (int i = 0; i < len; i++) {
if (str.charAt(i) != buff.charAt(i + from)) {
@@ -111,21 +83,4 @@ public final class EqualsReplacementConverter extends LogEventPatternConverter {
}
return false;
}
-
- /**
- * Adds the parsed substitution text to the specified buffer.
- *
- * @param event the current log event
- * @param substitutionBuffer the StringBuilder to append the parsed substitution text to
- */
- void parseSubstitution(final LogEvent event, final StringBuilder substitutionBuffer) {
- if (substitutionFormatters != null) {
- for (int i = 0; i < substitutionFormatters.size(); i++) {
- final PatternFormatter formatter = substitutionFormatters.get(i);
- formatter.format(event, substitutionBuffer);
- }
- } else {
- substitutionBuffer.append(substitution);
- }
- }
}