package com.ian.epimetheus.utils;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
/**
* Created by ian on 2016/10/25.
* 记录异常信息到日志文件中包括:已捕获和未捕获
*
*/
public class MLogger {
private static final String mylogstwo = Environment.getExternalStorageDirectory()
.getAbsolutePath() + "/alogs/";
protected static String I = "I", D = "D", E = "E", W = "W";
static File mLogFile;
public static void initMLogger(){
File logDir = new File(mylogstwo);
if (!logDir.exists()) {
logDir.mkdirs();
}
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.CHINESE);
long timestamp = System.currentTimeMillis();
String time = formatter.format(new Date());
String name = "crash_" + timestamp + "_" + time + ".log";
mLogFile = new File(logDir.getAbsolutePath(), name);
if (!mLogFile.exists()) {
try {
mLogFile.createNewFile();
} catch (IOException e) {
mLogFile = null;
e.printStackTrace();
}
}
}
/**
* 打印错误信息
* @param pTag 日志tag
* @param pMsg 日志信息
*/
public static void i(String pTag, String pMsg){
Log.i(pTag, pMsg);
WriteLogToSDcard(E, pTag, pMsg);
}
/**
* 打印错误信息
* @param pTag 日志tag
* @param pMsg 日志信息
*/
public static void d(String pTag, String pMsg){
Log.d(pTag, pMsg);
WriteLogToSDcard(E, pTag, pMsg);
}
/**
* 打印错误信息
* @param pTag 日志tag
* @param pMsg 日志信息
*/
public static void w(String pTag, String pMsg){
Log.w(pTag, pMsg);
WriteLogToSDcard(E, pTag, pMsg);
}
/**
* 打印错误信息
* @param pTag 日志tag
* @param pMsg 日志信息
*/
public static void e(String pTag, String pMsg){
Log.e(pTag, pMsg);
WriteLogToSDcard(E, pTag, pMsg);
}
private static void WriteLogToSDcard(String type, String tag, String msg) {
initMLogger();
String log = generateLog(type, tag, msg);
if (null != mLogFile && null != log) {
OutputStream outstream = null;
try {
outstream = new FileOutputStream(mLogFile, true);
byte[] logByte = log.getBytes();
outstream.write(logByte);
outstream.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (outstream != null) {
outstream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Description: 根据日志的类型,便签和内容生成日志信息。
*
* @param type
* 日志的类型, 例如"W","D","I","E"
* @param tag
* 日志的标签
* @param msg
* 日志的内容
* @return 日志的信息
*
* <pre><p>ModifiedLog:</p>
*/
protected static String generateLog(String type, String tag, String msg) {
final String space = " ";
StringBuffer sb = new StringBuffer();
sb.append(generateTimeStamp(true)).append(space).append(type)
.append(space).append("-").append(space).append(tag)
.append(space).append(msg).append("\n");
return sb.toString();
}
/**
* Description: 生成文件的时间戳和日志的时间戳。
*
* @param isLogStamp
* true 日志的时间戳, false 文件的时间戳
* @return
*
* <pre><p>ModifiedLog:</p>
*/
public static String generateTimeStamp(Boolean isLogStamp) {
String pattern = null;
if (isLogStamp) {
pattern = "HH:mm:ss.SSS";
} else {
pattern = "yyyyMMdd";
}
return generateTimeStamp(pattern,new Date());
}
/**
* 获取时间戳信息
* @param pattern 时间戳形式
* @param date 需要格式化的时间
* @return
* <br/>Create By: yanglei Email:yanglei@dcloud.io at 2013-5-18 下午12:17:14
*/
public static String generateTimeStamp(String pattern, Date date){
SimpleDateFormat format = new SimpleDateFormat(pattern, Locale.ENGLISH);
format.applyPattern(pattern);
return format.format(date);
}
/**
* 记录未捕获异常到日志文件
*/
public static void catchUnExecption() {
init(mylogstwo);
Thread.setDefaultUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
public void uncaughtException(Thread thread, Throwable ex) {
DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss", Locale.CHINESE);
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
String errorlog = sw.toString();
File carashDirectory = new File(mylogstwo);
if (!carashDirectory.exists()) {
carashDirectory.mkdirs();
}
long timestamp = System.currentTimeMillis();
String time = formatter.format(new Date());
String name = "crash_" + timestamp + "_" + time + ".log";
File file = new File(carashDirectory.getAbsolutePath(), name);
try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(errorlog.getBytes());
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
}
public static void init(final String path){
try {
new Thread(new Runnable() {
@Override
public void run() {
File f = new File(path);
File fs[] = f.listFiles();
if(fs != null && fs.length > 3){
for(int i = 3; i < fs.length ; i++){
fs[i].delete();
}
}
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
}