package com.xvli.cit.Util;
import android.content.Context;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* @param
* @Comments : 网络请求日志
* @Author : Lampo
* @ModifiedBy : Lampo
*/
public class HttpLogUtil {
private static HttpLogUtil INSTANCE = null;
private static String PATH_LOGCAT;
private LogDumper mLogDumper = null;
private File logFile;
public static HttpLogUtil getInstance() {
if (INSTANCE == null) {
INSTANCE = new HttpLogUtil();
}
return INSTANCE;
}
public void init(Context context) {
PATH_LOGCAT = FileUtils.CreateFiles(context, "HttpLog");
FileUtils.deleteExceedOneMonth(PATH_LOGCAT);
if (mLogDumper == null) {
mLogDumper = new LogDumper(String.valueOf(android.os.Process.myPid()), PATH_LOGCAT);
mLogDumper.start();
}
}
private class LogDumper extends Thread {
private Process logcatProc;
private BufferedReader mReader = null;
String cmds = null;
private String mPID;
private FileOutputStream out = null;
public LogDumper(String pid, String dir) {
mPID = pid;
try {
logFile = FileUtils.createNewLogFile(dir);
out = new FileOutputStream(logFile);
} catch (Exception e) {
e.printStackTrace();
}
/**
*
* 日志等级:*:v , *:d , *:w , *:e , *:f , *:s
*
* 显示当前mPID程序的 E和W等级的日志.
*
* */
// cmds = "logcat *:e *:w | grep \"(" + mPID + ")\"";
// cmds = "logcat | grep \"(" + mPID + ")\"";//打印所有日志信息
// cmds = "logcat -s way";//打印标签过滤信息
// cmds = "logcat *:e *:i *:d | grep \"(" + mPID + ")\"";
//MrFu测试==============1
// commandLine.clear();//清除所有的日志
// commandLine.add("logcat");
// commandLine.add("-d");
// commandLine.add("PDALog--->");
//MrFu测试==============2
// cmds = "logcat -f "+ logFile.getAbsolutePath() + " -v time *:V";//加上-f和后面的路径【logFile.getAbsolutePath()】就会叠加打印所有到文本
// cmds = "logcat -v time *:V";//只保存当前的到日志
// cmds = "logcat -s PDALog--->";//打印出Tag为“PDALog--->”的日志
cmds = "logcat -v time -s PDALog--->";//打印出Tag为“PDALog--->”的日志
}
@Override
public void run() {
try {
logcatProc = Runtime.getRuntime().exec(cmds);
mReader = new BufferedReader(new InputStreamReader(logcatProc.getInputStream()), 1024);
String line = null;
while ((line = mReader.readLine()) != null) {
if (line.length() == 0) {
continue;
}
if (out != null && line.contains(mPID)) {
out.write((Util.getNowDate_All() + "\r\n" + line + "\r\n").getBytes());
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (logcatProc != null) {
logcatProc.destroy();
logcatProc = null;
}
if (mReader != null) {
try {
mReader.close();
mReader = null;
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
out = null;
}
}
}
}
}
/**
* 在Cit文件夹下创建新的子文件夹
*
* @param context
* @param fileName
* @return
*/
public static String CreateFiles(Context context, String fileName) {
String PATH_LOGCAT;
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {// 优先保存到SD卡中
PATH_LOGCAT = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + Config.APP_NAME + "/" + fileName;
} else {// 如果SD卡不存在,就保存到本应用的目录下
PATH_LOGCAT = context.getFilesDir().getAbsolutePath() + File.separator + Config.APP_NAME + "/" + fileName;
}
File file = new File(PATH_LOGCAT);
if (!file.exists()) {
file.mkdirs();
}
return PATH_LOGCAT;
}
/**
* 删除超过一个月的log
*
* @param path 文件夹名称
*/
public static void deleteExceedOneMonth(String path) {
File file1 = new File(path);
File[] files = file1.listFiles();
if (files.length <= 30) {
return;
}
for (int i = 0; i < files.length; i++) {
String str = files[i].getName().replace(".log", "").replace("CIT-", "");
if (str.indexOf("U") != -1) {
str = str.replace("-U", "");
}
if (!Util.isExceedOneMonth(str)) {
deleteFile(path + "/" + files[i].getName());
}
}
}
/**
* 是否是30天内
*
* @param month
* @return
* @throws ParseException
*/
public static boolean isExceedOneMonth(String month) {
DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
DateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
Date date = null;
long day = 0;
try {
date = df.parse(month + " 00:00:00");
long time = date.getTime();
Date now = df1.parse(getNowDate());
long nowTime = now.getTime();
day = (nowTime - time) / (24 * 60 * 60 * 1000);
} catch (ParseException e) {
e.printStackTrace();
}
if (day <= 30) {
return true;
} else {
return false;
}
}
/**
* 单个删除文件
*
* @param path
*/
public static void deleteFile(String path) {
File file = new File(path);
if (file.exists() && file.isFile()) {//是否可以在该文件中找到该文件//该文件是否表示底层上
PDALogger.e("file.isFile true");
file.delete();
}
}