我正在尝试为我的项目自动执行一项任务,我必须解析一个日志文件并搜索特定的pid。如果那个pid遇到超过50次,那么我必须打印我的消息。
我的代码:
#!/usr/bin/perl -w
use strict;
open(my $fh,"<","UIlogs.txt") or die "can not open the file $!";
my @lines = <$fh>;
close($fh);
my $count=0;
for(my $i=0; $i<=$#lines;$i++)
{
if($lines[$i] =~ m/Input event injection from pid (\d+) failed/gi)
{
#print"UI freez\n";
print"$1\n";
}
}
日志:
07-10 16:36:43.632 784 784 W InputManager: Input event injection from pid 2019 failed.
07-10 16:36:43.632 784 795 W InputManager: Input event injection from pid 2019 failed.
07-10 16:36:45.114 2041 2041 D AndroidRuntime:
07-10 16:36:45.114 2041 2041 D AndroidRuntime: >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
07-10 16:36:45.114 2041 2041 D AndroidRuntime: CheckJNI is OFF
07-10 16:36:45.124 2041 2041 D dalvikvm: Trying to load lib libjavacore.so 0x0
07-10 16:36:45.124 2041 2041 D dalvikvm: Added shared lib libjavacore.so 0x0
07-10 16:36:45.134 2041 2041 D dalvikvm: Trying to load lib libnativehelper.so 0x0
07-10 16:36:45.134 2041 2041 D dalvikvm: Added shared lib libnativehelper.so 0x0
07-10 16:36:45.244 2041 2041 D AndroidRuntime: Calling main entry com.android.commands.input.Input
07-10 16:36:45.244 2041 2041 I Input : injectKeyEvent: KeyEvent { action=ACTION_DOWN, keyCode=KEYCODE_DPAD_CENTER, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=166218, downTime=166218, deviceId=-1, source=0x101 }
07-10 16:36:45.254 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:45.254 2041 2041 I Input : injectKeyEvent: KeyEvent { action=ACTION_UP, keyCode=KEYCODE_DPAD_CENTER, scanCode=0, metaState=0, flags=0x0, repeatCount=0, eventTime=166218, downTime=166218, deviceId=-1, source=0x101 }
07-10 16:36:45.254 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:45.254 2041 2041 D AndroidRuntime: Shutting down VM
07-10 16:36:45.254 2041 2050 D dalvikvm: GC_CONCURRENT freed 94K, 17% free 479K/576K, paused 0ms+0ms, total 3ms
07-10 16:36:45.254 784 1016 W InputManager: Input event injection from pid 2041 failed.
07-10 16:36:45.254 784 1083 W InputManager: Input event injection from pid 2041 failed.
07-10 16:36:52.932 2061 2061 D AndroidRuntime:
07-10 16:36:52.932 2061 2061 D AndroidRuntime: >>>>>> AndroidRuntime START com.android.internal.os.RuntimeInit <<<<<<
07-10 16:36:52.932 2061 2061 D AndroidRuntime: CheckJNI is OFF
07-10 16:36:52.942 2061 2061 D dalvikvm: Trying to load lib libjavacore.so 0x0
07-10 16:36:52.942 2061 2061 D dalvikvm: Added shared lib libjavacore.so 0x0
07-10 16:36:52.952 2061 2061 D dalvikvm: Trying to load lib libnativehelper.so 0x0
07-10 16:36:52.952 2061 2061 D dalvikvm: Added shared lib libnativehelper.so 0x0
07-10 16:36:53.082 2061 2061 D AndroidRuntime: Calling main entry com.android.commands.monkey.Monkey
07-10 16:36:53.092 2061 2061 D dalvikvm: Note: class Landroid/app/ActivityManagerNative; has 163 unimplemented (abstract) methods
07-10 16:36:53.112 784 1076 I ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.contacts/.activities.PeopleActivity} from pid 2061
07-10 16:36:53.122 784 1076 I ActivityManager: Start proc com.android.contacts for activity com.android.contacts/.activities.PeopleActivity: pid=2077 uid=10000 gids={50000, 3003, 1015, 1028}
07-10 16:36:53.212 2077 2077 I ContactsIntentResolver: Called with action: android.intent.action.MAIN
07-10 16:36:53.232 2077 2092 I AccountTypeManager: Loaded meta-data for 1 account types, 0 accounts in 19ms(wall) 5ms(cpu)
07-10 16:36:53.272 2077 2077 I ContactPhotoManager: Cache adj: 1.0
07-10 16:36:53.463 2077 2077 D libEGL : loaded /vendor/lib/egl/libEGL_adreno.so
07-10 16:36:53.463 2077 2077 D libEGL : loaded /vendor/lib/egl/libGLESv1_CM_adreno.so
07-10 16:36:53.463 2077 2077 D libEGL : loaded /vendor/lib/egl/libGLESv2_adreno.so
07-10 16:36:53.473 2077 2077 I Adreno-EGL: <qeglDrvAPI_eglInitialize:316>: EGL 1.4 QUALCOMM build: AU_LINUX_ANDROID_JB_2.6.04.02.02.109.046_msm8960_JB_2.6_CL3925444_release_AU (CL3925444)
07-10 16:36:53.473 2077 2077 I Adreno-EGL: OpenGL ES Shader Compiler Version: 17.01.08
07-10 16:36:53.473 2077 2077 I Adreno-EGL: Build Date: 07/09/13 Tue
07-10 16:36:53.473 2077 2077 I Adreno-EGL: Local Branch: mybranch1230998
07-10 16:36:53.473 2077 2077 I Adreno-EGL: Remote Branch: quic/jb_3.1
07-10 16:36:53.473 2077 2077 I Adreno-EGL: Local Patches: NONE
07-10 16:36:53.473 2077 2077 I Adreno-EGL: Reconstruct Branch: AU_LINUX_ANDROID_JB_2.6.04.02.02.109.046 + e363aa2 + 76767df + NOTHING
07-10 16:36:53.523 2077 2077 D OpenGLRenderer: Enabling debug mode 0
07-10 16:36:53.623 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:53.623 784 857 I ActivityManager: Displayed com.android.contacts/.activities.PeopleActivity: +511ms
07-10 16:36:53.623 784 1016 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:53.633 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:53.633 784 1103 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:54.133 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:54.133 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:54.133 784 1083 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:54.133 784 1016 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:54.634 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:54.634 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:54.634 784 796 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:54.634 784 795 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:55.144 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:55.144 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:55.144 784 1103 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:55.144 784 1191 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:55.645 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:55.645 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:55.645 784 1016 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:55.655 784 1083 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:56.156 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:56.156 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:56.156 784 784 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:56.156 784 795 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:56.666 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:56.666 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:56.666 784 796 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:56.666 784 1076 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:57.167 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:57.167 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:57.167 784 1190 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:57.167 784 1191 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:57.677 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:57.677 784 1103 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:57.677 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:57.677 784 1083 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:58.188 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:58.188 784 1016 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:58.188 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:58.188 784 795 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:58.698 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:58.698 784 784 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:58.698 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:58.698 784 796 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:59.039 784 910 W QCNEJ : |CORE| UNKOWN Unsolicited Event 6
07-10 16:36:59.209 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:59.209 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:59.209 784 1190 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:59.209 784 1103 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:59.719 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:59.719 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:36:59.719 784 1083 W InputManager: Input event injection from pid 2061 failed.
07-10 16:36:59.719 784 795 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:00.230 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:00.230 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:00.230 784 1191 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:00.230 784 1076 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:00.740 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:00.740 784 1103 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:00.740 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:00.740 784 1190 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:01.251 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:01.251 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:01.251 784 1016 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:01.251 784 784 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:01.762 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:01.762 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:01.762 784 796 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:01.762 784 1076 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:02.272 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:02.272 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:02.272 784 1191 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:02.272 784 1103 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:02.773 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:02.773 784 886 I InputDispatcher: Dropped event because input dispatch is disabled.
07-10 16:37:02.773 784 1190 W InputManager: Input event injection from pid 2061 failed.
07-10 16:37:02.783 784 795 W InputManager: Input event injection from pid 2061 failed.
使用我的代码我无法处理其他pid,即如何仅计算日志中存在的pid超过50次。如果UI freez将发生,那么只有一个pid会超过此计数。
答案 0 :(得分:3)
哈希对于跟踪出现 xxxxx 的数量非常有用。
e.g。
if($lines[$i] =~ m/Input event injection from pid (\d+) failed/gi)
{
my $foundpid = $1;
print "Found pid:$1\n";
$pids{$foundpid} += 1;
if ( $pids{$foundpid} > 100 ) {
print "PID $foundpid is extra naughty today!\n";
}
}
通常,您只想为超出限制的每个PID打印一条警告消息,因此我们会将散列的处理保存到最后,例如:
my %pids = (); # initialise hash
for ( ... ) { # process lines
if ( ... ) { # match line
$pids{$foundpid} += 1;
}
}
# now scan our hash of pids and filter those that failed the condition
foreach my $pid ( sort { $a <=> $b keys %pids ) ) {
if ( $pids{$pid} > 100 ) {
print( "Bad PID $pid was seen " . $pids{$pid} . " times!\n" );
}
}
稍后如果你想让你的代码更简洁,你可以重写最后一部分:
my @failedpids = grep { $pids{$_} > 100 } keys %pids;
print( "Failed PIDs were: " );
print( join( ", ", sort { $a <=> $b } @failedpids ) );
print( "\n" );
答案 1 :(得分:1)
这实际上只是一个简单的计算练习。您使用正则表达式捕获字符串,并使用哈希计算其频率:
perl -nlwe '$a{$1}++ if /Input event injection from pid (\d+)/
END { for (keys %a) { print qq($_ -> $a{$_}) } }' inject.log
我从你的输入中得到了这个输出:
2061 -> 38
2019 -> 2
2041 -> 2
作为程序文件,它看起来像这样:
while (<>) {
$a{$1}++ if /Input event injection from pid (\d+)/;
}
for my $key (keys %a) {
print "$key -> $a{$key}\n";
}
<>
菱形运算符读取STDIN,或将参数视为要用于输入的文件名。 $a{$1}++
使用从散列中的正则表达式捕获的字符串,++
递增其值。
当然,我为所有元素选择了一个简单的打印,但你可以简单地添加一个超过50的值的检查:
next if $a{$key} < 50;
关于您的代码的一些注意事项:
for my $line (@lines)
while (<$fh>)
- 这将在每次迭代中读取一行$_
(或您选择的变量,例如while (my $foo = <$fh>)
),直到结束档案eof
。use strict
和警告。通常,use warnings
比在shebang上使用-w
更受青睐,因为它更具可读性,而且它是一个词汇编纂,而不是全局。