Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/201.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从LogCat获取AndroidRuntime异常并写入文件_Android_Android Logcat - Fatal编程技术网

从LogCat获取AndroidRuntime异常并写入文件

从LogCat获取AndroidRuntime异常并写入文件,android,android-logcat,Android,Android Logcat,当我导航到android sdk文件夹下的platform tools文件夹时,我能够执行一些操作,以便从LogCat 比如这个命令 adb logcat AndroidRuntime:E *:E 这将列出带有标签AndroidRuntime的消息。为了测试这一点,我删除了我的onResume方法中的super.onResume(),并且在win7中我的命令行窗口中的输出如下所示: 到目前为止还不错,这就是我想要记录的信息类型。我的应用程序中有一个如下所示的方法: public class

当我导航到android sdk文件夹下的
platform tools
文件夹时,我能够执行一些操作,以便从
LogCat

比如这个命令

adb logcat AndroidRuntime:E *:E
这将列出带有标签
AndroidRuntime
的消息。为了测试这一点,我删除了我的
onResume
方法中的
super.onResume()
,并且在win7中我的命令行窗口中的输出如下所示:

到目前为止还不错,这就是我想要记录的信息类型。我的应用程序中有一个如下所示的方法:

public class LogCatReader {

    // constants
    private static final String CR = "\r\n";
    private static final String END_OF_DATE_TIME = "): ";
    private static final int DEFAULT_SEARCH_START_INDEX = 0;

    // member variables
    private StringBuilder mLog;
    private LogThread mLogThread = null;
    private String mLastLogReadToken = "";
    private String mLogCommand = "";
    private String mProcess = "";
    private int mStringCapacity;
    private File mFileTarget = null;


    // constructor
    public LogCatReader(String command, int capacity, String process) {
        mLogCommand = command;
        mStringCapacity = capacity;
        mProcess = process;
    }

    // returns complete logcat buffer
    // note: takes about 1.5sec to finish
    synchronized public StringBuilder getLogComplete() {
        try {
            // capacity should be about 25% bigger than buffer size since the
            // buffer is compressed
            mLog = new StringBuilder(mStringCapacity);

            // command to capture log
            Process process = Runtime.getRuntime().exec(mLogCommand);
            BufferedReader bufferedReader = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));

            String line;
            while ((line = bufferedReader.readLine()) != null) {
                // append() is costly if capacity needs to be increased, be sure
                // to reserve enough in the first place
                mLog.append(line + CR);
            }
        } catch (IOException e) {
        }

        return mLog;
    }

    public String getLogUpdatesOnly() {
        String strReturn = "";

        StringBuilder sbLog = getLogComplete();

        try {
            int iStartindex = DEFAULT_SEARCH_START_INDEX;

            // if there exists a token from a previous search then use that
            if (mLastLogReadToken.length() > 0) {
                iStartindex = sbLog.indexOf(mLastLogReadToken);

                // if string not found then start at beginning
                if (iStartindex == -1) {
                    // start search at beginning of log
                    iStartindex = DEFAULT_SEARCH_START_INDEX;
                }
            }

            int iEndindex = sbLog.length();

            // if token is found then move index to the next line
            if (iStartindex > DEFAULT_SEARCH_START_INDEX) {
                iStartindex = sbLog.indexOf(CR, iStartindex);

                if (iStartindex != -1) {
                    iStartindex += CR.length();
                } else {
                    // return an empty string
                    iStartindex = iEndindex;
                }
            }

            // grab the data between the start and end indices
            strReturn = sbLog.substring(iStartindex, iEndindex);

            // grab date/time token for next search
            iStartindex = sbLog.lastIndexOf(END_OF_DATE_TIME);

            if (iStartindex != -1) {
                iEndindex = iStartindex;
                iStartindex = sbLog.lastIndexOf(CR, iEndindex);
                iStartindex += CR.length();

                if (iStartindex == -1) {
                    // read from beginning
                    iStartindex = 0;
                }

                mLastLogReadToken = sbLog.substring(iStartindex, iEndindex);
            }
        } catch (Exception e) {
            strReturn = "";
        }

        return strReturn;
    }

    public void startPeriodicLogCatReader(int timePeriod, String logfilename) {
        if (mLogThread == null) {
            mLogThread = new LogThread(timePeriod, logfilename);
            mLogThread.start();
        }
    }

    public void stopPeriodicLogCatReader() {
        if (mLogThread != null) {
            mLogThread.interrupt();
            mLogThread = null;
        }
    }

    private class LogThread extends Thread {
        private boolean mInterrupted;
        private int mTimePeriod;// in seconds
        private String mLogref;
        private BufferedWriter mBuffWriter = null;
        public boolean mPauseLogCollection = false;

        // constructor: logfilename is optional - pass null to not use
        public LogThread(int timePeriod, String logfilename) {
            mTimePeriod = timePeriod;

            if (logfilename != null) {
                File fLogFolder = new File(
                        Environment.getExternalStorageDirectory() + "/SteriaFITMobile/CoreLogging");
                if (fLogFolder.exists() == false) {
                    if (fLogFolder.mkdirs() == false) {
                        Log.e("LogCatReader",
                                "Could not create "
                                        + fLogFolder.getAbsolutePath());
                    }
                }

                mFileTarget = new File(
                        Environment.getExternalStorageDirectory() + "/SteriaFITMobile/CoreLogging",
                        logfilename);

                if (mFileTarget.exists() == false) {
                    try {
                        // file doesn't yet exist - create a fresh one !
                        mFileTarget.createNewFile();
                    } catch (IOException e) {
                        e.printStackTrace();
                        mFileTarget = null;
                    }
                }
            }
        }

        @Override
        public void interrupt() {
            mInterrupted = true;
            super.interrupt();
        }

        @Override
        public void run() {
            super.run();

            // initialization
            mInterrupted = false;

            // set up storage
            if (mFileTarget != null) {
                try {
                    mBuffWriter = new BufferedWriter(new FileWriter(
                            mFileTarget, true), 10240);
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            while ((mInterrupted == false) && (mBuffWriter != null)) {
                if (mPauseLogCollection == false) {
                    // read log updates
                    mLogref = mProcess + ": " + getLogUpdatesOnly();

                    // save log updates to file
                    try {
                        mBuffWriter.append(mLogref);
                        mBuffWriter.flush();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                if (!mInterrupted) {
                    try {
                        sleep(mTimePeriod * 1000);
                    } catch (InterruptedException e) {
                    }
                }
            }

            if (mBuffWriter != null) {
                try {
                    mBuffWriter.close();
                    mBuffWriter = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }// end of inner class
}// end of outer class
此方法采用三个参数:命令、容量和进程名(ActivityName)

此方法仅对
Runtime.getRuntime().exec
方法调用执行
命令参数
,并将其保存到SD卡上的
.txt
文件中。我的问题是命令

adb logcat AndroidRuntime:E *:E
可以在windows中使用,但不能在android手机上使用

我想要实现的是将这种类型的AndroidRuntime错误注销到SD卡上的日志文件中。我只对导致应用程序执行ANR的AndroidRuntime错误感兴趣

你们中的一些人可能建议我只创建自己的
Log.d、Log.e、Log.v
并将它们写入文件,但我不希望这样


有人能帮我吗

在命令中,我用于执行adb logcat>>文件夹/filename.txt
也许这种格式可以帮助您

,那么请解释一下为什么不使用自定义标记来输出logcat错误?似乎您不应该尝试重新创建控制盘,而应该使用现有的功能。在Android手机上,您应该只使用
logcat
命令。@Booger我不太确定是否有其他方法可以注销AndroidRuntime异常,而不必在
Try/Catch
中执行此操作。在我的例子中,我应该如何记录我忘记了
super.onPause
?也许你应该使用类似ACRA或bugsense的东西。com@Stan谢谢,我来看看ACRA