Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 問題小結(22)--代碼實現logcat保存

問題小結(22)--代碼實現logcat保存

編輯:關於Android編程

讓logcat實時的輸出保存到某個目錄下,我們基本都知道可以通過adb命令來實現,那麼怎麼通過代碼實現呢?這是本篇博文要說的內容。

我這裡是把log保存到了sdcard的根目錄下,不羅嗦了,上代碼。

[java] 
import java.io.File; 
import java.text.SimpleDateFormat; 
import java.util.ArrayList; 
import java.util.Date; 
import java.util.List; 
 
import android.os.Environment; 
import android.util.Log; 
 
public class WriteLog { 
 
    private static final String TAG = "Log"; 
    // LogWrite  
    private String LOG_PATH_SDCARD_DIR; // log file path in sdcard  
 
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");// log  
                                                                                // name  
 
    private Process process; 
 
    private static WriteLog mLogDemo = null; 
 
    private WriteLog() { 
        init(); 
    } 
 
    public static WriteLog getInstance() { 
        if (mLogDemo == null) { 
            mLogDemo = new WriteLog(); 
        } 
 
        return mLogDemo; 
    } 
 
    public void startLog() { 
        createLog(); 
 
    } 
 
    public void stopLog() { 
        if (process != null) { 
            process.destroy(); 
        } 
 
    } 
 
    private void init() { 
 
        LOG_PATH_SDCARD_DIR = Environment.getExternalStorageDirectory() 
                .getAbsolutePath(); 
        createLogDir(); 
        Log.i(TAG, "Log onCreate"); 
    } 
 
    /**
     * write the log
     */ 
    public void createLog() { 
        // TODOWriteLog  
 
        List<String> commandList = new ArrayList<String>(); 
        commandList.add("logcat"); 
        commandList.add("-f"); 
        commandList.add(getLogPath()); 
        commandList.add("-v"); 
        commandList.add("time"); 
 
        try { 
            process = Runtime.getRuntime().exec( 
                    commandList.toArray(new String[commandList.size()])); 
        } catch (Exception e) { 
            Log.e(TAG,e.getMessage(), e); 
        } 
    } 
 
    /**
     * the path of the log file
     * 
     * @return
     */ 
    public String getLogPath() { 
        createLogDir(); 
        String logFileName = sdf.format(new Date()) + ".log";// name  
 
        Log.d(TAG, "Log stored in SDcard, the path is:" + LOG_PATH_SDCARD_DIR 
                + File.separator + logFileName); 
        return LOG_PATH_SDCARD_DIR + File.separator + logFileName; 
 
    } 
 
    /**
     * make the dir
     */ 
    private void createLogDir() { 
        File file; 
        boolean mkOk; 
 
        if (Environment.getExternalStorageState().equals( 
                Environment.MEDIA_MOUNTED)) { 
            file = new File(LOG_PATH_SDCARD_DIR); 
            if (!file.isDirectory()) { 
                mkOk = file.mkdirs(); 
                if (!mkOk) { 
                    return; 
                } 
            } 
        } 
    } 
 

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import android.os.Environment;
import android.util.Log;

public class WriteLog {

 private static final String TAG = "Log";
 // LogWrite
 private String LOG_PATH_SDCARD_DIR; // log file path in sdcard

 private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");// log
                    // name

 private Process process;

 private static WriteLog mLogDemo = null;

 private WriteLog() {
  init();
 }

 public static WriteLog getInstance() {
  if (mLogDemo == null) {
   mLogDemo = new WriteLog();
  }

  return mLogDemo;
 }

 public void startLog() {
  createLog();

 }

 public void stopLog() {
  if (process != null) {
   process.destroy();
  }

 }

 private void init() {

  LOG_PATH_SDCARD_DIR = Environment.getExternalStorageDirectory()
    .getAbsolutePath();
  createLogDir();
  Log.i(TAG, "Log onCreate");
 }

 /**
  * write the log
  */
 public void createLog() {
  // TODOWriteLog

  List<String> commandList = new ArrayList<String>();
  commandList.add("logcat");
  commandList.add("-f");
  commandList.add(getLogPath());
  commandList.add("-v");
  commandList.add("time");

  try {
   process = Runtime.getRuntime().exec(
     commandList.toArray(new String[commandList.size()]));
  } catch (Exception e) {
   Log.e(TAG,e.getMessage(), e);
  }
 }

 /**
  * the path of the log file
  *
  * @return
  */
 public String getLogPath() {
  createLogDir();
  String logFileName = sdf.format(new Date()) + ".log";// name

  Log.d(TAG, "Log stored in SDcard, the path is:" + LOG_PATH_SDCARD_DIR
    + File.separator + logFileName);
  return LOG_PATH_SDCARD_DIR + File.separator + logFileName;

 }

 /**
  * make the dir
  */
 private void createLogDir() {
  File file;
  boolean mkOk;

  if (Environment.getExternalStorageState().equals(
    Environment.MEDIA_MOUNTED)) {
   file = new File(LOG_PATH_SDCARD_DIR);
   if (!file.isDirectory()) {
    mkOk = file.mkdirs();
    if (!mkOk) {
     return;
    }
   }
  }
 }

}
使用的是單例模式,使用方法也很簡單,如下:

[java]
WriteLog log = WriteLog.getInstance(); 
log.startLog();//開始保存log 

WriteLog log = WriteLog.getInstance();
log.startLog();//開始保存log在你調起以後它就可以實時的去保存log至我們指定的路徑,如果需要停止log的保存輸出就需要調用stopLog()方法。

 

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved