Back to project page FrameLite.
The source code is released under:
GNU General Public License
If you think the Android project FrameLite listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.miku.framelite.utils; // w w w . j ava2s . c o m import java.io.File; import java.io.FileOutputStream; import java.io.FilenameFilter; import java.io.PrintWriter; import java.io.StringWriter; import java.net.UnknownHostException; import java.util.Date; import android.text.TextUtils; public class Log { private static final String TAG = Log.class.getSimpleName(); public static final int VERBOSE = android.util.Log.VERBOSE; public static final int DEBUG = android.util.Log.DEBUG; public static final int INFO = android.util.Log.INFO; public static final int WARN = android.util.Log.WARN; public static final int ERROR = android.util.Log.ERROR; private static int mLogLevel=INFO; private static String LOG_DIR; public static void setLogDir(String dir){ LOG_DIR=dir; } public static void setLogLevel(int level){ if(level<VERBOSE){ level=VERBOSE; } if(level>ERROR){ level=ERROR; } mLogLevel=level; } private static boolean isLogable(int level){ return level>=mLogLevel; } public static int v(String tag, String msg) { return v(tag,msg,null); } public static int v(String tag, String msg, Throwable tr) { if(isLogable(VERBOSE)){ return android.util.Log.v(tag, msg+(tr==null?"":("\n"+getStackTraceString(tr)))); } return 0; } public static int fv(String tag, String msg) { return fv(tag,msg,null); } public static int fv(String tag, String msg, Throwable tr) { writeLog(tag, msg, tr); if(isLogable(VERBOSE)){ return android.util.Log.v(tag, msg+(tr==null?"":("\n"+getStackTraceString(tr)))); } return 0; } public static int d(String tag, String msg) { return d(tag,msg,null); } public static int d(String tag, String msg, Throwable tr) { if(isLogable(DEBUG)){ return android.util.Log.d(tag, msg+(tr==null?"":("\n"+getStackTraceString(tr)))); } return 0; } public static int fd(String tag, String msg) { return fd(tag,msg,null); } public static int fd(String tag, String msg, Throwable tr) { writeLog(tag, msg, tr); if(isLogable(DEBUG)){ return android.util.Log.d(tag, msg+(tr==null?"":("\n"+getStackTraceString(tr)))); } return 0; } public static int i(String tag, String msg) { return i(tag,msg,null); } public static int i(String tag, String msg, Throwable tr) { if(isLogable(INFO)){ return android.util.Log.i(tag, msg+(tr==null?"":("\n"+getStackTraceString(tr)))); } return 0; } public static int fi(String tag, String msg) { return fi(tag,msg,null); } public static int fi(String tag, String msg, Throwable tr) { writeLog(tag, msg, tr); if(isLogable(INFO)){ return android.util.Log.i(tag, msg+(tr==null?"":("\n"+getStackTraceString(tr)))); } return 0; } public static int w(String tag, String msg) { return w(tag,msg,null); } public static int w(String tag, String msg, Throwable tr) { if(isLogable(WARN)){ return android.util.Log.w(tag, msg+(tr==null?"":("\n"+getStackTraceString(tr)))); } return 0; } public static int fw(String tag, String msg) { return fw(tag,msg,null); } public static int fw(String tag, String msg, Throwable tr) { writeLog(tag, msg, tr); if(isLogable(WARN)){ return android.util.Log.w(tag, msg+(tr==null?"":("\n"+getStackTraceString(tr)))); } return 0; } public static int e(String tag, String msg) { return e(tag,msg,null); } public static int e(String tag, String msg, Throwable tr) { if(isLogable(ERROR)){ return android.util.Log.e(tag, msg+(tr==null?"":("\n"+getStackTraceString(tr)))); } return 0; } public static int fe(String tag, String msg) { return e(tag,msg,null); } public static int fe(String tag, String msg, Throwable tr) { writeLog(tag, msg, tr); if(isLogable(ERROR)){ return android.util.Log.e(tag, msg+(tr==null?"":("\n"+getStackTraceString(tr)))); } return 0; } public static String getStackTraceString(Throwable tr) { if (tr == null) { return ""; } Throwable t = tr; while (t != null) { if (t instanceof UnknownHostException) { return ""; } t = t.getCause(); } StringWriter sw = new StringWriter(); PrintWriter pw = new PrintWriter(sw); tr.printStackTrace(pw); return sw.toString(); } public static void writeLog(String tag,String msg){ writeLog(null,tag,msg,null); } public static void writeLog(String tag,String msg,Throwable tr){ writeLog(null,tag,msg,tr); } public static void writeLog(Thread thread,String tag,String msg,Throwable tr){ try { File logDir=new File(LOG_DIR); if(!logDir.exists()){ logDir.mkdirs(); } File logFile=new File(logDir,DateUtils.format(new Date(), DateUtils.YMD)+"_error.txt"); FileOutputStream ost = new FileOutputStream(logFile, true); String time = DateUtils.format(new Date(), DateUtils.YMD_DASH_WITH_TIME); StringBuilder block =new StringBuilder("------" + time + "------\r\n"); if(thread==null){ thread=Thread.currentThread(); } block.append("------pid:" + android.os.Process.myPid() + "--tid:" + thread.getId() + "------\r\n"); if(!TextUtils.isEmpty(tag)){ block.append(tag).append("----"); } if(!TextUtils.isEmpty(msg)){ block.append(msg); }else{ block.append("\r\n"); } // ????? ost.write(block.toString().getBytes()); ost.write(getStackTraceString(tr).getBytes()); ost.write("\r\n".getBytes()); ost.flush(); ost.close(); } catch (Exception e) { Log.e(TAG, "an error occured while writing report file...", e); e.printStackTrace(); } } /** * ??nday??????? * @param nday ???????? */ public static void clearLogFile(final int nday){ if(nday>0){ File dir=new File(LOG_DIR); if(dir.exists()&&dir.isDirectory()){ File[] fileToDelete=dir.listFiles(new FilenameFilter(){ int dayBefore=Integer.parseInt(DateUtils.format(DateUtils.dayOffset(new Date(), -nday), DateUtils.YMD)); @Override public boolean accept(File dir, String filename) { try { String[] fileStrs=filename.split("_"); int day=Integer.parseInt(fileStrs[0]); if(day>dayBefore){ return false; } } catch (Exception e) { e.printStackTrace(); } return true; }}); if(fileToDelete!=null){ for(File f:fileToDelete){ f.delete(); } } } } } }