Skip to content

Commit

Permalink
add log printer
Browse files Browse the repository at this point in the history
  • Loading branch information
yjfnypeu committed Feb 2, 2018
1 parent f606de8 commit ddf7838
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 13 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/com/lzh/nonview/router/demo/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import com.haoge.studio.RouterRuleCreator;
import com.lzh.compiler.parceler.Parceler;
import com.lzh.compiler.parceler.annotation.FastJsonConverter;
import com.lzh.nonview.router.Router;
import com.lzh.nonview.router.RouterConfiguration;
import com.lzh.nonview.router.anno.RouteConfig;
import com.lzh.nonview.router.demo.action.SayHelloAction;
Expand Down Expand Up @@ -49,6 +50,9 @@ public void onCreate() {
// 对Router设置Activity Route Callback,作辅助功能
RouterConfiguration.get().setCallback(new DefaultCallback());

// 在开发环境下开启日志打印
Router.DEBUG = BuildConfig.DEBUG;

// 启动远程服务。一般在插件化环境下使用。
// RouterConfiguration.get().startHostService("com.lzh.nonview.router.demo", this);
// 对应于启动远程服务操作。可设置此远程数据创建者。也应在插件化环境下使用。
Expand Down
1 change: 1 addition & 0 deletions routerlib/src/main/java/com/lzh/nonview/router/Router.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public final class Router{
* </pre>
*/
public static final String RAW_URI = "_ROUTER_RAW_URI_KEY_";
public static boolean DEBUG = false;

private Uri uri;
private InternalCallback internalCallback;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.lzh.nonview.router.exception.NotFoundException;
import com.lzh.nonview.router.extras.RouteBundleExtras;
import com.lzh.nonview.router.module.RouteRule;
import com.lzh.nonview.router.tools.RouterLog;

import java.util.HashMap;
import java.util.Map;
Expand Down Expand Up @@ -53,26 +54,44 @@ public void onOpenFailed(Throwable e) {

void invoke() {
cache.put(uri, extras);
RouteCallback global = RouterConfiguration.get().getCallback();
invokeWithCallback(global);

RouteCallback callback = extras.getCallback();
invokeWithCallback(callback);
invokeWithCallback(RouterConfiguration.get().getCallback(),
extras.getCallback());
cache.remove(uri);
}

private void invokeWithCallback(RouteCallback callback) {
if (callback == null) {
return;
}
private void invokeWithCallback(RouteCallback global, RouteCallback callback) {
if (error != null && error instanceof NotFoundException) {
callback.notFound(uri, (NotFoundException) error);
RouterLog.d("[RouterLog] Could not found matched route for " + uri);
if (global != null) {
global.notFound(uri, (NotFoundException) error);
}
if (callback != null) {
callback.notFound(uri, (NotFoundException) error);
}
} else if (error != null) {
callback.onOpenFailed(uri, error);
RouterLog.e("[RouterLog] Launch route with " + uri + " failed.", error);
if (global != null) {
global.onOpenFailed(uri, error);
}
if (callback != null) {
callback.onOpenFailed(uri, error);
}
} else if (rule != null) {
callback.onOpenSuccess(uri, rule);
RouterLog.d("[RouterLog] Launch route with " + uri + " successful!, target class name is " + rule.getRuleClz());
if (global != null) {
global.onOpenSuccess(uri, rule);
}
if (callback != null) {
callback.onOpenSuccess(uri, rule);
}
} else {
callback.onOpenFailed(uri, new RuntimeException("Unknown error"));
RouterLog.e("[RouterLog] Launch route with " + uri + " failed.", null);
if (global != null) {
global.onOpenFailed(uri, new RuntimeException("Unknown error"));
}
if (callback != null) {
callback.onOpenFailed(uri, new RuntimeException("Unknown error"));
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.lzh.nonview.router.tools;

import android.util.Log;

import com.lzh.nonview.router.Router;

public final class RouterLog {
private static final String TAG = "RouterLog";

public static void d(String message) {
if (Router.DEBUG) {
Log.d(TAG, message);
}
}
public static void e(String message, Throwable t) {
if (Router.DEBUG) {
Log.e(TAG, message, t);
}
}
}

0 comments on commit ddf7838

Please sign in to comment.