0

我在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
4

2 回答 2

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>
    (...)
于 2013-04-03T23:48:00.790 回答
0

你不能在现代安卓上。从 4.2 开始,应用程序无法再访问日志文件。您必须在 PC 上执行此操作。

于 2013-04-03T20:52:07.497 回答