From 6529aae8c3a4f6ecf9ed90051864f84237f4b250 Mon Sep 17 00:00:00 2001
From: endian11 <Dana_Lee1016@126.com>
Date: 星期三, 25 九月 2019 17:08:03 +0800
Subject: [PATCH] 修改MyLog

---
 app/src/main/java/safeluck/drive/evaluation/util/MyLog.java |  163 +++++++++++++++++++++++++-----------------------------
 1 files changed, 75 insertions(+), 88 deletions(-)

diff --git a/app/src/main/java/safeluck/drive/evaluation/util/MyLog.java b/app/src/main/java/safeluck/drive/evaluation/util/MyLog.java
index 5df62fb..30342d7 100644
--- a/app/src/main/java/safeluck/drive/evaluation/util/MyLog.java
+++ b/app/src/main/java/safeluck/drive/evaluation/util/MyLog.java
@@ -10,6 +10,7 @@
 
 import android.content.Context;
 import android.os.Environment;
+import android.text.TextUtils;
 import android.util.Log;
 
 import java.io.BufferedWriter;
@@ -23,19 +24,19 @@
 public class MyLog {
 
 
-
     private static final String TAG = "AYJiaKao";
     private static Boolean MYLOG_SWITCH = true; // 鏃ュ織鏂囦欢鎬诲紑鍏�
 
     private static Boolean MYLOG_WRITE_TO_FILE = true;// 鏃ュ織鍐欏叆鏂囦欢寮�鍏�
+    private static File file;
 
     // 杈撳叆鏃ュ織绫诲瀷锛寃浠h〃鍙緭鍑哄憡璀︿俊鎭瓑锛寁浠h〃杈撳嚭鎵�鏈変俊鎭�
     private static char MYLOG_TYPE = 'v';
 
     // 鏃ュ織鏂囦欢鍦╯dcard涓殑璺緞
-    private static String MYLOG_PATH_SDCARD_DIR = "/sdcard/nvjiakaolog";
+    private static String MYLOG_PATH_SDCARD_DIR = "/sdcard/JiaKaolog";
 
-    private static int SDCARD_LOG_FILE_SAVE_DAYS = 3;// sd鍗′腑鏃ュ織鏂囦欢鐨勬渶澶氫繚瀛樺ぉ鏁�
+    private static int SDCARD_LOG_FILE_SAVE_DAYS = 2;// sd鍗′腑鏃ュ織鏂囦欢鐨勬渶澶氫繚瀛樺ぉ鏁�
 
     private static String MYLOGFILEName = "Log.txt";// 鏈被杈撳嚭鐨勬棩蹇楁枃浠跺悕绉�
 
@@ -46,13 +47,11 @@
     public Context context;
 
 
-
     public static void w(String tag, Object msg) { // 璀﹀憡淇℃伅
 
         log(tag, msg.toString(), 'w');
 
     }
-
 
 
     public static void e(String tag, Object msg) { // 閿欒淇℃伅
@@ -62,7 +61,6 @@
     }
 
 
-
     public static void d(String tag, Object msg) {// 璋冭瘯淇℃伅
 
         log(tag, msg.toString(), 'd');
@@ -70,18 +68,17 @@
     }
 
 
-
     public static void i(String tag, Object msg) {//
 
         log(tag, msg.toString(), 'i');
 
     }
+
     public static void i(Object msg) {//
 
         log(TAG, msg.toString(), 'i');
 
     }
-
 
 
     public static void v(String tag, Object msg) {
@@ -91,13 +88,11 @@
     }
 
 
-
     public static void w(String tag, String text) {
 
         log(tag, text, 'w');
 
     }
-
 
 
     public static void e(String tag, String text) {
@@ -107,7 +102,6 @@
     }
 
 
-
     public static void d(String tag, String text) {
 
         log(tag, text, 'd');
@@ -115,18 +109,17 @@
     }
 
 
-
     public static void i(String tag, String text) {
 
         log(tag, text, 'i');
 
     }
-    public static void i( String text) {
+
+    public static void i(String text) {
 
         log(TAG, text, 'i');
 
     }
-
 
 
     public static void v(String tag, String text) {
@@ -136,20 +129,15 @@
     }
 
 
-
     /**
-
      * 鏍规嵁tag, msg鍜岀瓑绾э紝杈撳嚭鏃ュ織
-
+     *
      * @param tag
-
      * @param msg
-
      * @param level
-
      */
 
-    private static void log(String tag, String msg, char level) {
+    private static void log(final String tag, final String msg, final char level) {
 
         if (MYLOG_SWITCH) {//鏃ュ織鏂囦欢鎬诲紑鍏�
 
@@ -176,108 +164,110 @@
             }
 
             if (MYLOG_WRITE_TO_FILE)//鏃ュ織鍐欏叆鏂囦欢寮�鍏�
+                new Thread(new Runnable() {
+                    @Override
+                    public void run() {
+                        writeLogtoFile(String.valueOf(level), tag, msg);
+                    }
+                }
+                ).start();
 
-                writeLogtoFile(String.valueOf(level), tag, msg);
 
         }
 
     }
 
+    public static void createIfNotExist() {
 
+
+        delFile();
+
+
+        String needWriteFiel = logfile.format(System.currentTimeMillis());
+
+
+        Log.i(TAG, "createIfNotExist: " + needWriteFiel);
+
+
+        File dirsFile = new File(MYLOG_PATH_SDCARD_DIR);
+
+        if (!dirsFile.exists()) {
+
+            dirsFile.mkdirs();
+
+        }
+
+        //Log.i("鍒涘缓鏂囦欢","鍒涘缓鏂囦欢");
+
+        file = new File(dirsFile.toString(), needWriteFiel + MYLOGFILEName);// MYLOG_PATH_SDCARD_DIR
+
+        if (!file.exists()) {
+
+            try {
+
+                //鍦ㄦ寚瀹氱殑鏂囦欢澶逛腑鍒涘缓鏂囦欢
+
+                file.createNewFile();
+
+            } catch (Exception e) {
+
+            }
+
+        }
+    }
 
     /**
-
      * 鎵撳紑鏃ュ織鏂囦欢骞跺啓鍏ユ棩蹇�
-
+     *
      * @param mylogtype
-
      * @param tag
-
      * @param text
-
      */
 
     private static void writeLogtoFile(String mylogtype, final String tag, final String text) {// 鏂板缓鎴栨墦寮�鏃ュ織鏂囦欢
 
-        new Thread(new Runnable() {
-            @Override
-            public void run() {
-                delFile();
-                Date nowtime = new Date();
-
-                String needWriteFiel = logfile.format(nowtime);
-
-                String needWriteMessage = myLogSdf.format(nowtime) + " "+ tag + "  " + text;
 
 
 
-
-                File dirsFile = new File(MYLOG_PATH_SDCARD_DIR);
-
-                if (!dirsFile.exists()){
-
-                    dirsFile.mkdirs();
-
-                }
-
-                //Log.i("鍒涘缓鏂囦欢","鍒涘缓鏂囦欢");
-
-                File file = new File(dirsFile.toString(), needWriteFiel + MYLOGFILEName);// MYLOG_PATH_SDCARD_DIR
-
-                if (!file.exists()) {
-
-                    try {
-
-                        //鍦ㄦ寚瀹氱殑鏂囦欢澶逛腑鍒涘缓鏂囦欢
-
-                        file.createNewFile();
-
-                    } catch (Exception e) {
-
-                    }
-
-                }
+        String needWriteMessage = myLogSdf.format(System.currentTimeMillis()) + " " + tag + "  " + text;
 
 
-
-                try {
+        try {
 /**鍚庨潰杩欎釜鍙傛暟浠h〃鏄笉鏄鎺ヤ笂鏂囦欢涓師鏉ョ殑鏁版嵁锛屼笉杩涜瑕嗙洊**/
-                    FileWriter filerWriter = new FileWriter(file, true);
 
-                    BufferedWriter bufWriter = new BufferedWriter(filerWriter);
-
-                    bufWriter.write(needWriteMessage);
-
-                    bufWriter.newLine();
-
-                    bufWriter.close();
-
-                    filerWriter.close();
-
-                } catch (IOException e) {
-
-                    e.printStackTrace();
-
-                }
+            if (file == null) {
+                Log.i(TAG, "writeLogtoFile: file==null");
+                createIfNotExist();
             }
-        }).start();
+            FileWriter filerWriter = new FileWriter(file, true);
 
+            BufferedWriter bufWriter = new BufferedWriter(filerWriter);
 
+            bufWriter.write(needWriteMessage);
+
+            bufWriter.newLine();
+
+            bufWriter.close();
+
+            filerWriter.close();
+
+        } catch (IOException e) {
+
+            e.printStackTrace();
+
+        }
     }
 
 
-
     /**
-
      * 鍒犻櫎鍒跺畾鐨勬棩蹇楁枃浠�
-
      */
 
     public static void delFile() {// 鍒犻櫎鏃ュ織鏂囦欢
 
         String needDelFiel = logfile.format(getDateBefore());
 
-        String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath()+"/nvlog";
+        String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JiaKaolog";
 
         File file = new File(dirPath, needDelFiel + MYLOGFILEName);// MYLOG_PATH_SDCARD_DIR
 
@@ -290,11 +280,8 @@
     }
 
 
-
     /**
-
      * 寰楀埌鐜板湪鏃堕棿鍓嶇殑鍑犲ぉ鏃ユ湡锛岀敤鏉ュ緱鍒伴渶瑕佸垹闄ょ殑鏃ュ織鏂囦欢鍚�
-
      */
 
     private static Date getDateBefore() {

--
Gitblit v1.8.0