解析不规则活动的文件,请求对方法的反馈

时间:2011-01-14 22:04:48

标签: parsing

您好 我想就如何解决解析此文件的任务获得一些想法和反馈。 我基本上需要“开始测试”和“完成测试”之间每个子测试的名称和状态 问题在于,有时没有“完成”,并且由于测试崩溃而直接启动。

我开始逐行试图解决所有场景,但逻辑开始变得复杂和复杂,所以我不禁想到必须有一个更聪明的方法来解决这个问题。 任何想法,代码,伪代码将不胜感激。

    myfile.log
****Starting test of test1
Message: Total Tests: 5
Log 23w4arg
Log 3958300
Message: PASS : test1SubTest1(): Pass
Log 23w4arg
Log efasf0
Message: PASS : test1SubTest2(): Pass
Log eafsarg
Log 3asf0
Message: FAIL: test1SubTest3(): Pass
Log 23asffg
Log 3fsaf00
Message: PASS : test1SubTest4(): Pass
Log 23fsafg
Log 3ag300
Message: PASS : test1SubTest5(): Pass
Message: Totals: 4 passed, 1 failed, 0 skipped
****Finished testing test1
Log 3ag3aw0
Log 3ag340
****Starting test of test2
Message: Total Tests: 3
Log 23w4arg
Log 3958300
Message: PASS : test2SubTest1(): Pass
Log 23w4arg
Log efasf0
Message: PASS : test2SubTest2(): Pass
Log 3ag340
****Starting test of test3
Log 234rg
Log 394w300
Message: PASS : test3SubTest1(): Pass
Log 23w4gdg
Log esdsf0
Message: PASS : test3SubTest2(): Pass
Log eafjhg
Log 3hj0
Message: FAIL: test3SubTest3(): Pass
Message: Totals: 2 passed, 0 failed, 0 skipped
****Finished testing test3

1 个答案:

答案 0 :(得分:2)

    <?php

$file = "log.txt";
$fh = fopen($file, "r");
$contents = fread($fh, filesize($file));
$lines = explode("\n", $contents);
foreach($lines as $l) {
        $startStr = "****Starting test of ";
        $l = trim($l);
        if(stristr($l, $startStr)) {
                $curTest = str_replace($startStr, "", $l);
                echo "$curTest info:\n";
        }
        if(substr($l, 0, 7)=='Message') {
                if(stristr($l, "Total Tests")) {
                        echo "$l\n";
                }
                else {
                        // Test result
                        $infoArr = explode(":", $l);
                        echo "$infoArr[2] completed with result $infoArr[1]\n";
                }
        }

}

?>


Produces:
test1 info:
Message: Total Tests: 5
 test1SubTest1() completed with result  PASS 
 test1SubTest2() completed with result  PASS 
 test1SubTest3() completed with result  FAIL
 test1SubTest4() completed with result  PASS 
 test1SubTest5() completed with result  PASS 
 4 passed, 1 failed, 0 skipped completed with result  Totals
test2 info:
Message: Total Tests: 3
 test2SubTest1() completed with result  PASS 
 test2SubTest2() completed with result  PASS 
test3 info:
 test3SubTest1() completed with result  PASS 
 test3SubTest2() completed with result  PASS 
 test3SubTest3() completed with result  FAIL
 2 passed, 0 failed, 0 skipped completed with result  Totals

我会添加一些检查失败的测试和东西,但这应该足以让你开始。