LOG4J2-2225 move time-related classes from core.util to core.time.internal package
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/ba658a0f Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/ba658a0f Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/ba658a0f Branch: refs/heads/master Commit: ba658a0fa6c18952e2ae110eee286182b3605d7b Parents: 21bc3aa Author: rpopma <[email protected]> Authored: Sun Jan 28 23:50:13 2018 +0900 Committer: rpopma <[email protected]> Committed: Sun Jan 28 23:50:13 2018 +0900 ---------------------------------------------------------------------- .../cassandra/ClockTimestampGenerator.java | 2 +- .../appender/rolling/action/IfLastModified.java | 2 +- .../logging/log4j/core/async/AsyncLogger.java | 2 +- .../core/config/AbstractConfiguration.java | 2 +- .../logging/log4j/core/filter/TimeFilter.java | 2 +- .../logging/log4j/core/impl/Log4jLogEvent.java | 2 + .../core/impl/ReusableLogEventFactory.java | 2 +- .../log4j/core/pattern/PatternParser.java | 2 +- .../log4j/core/time/internal/CachedClock.java | 91 +++++++++++++++ .../log4j/core/time/internal/ClockFactory.java | 110 +++++++++++++++++++ .../core/time/internal/CoarseCachedClock.java | 81 ++++++++++++++ .../core/time/internal/DummyNanoClock.java | 50 +++++++++ .../log4j/core/time/internal/SystemClock.java | 35 ++++++ .../core/time/internal/SystemMillisClock.java | 36 ++++++ .../core/time/internal/SystemNanoClock.java | 35 ++++++ .../logging/log4j/core/util/CachedClock.java | 88 --------------- .../logging/log4j/core/util/ClockFactory.java | 108 ------------------ .../log4j/core/util/CoarseCachedClock.java | 78 ------------- .../logging/log4j/core/util/DummyNanoClock.java | 48 -------- .../logging/log4j/core/util/SystemClock.java | 33 ------ .../log4j/core/util/SystemMillisClock.java | 34 ------ .../log4j/core/util/SystemNanoClock.java | 33 ------ .../log4j/core/TimestampMessageTest.java | 4 +- .../appender/JsonCompleteFileAppenderTest.java | 5 +- .../log4j/core/async/AsyncLoggerTest.java | 2 +- .../core/async/AsyncLoggerTestNanoTime.java | 4 +- .../async/AsyncLoggerTimestampMessageTest.java | 4 +- .../core/async/RingBufferLogEventTest.java | 2 +- .../log4j/core/filter/TimeFilterTest.java | 4 +- .../core/impl/Log4jLogEventNanoTimeTest.java | 4 +- .../log4j/core/impl/Log4jLogEventTest.java | 6 +- .../log4j/core/pattern/PatternParserTest.java | 4 +- .../core/time/internal/ClockFactoryTest.java | 108 ++++++++++++++++++ .../core/time/internal/DummyNanoClockTest.java | 40 +++++++ .../core/time/internal/SystemClockTest.java | 47 ++++++++ .../core/time/internal/SystemNanoClockTest.java | 41 +++++++ .../log4j/core/util/ClockFactoryTest.java | 103 ----------------- .../log4j/core/util/DummyNanoClockTest.java | 39 ------- .../log4j/core/util/SystemClockTest.java | 46 -------- .../log4j/core/util/SystemNanoClockTest.java | 39 ------- .../logging/log4j/perf/jmh/ClocksBenchmark.java | 6 +- 41 files changed, 704 insertions(+), 680 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-cassandra/src/main/java/org/apache/logging/log4j/cassandra/ClockTimestampGenerator.java ---------------------------------------------------------------------- diff --git a/log4j-cassandra/src/main/java/org/apache/logging/log4j/cassandra/ClockTimestampGenerator.java b/log4j-cassandra/src/main/java/org/apache/logging/log4j/cassandra/ClockTimestampGenerator.java index 1c7328b..50d7bc4 100644 --- a/log4j-cassandra/src/main/java/org/apache/logging/log4j/cassandra/ClockTimestampGenerator.java +++ b/log4j-cassandra/src/main/java/org/apache/logging/log4j/cassandra/ClockTimestampGenerator.java @@ -18,7 +18,7 @@ package org.apache.logging.log4j.cassandra; import com.datastax.driver.core.TimestampGenerator; import org.apache.logging.log4j.core.util.Clock; -import org.apache.logging.log4j.core.util.ClockFactory; +import org.apache.logging.log4j.core.time.internal.ClockFactory; /** * A {@link TimestampGenerator} implementation using the configured {@link Clock}. http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java index 4a4c717..66902d7 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/appender/rolling/action/IfLastModified.java @@ -31,7 +31,7 @@ import org.apache.logging.log4j.core.config.plugins.PluginAttribute; import org.apache.logging.log4j.core.config.plugins.PluginElement; import org.apache.logging.log4j.core.config.plugins.PluginFactory; import org.apache.logging.log4j.core.util.Clock; -import org.apache.logging.log4j.core.util.ClockFactory; +import org.apache.logging.log4j.core.time.internal.ClockFactory; import org.apache.logging.log4j.status.StatusLogger; /** http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java index 358422c..fd7c6f6 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/async/AsyncLogger.java @@ -31,7 +31,7 @@ import org.apache.logging.log4j.core.config.ReliabilityStrategy; import org.apache.logging.log4j.core.impl.ContextDataFactory; import org.apache.logging.log4j.core.impl.ContextDataInjectorFactory; import org.apache.logging.log4j.core.util.Clock; -import org.apache.logging.log4j.core.util.ClockFactory; +import org.apache.logging.log4j.core.time.internal.ClockFactory; import org.apache.logging.log4j.core.util.NanoClock; import org.apache.logging.log4j.message.Message; import org.apache.logging.log4j.message.MessageFactory; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java index 3c0e810..98f072a 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java @@ -62,7 +62,7 @@ import org.apache.logging.log4j.core.script.AbstractScript; import org.apache.logging.log4j.core.script.ScriptManager; import org.apache.logging.log4j.core.script.ScriptRef; import org.apache.logging.log4j.core.util.Constants; -import org.apache.logging.log4j.core.util.DummyNanoClock; +import org.apache.logging.log4j.core.time.internal.DummyNanoClock; import org.apache.logging.log4j.core.util.Loader; import org.apache.logging.log4j.core.util.NameUtil; import org.apache.logging.log4j.core.util.NanoClock; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/TimeFilter.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/TimeFilter.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/TimeFilter.java index 3d01e35..8cab11f 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/TimeFilter.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/filter/TimeFilter.java @@ -31,7 +31,7 @@ import org.apache.logging.log4j.core.config.plugins.Plugin; import org.apache.logging.log4j.core.config.plugins.PluginAttribute; import org.apache.logging.log4j.core.config.plugins.PluginFactory; import org.apache.logging.log4j.core.util.Clock; -import org.apache.logging.log4j.core.util.ClockFactory; +import org.apache.logging.log4j.core.time.internal.ClockFactory; import org.apache.logging.log4j.message.Message; import org.apache.logging.log4j.util.PerformanceSensitive; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java index 2fe5a9b..48076d8 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/Log4jLogEvent.java @@ -29,6 +29,8 @@ import org.apache.logging.log4j.Level; import org.apache.logging.log4j.Marker; import org.apache.logging.log4j.ThreadContext; import org.apache.logging.log4j.core.ContextDataInjector; +import org.apache.logging.log4j.core.time.internal.ClockFactory; +import org.apache.logging.log4j.core.time.internal.DummyNanoClock; import org.apache.logging.log4j.core.util.*; import org.apache.logging.log4j.core.time.Instant; import org.apache.logging.log4j.core.time.MutableInstant; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ReusableLogEventFactory.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ReusableLogEventFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ReusableLogEventFactory.java index 0ecefed..44a2572 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ReusableLogEventFactory.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/impl/ReusableLogEventFactory.java @@ -26,7 +26,7 @@ import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.async.ThreadNameCachingStrategy; import org.apache.logging.log4j.core.config.Property; import org.apache.logging.log4j.core.util.Clock; -import org.apache.logging.log4j.core.util.ClockFactory; +import org.apache.logging.log4j.core.time.internal.ClockFactory; import org.apache.logging.log4j.message.Message; import org.apache.logging.log4j.util.StringMap; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java index 91c11ec..ac6940a 100644 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/PatternParser.java @@ -29,7 +29,7 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.config.Configuration; import org.apache.logging.log4j.core.config.plugins.util.PluginManager; import org.apache.logging.log4j.core.config.plugins.util.PluginType; -import org.apache.logging.log4j.core.util.SystemNanoClock; +import org.apache.logging.log4j.core.time.internal.SystemNanoClock; import org.apache.logging.log4j.status.StatusLogger; import org.apache.logging.log4j.util.Strings; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/CachedClock.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/CachedClock.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/CachedClock.java new file mode 100644 index 0000000..9ff7ebd --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/CachedClock.java @@ -0,0 +1,91 @@ +/* + * 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.time.internal; + +import org.apache.logging.log4j.core.util.Clock; +import org.apache.logging.log4j.core.util.Log4jThread; + +import java.util.concurrent.locks.LockSupport; + +/** + * Implementation of the {@code Clock} interface that tracks the time in a + * private long field that is updated by a background thread once every + * millisecond. Timers on most platforms do not have millisecond granularity, so + * the returned value may "jump" every 10 or 16 milliseconds. To reduce this + * problem, this class also updates the internal time value every 1024 calls to + * {@code currentTimeMillis()}. + */ +public final class CachedClock implements Clock { + private static final int UPDATE_THRESHOLD = 1000; + private static volatile CachedClock instance; + private static final Object INSTANCE_LOCK = new Object(); + private volatile long millis = System.currentTimeMillis(); + private short count = 0; + + private CachedClock() { + final Thread updater = new Log4jThread(new Runnable() { + @Override + public void run() { + while (true) { + final long time = System.currentTimeMillis(); + millis = time; + + // avoid explicit dependency on sun.misc.Util + LockSupport.parkNanos(1000 * 1000); + } + } + }, "CachedClock Updater Thread"); + updater.setDaemon(true); + updater.start(); + } + + public static CachedClock instance() { + // LOG4J2-819: use lazy initialization of threads + CachedClock result = instance; + if (result == null) { + synchronized (INSTANCE_LOCK) { + result = instance; + if (result == null) { + instance = result = new CachedClock(); + } + } + } + return result; + } + + /** + * Returns the value of a private long field that is updated by a background + * thread once every millisecond. Timers on most platforms do not + * have millisecond granularity, the returned value may "jump" every 10 or + * 16 milliseconds. To reduce this problem, this method also updates the + * internal time value every 1024 calls. + * @return the cached time + */ + @Override + public long currentTimeMillis() { + + // The count field is not volatile on purpose to reduce contention on this field. + // This means that some threads may not see the increments made to this field + // by other threads. This is not a problem: the timestamp does not need to be + // updated exactly every 1000 calls. + if (++count > UPDATE_THRESHOLD) { + millis = System.currentTimeMillis(); // update volatile field: store-store barrier + count = 0; // after a memory barrier: this change _is_ visible to other threads + } + return millis; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/ClockFactory.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/ClockFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/ClockFactory.java new file mode 100644 index 0000000..415e903 --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/ClockFactory.java @@ -0,0 +1,110 @@ +/* + * 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.time.internal; + +import org.apache.logging.log4j.core.time.PreciseClock; +import org.apache.logging.log4j.core.util.Clock; +import org.apache.logging.log4j.core.util.Loader; +import org.apache.logging.log4j.status.StatusLogger; +import org.apache.logging.log4j.util.PropertiesUtil; +import org.apache.logging.log4j.util.Supplier; + +import java.util.HashMap; +import java.util.Map; + +/** + * Factory for {@code Clock} objects. + */ +public final class ClockFactory { + + /** + * Name of the system property that can be used to specify a {@code Clock} + * implementation class. The value of this property is {@value}. + */ + public static final String PROPERTY_NAME = "log4j.Clock"; + private static final StatusLogger LOGGER = StatusLogger.getLogger(); + + // private static final Clock clock = createClock(); + + private ClockFactory() { + } + + /** + * Returns a {@code Clock} instance depending on the value of system + * property {@link #PROPERTY_NAME}. + * <p> + * If system property {@code log4j.Clock=CachedClock} is specified, + * this method returns an instance of {@link CachedClock}. If system + * property {@code log4j.Clock=CoarseCachedClock} is specified, this + * method returns an instance of {@link CoarseCachedClock}. + * </p> + * <p> + * If another value is specified, this value is taken as the fully qualified + * class name of a class that implements the {@code Clock} interface. An + * object of this class is instantiated and returned. + * </p> + * <p> + * If no value is specified, or if the specified value could not correctly + * be instantiated or did not implement the {@code Clock} interface, then an + * instance of {@link SystemClock} is returned. + * </p> + * + * @return a {@code Clock} instance + */ + public static Clock getClock() { + return createClock(); + } + + private static Map<String, Supplier<Clock>> aliases() { + Map<String, Supplier<Clock>> result = new HashMap<>(); + result.put("SystemClock", new Supplier<Clock>() { @Override public Clock get() { return new SystemClock(); } }); + result.put("SystemMillisClock", new Supplier<Clock>() { @Override public Clock get() { return new SystemMillisClock(); } }); + result.put("CachedClock", new Supplier<Clock>() { @Override public Clock get() { return CachedClock.instance(); } }); + result.put("CoarseCachedClock", new Supplier<Clock>() { @Override public Clock get() { return CoarseCachedClock.instance(); } }); + result.put("org.apache.logging.log4j.core.time.internal.CachedClock", new Supplier<Clock>() { @Override public Clock get() { return CachedClock.instance(); } }); + result.put("org.apache.logging.log4j.core.time.internal.CoarseCachedClock", new Supplier<Clock>() { @Override public Clock get() { return CoarseCachedClock.instance(); } }); + return result; + } + + private static Clock createClock() { + final String userRequest = PropertiesUtil.getProperties().getStringProperty(PROPERTY_NAME); + if (userRequest == null) { + LOGGER.trace("Using default SystemClock for timestamps."); + return logSupportedPrecision(new SystemClock()); + } + Supplier<Clock> specified = aliases().get(userRequest); + if (specified != null) { + LOGGER.trace("Using specified {} for timestamps.", userRequest); + return logSupportedPrecision(specified.get()); + } + try { + final Clock result = Loader.newCheckedInstanceOf(userRequest, Clock.class); + LOGGER.trace("Using {} for timestamps.", result.getClass().getName()); + return logSupportedPrecision(result); + } catch (final Exception e) { + final String fmt = "Could not create {}: {}, using default SystemClock for timestamps."; + LOGGER.error(fmt, userRequest, e); + return logSupportedPrecision(new SystemClock()); + } + } + + private static Clock logSupportedPrecision(Clock clock) { + String support = clock instanceof PreciseClock ? "supports" : "does not support"; + LOGGER.debug("{} {} precise timestamps.", clock.getClass().getName(), support); + return clock; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/CoarseCachedClock.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/CoarseCachedClock.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/CoarseCachedClock.java new file mode 100644 index 0000000..bbbc1bd --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/CoarseCachedClock.java @@ -0,0 +1,81 @@ +/* + * 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.time.internal; + +import org.apache.logging.log4j.core.util.Clock; +import org.apache.logging.log4j.core.util.Log4jThread; + +import java.util.concurrent.locks.LockSupport; + +/** + * This Clock implementation is similar to CachedClock. It is slightly faster at + * the cost of some accuracy. + */ +public final class CoarseCachedClock implements Clock { + private static volatile CoarseCachedClock instance; + private static final Object INSTANCE_LOCK = new Object(); + // ignore IDE complaints; volatile long is fine + private volatile long millis = System.currentTimeMillis(); + + private final Thread updater = new Log4jThread("CoarseCachedClock Updater Thread") { + @Override + public void run() { + while (true) { + millis = System.currentTimeMillis(); + + // avoid explicit dependency on sun.misc.Util + LockSupport.parkNanos(1000 * 1000); + } + } + }; + + private CoarseCachedClock() { + updater.setDaemon(true); + updater.start(); + } + + /** + * Returns the singleton instance. + * + * @return the singleton instance + */ + public static CoarseCachedClock instance() { + // LOG4J2-819: use lazy initialization of threads + CoarseCachedClock result = instance; + if (result == null) { + synchronized (INSTANCE_LOCK) { + result = instance; + if (result == null) { + instance = result = new CoarseCachedClock(); + } + } + } + return result; + } + + /** + * Returns the value of a private long field that is updated by a background + * thread once every millisecond. Because timers on most platforms do not + * have millisecond granularity, the returned value may "jump" every 10 or + * 16 milliseconds. + * @return the cached time + */ + @Override + public long currentTimeMillis() { + return millis; + } +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/DummyNanoClock.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/DummyNanoClock.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/DummyNanoClock.java new file mode 100644 index 0000000..f89f6d8 --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/DummyNanoClock.java @@ -0,0 +1,50 @@ +/* + * 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.time.internal; + +import org.apache.logging.log4j.core.util.NanoClock; + +/** + * Implementation of the {@code NanoClock} interface that always returns a fixed value. + */ +public final class DummyNanoClock implements NanoClock { + + private final long fixedNanoTime; + + public DummyNanoClock() { + this(0L); + } + + /** + * Constructs a new DummyNanoClock with the specified value to return. + * @param fixedNanoTime the value to return from {@link #nanoTime()}. + */ + public DummyNanoClock(final long fixedNanoTime) { + this.fixedNanoTime = fixedNanoTime; + } + + /** + * Returns the constructor value. + * + * @return the constructor value + */ + @Override + public long nanoTime() { + return fixedNanoTime; + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/SystemClock.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/SystemClock.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/SystemClock.java new file mode 100644 index 0000000..0626149 --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/SystemClock.java @@ -0,0 +1,35 @@ +/* + * 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.time.internal; + +import org.apache.logging.log4j.core.util.Clock; + +/** + * Implementation of the {@code Clock} interface that returns the system time. + */ +public final class SystemClock implements Clock { + + /** + * Returns the system time. + * @return the result of calling {@code System.currentTimeMillis()} + */ + @Override + public long currentTimeMillis() { + return System.currentTimeMillis(); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/SystemMillisClock.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/SystemMillisClock.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/SystemMillisClock.java new file mode 100644 index 0000000..49b7ffc --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/SystemMillisClock.java @@ -0,0 +1,36 @@ +/* + * 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.time.internal; + +import org.apache.logging.log4j.core.util.Clock; + +/** + * Implementation of the {@code Clock} interface that returns the system time in millisecond granularity. + * @since 2.11 + */ +public final class SystemMillisClock implements Clock { + + /** + * Returns the system time. + * @return the result of calling {@code System.currentTimeMillis()} + */ + @Override + public long currentTimeMillis() { + return System.currentTimeMillis(); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/SystemNanoClock.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/SystemNanoClock.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/SystemNanoClock.java new file mode 100644 index 0000000..1cd1a6f --- /dev/null +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/time/internal/SystemNanoClock.java @@ -0,0 +1,35 @@ +/* + * 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.time.internal; + +import org.apache.logging.log4j.core.util.NanoClock; + +/** + * Implementation of the {@code NanoClock} interface that returns the system nano time. + */ +public final class SystemNanoClock implements NanoClock { + + /** + * Returns the system high-resolution time. + * @return the result of calling {@code System.nanoTime()} + */ + @Override + public long nanoTime() { + return System.nanoTime(); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CachedClock.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CachedClock.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CachedClock.java deleted file mode 100644 index f6d51ab..0000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CachedClock.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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.util; - -import java.util.concurrent.locks.LockSupport; - -/** - * Implementation of the {@code Clock} interface that tracks the time in a - * private long field that is updated by a background thread once every - * millisecond. Timers on most platforms do not have millisecond granularity, so - * the returned value may "jump" every 10 or 16 milliseconds. To reduce this - * problem, this class also updates the internal time value every 1024 calls to - * {@code currentTimeMillis()}. - */ -public final class CachedClock implements Clock { - private static final int UPDATE_THRESHOLD = 1000; - private static volatile CachedClock instance; - private static final Object INSTANCE_LOCK = new Object(); - private volatile long millis = System.currentTimeMillis(); - private short count = 0; - - private CachedClock() { - final Thread updater = new Log4jThread(new Runnable() { - @Override - public void run() { - while (true) { - final long time = System.currentTimeMillis(); - millis = time; - - // avoid explicit dependency on sun.misc.Util - LockSupport.parkNanos(1000 * 1000); - } - } - }, "CachedClock Updater Thread"); - updater.setDaemon(true); - updater.start(); - } - - public static CachedClock instance() { - // LOG4J2-819: use lazy initialization of threads - CachedClock result = instance; - if (result == null) { - synchronized (INSTANCE_LOCK) { - result = instance; - if (result == null) { - instance = result = new CachedClock(); - } - } - } - return result; - } - - /** - * Returns the value of a private long field that is updated by a background - * thread once every millisecond. Timers on most platforms do not - * have millisecond granularity, the returned value may "jump" every 10 or - * 16 milliseconds. To reduce this problem, this method also updates the - * internal time value every 1024 calls. - * @return the cached time - */ - @Override - public long currentTimeMillis() { - - // The count field is not volatile on purpose to reduce contention on this field. - // This means that some threads may not see the increments made to this field - // by other threads. This is not a problem: the timestamp does not need to be - // updated exactly every 1000 calls. - if (++count > UPDATE_THRESHOLD) { - millis = System.currentTimeMillis(); // update volatile field: store-store barrier - count = 0; // after a memory barrier: this change _is_ visible to other threads - } - return millis; - } -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ClockFactory.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ClockFactory.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ClockFactory.java deleted file mode 100644 index 8b965b6..0000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/ClockFactory.java +++ /dev/null @@ -1,108 +0,0 @@ -/* - * 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.util; - -import org.apache.logging.log4j.core.time.PreciseClock; -import org.apache.logging.log4j.status.StatusLogger; -import org.apache.logging.log4j.util.PropertiesUtil; -import org.apache.logging.log4j.util.Supplier; - -import java.util.HashMap; -import java.util.Map; - -/** - * Factory for {@code Clock} objects. - */ -public final class ClockFactory { - - /** - * Name of the system property that can be used to specify a {@code Clock} - * implementation class. The value of this property is {@value}. - */ - public static final String PROPERTY_NAME = "log4j.Clock"; - private static final StatusLogger LOGGER = StatusLogger.getLogger(); - - // private static final Clock clock = createClock(); - - private ClockFactory() { - } - - /** - * Returns a {@code Clock} instance depending on the value of system - * property {@link #PROPERTY_NAME}. - * <p> - * If system property {@code log4j.Clock=CachedClock} is specified, - * this method returns an instance of {@link CachedClock}. If system - * property {@code log4j.Clock=CoarseCachedClock} is specified, this - * method returns an instance of {@link CoarseCachedClock}. - * </p> - * <p> - * If another value is specified, this value is taken as the fully qualified - * class name of a class that implements the {@code Clock} interface. An - * object of this class is instantiated and returned. - * </p> - * <p> - * If no value is specified, or if the specified value could not correctly - * be instantiated or did not implement the {@code Clock} interface, then an - * instance of {@link SystemClock} is returned. - * </p> - * - * @return a {@code Clock} instance - */ - public static Clock getClock() { - return createClock(); - } - - private static Map<String, Supplier<Clock>> aliases() { - Map<String, Supplier<Clock>> result = new HashMap<>(); - result.put("SystemClock", new Supplier<Clock>() { @Override public Clock get() { return new SystemClock(); } }); - result.put("SystemMillisClock", new Supplier<Clock>() { @Override public Clock get() { return new SystemMillisClock(); } }); - result.put("CachedClock", new Supplier<Clock>() { @Override public Clock get() { return CachedClock.instance(); } }); - result.put("CoarseCachedClock", new Supplier<Clock>() { @Override public Clock get() { return CoarseCachedClock.instance(); } }); - result.put("org.apache.logging.log4j.core.util.CachedClock", new Supplier<Clock>() { @Override public Clock get() { return CachedClock.instance(); } }); - result.put("org.apache.logging.log4j.core.util.CoarseCachedClock", new Supplier<Clock>() { @Override public Clock get() { return CoarseCachedClock.instance(); } }); - return result; - } - - private static Clock createClock() { - final String userRequest = PropertiesUtil.getProperties().getStringProperty(PROPERTY_NAME); - if (userRequest == null) { - LOGGER.trace("Using default SystemClock for timestamps."); - return logSupportedPrecision(new SystemClock()); - } - Supplier<Clock> specified = aliases().get(userRequest); - if (specified != null) { - LOGGER.trace("Using specified {} for timestamps.", userRequest); - return logSupportedPrecision(specified.get()); - } - try { - final Clock result = Loader.newCheckedInstanceOf(userRequest, Clock.class); - LOGGER.trace("Using {} for timestamps.", result.getClass().getName()); - return logSupportedPrecision(result); - } catch (final Exception e) { - final String fmt = "Could not create {}: {}, using default SystemClock for timestamps."; - LOGGER.error(fmt, userRequest, e); - return logSupportedPrecision(new SystemClock()); - } - } - - private static Clock logSupportedPrecision(Clock clock) { - String support = clock instanceof PreciseClock ? "supports" : "does not support"; - LOGGER.debug("{} {} precise timestamps.", clock.getClass().getName(), support); - return clock; - } -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CoarseCachedClock.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CoarseCachedClock.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CoarseCachedClock.java deleted file mode 100644 index 144f470..0000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/CoarseCachedClock.java +++ /dev/null @@ -1,78 +0,0 @@ -/* - * 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.util; - -import java.util.concurrent.locks.LockSupport; - -/** - * This Clock implementation is similar to CachedClock. It is slightly faster at - * the cost of some accuracy. - */ -public final class CoarseCachedClock implements Clock { - private static volatile CoarseCachedClock instance; - private static final Object INSTANCE_LOCK = new Object(); - // ignore IDE complaints; volatile long is fine - private volatile long millis = System.currentTimeMillis(); - - private final Thread updater = new Log4jThread("CoarseCachedClock Updater Thread") { - @Override - public void run() { - while (true) { - millis = System.currentTimeMillis(); - - // avoid explicit dependency on sun.misc.Util - LockSupport.parkNanos(1000 * 1000); - } - } - }; - - private CoarseCachedClock() { - updater.setDaemon(true); - updater.start(); - } - - /** - * Returns the singleton instance. - * - * @return the singleton instance - */ - public static CoarseCachedClock instance() { - // LOG4J2-819: use lazy initialization of threads - CoarseCachedClock result = instance; - if (result == null) { - synchronized (INSTANCE_LOCK) { - result = instance; - if (result == null) { - instance = result = new CoarseCachedClock(); - } - } - } - return result; - } - - /** - * Returns the value of a private long field that is updated by a background - * thread once every millisecond. Because timers on most platforms do not - * have millisecond granularity, the returned value may "jump" every 10 or - * 16 milliseconds. - * @return the cached time - */ - @Override - public long currentTimeMillis() { - return millis; - } -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/util/DummyNanoClock.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/DummyNanoClock.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/DummyNanoClock.java deleted file mode 100644 index e3d6c2b..0000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/DummyNanoClock.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * 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.util; - -/** - * Implementation of the {@code NanoClock} interface that always returns a fixed value. - */ -public final class DummyNanoClock implements NanoClock { - - private final long fixedNanoTime; - - public DummyNanoClock() { - this(0L); - } - - /** - * Constructs a new DummyNanoClock with the specified value to return. - * @param fixedNanoTime the value to return from {@link #nanoTime()}. - */ - public DummyNanoClock(final long fixedNanoTime) { - this.fixedNanoTime = fixedNanoTime; - } - - /** - * Returns the constructor value. - * - * @return the constructor value - */ - @Override - public long nanoTime() { - return fixedNanoTime; - } - -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemClock.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemClock.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemClock.java deleted file mode 100644 index 6dab557..0000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemClock.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.util; - -/** - * Implementation of the {@code Clock} interface that returns the system time. - */ -public final class SystemClock implements Clock { - - /** - * Returns the system time. - * @return the result of calling {@code System.currentTimeMillis()} - */ - @Override - public long currentTimeMillis() { - return System.currentTimeMillis(); - } - -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemMillisClock.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemMillisClock.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemMillisClock.java deleted file mode 100644 index f267320..0000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemMillisClock.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * 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.util; - -/** - * Implementation of the {@code Clock} interface that returns the system time in millisecond granularity. - * @since 2.11 - */ -public final class SystemMillisClock implements Clock { - - /** - * Returns the system time. - * @return the result of calling {@code System.currentTimeMillis()} - */ - @Override - public long currentTimeMillis() { - return System.currentTimeMillis(); - } - -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemNanoClock.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemNanoClock.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemNanoClock.java deleted file mode 100644 index 9310e6c..0000000 --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/util/SystemNanoClock.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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.util; - -/** - * Implementation of the {@code NanoClock} interface that returns the system nano time. - */ -public final class SystemNanoClock implements NanoClock { - - /** - * Returns the system high-resolution time. - * @return the result of calling {@code System.nanoTime()} - */ - @Override - public long nanoTime() { - return System.nanoTime(); - } - -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/TimestampMessageTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/TimestampMessageTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/TimestampMessageTest.java index 60d643b..a2c02fb 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/TimestampMessageTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/TimestampMessageTest.java @@ -20,8 +20,8 @@ import java.util.List; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.util.Clock; -import org.apache.logging.log4j.core.util.ClockFactory; -import org.apache.logging.log4j.core.util.ClockFactoryTest; +import org.apache.logging.log4j.core.time.internal.ClockFactory; +import org.apache.logging.log4j.core.time.internal.ClockFactoryTest; import org.apache.logging.log4j.core.util.Constants; import org.apache.logging.log4j.junit.LoggerContextRule; import org.apache.logging.log4j.message.Message; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/JsonCompleteFileAppenderTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/JsonCompleteFileAppenderTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/JsonCompleteFileAppenderTest.java index 323ca13..fcbd40c 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/JsonCompleteFileAppenderTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/JsonCompleteFileAppenderTest.java @@ -16,20 +16,17 @@ */ package org.apache.logging.log4j.core.appender; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; import java.nio.charset.Charset; import java.nio.file.Files; import java.util.List; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.categories.Layouts; -import org.apache.logging.log4j.core.filter.TimeFilterTest; import org.apache.logging.log4j.core.impl.Log4jLogEventTest; import org.apache.logging.log4j.core.selector.ContextSelector; import org.apache.logging.log4j.core.selector.CoreContextSelectors; -import org.apache.logging.log4j.core.util.ClockFactory; +import org.apache.logging.log4j.core.time.internal.ClockFactory; import org.apache.logging.log4j.junit.CleanFiles; import org.apache.logging.log4j.junit.LoggerContextRule; import org.junit.AfterClass; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTest.java index aad8ce7..afea703 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTest.java @@ -25,7 +25,7 @@ import org.apache.logging.log4j.categories.AsyncLoggers; import org.apache.logging.log4j.core.CoreLoggerContexts; import org.apache.logging.log4j.core.config.ConfigurationFactory; import org.apache.logging.log4j.core.util.Constants; -import org.apache.logging.log4j.core.util.DummyNanoClock; +import org.apache.logging.log4j.core.time.internal.DummyNanoClock; import org.apache.logging.log4j.util.Strings; import org.junit.AfterClass; import org.junit.BeforeClass; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestNanoTime.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestNanoTime.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestNanoTime.java index d9e15d0..628e685 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestNanoTime.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestNanoTime.java @@ -26,8 +26,8 @@ import org.apache.logging.log4j.categories.AsyncLoggers; import org.apache.logging.log4j.core.CoreLoggerContexts; import org.apache.logging.log4j.core.config.ConfigurationFactory; import org.apache.logging.log4j.core.util.Constants; -import org.apache.logging.log4j.core.util.DummyNanoClock; -import org.apache.logging.log4j.core.util.SystemNanoClock; +import org.apache.logging.log4j.core.time.internal.DummyNanoClock; +import org.apache.logging.log4j.core.time.internal.SystemNanoClock; import org.apache.logging.log4j.util.Strings; import org.junit.AfterClass; import org.junit.BeforeClass; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTimestampMessageTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTimestampMessageTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTimestampMessageTest.java index f797daf..4c9a00f 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTimestampMessageTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTimestampMessageTest.java @@ -26,8 +26,8 @@ import org.apache.logging.log4j.categories.AsyncLoggers; import org.apache.logging.log4j.core.CoreLoggerContexts; import org.apache.logging.log4j.core.config.ConfigurationFactory; import org.apache.logging.log4j.core.util.Clock; -import org.apache.logging.log4j.core.util.ClockFactory; -import org.apache.logging.log4j.core.util.ClockFactoryTest; +import org.apache.logging.log4j.core.time.internal.ClockFactory; +import org.apache.logging.log4j.core.time.internal.ClockFactoryTest; import org.apache.logging.log4j.core.util.Constants; import org.apache.logging.log4j.message.Message; import org.apache.logging.log4j.message.SimpleMessage; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/async/RingBufferLogEventTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/RingBufferLogEventTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/RingBufferLogEventTest.java index e3920f7..cc50b42 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/RingBufferLogEventTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/RingBufferLogEventTest.java @@ -30,7 +30,7 @@ import org.apache.logging.log4j.MarkerManager; import org.apache.logging.log4j.ThreadContext.ContextStack; import org.apache.logging.log4j.categories.AsyncLoggers; import org.apache.logging.log4j.core.LogEvent; -import org.apache.logging.log4j.core.util.DummyNanoClock; +import org.apache.logging.log4j.core.time.internal.DummyNanoClock; import org.apache.logging.log4j.core.time.internal.FixedPreciseClock; import org.apache.logging.log4j.util.FilteredObjectInputStream; import org.apache.logging.log4j.util.StringMap; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/TimeFilterTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/TimeFilterTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/TimeFilterTest.java index 4ac0b40..9b026ac 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/TimeFilterTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/filter/TimeFilterTest.java @@ -24,8 +24,8 @@ import org.apache.logging.log4j.core.Filter; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.impl.Log4jLogEvent; import org.apache.logging.log4j.core.util.Clock; -import org.apache.logging.log4j.core.util.ClockFactory; -import org.apache.logging.log4j.core.util.ClockFactoryTest; +import org.apache.logging.log4j.core.time.internal.ClockFactory; +import org.apache.logging.log4j.core.time.internal.ClockFactoryTest; import org.junit.AfterClass; import org.junit.BeforeClass; import org.junit.Test; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/Log4jLogEventNanoTimeTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/Log4jLogEventNanoTimeTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/Log4jLogEventNanoTimeTest.java index 93d60c4..5355774 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/Log4jLogEventNanoTimeTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/Log4jLogEventNanoTimeTest.java @@ -26,8 +26,8 @@ import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.core.CoreLoggerContexts; import org.apache.logging.log4j.core.config.ConfigurationFactory; import org.apache.logging.log4j.core.util.Constants; -import org.apache.logging.log4j.core.util.DummyNanoClock; -import org.apache.logging.log4j.core.util.SystemNanoClock; +import org.apache.logging.log4j.core.time.internal.DummyNanoClock; +import org.apache.logging.log4j.core.time.internal.SystemNanoClock; import org.apache.logging.log4j.util.Strings; import org.junit.AfterClass; import org.junit.BeforeClass; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/Log4jLogEventTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/Log4jLogEventTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/Log4jLogEventTest.java index 9a8fa7c..eab4949 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/Log4jLogEventTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/Log4jLogEventTest.java @@ -39,9 +39,9 @@ import org.apache.logging.log4j.ThreadContext.ContextStack; import org.apache.logging.log4j.core.LogEvent; import org.apache.logging.log4j.core.config.plugins.convert.Base64Converter; import org.apache.logging.log4j.core.util.Clock; -import org.apache.logging.log4j.core.util.ClockFactory; -import org.apache.logging.log4j.core.util.ClockFactoryTest; -import org.apache.logging.log4j.core.util.DummyNanoClock; +import org.apache.logging.log4j.core.time.internal.ClockFactory; +import org.apache.logging.log4j.core.time.internal.ClockFactoryTest; +import org.apache.logging.log4j.core.time.internal.DummyNanoClock; import org.apache.logging.log4j.message.Message; import org.apache.logging.log4j.message.ObjectMessage; import org.apache.logging.log4j.message.ReusableMessage; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java index 1c15091..5b61b69 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/pattern/PatternParserTest.java @@ -33,8 +33,8 @@ import org.apache.logging.log4j.core.config.NullConfiguration; import org.apache.logging.log4j.core.impl.ContextDataFactory; import org.apache.logging.log4j.core.impl.Log4jLogEvent; import org.apache.logging.log4j.core.impl.ThrowableFormatOptions; -import org.apache.logging.log4j.core.util.DummyNanoClock; -import org.apache.logging.log4j.core.util.SystemNanoClock; +import org.apache.logging.log4j.core.time.internal.DummyNanoClock; +import org.apache.logging.log4j.core.time.internal.SystemNanoClock; import org.apache.logging.log4j.message.SimpleMessage; import org.apache.logging.log4j.util.StringMap; import org.apache.logging.log4j.util.Strings; http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/ClockFactoryTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/ClockFactoryTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/ClockFactoryTest.java new file mode 100644 index 0000000..2db66f2 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/ClockFactoryTest.java @@ -0,0 +1,108 @@ +/* + * 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.time.internal; + +import java.lang.reflect.Field; + +import org.apache.commons.lang3.reflect.FieldUtils; +import org.apache.logging.log4j.core.async.AsyncLogger; +import org.apache.logging.log4j.core.impl.Log4jLogEvent; +import org.apache.logging.log4j.core.time.internal.CachedClock; +import org.apache.logging.log4j.core.time.internal.ClockFactory; +import org.apache.logging.log4j.core.time.internal.CoarseCachedClock; +import org.apache.logging.log4j.core.time.internal.SystemClock; +import org.apache.logging.log4j.core.util.Clock; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ClockFactoryTest { + + public static void resetClocks() throws IllegalAccessException { + resetClock(Log4jLogEvent.class); + resetClock(AsyncLogger.class); + } + + public static void resetClock(final Class<?> clazz) throws IllegalAccessException { + System.clearProperty(ClockFactory.PROPERTY_NAME); + final Field field = FieldUtils.getField(clazz, "CLOCK", true); + FieldUtils.removeFinalModifier(field, true); + FieldUtils.writeStaticField(field, ClockFactory.getClock(), false); + } + + @Before + public void setUp() throws Exception { + resetClocks(); + } + + @Test + public void testDefaultIsSystemClock() { + System.clearProperty(ClockFactory.PROPERTY_NAME); + assertSame(SystemClock.class, ClockFactory.getClock().getClass()); + } + + @Test + public void testSpecifySystemClockShort() { + System.setProperty(ClockFactory.PROPERTY_NAME, "SystemClock"); + assertSame(SystemClock.class, ClockFactory.getClock().getClass()); + } + + @Test + public void testSpecifySystemClockLong() { + System.setProperty(ClockFactory.PROPERTY_NAME, SystemClock.class.getName()); + assertSame(SystemClock.class, ClockFactory.getClock().getClass()); + } + + @Test + public void testSpecifyCachedClockShort() { + System.setProperty(ClockFactory.PROPERTY_NAME, "CachedClock"); + assertSame(CachedClock.class, ClockFactory.getClock().getClass()); + } + + @Test + public void testSpecifyCachedClockLong() { + System.setProperty(ClockFactory.PROPERTY_NAME, CachedClock.class.getName()); + assertSame(CachedClock.class, ClockFactory.getClock().getClass()); + } + + @Test + public void testSpecifyCoarseCachedClockShort() { + System.setProperty(ClockFactory.PROPERTY_NAME, "CoarseCachedClock"); + assertSame(CoarseCachedClock.class, ClockFactory.getClock().getClass()); + } + + @Test + public void testSpecifyCoarseCachedClockLong() { + System.setProperty(ClockFactory.PROPERTY_NAME, CoarseCachedClock.class.getName()); + assertSame(CoarseCachedClock.class, ClockFactory.getClock().getClass()); + } + + public static class MyClock implements Clock { + @Override + public long currentTimeMillis() { + return 42; + } + } + + @Test + public void testCustomClock() { + System.setProperty(ClockFactory.PROPERTY_NAME, MyClock.class.getName()); + assertSame(MyClock.class, ClockFactory.getClock().getClass()); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/DummyNanoClockTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/DummyNanoClockTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/DummyNanoClockTest.java new file mode 100644 index 0000000..04f8956 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/DummyNanoClockTest.java @@ -0,0 +1,40 @@ +/* + * 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.time.internal; + +import org.apache.logging.log4j.core.time.internal.DummyNanoClock; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests the DummyNanoClock. + */ +public class DummyNanoClockTest { + + @Test + public void testReturnsZeroByDefault() { + assertEquals(0, new DummyNanoClock().nanoTime()); + } + + @Test + public void testReturnsConstructorValue() { + assertEquals(123, new DummyNanoClock(123).nanoTime()); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/SystemClockTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/SystemClockTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/SystemClockTest.java new file mode 100644 index 0000000..dd4b5c1 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/SystemClockTest.java @@ -0,0 +1,47 @@ +/* + * 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.time.internal; + +import org.apache.logging.log4j.core.time.internal.SystemClock; +import org.junit.Test; + +import static org.junit.Assert.*; + +public class SystemClockTest { + + @Test + public void testLessThan2Millis() { + final long millis1 = new SystemClock().currentTimeMillis(); + final long sysMillis = System.currentTimeMillis(); + + final long diff = sysMillis - millis1; + + assertTrue("diff too large: " + diff, diff <= 1); + } + + @Test + public void testAfterWaitStillLessThan2Millis() throws Exception { + Thread.sleep(100); + final long millis1 = new SystemClock().currentTimeMillis(); + final long sysMillis = System.currentTimeMillis(); + + final long diff = sysMillis - millis1; + + assertTrue("diff too large: " + diff, diff <= 1); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/SystemNanoClockTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/SystemNanoClockTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/SystemNanoClockTest.java new file mode 100644 index 0000000..7b72952 --- /dev/null +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/time/internal/SystemNanoClockTest.java @@ -0,0 +1,41 @@ +/* + * 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.time.internal; + +import java.util.concurrent.TimeUnit; + +import org.apache.logging.log4j.core.time.internal.SystemNanoClock; +import org.apache.logging.log4j.core.util.NanoClock; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Tests the SystemNanoClock. + */ +public class SystemNanoClockTest { + + @Test + public void testReturnsSystemNanoTime() { + final NanoClock clock = new SystemNanoClock(); + final long expected = System.nanoTime(); + final long actual = clock.nanoTime(); + assertTrue("smal difference", actual - expected < TimeUnit.SECONDS.toNanos(1)); + } + +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/util/ClockFactoryTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/ClockFactoryTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/ClockFactoryTest.java deleted file mode 100644 index 309cc77..0000000 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/ClockFactoryTest.java +++ /dev/null @@ -1,103 +0,0 @@ -/* - * 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.util; - -import java.lang.reflect.Field; - -import org.apache.commons.lang3.reflect.FieldUtils; -import org.apache.logging.log4j.core.async.AsyncLogger; -import org.apache.logging.log4j.core.impl.Log4jLogEvent; -import org.junit.Before; -import org.junit.Test; - -import static org.junit.Assert.*; - -public class ClockFactoryTest { - - public static void resetClocks() throws IllegalAccessException { - resetClock(Log4jLogEvent.class); - resetClock(AsyncLogger.class); - } - - public static void resetClock(final Class<?> clazz) throws IllegalAccessException { - System.clearProperty(ClockFactory.PROPERTY_NAME); - final Field field = FieldUtils.getField(clazz, "CLOCK", true); - FieldUtils.removeFinalModifier(field, true); - FieldUtils.writeStaticField(field, ClockFactory.getClock(), false); - } - - @Before - public void setUp() throws Exception { - resetClocks(); - } - - @Test - public void testDefaultIsSystemClock() { - System.clearProperty(ClockFactory.PROPERTY_NAME); - assertSame(SystemClock.class, ClockFactory.getClock().getClass()); - } - - @Test - public void testSpecifySystemClockShort() { - System.setProperty(ClockFactory.PROPERTY_NAME, "SystemClock"); - assertSame(SystemClock.class, ClockFactory.getClock().getClass()); - } - - @Test - public void testSpecifySystemClockLong() { - System.setProperty(ClockFactory.PROPERTY_NAME, SystemClock.class.getName()); - assertSame(SystemClock.class, ClockFactory.getClock().getClass()); - } - - @Test - public void testSpecifyCachedClockShort() { - System.setProperty(ClockFactory.PROPERTY_NAME, "CachedClock"); - assertSame(CachedClock.class, ClockFactory.getClock().getClass()); - } - - @Test - public void testSpecifyCachedClockLong() { - System.setProperty(ClockFactory.PROPERTY_NAME, CachedClock.class.getName()); - assertSame(CachedClock.class, ClockFactory.getClock().getClass()); - } - - @Test - public void testSpecifyCoarseCachedClockShort() { - System.setProperty(ClockFactory.PROPERTY_NAME, "CoarseCachedClock"); - assertSame(CoarseCachedClock.class, ClockFactory.getClock().getClass()); - } - - @Test - public void testSpecifyCoarseCachedClockLong() { - System.setProperty(ClockFactory.PROPERTY_NAME, CoarseCachedClock.class.getName()); - assertSame(CoarseCachedClock.class, ClockFactory.getClock().getClass()); - } - - public static class MyClock implements Clock { - @Override - public long currentTimeMillis() { - return 42; - } - } - - @Test - public void testCustomClock() { - System.setProperty(ClockFactory.PROPERTY_NAME, MyClock.class.getName()); - assertSame(MyClock.class, ClockFactory.getClock().getClass()); - } - -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/util/DummyNanoClockTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/DummyNanoClockTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/DummyNanoClockTest.java deleted file mode 100644 index 8fc16b6..0000000 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/DummyNanoClockTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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.util; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Tests the DummyNanoClock. - */ -public class DummyNanoClockTest { - - @Test - public void testReturnsZeroByDefault() { - assertEquals(0, new DummyNanoClock().nanoTime()); - } - - @Test - public void testReturnsConstructorValue() { - assertEquals(123, new DummyNanoClock(123).nanoTime()); - } - -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/util/SystemClockTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/SystemClockTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/SystemClockTest.java deleted file mode 100644 index 5421a7d..0000000 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/SystemClockTest.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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.util; - -import org.junit.Test; - -import static org.junit.Assert.*; - -public class SystemClockTest { - - @Test - public void testLessThan2Millis() { - final long millis1 = new SystemClock().currentTimeMillis(); - final long sysMillis = System.currentTimeMillis(); - - final long diff = sysMillis - millis1; - - assertTrue("diff too large: " + diff, diff <= 1); - } - - @Test - public void testAfterWaitStillLessThan2Millis() throws Exception { - Thread.sleep(100); - final long millis1 = new SystemClock().currentTimeMillis(); - final long sysMillis = System.currentTimeMillis(); - - final long diff = sysMillis - millis1; - - assertTrue("diff too large: " + diff, diff <= 1); - } - -} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/ba658a0f/log4j-core/src/test/java/org/apache/logging/log4j/core/util/SystemNanoClockTest.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/SystemNanoClockTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/SystemNanoClockTest.java deleted file mode 100644 index 9adea85..0000000 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/SystemNanoClockTest.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * 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.util; - -import java.util.concurrent.TimeUnit; - -import org.junit.Test; - -import static org.junit.Assert.*; - -/** - * Tests the SystemNanoClock. - */ -public class SystemNanoClockTest { - - @Test - public void testReturnsSystemNanoTime() { - final NanoClock clock = new SystemNanoClock(); - final long expected = System.nanoTime(); - final long actual = clock.nanoTime(); - assertTrue("smal difference", actual - expected < TimeUnit.SECONDS.toNanos(1)); - } - -}
