Skip to content

Commit 114b885

Browse files
committedDec 19, 2018
Add API to Glide to include request origins when Glide requests fail.
------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=219699654
1 parent e515f47 commit 114b885

File tree

6 files changed

+63
-2
lines changed

6 files changed

+63
-2
lines changed
 

‎library/src/main/java/com/bumptech/glide/Glide.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,8 @@ private static void throwIncorrectGlideModule(Exception e) {
322322
int logLevel,
323323
@NonNull RequestOptions defaultRequestOptions,
324324
@NonNull Map<Class<?>, TransitionOptions<?, ?>> defaultTransitionOptions,
325-
@NonNull List<RequestListener<Object>> defaultRequestListeners) {
325+
@NonNull List<RequestListener<Object>> defaultRequestListeners,
326+
boolean isLoggingRequestOriginsEnabled) {
326327
this.engine = engine;
327328
this.bitmapPool = bitmapPool;
328329
this.arrayPool = arrayPool;
@@ -522,6 +523,7 @@ Uri.class, Bitmap.class, new ResourceBitmapDecoder(resourceDrawableDecoder, bitm
522523
defaultTransitionOptions,
523524
defaultRequestListeners,
524525
engine,
526+
isLoggingRequestOriginsEnabled,
525527
logLevel);
526528
}
527529

‎library/src/main/java/com/bumptech/glide/GlideBuilder.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public final class GlideBuilder {
5555
private boolean isActiveResourceRetentionAllowed;
5656
@Nullable
5757
private List<RequestListener<Object>> defaultRequestListeners;
58+
private boolean isLoggingRequestOriginsEnabled;
5859

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

411+
/**
412+
* Set to {@code true} to make Glide populate
413+
* {@link com.bumptech.glide.load.engine.GlideException#setOrigin(Exception)} for failed requests.
414+
*
415+
* <p>The exception set by this method is not printed by {@link GlideException} and can only be
416+
* viewed via a {@link RequestListener} that reads the field via
417+
* {@link GlideException#getOrigin()}.
418+
*
419+
* <p>This is an experimental API that may be removed in the future.
420+
*/
421+
public GlideBuilder setLogRequestOrigins(boolean isEnabled) {
422+
isLoggingRequestOriginsEnabled = isEnabled;
423+
return this;
424+
}
425+
410426
void setRequestManagerFactory(@Nullable RequestManagerFactory factory) {
411427
this.requestManagerFactory = factory;
412428
}
@@ -492,6 +508,7 @@ Glide build(@NonNull Context context) {
492508
logLevel,
493509
defaultRequestOptions.lock(),
494510
defaultTransitionOptions,
495-
defaultRequestListeners);
511+
defaultRequestListeners,
512+
isLoggingRequestOriginsEnabled);
496513
}
497514
}

‎library/src/main/java/com/bumptech/glide/GlideContext.java

+13
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ public class GlideContext extends ContextWrapper {
3333
private final List<RequestListener<Object>> defaultRequestListeners;
3434
private final Map<Class<?>, TransitionOptions<?, ?>> defaultTransitionOptions;
3535
private final Engine engine;
36+
private boolean isLoggingRequestOriginsEnabled;
3637
private final int logLevel;
3738

3839
public GlideContext(
@@ -44,6 +45,7 @@ public GlideContext(
4445
@NonNull Map<Class<?>, TransitionOptions<?, ?>> defaultTransitionOptions,
4546
@NonNull List<RequestListener<Object>> defaultRequestListeners,
4647
@NonNull Engine engine,
48+
boolean isLoggingRequestOriginsEnabled,
4749
int logLevel) {
4850
super(context.getApplicationContext());
4951
this.arrayPool = arrayPool;
@@ -53,6 +55,7 @@ public GlideContext(
5355
this.defaultRequestListeners = defaultRequestListeners;
5456
this.defaultTransitionOptions = defaultTransitionOptions;
5557
this.engine = engine;
58+
this.isLoggingRequestOriginsEnabled = isLoggingRequestOriginsEnabled;
5659
this.logLevel = logLevel;
5760

5861
mainHandler = new Handler(Looper.getMainLooper());
@@ -112,4 +115,14 @@ public int getLogLevel() {
112115
public ArrayPool getArrayPool() {
113116
return arrayPool;
114117
}
118+
119+
/**
120+
* Returns {@code true} if Glide should populate
121+
* {@link com.bumptech.glide.load.engine.GlideException#setOrigin(Exception)} for failed requests.
122+
*
123+
* <p>This is an experimental API that may be removed in the future.
124+
*/
125+
public boolean isLoggingRequestOriginsEnabled() {
126+
return isLoggingRequestOriginsEnabled;
127+
}
115128
}

‎library/src/main/java/com/bumptech/glide/load/engine/GlideException.java

+20
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ public final class GlideException extends Exception {
2727
private DataSource dataSource;
2828
private Class<?> dataClass;
2929
private String detailMessage;
30+
@Nullable
31+
private Exception exception;
3032

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

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

66+
/**
67+
* Returns an {@link Exception} with a stack trace that includes where the request originated
68+
* (if previously set via {@link #setOrigin(Exception)})
69+
*
70+
* <p>This is an experimental API that may be removed in the future.
71+
*/
72+
@Nullable
73+
public Exception getOrigin() {
74+
return exception;
75+
}
5676

5777
// No need to synchronize when doing nothing whatsoever.
5878
@SuppressWarnings("UnsynchronizedOverridesSynchronized")

‎library/src/main/java/com/bumptech/glide/request/SingleRequest.java

+8
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,8 @@ private enum Status {
108108
private Drawable fallbackDrawable;
109109
private int width;
110110
private int height;
111+
@Nullable
112+
private RuntimeException requestOrigin;
111113

112114
public static <R> SingleRequest<R> obtain(
113115
Context context,
@@ -183,6 +185,10 @@ private void init(
183185
this.engine = engine;
184186
this.animationFactory = animationFactory;
185187
status = Status.PENDING;
188+
189+
if (glideContext.isLoggingRequestOriginsEnabled()) {
190+
requestOrigin = new RuntimeException("Glide request origin trace");
191+
}
186192
}
187193

188194
@NonNull
@@ -212,6 +218,7 @@ public void recycle() {
212218
fallbackDrawable = null;
213219
width = -1;
214220
height = -1;
221+
requestOrigin = null;
215222
POOL.release(this);
216223
}
217224

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

585592
private void onLoadFailed(GlideException e, int maxLogLevel) {
586593
stateVerifier.throwIfRecycled();
594+
e.setOrigin(requestOrigin);
587595
int logLevel = glideContext.getLogLevel();
588596
if (logLevel <= maxLogLevel) {
589597
Log.w(GLIDE_TAG, "Load failed for " + model + " with size [" + width + "x" + height + "]", e);

‎library/test/src/test/java/com/bumptech/glide/GlideContextTest.java

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public void setUp() {
4545
transitionOptions,
4646
/*defaultRequestListeners=*/ Collections.<RequestListener<Object>>emptyList(),
4747
mock(Engine.class),
48+
/*isLoggingRequestOriginsEnabled=*/ false,
4849
Log.DEBUG);
4950
}
5051

0 commit comments

Comments
 (0)
Please sign in to comment.