From c536e840b862977a0f71a7989481ba8032955607 Mon Sep 17 00:00:00 2001
From: lizhanwei <Dana_Lee1016@126.com>
Date: 星期二, 28 七月 2020 13:38:27 +0800
Subject: [PATCH] 修改信号配置布局文件

---
 im_lib/src/main/java/com/anyun/im_lib/util/MyLog.java |  168 +++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 files changed, 147 insertions(+), 21 deletions(-)

diff --git a/im_lib/src/main/java/com/anyun/im_lib/util/MyLog.java b/im_lib/src/main/java/com/anyun/im_lib/util/MyLog.java
index c2ad1ff..6ebeba7 100644
--- a/im_lib/src/main/java/com/anyun/im_lib/util/MyLog.java
+++ b/im_lib/src/main/java/com/anyun/im_lib/util/MyLog.java
@@ -25,12 +25,17 @@
 import java.text.SimpleDateFormat;
 import java.util.Calendar;
 import java.util.Date;
+import java.util.concurrent.Executors;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
 public class MyLog {
 
-     private static final String TAG = "AYJiaKao";
+    private static final String TAG = "AYJiaKao";
+
+
+    // 鏃ュ織鏂囦欢鍦╯dcard涓殑璺緞
+    private static String MYLOG_PATH_SDCARD_DIR = "/sdcard/JiaKaolog";
 
 
     private static Boolean MYLOG_SWITCH = true; // 鏃ュ織鏂囦欢鎬诲紑鍏�
@@ -42,9 +47,8 @@
     private static char MYLOG_TYPE = 'v';
 
     // 鏃ュ織鏂囦欢鍦╯dcard涓殑璺緞
-    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 = 7;// sd鍗′腑鏃ュ織鏂囦欢鐨勬渶澶氫繚瀛樺ぉ鏁�
 
     private static String MYLOGFILEName = "Log.txt";// 鏈被杈撳嚭鐨勬棩蹇楁枃浠跺悕绉�
 
@@ -172,14 +176,13 @@
             }
 
             if (MYLOG_WRITE_TO_FILE)//鏃ュ織鍐欏叆鏂囦欢寮�鍏�
-                new Thread(new Runnable() {
+
+                Executors.newSingleThreadExecutor().execute(new Runnable() {
                     @Override
                     public void run() {
-                        writeLogtoFile(String.valueOf(level), tag, msg);
+                        writeLogtoFile( tag, msg);
                     }
-                }
-                ).start();
-
+                });
 
         }
 
@@ -235,7 +238,12 @@
 
         try {
 /**鍚庨潰杩欎釜鍙傛暟浠h〃鏄笉鏄鎺ヤ笂鏂囦欢涓師鏉ョ殑鏁版嵁锛屼笉杩涜瑕嗙洊**/
-
+            if (file != null){
+                if (!file.getName().contains(logfile.format(System.currentTimeMillis()))){
+                    Log.i(TAG, "writeLogtoFile: 褰撳墠file涓嶆槸褰撳墠鏃ユ湡鐨勶紝缃负null");
+                    file = null;
+                }
+            }
             if (file == null) {
                 Log.i(TAG, "writeLogtoFile: file==null");
                 createIfNotExist();
@@ -258,6 +266,92 @@
 
         }
     }
+    /**
+     * 鎵撳紑鏃ュ織鏂囦欢骞跺啓鍏ユ棩蹇�
+     *
+     * @param tag
+     * @param text
+     */
+    static int count=0;
+    private static void writeLogtoFile( final String tag, final String text) {// 鏂板缓鎴栨墦寮�鏃ュ織鏂囦欢
+
+        String needWriteMessage = myLogSdf.format(System.currentTimeMillis()) + " " + tag + "  " + text;
+
+        try {
+/**鍚庨潰杩欎釜鍙傛暟浠h〃鏄笉鏄鎺ヤ笂鏂囦欢涓師鏉ョ殑鏁版嵁锛屼笉杩涜瑕嗙洊**/
+
+
+            //鍏堟鏌ュ瓨鍦ㄧ殑file锛屽鏋滃瓨鍦ㄦ病鏈夊啓婊★紙灏忎簬1MB鐨勶級鐨� 鍒欒繑鍥濬ile銆傚鏋滆繑鍥瀗ull,鍒欒鏂板缓
+
+            file =  checkExistFilesAndCreateFile();
+
+
+            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();
+
+        }
+    }
+
+    private static final String PREFREIX = "jiaKao_appLog";
+    private static synchronized File checkExistFilesAndCreateFile() {
+        String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JiaKaolog";
+        File dirsFile  = new File(dirPath);
+        if (!dirsFile.exists()){
+            Log.i(TAG,"鏂囦欢鐩綍涓嶅瓨鍦紝鍒涘缓/nvlog...");
+            dirsFile.mkdirs();
+        }
+        File tempFile = null;
+        for (int j = 0; j < 5; j++) {
+            tempFile = new File(dirPath,PREFREIX+j+".txt");
+            Log.i(TAG,"tempFIle Name= "+tempFile.getName());
+
+            if (tempFile.length()>20*1024*1024) {
+                if (j==4){
+                    Log.i(TAG,"鍒犻櫎绗竴涓枃浠�");
+                    File file = new File(dirPath,PREFREIX+"0.txt");
+                    file.delete();
+                    Log.i(TAG,"閲嶅懡鍚嶅叾浠栨枃浠�");
+                    rename();
+                    return new File(dirPath,PREFREIX+"4.txt");
+                }
+
+
+                continue;
+
+
+
+            }else{
+                Log.i(TAG,String.format("杩斿洖鏂囦欢%s",tempFile.getName()));
+                break;
+            }
+
+        }
+//        Log.i(TAG,String.format("杩斿洖鏂囦欢null"));
+        return tempFile;
+    }
+
+    private static void rename() {
+        String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JiaKaolog";
+        for (int i = 1; i < 5; i++) {
+            File file = new File(dirPath,PREFREIX+i+".txt");
+            File fileSrc = new File(dirPath,PREFREIX+(i-1)+".txt");
+            file.renameTo(fileSrc);
+        }
+
+    }
 
 
     /**
@@ -268,7 +362,7 @@
 
         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
 
@@ -279,20 +373,51 @@
         }
 
     }
-
-    public static void delSubDirLogs() {
-        String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/jiakaolog";
+    private static long filesLength  = 0;
+    public static void checkTotalFileSpace(){
+        String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JiaKaolog";
         String regx = "^[0-9]{4}-";
         Pattern pattern = Pattern.compile(regx);
         File file = new File(dirPath);
-        boolean needDel = false;
+        filesLength = 0;
         if (file.isDirectory()) {
-            Log.i(TAG, "subDirLogs: is a directory");
             File[] subFiles = file.listFiles();
             for (File f :
                     subFiles) {
                 String fName = f.getName();
-                Log.i(TAG, "subDirLogs: 鏂囦欢鍚嶇О锛�" + fName);
+                if (!TextUtils.isEmpty(fName)) {
+                    Matcher matcher = pattern.matcher(fName);
+                    if (matcher.find()) {
+                        Log.i(TAG, "鏂囦欢鍚嶇О锛�" + fName+"checkTotalFileSpace: "+f.length());
+                        if (f.length()>20*1024*1024){
+                            Log.i(TAG, "checkTotalFileSpace: 鍗曚釜鏂囦欢瓒呰繃20MB 锛屽垹闄�"+f.getName());
+                            f.delete();
+                            continue;
+                        }
+                        filesLength +=f.length();
+                    }
+                }
+            }
+            if (filesLength >= 2*1024*1024){
+                Log.i(TAG, "checkTotalFileSpace: 鎬诲ぇ灏忚秴杩�20MB 鍒犻櫎"+filesLength+"B");
+                delSubDirLogs();
+            }
+        }
+    }
+
+    public static void delSubDirLogs() {
+        String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/JiaKaolog";
+        String regx = "^[0-9]{4}-";
+        Pattern pattern = Pattern.compile(regx);
+        File file = new File(dirPath);
+        boolean needDel = true;
+        if (file.isDirectory()) {
+
+            File[] subFiles = file.listFiles();
+            for (File f :
+                    subFiles) {
+                String fName = f.getName();
+
                 if (!TextUtils.isEmpty(fName)) {
                     Matcher matcher = pattern.matcher(fName);
                     if (matcher.find()) {
@@ -300,7 +425,6 @@
                         Log.i(TAG, "subDirLogs: 绗﹀悎瑙勫垯^[0-9]{4}- : " + matcher.group(0));
                         for (int i = 0; i < SDCARD_LOG_FILE_SAVE_DAYS; i++) {
                             String needSaveFileName = logfile.format(getDateBefore(i));
-                            Log.i(TAG,"needsaveFileName="+needSaveFileName);
                             if (fName.contains(needSaveFileName)) {
                                 Log.i(TAG, "delSubDirLogs: 姝og闇�瑕佷繚鐣欙細" + fName + " 鏃ユ湡锛�" + needSaveFileName);
                                 needDel = false;
@@ -313,16 +437,18 @@
                             Log.i(TAG, "delSubDirLogs: 鍒犻櫎璇ユ枃浠讹細" + fName);
                             f.delete();
                         }
-
-
-                    } else {
-                        Log.i(TAG, "delSubDirLogs: 涓嶇鍚堝垹闄よ鍒欙紝鍒欎笉鍒犻櫎锛�" + fName);
                     }
+
+
+
+                } else {
+                    Log.i(TAG, "delSubDirLogs: 涓嶇鍚堝垹闄よ鍒欙紝鍒欎笉鍒犻櫎锛�" + fName);
                 }
             }
         } else {
             Log.i(TAG, "subDirLogs: is not a directory");
         }
+
     }
 
 

--
Gitblit v1.8.0