将logcat写入android中的xml文件

时间:2013-04-03 20:41:59

标签: android xml logcat motionevent

我在android中有点新手。我实际上想将logcat中显示的信息写入将保存在手机或计算机中的xml文件中。 这是我的应用程序显示的logcat,我想实际将压力和大小保存到压力/大小标签下的xml文件中。我怎么能这样做?

04-04 02:38:45.057: I/System.out(14272): Pressure 0.23333335
04-04 02:38:45.057: I/System.out(14272): Size 0.227451
04-04 02:38:46.217: I/System.out(14272): Pressure 0.20000002
04-04 02:38:46.217: I/System.out(14272): Size 0.22352943
04-04 02:38:46.702: I/System.out(14272): Pressure 0.26666668
04-04 02:38:46.702: I/System.out(14272): Size 0.27450982
04-04 02:38:47.222: I/System.out(14272): Pressure 0.23333335
04-04 02:38:47.222: I/System.out(14272): Size 0.26666668
04-04 02:38:47.657: I/System.out(14272): Pressure 0.23333335
04-04 02:38:47.657: I/System.out(14272): Size 0.23137257
04-04 02:38:48.087: I/System.out(14272): Pressure 0.16666667
04-04 02:38:48.087: I/System.out(14272): Size 0.16862746
04-04 02:38:48.502: I/System.out(14272): Pressure 0.3
04-04 02:38:48.502: I/System.out(14272): Size 0.3019608
04-04 02:38:48.907: I/System.out(14272): Pressure 0.23333335
04-04 02:38:48.907: I/System.out(14272): Size 0.2392157
04-04 02:38:49.267: I/System.out(14272): Pressure 0.23333335
04-04 02:38:49.267: I/System.out(14272): Size 0.25882354
04-04 02:38:49.732: I/System.out(14272): Pressure 0.20000002
04-04 02:38:49.732: I/System.out(14272): Size 0.21568629
04-04 02:38:50.137: I/System.out(14272): Pressure 0.23333335
04-04 02:38:50.137: I/System.out(14272): Size 0.2509804
04-04 02:38:50.647: I/System.out(14272): Pressure 0.23333335
04-04 02:38:50.647: I/System.out(14272): Size 0.24705884
04-04 02:38:51.042: I/System.out(14272): Pressure 0.16666667
04-04 02:38:51.042: I/System.out(14272): Size 0.18823531
04-04 02:39:10.822: I/System.out(14272): Pressure 0.23333335
04-04 02:39:10.822: I/System.out(14272): Size 0.22352943
04-04 02:39:10.987: I/System.out(14272): Pressure 0.26666668
04-04 02:39:10.987: I/System.out(14272): Size 0.26666668
04-04 02:39:11.177: I/System.out(14272): Pressure 0.23333335
04-04 02:39:11.177: I/System.out(14272): Size 0.26666668
04-04 02:39:11.482: I/System.out(14272): Pressure 0.20000002
04-04 02:39:11.482: I/System.out(14272): Size 0.227451
04-04 02:39:11.787: I/System.out(14272): Pressure 0.23333335
04-04 02:39:11.787: I/System.out(14272): Size 0.21960786
04-04 02:39:12.137: I/System.out(14272): Pressure 0.23333335
04-04 02:39:12.137: I/System.out(14272): Size 0.23529413
04-04 02:39:12.472: I/System.out(14272): Pressure 0.20000002
04-04 02:39:12.472: I/System.out(14272): Size 0.22352943
04-04 02:39:12.792: I/System.out(14272): Pressure 0.20000002
04-04 02:39:12.792: I/System.out(14272): Size 0.1764706
04-04 02:39:13.122: I/System.out(14272): Pressure 0.23333335
04-04 02:39:13.122: I/System.out(14272): Size 0.2627451
04-04 02:39:13.937: I/System.out(14272): Pressure 0.26666668
04-04 02:39:13.937: I/System.out(14272): Size 0.27450982
04-04 02:39:14.472: I/System.out(14272): Pressure 0.16666667
04-04 02:39:14.472: I/System.out(14272): Size 0.13725491
04-04 02:39:15.007: I/System.out(14272): Pressure 0.3
04-04 02:39:15.007: I/System.out(14272): Size 0.30588236
04-04 02:39:15.482: I/System.out(14272): Pressure 0.26666668
04-04 02:39:15.482: I/System.out(14272): Size 0.26666668
04-04 02:39:15.877: I/System.out(14272): Pressure 0.20000002
04-04 02:39:15.877: I/System.out(14272): Size 0.24705884
04-04 02:39:16.097: I/System.out(14272): Pressure 0.26666668
04-04 02:39:16.097: I/System.out(14272): Size 0.26666668

2 个答案:

答案 0 :(得分:0)

你不能在现代的android上。从4.2开始,应用程序无法再访问日志文件。你必须在电脑上这样做。

答案 1 :(得分:0)

使用在本地PC上运行的Perl脚本:

要求logcat

中的input_file.log输出
#!/usr/bin/perl

use strict; use warnings;
use XML::Simple;

my $h = {};

print "<?xml version='1.0' encoding='UTF-8'?>\n";

while (<>) {
    chomp;
    my @F = split;
    push @{ $h->{item} }, {
        date => "$F[0] $F[1]",
        system => $F[2],
        $F[3] => $F[4]
    }
}

my $xs = new XML::Simple;
my $xml = $xs->XMLout($h, NoAttr => 1, RootName => "tree");
print $xml;

用法

./script.pl < input_file.log

输出

<?xml version='1.0' encoding='UTF-8'?>
<tree>
  <item>
    <Pressure>0.23333335</Pressure>
    <date>04-04 02:38:45.057:</date>
    <system>I/System.out(14272):</system>
  </item>
  <item>
    <Size>0.227451</Size>
    <date>04-04 02:38:45.057:</date>
    <system>I/System.out(14272):</system>
  </item>
  <item>
    <Pressure>0.20000002</Pressure>
    <date>04-04 02:38:46.217:</date>
    <system>I/System.out(14272):</system>
    (...)