gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 14:21:46 25 4
gpt4 key购买 nike

当我导航到 android-sdk 文件夹下的 platform-tools 文件夹时,我可以执行一些操作,以便从 LogCat 获取输出。 p>

比如这个命令

adb logcat AndroidRuntime:E *:E

这将列出带有标签 AndroidRuntime 的消息。为了对此进行测试,我删除了 onResume 方法中的 super.onResume(),在 win7 的命令行窗口中,输出看起来像这样:

enter image description here

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

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 方法调用,并将其保存到.txt SD卡上的文件。我的问题是命令

adb logcat AndroidRuntime:E *:E

适用于 Windows,但不适用于 Android 手机。

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

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

有人能帮帮我吗?

最佳答案

在我用来执行 adb logcat 的命令中 >> folder/filename.txt也许这种格式可以帮助你

关于android - 从 LogCat 获取 AndroidRuntime 异常并写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16237457/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com