Commit 4166c531 authored by Firewayer's avatar Firewayer Committed by wei.xie04

Debugger

1.增加safeMode字段接口,设置为true后Debugger.exception不再Throw Exception, 默认为false。
2.增加Log实现接口,方便外部获取Boost相关log信息。
parent 19b1c101
/* /*
* The MIT License (MIT) * The MIT License (MIT)
* *
* Copyright (c) 2019 Alibaba Group * Copyright (c) 2019 Alibaba Group
* *
* Permission is hereby granted, free of charge, to any person obtaining a copy * Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal * of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights * in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is * copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions: * furnished to do so, subject to the following conditions:
* *
* The above copyright notice and this permission notice shall be included in * The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software. * all copies or substantial portions of the Software.
* *
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
...@@ -26,15 +26,21 @@ package com.idlefish.flutterboost; ...@@ -26,15 +26,21 @@ package com.idlefish.flutterboost;
import android.util.Log; import android.util.Log;
import com.idlefish.flutterboost.log.AndroidLog;
import com.idlefish.flutterboost.log.ILog;
public class Debuger { public class Debuger {
private static final String TAG = "FlutterBoost#"; private static final String TAG = "FlutterBoost#";
private static final Debuger DEBUG = new Debuger(); private static final Debuger DEBUG = new Debuger();
private static boolean sSafeMode = false;
private static ILog sLog = new AndroidLog();
private Debuger(){ } private Debuger() {
}
private void print(String info) { private void print(String info) {
if(isDebug()) { if (isDebug()) {
Log.e(TAG, info); sLog.e(TAG, info);
} }
} }
...@@ -43,26 +49,50 @@ public class Debuger { ...@@ -43,26 +49,50 @@ public class Debuger {
} }
public static void exception(String message) { public static void exception(String message) {
if(isDebug()) { if (canThrowError()) {
throw new RuntimeException(message); throw new RuntimeException(message);
}else{ } else {
Log.e(TAG,"exception",new RuntimeException(message)); sLog.e(TAG, "exception", new RuntimeException(message));
} }
} }
public static void exception(Throwable t) { public static void exception(Throwable t) {
if(isDebug()) { if (canThrowError()) {
throw new RuntimeException(t); throw new RuntimeException(t);
}else{ } else {
Log.e(TAG,"exception",t); sLog.e(TAG, "exception", t);
} }
} }
public static boolean isDebug(){ public static boolean isDebug() {
try { try {
return FlutterBoost.instance().platform().isDebug(); return FlutterBoost.instance().platform().isDebug();
}catch (Throwable t){ } catch (Throwable t) {
return false; return false;
} }
} }
/**
* 设置boost log接收实现,默认为Anroid自带Log
*
* @param log
*/
public static void setLog(ILog log) {
if (log != null) {
sLog = log;
}
}
/**
* 设置Debugger策略倾向,如tue,则优先保证程序稳定,false,可抛Error
*
* @param safeMode
*/
public static void setSafeMode(boolean safeMode) {
sSafeMode = safeMode;
}
private static boolean canThrowError() {
return isDebug() && !sSafeMode;
}
} }
package com.idlefish.flutterboost.log;
import android.util.Log;
public class AndroidLog implements ILog {
@Override
public void d(String tag, String msg) {
Log.d(tag, msg);
}
@Override
public void d(String tag, String msg, Throwable throwable) {
Log.d(tag, msg, throwable);
}
@Override
public void e(String tag, String msg) {
Log.e(tag, msg);
}
@Override
public void e(String tag, String msg, Throwable throwable) {
Log.e(tag, msg, throwable);
}
@Override
public void i(String tag, String msg) {
Log.i(tag, msg);
}
@Override
public void i(String tag, String msg, Throwable throwable) {
Log.i(tag, msg, throwable);
}
@Override
public void v(String tag, String msg) {
Log.v(tag, msg);
}
@Override
public void v(String tag, String msg, Throwable throwable) {
Log.v(tag, msg, throwable);
}
@Override
public void w(String tag, String msg) {
Log.w(tag, msg);
}
@Override
public void w(String tag, String msg, Throwable throwable) {
Log.w(tag, msg, throwable);
}
@Override
public boolean isLogLevelEnabled(int level) {
return true;
}
}
package com.idlefish.flutterboost.log;
public interface ILog {
public enum LogLevelEnum {
VERBOSE(0, "V"), DEBUG(1, "D"), INFO(2, "I"), WARNING(3, "W"), ERROR(4, "E");
private String logLevelName;
private int loglevel;
LogLevelEnum(int loglevel, String name) {
this.loglevel = loglevel;
this.logLevelName = name;
}
public String getLogLevelName() {
return logLevelName;
}
public int getLoglevel() {
return loglevel;
}
}
void d(String tag, String msg);
void d(String tag, String msg, Throwable throwable);
void e(String tag, String msg);
void e(String tag, String msg, Throwable throwable);
void i(String tag, String msg);
void i(String tag, String msg, Throwable throwable);
void v(String tag, String msg);
void v(String tag, String msg, Throwable throwable);
void w(String tag, String msg);
void w(String tag, String msg, Throwable throwable);
boolean isLogLevelEnabled(int level);
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment