Back to project page Avatar.
The source code is released under:
GNU General Public License
If you think the Android project Avatar 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.syw.avatar.util; /* w w w. ja v a 2s . co m*/ import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import android.os.Environment; public class SLog { // ???????(???????????????????????) public static final char SHOW_VERBOSE_LOG = 0x01; public static final char SHOW_DEBUG_LOG = 0x02; public static final char SHOW_INFO_LOG = 0x04; public static final char SHOW_WARN_LOG = 0x08; public static final char SHOW_ERROR_LOG = 0x10; // ?????????????? LogCat ????? public static char m_cLogCatShowLogType = SHOW_VERBOSE_LOG | SHOW_DEBUG_LOG | SHOW_INFO_LOG | SHOW_WARN_LOG | SHOW_ERROR_LOG; // ???????????????? ???? ?????? public static char m_cFileSaveLogType = 0x00; // ??????????????????????????????????? // public static char m_cFileSaveLogType = SHOW_VERBOSE_LOG | // SHOW_DEBUG_LOG | // SHOW_INFO_LOG | // SHOW_WARN_LOG | // SHOW_ERROR_LOG; // ???????????? public static String m_strLogFolderPath = ""; private static void SaveLog2File(String TAG, String strMsg) { FileWriter objFilerWriter = null; BufferedWriter objBufferedWriter = null; do // ????????????????????? { String state = Environment.getExternalStorageState(); // ??? SD ??? if (true != Environment.MEDIA_MOUNTED.equals(state)) { android.util.Log.d(TAG, "Not mount SD card!"); break; } // SD ????????? if (true == Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { android.util.Log.d(TAG, "Not allow write SD card!"); break; } // ???????? SD ????????????????????????????????? // ????????????????????????????? SD ???????? custom_android_log ??? if (true == m_strLogFolderPath.trim().equals("")) { String strSaveLogPath = Environment .getExternalStorageDirectory() + "/imuse/log"; File fileSaveLogFolderPath = new File(strSaveLogPath); // ?????????????????????? if (true != fileSaveLogFolderPath.exists()) { fileSaveLogFolderPath.mkdirs(); } // ????????????????????????????????? if (true != fileSaveLogFolderPath.exists()) { android.util.Log.d(TAG, "Create log folder failed!"); break; } // ????????????????????????????? m_strLogFolderPath = strSaveLogPath; } // ?????????????????? String strDateTimeFileName = new SimpleDateFormat("yyyy-MM-dd") .format(new Date()); File fileLogFilePath = new File(m_strLogFolderPath, strDateTimeFileName + ".log"); // ??????????????? if (true != fileLogFilePath.exists()) { try { fileLogFilePath.createNewFile(); } catch (IOException e) { e.printStackTrace(); break; } } // ??????????????????????????? if (true != fileLogFilePath.exists()) { android.util.Log.d(TAG, "Create log file failed!"); break; } try { objFilerWriter = new FileWriter(fileLogFilePath, // true); // ?????? } catch (IOException e1) { android.util.Log.d(TAG, "New FileWriter Instance failed"); e1.printStackTrace(); break; } objBufferedWriter = new BufferedWriter(objFilerWriter); // ?????????????????? String strDateTimeLogHead = new SimpleDateFormat( "yyyy-MM-dd_HH:mm:ss").format(new Date()); // ?????????????????????? strMsg = TAG + " " + strDateTimeLogHead + " " + strMsg + "\n\n"; try { objBufferedWriter.write(strMsg); objBufferedWriter.flush(); } catch (IOException e) { android.util.Log .d(TAG, "objBufferedWriter.write or objBufferedWriter.flush failed"); e.printStackTrace(); } } while (false); if (null != objBufferedWriter) { try { objBufferedWriter.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != objFilerWriter) { try { objFilerWriter.close(); } catch (IOException e) { e.printStackTrace(); } } } public static void v(String TAG, String msg) { // ???????????????????????????? if ((null == msg) || (true == msg.trim().equals(""))) { return; } String strMsg = "\nfile: " + new Throwable().getStackTrace()[1].getFileName() + " \nclass: " + new Throwable().getStackTrace()[1].getClassName() + " \nmethod: " + new Throwable().getStackTrace()[1].getMethodName() + " \nline: " + new Throwable().getStackTrace()[1].getLineNumber() + " \n" + msg + "\n\n"; if (0 != (SHOW_VERBOSE_LOG & m_cLogCatShowLogType)) { android.util.Log.v(TAG, msg); } if (0 != (SHOW_VERBOSE_LOG & m_cFileSaveLogType)) { SaveLog2File(TAG, strMsg); } } public static void d(String TAG, String msg) { // ???????????????????????????? if ((null == msg) || (true == msg.trim().equals(""))) { return; } // String strMsg = "\nfile: " // + new Throwable().getStackTrace()[1].getFileName() // + " \nclass: " // + new Throwable().getStackTrace()[1].getClassName() // + " \nmethod: " // + new Throwable().getStackTrace()[1].getMethodName() // + " \nline: " // + new Throwable().getStackTrace()[1].getLineNumber() + " \n" // + msg; String strMsg = new Throwable().getStackTrace()[1].getFileName() + " [M:" + new Throwable().getStackTrace()[1].getMethodName() + "]" + " [L:" + new Throwable().getStackTrace()[1].getLineNumber() + "]\n" + msg; if (0 != (SHOW_DEBUG_LOG & m_cLogCatShowLogType)) { android.util.Log.d(TAG, strMsg); } if (0 != (SHOW_DEBUG_LOG & m_cFileSaveLogType)) { SaveLog2File(TAG, strMsg); } } public static void i(String TAG, String msg) { // ???????????????????????????? if ((null == msg) || (true == msg.trim().equals(""))) { return; } String strMsg = "\nfile: " + new Throwable().getStackTrace()[1].getFileName() + " \nclass: " + new Throwable().getStackTrace()[1].getClassName() + " \nmethod: " + new Throwable().getStackTrace()[1].getMethodName() + " \nline: " + new Throwable().getStackTrace()[1].getLineNumber() + " \n" + msg; if (0 != (SHOW_INFO_LOG & m_cLogCatShowLogType)) { android.util.Log.i(TAG, msg); } if (0 != (SHOW_INFO_LOG & m_cFileSaveLogType)) { SaveLog2File(TAG, strMsg); } } public static void w(String TAG, String msg) { // ???????????????????????????? if ((null == msg) || (true == msg.trim().equals(""))) { return; } String strMsg = "\nfile: " + new Throwable().getStackTrace()[1].getFileName() + " \nclass: " + new Throwable().getStackTrace()[1].getClassName() + " \nmethod: " + new Throwable().getStackTrace()[1].getMethodName() + " \nline: " + new Throwable().getStackTrace()[1].getLineNumber() + " \n" + msg; if (0 != (SHOW_WARN_LOG & m_cLogCatShowLogType)) { android.util.Log.w(TAG, msg); } if (0 != (SHOW_WARN_LOG & m_cFileSaveLogType)) { SaveLog2File(TAG, strMsg); } } public static void e(String TAG, String msg) { // ???????????????????????????? if ((null == msg) || (true == msg.trim().equals(""))) { return; } String strMsg = "\nfile: " + new Throwable().getStackTrace()[1].getFileName() + " \nclass: " + new Throwable().getStackTrace()[1].getClassName() + " \nmethod: " + new Throwable().getStackTrace()[1].getMethodName() + " \nline: " + new Throwable().getStackTrace()[1].getLineNumber() + " \n" + msg; if (0 != (SHOW_ERROR_LOG & m_cLogCatShowLogType)) { android.util.Log.e(TAG, msg); } if (0 != (SHOW_ERROR_LOG & m_cFileSaveLogType)) { SaveLog2File(TAG, strMsg); } } }