- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
目标
收集 LogCat 的定期更新并将这些文本 block 保存(附加)到 SD 卡上的文件中
问题
Log 类不提供自特定时间戳以来的更新
可能的解决方案
我的计划是定期运行类似于以下代码:http://www.helloandroid.com/tutorials/reading-logs-programatically或 https://stackoverflow.com/a/9039352/550471
但是,有一个显着的区别:使用 -v time
参数来确保每一行都带有时间戳。
每次收集 LogCat 数据后,应用程序将存储最后一个 Log 条目的时间戳。下次收集 LogCat 数据时,应用程序将搜索文本以查找时间戳,然后将数据 block 保存到自指定时间戳以来添加到日志中的 sdcard。
可能的问题
如果 LogCat 数据的收集周期太短,则 CPU 会忙于处理大量“旧”数据。如果 Logcat 数据的收集时间过长,则可能会遗漏一些数据。
有没有更好的办法?
最佳答案
这就是我想出的 - 它在不卡住时效果很好。
您可能知道,Runtime.getRuntime().exec("")
很可能比 Jelly Bean 更早地在 Android 中引起 ANR。如果有人有克服 ANR 的解决方案,请分享。
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import android.os.Environment;
import android.util.Log;
/*
* For (compressed) buffer sizes, see: http://elinux.org/Android_Logging_System
* buffer:main = 64KB
* buffer:radio = 64KB
* buffer:system = 64KB
* buffer:event = 256KB
*
* NOTE: the 'command' must include "-d -v time" !!
* to switch buffers, use "-b <buffer>"
*/
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 int mStringCapacity;
private File mFileTarget = null;
// constructor
public LogCatReader(String command, int capacity) {
mLogCommand = command;
mStringCapacity = capacity;
}
// 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() + "/logfiles");
if (fLogFolder.exists() == false) {
if (fLogFolder.mkdirs() == false) {
Log.e("LogCatReader",
"Could not create "
+ fLogFolder.getAbsolutePath());
}
}
mFileTarget = new File(
Environment.getExternalStorageDirectory() + "/logfiles",
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 = 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
我用来仅查找更新的过程是捕获 logcat 最后一行的日期和时间,并在下次使用它作为搜索标记。
要使用这个类,下面是一个例子:
LogCatReader logcatPeriodicReader = new LogCatReader("logcat -b main -d -v time", 80 * 1024);//collect "main" buffer, exit after reading logcat
logcatPeriodicReader.startPeriodicLogReader(90, "log.txt");//read logcat every 90 secs
关于android - 定期 LogCat 轮询更新并保存到 SD 卡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11570458/
我是 .NET 编程的新手,目前我正在开发一个计算机健康监控系统,该系统目前处于初级阶段。我将使用 C# 2010 并使用 WMI 查询来查询计算机信息。 在进一步开发应用程序之前,我创建了一个迷你测
我正在考虑开发一个站点,服务器将定期抓取另一个站点,以便为我的数据库中的某些条目收集内容。我的问题如下... 如何让服务器及时执行抓取? 你能让它执行 php 还是你用什么语言来执行抓取? 有什么好的
使用 JobScheduler , 我设置了一个简单的 JobService如下: @TargetApi(21) public class SimpleJobService extends JobSe
我想定期从我的 JavaFX 应用程序执行任务。该任务从远程流中提取数据。 虽然我知道我可以按照下面的建议使用计时器: JavaFX periodic background task 我相信这应该能够
上下文 我们使用多个具有相同工作流程的 git 存储库,涉及两个分支,并且想知道如何最好地将一个提交“同步”到另一个。 简单地说,我们的 git 存储库包含: 长寿枝 两个分支: master(持续开
我正在编写一个需要以高精度和低频率进行后台位置更新的应用程序。解决方案似乎是一个后台 NSTimer 任务,它启动位置管理器的更新,然后立即关闭。之前有人问过这个问题: How do I get a
我有一个包含产品的商业系统。产品被分类并且还具有属性组。 我想同步/更新将来自商务系统的产品转换为在 Orchard 中定义的产品项目的内容类型,以便将数据从商务系统推送到 Orchard 内容项目。
所以我想做的只是按时间间隔更新 Activity 的 View ,比如每秒一次。在这个特定的例子中有几个按钮,我想要改变的只是它们的文本。我在这里阅读了很多解决相同问题的问题,但我似乎比其他人更容易陷
我有一个项目的 recyclerView,(除其他外)包含从 WS 检索的游戏持续时间的时间戳。为了显示比赛的持续时间,我必须获取当前时间戳,做垫子并将其转换为可读格式(例如:5 分钟 5 秒)。只有
这可能是一个愚蠢的问题,但经过几个小时的谷歌搜索后我找不到合适的答案.. 我们有一个在 cloudbees 上运行的业务关键型应用程序。源代码已正确备份,我们希望我们的数据库也有相同的备份。 Clou
我一直在观察我的 PostgreSQL 只读副本显示复制滞后的周期性延迟。延迟似乎会增加到 30-40 分钟,然后自动下降到 0。这与 CPU 利用率 相关,但它远未接近 CPU 限制。 读取流量来自
我已完成 Keepalived + MySQL(master - master)设置。 我将 MASTER 和 BACKUP 的优先级保持不变,因为我不希望它们开始频繁抖动(一次切换 VIP 就足够了
我目前有一个图像,如果本地内容可用,它会被 ping 以仅显示一个 div。 但我认为更好的实现方式是以 30 秒为间隔定期检查 ping,如果图像未成功 ping,则显示 #offline div。
我正在尝试编写一项服务,该服务将每隔 X 分钟尝试获取设备的 GPS 位置,并在后台运行和记录,即使应用程序未处于焦点状态也是如此。 那么,是时候创建服务了。 我创建了一个服务,设置了 locatio
我在这里阅读了很多问题,但无法弄清楚问题是什么。 我正在为 Android 编写现场服务应用程序。在其中一个 Activity (MyActivity.java) 中,我有两个按钮:开始和停止。 当现
假设我有这样的代码: git fetch && git diff origin origin/master --quiet || echo "untracked" 我希望它定期运行。比方说,每秒一次
我想建立与 HTTPS 服务器(例如 google.com)的连接并定期获取新鲜内容。 我编写了简单的 HTTP 客户端: public class AsyncLoader { private
我想定期进行 wifi 扫描(可能每 5 分钟一次)并将结果导出到一个 txt 文件,以便它显示 AP SSID 和 MAC 以及接收到的信号强度。我是 Android 开发的新手,所以我只知道基础知
当手机处于 sleep 模式时,Workmanager 不会向我的服务器发送坐标。异步任务被杀死? 我使用 Workmanager (PeriodicWorkRequest) 每 15 分钟获取一次坐
目标 收集 LogCat 的定期更新并将这些文本 block 保存(附加)到 SD 卡上的文件中 问题 Log 类不提供自特定时间戳以来的更新 可能的解决方案 我的计划是定期运行类似于以下代码:htt
我是一名优秀的程序员,十分优秀!