Skip to content

Commit

Permalink
Provide a way to enable/disable internal debug logging (#102)
Browse files Browse the repository at this point in the history
  • Loading branch information
rciovati authored Jul 17, 2018
1 parent 00dad3b commit 28de683
Showing 1 changed file with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ public final class LightCycles {
private static final String TAG = LightCycles.class.getSimpleName();
private static final String ANDROID_PREFIX = "android.";
private static final String JAVA_PREFIX = "java.";
private static boolean debugLoggingEnabled = false;

@SuppressWarnings("PMD.EmptyCatchBlock")
public static void bind(LightCycleDispatcher<?> target) {
Expand All @@ -30,6 +31,16 @@ public static void bind(LightCycleDispatcher<?> target) {
}
}

public static void enableDebugLogging(boolean enabled) {
debugLoggingEnabled = enabled;
}

private static void debugLog(String message, Object... args) {
if (debugLoggingEnabled) {
Log.d(TAG, String.format(message, args));
}
}

private static String getInjectorClassName(String clsName) {
return clsName + "$LightCycleBinder";
}
Expand All @@ -39,15 +50,15 @@ private static Method findBinderForClass(Class<?> cls)
Method lightCycleInjectionMethod;
String clsName = cls.getName();
if (clsName.startsWith(ANDROID_PREFIX) || clsName.startsWith(JAVA_PREFIX)) {
Log.d(TAG, "MISS: Reached framework class. Abandoning search.");
debugLog("MISS: Reached framework class. Abandoning search.");
return null;
}
try {
Class<?> binder = Class.forName(getInjectorClassName(clsName));
lightCycleInjectionMethod = binder.getMethod("bind", cls);
Log.d(TAG, "HIT: Loaded LightCycle binder class.");
debugLog("HIT: Loaded LightCycle binder class.");
} catch (ClassNotFoundException e) {
Log.d(TAG, "Not found. Trying superclass " + cls.getSuperclass().getName());
debugLog("Not found. Trying superclass %s", cls.getSuperclass().getName());
lightCycleInjectionMethod = findBinderForClass(cls.getSuperclass());
}
return lightCycleInjectionMethod;
Expand Down

0 comments on commit 28de683

Please sign in to comment.