Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.apache.maven.api;

import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZoneOffset;
Expand Down Expand Up @@ -77,6 +78,27 @@ public static Instant now() {
return get().instant();
}

/**
* Returns the initialization time of this monotonic clock.
* This is a convenience method equivalent to {@code get().start()}.
*
* @return the instant when this monotonic clock was initialized
* @see #startInstant()
*/
public static Instant start() {
return get().startInstant();
}

/**
* Returns the elapsed time since clock initialization.
* This is a convenience method equivalent to {@code get().elapsedTime()}.
*
* @return the duration since clock initialization
*/
public static Duration elapsed() {
return get().elapsedTime();
}

/**
* Returns a monotonically increasing instant.
* <p>
Expand All @@ -93,6 +115,36 @@ public Instant instant() {
return startInstant.plusNanos(elapsedNanos);
}

/**
* Returns the wall clock time captured when this monotonic clock was initialized.
* <p>
* This instant serves as the base time from which all subsequent {@link #instant()}
* calls are calculated by adding the elapsed monotonic duration. This ensures
* consistency between the monotonic measurements and wall clock time.
*
* @return the initial wall clock instant when this clock was created
* @see #instant()
*/
public Instant startInstant() {
return startInstant;
}

/**
* Returns the duration elapsed since this clock was initialized.
* <p>
* The returned duration is calculated using {@link System#nanoTime()}
* to ensure monotonic behavior. This duration represents the exact time
* span between clock initialization and the current instant.
*
* @return the duration since clock initialization
* @see #startInstant()
* @see #instant()
*/
public Duration elapsedTime() {
long elapsedNanos = System.nanoTime() - startNanos;
return Duration.ofNanos(elapsedNanos);
}

/**
* Returns the zone ID of this clock, which is always UTC.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@
package org.apache.maven.slf4j;

import java.io.PrintStream;
import java.time.Clock;
import java.time.Duration;
import java.time.Instant;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

Expand Down Expand Up @@ -146,9 +147,6 @@
*/
public class MavenBaseLogger extends LegacyAbstractLogger {

private static final Clock MONOTONIC_CLOCK = Clock.tick(Clock.systemUTC(), Duration.ofMillis(1));
private static final Instant START_TIME = MonotonicClock.now();

protected static final int LOG_LEVEL_TRACE = LocationAwareLogger.TRACE_INT;
protected static final int LOG_LEVEL_DEBUG = LocationAwareLogger.DEBUG_INT;
protected static final int LOG_LEVEL_INFO = LocationAwareLogger.INFO_INT;
Expand Down Expand Up @@ -266,15 +264,6 @@ protected void writeThrowable(Throwable t, PrintStream targetStream) {
}
}

protected String getFormattedDate() {
Instant now = MonotonicClock.now();
String dateText;
synchronized (CONFIG_PARAMS.dateFormatter) {
dateText = CONFIG_PARAMS.dateFormatter.format(now);
}
return dateText;
}

protected String computeShortName() {
return name.substring(name.lastIndexOf(".") + 1);
}
Expand Down Expand Up @@ -380,11 +369,15 @@ private void innerHandleNormalizedLoggingCall(

// Append date-time if so configured
if (CONFIG_PARAMS.showDateTime) {
if (CONFIG_PARAMS.dateFormatter != null) {
buf.append(getFormattedDate());
DateTimeFormatter formatter = CONFIG_PARAMS.dateFormatter;
Instant now = MonotonicClock.now();
if (formatter != null) {
ZonedDateTime zonedDateTime = now.atZone(ZoneId.systemDefault());
String dateText = formatter.format(zonedDateTime);
buf.append(dateText);
buf.append(SP);
} else {
buf.append(Duration.between(START_TIME, MonotonicClock.now()).toMillis());
buf.append(MonotonicClock.elapsed().toMillis());
buf.append(SP);
}
}
Expand Down
Loading