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;
}
}
安卓日志类
于 2024-03-27 14:05:42 首次发布