安卓日志类



import android.os.SystemClock;
import android.text.TextUtils;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedDeque;

public class LogManager {
    private String TAG="LOG";
    private static Queue<String> padLogCash=new ConcurrentLinkedDeque<>();
    private static Queue<String> conLogCash=new ConcurrentLinkedDeque<>();
    private static LogManager instance=null;
    public static void WritePadLog(String log){
        if(instance==null){
            instance=new LogManager();
        }
        padLogCash.add(log);
    }

    public static void WriteConLog(String log){
        if(instance==null){
            instance=new LogManager();
        }
        conLogCash.add(log);
    }

    ///sdcard/log/pad20221223.txt;
    String logpath=CommonTool.getLyLogDirPath()+File.separator;
    String padFileName="";
    String conFileName="";
    public LogManager(){
        padFileName=logpath+"pad"+DateUtil.getCurrentDateStr(DateUtil.SHORT_PATTERN_NUM)+".txt";
        conFileName=logpath+"con"+DateUtil.getCurrentDateStr(DateUtil.SHORT_PATTERN_NUM)+".txt";
        init();
        startPadWriteThread();
        startConWriteThread();
    }

    private void init(){
        try {
            File padFile = new File(padFileName);
            createFile(padFile);
            File conFile = new File(conFileName);
            createFile(conFile);
            File dir = new File(logpath);
            String k = DateUtil.getCurrentDateStr(DateUtil.SHORT_PATTERN_NUM);
            if (dir.length() > 0) {
                for (String name : dir.list()) {
                    String fileDate = name.substring(3, 11);
                    //Log.v("TEST",fileDate+"|"+k);
                    if (Long.parseLong(fileDate) < Long.parseLong(k) - 30) {
                        File temp = new File(logpath + name);
                        temp.delete();
                    }
                }
            }
        }
        catch (Exception ex){}
    }

    private void startPadWriteThread(){
        Thread t=new Thread(new Runnable() {
            @Override
            public void run() {
                StringBuilder padSb=new StringBuilder();
                int count=0;
                while (true){
                    SystemClock.sleep(1000);
                    try
                    {
                        padSb.delete(0,padSb.length());
                        count=0;
                        while (count<5&&padLogCash.size()>0){
                            padSb.append(padLogCash.poll()+"\r\n");
                            count++;
                        }

                        if(padSb.length()>0){
                            try {
                                FileWriter  writer = new FileWriter(padFileName, true);
                                writer.write(padSb.toString());
                                writer.close();
                            } catch (IOException e) {
                                //Log.e("LOGM",e.getMessage());
                            }
                        }
                    }
                    catch (Exception ex){
                        //Log.e("LOGM",ex.getMessage());
                    }
                }
            }
        });
        t.start();
    }

    private void startConWriteThread(){
        Thread t=new Thread(new Runnable() {
            @Override
            public void run() {
                StringBuilder padSb=new StringBuilder();
                int count=0;
                while (true){
                    SystemClock.sleep(1000);
                    try
                    {
                        padSb.delete(0,padSb.length());
                        count=0;
                        while (count<5&&conLogCash.size()>0){
                            padSb.append(conLogCash.poll()+"\r\n");
                            count++;
                        }

                        if(padSb.length()>0){
                            try {
                                FileWriter writer = new FileWriter(conFileName, true);
                                writer.write(padSb.toString());
                                writer.close();
                            } catch (IOException e) {
                                //Log.e("LOGM",e.getMessage());
                            }
                        }
                    }
                    catch (Exception ex){
                       // Log.e("LOGM",ex.getMessage());
                    }
                }
            }
        });
        t.start();
    }

    private boolean createFile(String path){
        if(TextUtils.isEmpty(path)) return false;
        return createFile(new File(path));
    }

    private boolean createFile(File file){
        if(file == null) return false;
        if (file.exists()) {
            return true;
        }
        File parentFile = file.getParentFile();
        if(parentFile == null) return false;
        if(!parentFile.exists()){
            parentFile.mkdirs();
        }
        try {
            file.createNewFile();
        } catch (IOException e) {
            return false;
        }
        return true;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值