提交 3d791122 编写于 作者: B Blankj

see 03/26 log

上级 2f9b36a2
......@@ -38,6 +38,6 @@ public class UtilsApp extends Application {
appContext = this;
Utils.init(appContext);
CrashUtils.getInstance().init();
lBuilder = new LogUtils.Builder();
lBuilder = new LogUtils.Builder().setLogSwitch(BuildConfig.DEBUG);
}
}
......@@ -30,13 +30,14 @@ public class LogActivity extends Activity
private String globalTag = "";
private boolean border = false;
private boolean file = false;
private int filter = LogUtils.V;
private static final int UPDATE_TAG = 0x01;
private static final int UPDATE_BORDER = 0x01 << 1;
private static final int UPDATE_FILTER = 0x01 << 2;
private static final int UPDATE_FILE = 0x01 << 1;
private static final int UPDATE_BORDER = 0x01 << 2;
private static final int UPDATE_FILTER = 0x01 << 3;
private Thread mThread;
private Runnable mRunnable = new Runnable() {
@Override
public void run() {
......@@ -55,7 +56,7 @@ public class LogActivity extends Activity
static {
StringBuilder sb = new StringBuilder();
sb.append("len = 10400\n content = \"");
sb.append("len = 10400\ncontent = \"");
for (int i = 0; i < 800; ++i) {
sb.append("Hello world. ");
}
......@@ -71,6 +72,7 @@ public class LogActivity extends Activity
mBuilder = UtilsApp.lBuilder;
findViewById(R.id.btn_toggle_tag).setOnClickListener(this);
findViewById(R.id.btn_toggle_border).setOnClickListener(this);
findViewById(R.id.btn_toggle_file).setOnClickListener(this);
findViewById(R.id.btn_toggle_filter).setOnClickListener(this);
findViewById(R.id.btn_log_no_tag).setOnClickListener(this);
findViewById(R.id.btn_log_with_tag).setOnClickListener(this);
......@@ -91,6 +93,9 @@ public class LogActivity extends Activity
case R.id.btn_toggle_tag:
updateAbout(UPDATE_TAG);
break;
case R.id.btn_toggle_file:
updateAbout(UPDATE_FILE);
break;
case R.id.btn_toggle_border:
updateAbout(UPDATE_BORDER);
break;
......@@ -114,8 +119,8 @@ public class LogActivity extends Activity
LogUtils.a("customTag", "assert");
break;
case R.id.btn_log_in_new_thread:
mThread = new Thread(mRunnable);
mThread.start();
Thread thread = new Thread(mRunnable);
thread.start();
break;
case R.id.btn_log_null:
LogUtils.v(null);
......@@ -153,6 +158,9 @@ public class LogActivity extends Activity
case UPDATE_TAG:
globalTag = globalTag.equals(TAG) ? "" : TAG;
break;
case UPDATE_FILE:
file = !file;
break;
case UPDATE_BORDER:
border = !border;
break;
......@@ -161,9 +169,11 @@ public class LogActivity extends Activity
break;
}
mBuilder.setGlobalTag(globalTag)
.setLog2FileSwitch(file)
.setBorderSwitch(border)
.setLogFilter(filter);
tvAboutLog.setText("tag: " + (globalTag.equals("") ? "null" : TAG)
+ "\nfile: " + String.valueOf(file)
+ "\nborder: " + String.valueOf(border)
+ "\nfilter: " + (filter == LogUtils.V ? "Verbose" : "Warn")
);
......
......@@ -10,13 +10,6 @@
android:orientation="vertical"
android:padding="@dimen/spacing_small">
<Button
android:id="@+id/btn_launch_image_activity"
style="@style/BtnFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/activity.start_imageActivity" />
<TextView
android:id="@+id/tv_about_activity"
style="@style/Font"
......@@ -24,5 +17,12 @@
android:layout_height="wrap_content"
android:gravity="center" />
<Button
android:id="@+id/btn_launch_image_activity"
style="@style/BtnFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/activity.start_imageActivity" />
</LinearLayout>
</ScrollView>
......@@ -11,6 +11,14 @@
android:orientation="vertical"
android:padding="@dimen/spacing_small">
<TextView
android:id="@+id/tv_about_app"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
<Button
android:id="@+id/btn_install_app"
style="@style/BtnFont"
......@@ -59,12 +67,5 @@
android:text="@string/app.get_details_settings"
/>
<TextView
android:id="@+id/tv_about_app"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
</ScrollView>
......@@ -10,6 +10,13 @@
android:orientation="vertical"
android:padding="@dimen/spacing_small">
<TextView
android:id="@+id/tv_about_device"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
<Button
android:id="@+id/btn_shutdown"
......@@ -19,7 +26,6 @@
android:text="@string/device.shutdown"
/>
<Button
android:id="@+id/btn_reboot"
style="@style/BtnFont"
......@@ -44,13 +50,5 @@
android:text="@string/device.reboot_to_bootloader"
/>
<TextView
android:id="@+id/tv_about_device"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
</ScrollView>
......@@ -11,6 +11,14 @@
android:orientation="vertical"
android:padding="@dimen/spacing_small">
<TextView
android:id="@+id/tv_about_flashlight"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
<Button
android:id="@+id/btn_open_flashlight"
style="@style/BtnFont"
......@@ -27,13 +35,5 @@
android:text="@string/flashlight.close"
/>
<TextView
android:id="@+id/tv_about_flashlight"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
</ScrollView>
......@@ -10,14 +10,6 @@
android:orientation="vertical"
android:padding="@dimen/spacing_small">
<Button
android:id="@+id/btn_send_msg_after_3s"
style="@style/BtnFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/handler.send_msg_after_3s"
/>
<TextView
android:id="@+id/tv_about_handler"
style="@style/Font"
......@@ -26,6 +18,13 @@
android:gravity="center"
/>
<Button
android:id="@+id/btn_send_msg_after_3s"
style="@style/BtnFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/handler.send_msg_after_3s"
/>
</LinearLayout>
</ScrollView>
......@@ -11,6 +11,14 @@
android:orientation="vertical"
android:padding="@dimen/spacing_small">
<TextView
android:id="@+id/tv_about_keyboard"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
<EditText
android:id="@+id/et"
android:layout_width="match_parent"
......@@ -40,13 +48,5 @@
android:text="@string/keyboard.toggle_soft_input"
/>
<TextView
android:id="@+id/tv_about_keyboard"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
</ScrollView>
......@@ -10,6 +10,13 @@
android:orientation="vertical"
android:padding="@dimen/spacing_small">
<TextView
android:id="@+id/tv_about_log"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
<Button
android:id="@+id/btn_toggle_tag"
style="@style/BtnFont"
......@@ -17,6 +24,13 @@
android:layout_height="wrap_content"
android:text="@string/log.toggle_tag" />
<Button
android:id="@+id/btn_toggle_file"
style="@style/BtnFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/log.toggle_file" />
<Button
android:id="@+id/btn_toggle_border"
style="@style/BtnFont"
......@@ -94,12 +108,5 @@
android:layout_height="wrap_content"
android:text="@string/log.xml" />
<TextView
android:id="@+id/tv_about_log"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
</ScrollView>
......@@ -11,6 +11,14 @@
android:orientation="vertical"
android:padding="@dimen/spacing_small">
<TextView
android:id="@+id/tv_about_network"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
<Button
android:id="@+id/btn_open_wireless_settings"
style="@style/BtnFont"
......@@ -35,14 +43,5 @@
android:text="@string/network.set_wifi_enabled"
/>
<TextView
android:id="@+id/tv_about_network"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
</ScrollView>
......@@ -11,6 +11,14 @@
android:orientation="vertical"
android:padding="@dimen/spacing_small">
<TextView
android:id="@+id/tv_about_network"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
<Button
android:id="@+id/btn_check_permission"
style="@style/BtnFont"
......@@ -35,14 +43,5 @@
android:text="@string/network.set_wifi_enabled"
/>
<TextView
android:id="@+id/tv_about_network"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
</ScrollView>
......@@ -11,6 +11,14 @@
android:orientation="vertical"
android:padding="@dimen/spacing_small">
<TextView
android:id="@+id/tv_about_phone"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
<Button
android:id="@+id/btn_dial"
style="@style/BtnFont"
......@@ -43,13 +51,5 @@
android:text="@string/phone.send_sms_silent"
/>
<TextView
android:id="@+id/tv_about_phone"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
</ScrollView>
......@@ -10,14 +10,6 @@
android:orientation="vertical"
android:padding="@dimen/spacing_small">
<Button
android:id="@+id/btn_kill_all_background_processes"
style="@style/BtnFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/process.kill_all_background"
/>
<TextView
android:id="@+id/tv_about_process"
style="@style/Font"
......@@ -26,5 +18,13 @@
android:gravity="center"
/>
<Button
android:id="@+id/btn_kill_all_background_processes"
style="@style/BtnFont"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/process.kill_all_background"
/>
</LinearLayout>
</ScrollView>
......@@ -11,6 +11,14 @@
android:orientation="vertical"
android:padding="@dimen/spacing_small">
<TextView
android:id="@+id/tv_about_toast"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
<Button
android:id="@+id/btn_is_jump_when_more"
style="@style/BtnFont"
......@@ -57,13 +65,5 @@
android:text="@string/toast.cancel"
/>
<TextView
android:id="@+id/tv_about_toast"
style="@style/Font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
/>
</LinearLayout>
</ScrollView>
......@@ -80,6 +80,7 @@
<string name="log.toggle_tag">Toggle Tag</string>
<string name="log.toggle_border">Toggle Border</string>
<string name="log.toggle_filter">Toggle Filter</string>
<string name="log.toggle_file">Toggle File</string>
<string name="log.with_no_tag">Log With No Tag</string>
<string name="log.with_tag">Log With Tag</string>
<string name="log.in_new_thread">Log In New Thread</string>
......
......@@ -61,22 +61,24 @@ public final class LogUtils {
private static int stackIndex = 0;
private static Callable<Boolean> sTask;
private static final String LINE_SEPARATOR = System.getProperty("line.separator");
private static final String TOP_BORDER = "╔═══════════════════════════════════════════════════════════════════════════════════════════════════";
private static final String LEFT_BORDER = "║ ";
private static final String BOTTOM_BORDER = "╚═══════════════════════════════════════════════════════════════════════════════════════════════════";
private static final int MAX_LEN = 4000;
private static final String NULL_TIPS = "Log with null object.";
private static final String NULL = "null";
private static final String PARAM = "Param";
private static final String ARGS = "args";
public static class Builder {
public Builder() {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
dir = Utils.getContext().getExternalCacheDir().getPath() + File.separator + "log" + File.separator;
dir = Utils.getContext().getExternalCacheDir() + File.separator + "log" + File.separator;
} else {
dir = Utils.getContext().getCacheDir().getPath() + File.separator + "log" + File.separator;
dir = Utils.getContext().getCacheDir() + File.separator + "log" + File.separator;
}
}
......@@ -190,7 +192,6 @@ public final class LogUtils {
tag = processContents[0];
String head = processContents[1];
String body = processContents[2];
Log.d("1cmj", body);
switch (type) {
case V:
case D:
......@@ -252,7 +253,7 @@ public final class LogUtils {
StringBuilder sb = new StringBuilder();
for (int i = 0, len = contents.length; i < len; ++i) {
Object content = contents[i];
sb.append(PARAM)
sb.append(ARGS)
.append("[")
.append(i)
.append("]")
......@@ -263,18 +264,27 @@ public final class LogUtils {
body = sb.toString();
}
}
if (sLogBorderSwitch) {
StringBuilder sb = new StringBuilder();
String[] lines = body.split(LINE_SEPARATOR);
for (String line : lines) {
sb.append(LEFT_BORDER).append(line).append(LINE_SEPARATOR);
}
body = sb.substring(2);
}
return new String[]{tag, head, body};
}
private static void printLog(int type, String tag, String head, String msg) {
if (sLogBorderSwitch) printSubLog(type, tag, TOP_BORDER);
if (sLogBorderSwitch) printBorder(type, tag, true);
printSubLog(type, tag, head);
int len = msg.length();
int countOfSub = len / MAX_LEN;
if (countOfSub > 0) {
int index = 0;
String sub;
for (int i = 0; i < countOfSub; i++) {
String sub = msg.substring(index, index + MAX_LEN);
sub = msg.substring(index, index + MAX_LEN);
printSubLog(type, tag, sub);
index += MAX_LEN;
}
......@@ -282,10 +292,11 @@ public final class LogUtils {
} else {
printSubLog(type, tag, msg);
}
if (sLogBorderSwitch) printSubLog(type, tag, BOTTOM_BORDER);
if (sLogBorderSwitch) printBorder(type, tag, false);
}
private static void printSubLog(int type, final String tag, final String msg) {
private static void printSubLog(final int type, final String tag, String msg) {
if (sLogBorderSwitch) msg = LEFT_BORDER + msg;
switch (type) {
case V:
Log.v(tag, msg);
......@@ -308,6 +319,30 @@ public final class LogUtils {
}
}
private static void printBorder(int type, String tag, boolean isTop) {
String border = isTop ? TOP_BORDER : BOTTOM_BORDER;
switch (type) {
case V:
Log.v(tag, border);
break;
case D:
Log.d(tag, border);
break;
case I:
Log.i(tag, border);
break;
case W:
Log.w(tag, border);
break;
case E:
Log.e(tag, border);
break;
case A:
Log.wtf(tag, border);
break;
}
}
private synchronized static void print2File(int type, String tag, final String head, final String msg) {
boolean isSuccess = false;
Date now = new Date();
......@@ -360,9 +395,9 @@ public final class LogUtils {
}
}
if (isSuccess) {
printSubLog(type, tag, "log to " + fullPath + "success >> ");
Log.d(tag, "log to " + fullPath + "success!");
} else {
printSubLog(type, tag, "log to file failed!");
Log.e(tag, "log to " + fullPath + " failed!");
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册