Skip to content

Commit

Permalink
Inlined some classes into DefaultProgressLoggerFactory and simplified…
Browse files Browse the repository at this point in the history
… implementation.
  • Loading branch information
adammurdoch committed Jul 15, 2015
1 parent 6b4dd62 commit 9f04302
Show file tree
Hide file tree
Showing 9 changed files with 198 additions and 396 deletions.
Expand Up @@ -22,7 +22,7 @@ public interface LoggerProvider {

ProgressLogger getLogger();

public static LoggerProvider NO_OP = new LoggerProvider() {
LoggerProvider NO_OP = new LoggerProvider() {
public ProgressLogger getLogger() {
return null;
}
Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -16,8 +16,6 @@

package org.gradle.logging;

import org.gradle.internal.progress.OperationIdentifier;

/**
* Used to log the progress of a potentially long running operation.
*
Expand Down Expand Up @@ -52,7 +50,7 @@ public interface ProgressLogger {
*
* @param description The description.
*/
void setDescription(String description);
ProgressLogger setDescription(String description);

/**
* Returns the short description of the operation. This is used in place of the full description when display space is limited.
Expand All @@ -68,7 +66,7 @@ public interface ProgressLogger {
*
* @param description The short description.
*/
void setShortDescription(String description);
ProgressLogger setShortDescription(String description);

/**
* <p>Returns the logging header for the operation. This is logged before any other log messages for this operation are logged. It is usually
Expand All @@ -86,7 +84,7 @@ public interface ProgressLogger {
*
* @param header The header. May be empty or null.
*/
void setLoggingHeader(String header);
ProgressLogger setLoggingHeader(String header);

/**
* Convenience method that sets descriptions and logs started() event.
Expand Down Expand Up @@ -125,6 +123,4 @@ public interface ProgressLogger {
* @param status The final status message. Can be null or empty.
*/
void completed(String status);

OperationIdentifier currentOperationId();
}
Expand Up @@ -16,8 +16,9 @@

package org.gradle.logging;

import org.gradle.TaskExecutionLogger;

/**
* Thread-safe, however the progress logger instances created are not.
*/
public interface ProgressLoggerFactory {
/**
* Creates a new long-running operation which has not been started.
Expand All @@ -33,7 +34,7 @@ public interface ProgressLoggerFactory {
* @param loggerCategory The logger category.
* @return The progress logger for the operation.
*/
ProgressLogger newOperation(Class loggerCategory);
ProgressLogger newOperation(Class<?> loggerCategory);

ProgressLogger newOperation(Class<TaskExecutionLogger> taskExecutionLoggerClass, ProgressLogger parent);
ProgressLogger newOperation(Class<?> loggerClass, ProgressLogger parent);
}
Expand Up @@ -18,16 +18,17 @@

import org.gradle.internal.TimeProvider;
import org.gradle.internal.progress.OperationIdentifier;
import org.gradle.internal.progress.OperationsHierarchy;
import org.gradle.internal.progress.OperationsHierarchyKeeper;
import org.gradle.logging.ProgressLogger;
import org.gradle.logging.ProgressLoggerFactory;
import org.gradle.util.GUtil;

import java.util.concurrent.atomic.AtomicLong;

public class DefaultProgressLoggerFactory implements ProgressLoggerFactory {
private final ProgressListener progressListener;
private final TimeProvider timeProvider;
private final OperationsHierarchyKeeper hierarchyKeeper = new OperationsHierarchyKeeper();
private final AtomicLong nextId = new AtomicLong();
private final ThreadLocal<ProgressLoggerImpl> current = new ThreadLocal<ProgressLoggerImpl>();

public DefaultProgressLoggerFactory(ProgressListener progressListener, TimeProvider timeProvider) {
this.progressListener = progressListener;
Expand All @@ -42,28 +43,34 @@ public ProgressLogger newOperation(String loggerCategory) {
return init(loggerCategory, null);
}

public ProgressLogger newOperation(Class loggerCategory, ProgressLogger parent) {
return init(loggerCategory.toString(), parent);
public ProgressLogger newOperation(Class loggerClass, ProgressLogger parent) {
return init(loggerClass.toString(), parent);
}

private ProgressLogger init(String loggerCategory, ProgressLogger parentHint) {
return new ProgressLoggerImpl(hierarchyKeeper.currentHierarchy(parentHint), loggerCategory, progressListener, timeProvider);
private ProgressLogger init(String loggerCategory, ProgressLogger parentOperation) {
if (parentOperation != null && !(parentOperation instanceof ProgressLoggerImpl)) {
throw new IllegalArgumentException("Unexpected parent logger.");
}
return new ProgressLoggerImpl((ProgressLoggerImpl) parentOperation, nextId.getAndIncrement(), loggerCategory, progressListener, timeProvider);
}

private static class ProgressLoggerImpl implements ProgressLogger {
private enum State { idle, started, completed }
private enum State { idle, started, completed }

private final OperationsHierarchy hierarchy;
private class ProgressLoggerImpl implements ProgressLogger {
private final long id;
private final String category;
private final ProgressListener listener;
private final TimeProvider timeProvider;
private ProgressLoggerImpl parent;
private OperationIdentifier operationIdentifier;
private String description;
private String shortDescription;
private String loggingHeader;
private State state = State.idle;

public ProgressLoggerImpl(OperationsHierarchy hierarchy, String category, ProgressListener listener, TimeProvider timeProvider) {
this.hierarchy = hierarchy;
public ProgressLoggerImpl(ProgressLoggerImpl parent, long id, String category, ProgressListener listener, TimeProvider timeProvider) {
this.parent = parent;
this.id = id;
this.category = category;
this.listener = listener;
this.timeProvider = timeProvider;
Expand All @@ -73,27 +80,30 @@ public String getDescription() {
return description;
}

public void setDescription(String description) {
public ProgressLogger setDescription(String description) {
assertCanConfigure();
this.description = description;
return this;
}

public String getShortDescription() {
return shortDescription;
}

public void setShortDescription(String shortDescription) {
public ProgressLogger setShortDescription(String shortDescription) {
assertCanConfigure();
this.shortDescription = shortDescription;
return this;
}

public String getLoggingHeader() {
return loggingHeader;
}

public void setLoggingHeader(String loggingHeader) {
public ProgressLogger setLoggingHeader(String loggingHeader) {
assertCanConfigure();
this.loggingHeader = loggingHeader;
return this;
}

public ProgressLogger start(String description, String shortDescription) {
Expand All @@ -116,45 +126,46 @@ public void started(String status) {
}
assertNotCompleted();
state = State.started;
OperationIdentifier id = hierarchy.start();
listener.started(new ProgressStartEvent(id, timeProvider.getCurrentTime(), category, description, shortDescription, loggingHeader, toStatus(status)));
if (parent == null) {
parent = current.get();
} else {
parent.assertRunning();
}
current.set(this);
operationIdentifier = new OperationIdentifier(this.id, parent == null ? null : parent.id);
listener.started(new ProgressStartEvent(operationIdentifier, timeProvider.getCurrentTime(), category, description, shortDescription, loggingHeader, toStatus(status)));
}

public void progress(String status) {
assertStarted();
assertNotCompleted();
listener.progress(new ProgressEvent(hierarchy.currentOperationId(), timeProvider.getCurrentTime(), category, toStatus(status)));
assertRunning();
listener.progress(new ProgressEvent(operationIdentifier, timeProvider.getCurrentTime(), category, toStatus(status)));
}

public void completed() {
completed(null);
}

public void completed(String status) {
assertStarted();
assertNotCompleted();
assertRunning();
state = State.completed;
listener.completed(new ProgressCompleteEvent(hierarchy.completeCurrentOperation(),
current.set(parent);
listener.completed(new ProgressCompleteEvent(operationIdentifier,
timeProvider.getCurrentTime(), category, description, toStatus(status)));
}

public OperationIdentifier currentOperationId() {
return hierarchy.currentOperationId();
}

private String toStatus(String status) {
return status == null ? "" : status;
}

private void assertNotCompleted() {
if (state == ProgressLoggerImpl.State.completed) {
if (state == State.completed) {
throw new IllegalStateException("This operation has completed.");
}
}

private void assertStarted() {
if (state == ProgressLoggerImpl.State.idle) {
throw new IllegalStateException("This operation has not been started.");
private void assertRunning() {
if (state != State.started) {
throw new IllegalStateException("This operation is not running.");
}
}

Expand Down

0 comments on commit 9f04302

Please sign in to comment.