Skip to content

Commit

Permalink
Add API to Glide to include request origins when Glide requests fail.
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=219699654
  • Loading branch information
sjudd committed Dec 19, 2018
1 parent e515f47 commit 114b885
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 2 deletions.
4 changes: 3 additions & 1 deletion library/src/main/java/com/bumptech/glide/Glide.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,8 @@ private static void throwIncorrectGlideModule(Exception e) {
int logLevel,
@NonNull RequestOptions defaultRequestOptions,
@NonNull Map<Class<?>, TransitionOptions<?, ?>> defaultTransitionOptions,
@NonNull List<RequestListener<Object>> defaultRequestListeners) {
@NonNull List<RequestListener<Object>> defaultRequestListeners,
boolean isLoggingRequestOriginsEnabled) {
this.engine = engine;
this.bitmapPool = bitmapPool;
this.arrayPool = arrayPool;
Expand Down Expand Up @@ -522,6 +523,7 @@ Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitm
defaultTransitionOptions,
defaultRequestListeners,
engine,
isLoggingRequestOriginsEnabled,
logLevel);
}

Expand Down
19 changes: 18 additions & 1 deletion library/src/main/java/com/bumptech/glide/GlideBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ public final class GlideBuilder {
private boolean isActiveResourceRetentionAllowed;
@Nullable
private List<RequestListener<Object>> defaultRequestListeners;
private boolean isLoggingRequestOriginsEnabled;

/**
* Sets the {@link com.bumptech.glide.load.engine.bitmap_recycle.BitmapPool} implementation to use
Expand Down Expand Up @@ -407,6 +408,21 @@ public GlideBuilder addGlobalRequestListener(@NonNull RequestListener<Object> li
return this;
}

/**
* Set to {@code true} to make Glide populate
* {@link com.bumptech.glide.load.engine.GlideException#setOrigin(Exception)} for failed requests.
*
* <p>The exception set by this method is not printed by {@link GlideException} and can only be
* viewed via a {@link RequestListener} that reads the field via
* {@link GlideException#getOrigin()}.
*
* <p>This is an experimental API that may be removed in the future.
*/
public GlideBuilder setLogRequestOrigins(boolean isEnabled) {
isLoggingRequestOriginsEnabled = isEnabled;
return this;
}

void setRequestManagerFactory(@Nullable RequestManagerFactory factory) {
this.requestManagerFactory = factory;
}
Expand Down Expand Up @@ -492,6 +508,7 @@ Glide build(@NonNull Context context) {
logLevel,
defaultRequestOptions.lock(),
defaultTransitionOptions,
defaultRequestListeners);
defaultRequestListeners,
isLoggingRequestOriginsEnabled);
}
}
13 changes: 13 additions & 0 deletions library/src/main/java/com/bumptech/glide/GlideContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public class GlideContext extends ContextWrapper {
private final List<RequestListener<Object>> defaultRequestListeners;
private final Map<Class<?>, TransitionOptions<?, ?>> defaultTransitionOptions;
private final Engine engine;
private boolean isLoggingRequestOriginsEnabled;
private final int logLevel;

public GlideContext(
Expand All @@ -44,6 +45,7 @@ public GlideContext(
@NonNull Map<Class<?>, TransitionOptions<?, ?>> defaultTransitionOptions,
@NonNull List<RequestListener<Object>> defaultRequestListeners,
@NonNull Engine engine,
boolean isLoggingRequestOriginsEnabled,
int logLevel) {
super(context.getApplicationContext());
this.arrayPool = arrayPool;
Expand All @@ -53,6 +55,7 @@ public GlideContext(
this.defaultRequestListeners = defaultRequestListeners;
this.defaultTransitionOptions = defaultTransitionOptions;
this.engine = engine;
this.isLoggingRequestOriginsEnabled = isLoggingRequestOriginsEnabled;
this.logLevel = logLevel;

mainHandler = new Handler(Looper.getMainLooper());
Expand Down Expand Up @@ -112,4 +115,14 @@ public int getLogLevel() {
public ArrayPool getArrayPool() {
return arrayPool;
}

/**
* Returns {@code true} if Glide should populate
* {@link com.bumptech.glide.load.engine.GlideException#setOrigin(Exception)} for failed requests.
*
* <p>This is an experimental API that may be removed in the future.
*/
public boolean isLoggingRequestOriginsEnabled() {
return isLoggingRequestOriginsEnabled;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public final class GlideException extends Exception {
private DataSource dataSource;
private Class<?> dataClass;
private String detailMessage;
@Nullable
private Exception exception;

public GlideException(String message) {
this(message, Collections.<Throwable>emptyList());
Expand All @@ -52,7 +54,25 @@ void setLoggingDetails(Key key, DataSource dataSource, Class<?> dataClass) {
this.dataClass = dataClass;
}

/**
* Sets a stack trace that includes where the request originated.
*
* <p>This is an experimental API that may be removed in the future.
*/
public void setOrigin(@Nullable Exception exception) {
this.exception = exception;
}

/**
* Returns an {@link Exception} with a stack trace that includes where the request originated
* (if previously set via {@link #setOrigin(Exception)})
*
* <p>This is an experimental API that may be removed in the future.
*/
@Nullable
public Exception getOrigin() {
return exception;
}

// No need to synchronize when doing nothing whatsoever.
@SuppressWarnings("UnsynchronizedOverridesSynchronized")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ private enum Status {
private Drawable fallbackDrawable;
private int width;
private int height;
@Nullable
private RuntimeException requestOrigin;

public static <R> SingleRequest<R> obtain(
Context context,
Expand Down Expand Up @@ -183,6 +185,10 @@ private void init(
this.engine = engine;
this.animationFactory = animationFactory;
status = Status.PENDING;

if (glideContext.isLoggingRequestOriginsEnabled()) {
requestOrigin = new RuntimeException("Glide request origin trace");
}
}

@NonNull
Expand Down Expand Up @@ -212,6 +218,7 @@ public void recycle() {
fallbackDrawable = null;
width = -1;
height = -1;
requestOrigin = null;
POOL.release(this);
}

Expand Down Expand Up @@ -584,6 +591,7 @@ public void onLoadFailed(GlideException e) {

private void onLoadFailed(GlideException e, int maxLogLevel) {
stateVerifier.throwIfRecycled();
e.setOrigin(requestOrigin);
int logLevel = glideContext.getLogLevel();
if (logLevel <= maxLogLevel) {
Log.w(GLIDE_TAG, "Load failed for " + model + " with size [" + width + "x" + height + "]", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public void setUp() {
transitionOptions,
/*defaultRequestListeners=*/ Collections.<RequestListener<Object>>emptyList(),
mock(Engine.class),
/*isLoggingRequestOriginsEnabled=*/ false,
Log.DEBUG);
}

Expand Down

0 comments on commit 114b885

Please sign in to comment.