2017-02-22 61 views
2

我在用boost :: spirit解析複雜日誌時遇到問題。我無法按需要獲取數據,主要是因爲空白隊長將所有內容弄糟。用提升精神解析複雜日誌

我有一個名爲log.txt的下一個文本文件:

1:[2017-Feb-18 01:57:55.341100] <INFO, SIMULATING> => CPU | Name: CAR (ID: 0) - ID: 1 
2:[2017-Feb-18 01:57:55.344100] <INFO, SENDING_DATA> => IO | Io_out - ABS: 1 
3:[2017-Feb-18 01:57:55.344100] <INFO, SIMULATING> => CPU | Status: Ok 
4:[2017-Feb-18 01:57:55.346100] <INFO, SIMULATING> => MSS | Random Number: 0x4D080020 
5:[2017-Feb-18 01:57:55.346100] <INFO, SIMULATING> => CPU | Entering mode: AUTO 
6:[2017-Feb-18 01:57:59.583342] <INFO, SENDING_DATA> => IO | Io_in - BRK: 1 
7:[2017-Feb-18 01:58:24.604773] <INFO, RECEIVING_DATA> => DET | Point: 004811 
8:[2017-Feb-18 01:58:24.844787] <INFO, SENDING_DATA> => PC | Send msg 1: 0101000000000000 
9:[2017-Feb-18 01:58:26.204865] <INFO, RECEIVING_DATA> => PC2 | Receive msg 8: 0801000000000000 
10:[2017-Feb-18 01:58:28.706008] <INFO, RECEIVING_DATA> => PC1 | Receive msg 2: 0201000000000000 
11:[2017-Feb-18 01:58:29.345045] <INFO, SENDING_DATA> => PC | Send msg 3: 0301000000000000 
12:[2017-Feb-18 01:58:29.706065] <INFO, RECEIVING_DATA> => PC1 | Receive msg 4: 04010000F8B8C1A7 
13:[2017-Feb-18 01:58:29.846073] <INFO, SENDING_DATA> => PC | Send msg 5: 05010000F8B8C1A7 
14:[2017-Feb-18 01:58:32.206208] <INFO, RECEIVING_DATA> => PC1 | Receive msg 6: 06010001F8B8C1A8 
15:[2017-Feb-18 01:58:32.366217] <INFO, SENDING_DATA> => PC | Send msg 7: 07010001F8B8C1A8 
17:[2017-Feb-18 01:58:32.406220] <INFO, RECEIVING_DATA> => PC2 | Receive msg 6: 06010001F8B8C1A8 
18:[2017-Feb-18 01:58:32.875246] <INFO, SENDING_DATA> => PC | Send msg 7: 07010001F8B8C1A9 
19:[2017-Feb-18 01:58:32.906248] <INFO, RECEIVING_DATA> => PC1 | Receive msg 6: 06010001F8B8C1A9 
20:[2017-Feb-18 01:58:33.386276] <INFO, SENDING_DATA> => PC | Send msg 7: 07010001F8B8C1AA 

,我使用的下一個代碼解析成一個提振融合適應結構:

#include <fstream> 

#include <boost/config/warning_disable.hpp> 
#include <boost/fusion/include/adapt_struct.hpp> 
#include <boost/spirit/include/qi.hpp> 
#include <boost/spirit/repository/include/qi_seek.hpp> 


struct Message 
{ 
    std::string line; 
    std::string date; 
    std::string time; 
    char id; 
    std::string hex; 
}; 

BOOST_FUSION_ADAPT_STRUCT(
    Message, 
    (std::string, Message::line) 
    (std::string, Message::date) 
    (std::string, Message::time) 
    (char, Message::id) 
    (std::string, Message::hex) 
) 

std::vector<Message> messages; 

namespace qi = boost::spirit::qi; 
namespace repo = boost::spirit::repository; 
namespace ascii = boost::spirit::ascii; 

void main() 
{ 
    std::ifstream in("C:/log.txt", std::ios_base::in); 
    in >> std::noskipws;//No white space skipping 

    if (!in) 
    { 
     std::cerr << "Error: Could not open input file: " << std::endl; 
     return; 
    }//if 

    boost::spirit::istream_iterator first(in); 
    boost::spirit::istream_iterator last; 

    bool result = qi::phrase_parse(first, last, 
     *repo::seek[qi::eol 
      >> +ascii::char_("0-9") 
      >> ":[" 
      >> +ascii::char_("0-9a-fA-F-") 
      >> +ascii::char_("0-9.:") 
      >> "] <INFO, RECEIVING_DATA> => PC" 
      >> ascii::char_('1', '2') 
      >> "| Receive msg 6:" 
      >> +ascii::char_("0-9a-fA-F") 
      >> qi::eol], 
     ascii::blank, 
     messages); 

    return; 
} 

當執行代碼,數據在結構中格式不正確。有人可能試圖幫助我解決這個問題嗎?

+0

您是否試過使用'qi :: lexeme'?似乎在結果中附加了id。 – Aleph0

回答

2

我加了lexeme後,我能夠解析下面一行日誌文件:

14:[2017-Feb-18 01:58:32.206208] <INFO, RECEIVING_DATA> => PC1 | Receive msg 6: 06010001F8B8C1A8 

這是我的代碼。它仍然沒有閱讀原始log.txt。但我不確定你想要達到的目標。

#include <fstream> 

#include <boost/config/warning_disable.hpp> 
#include <boost/fusion/include/adapt_struct.hpp> 
#include <boost/spirit/include/qi.hpp> 
#include <boost/spirit/repository/include/qi_seek.hpp> 
#include <boost/spirit/include/phoenix.hpp> 
#include <boost/spirit/include/phoenix_operator.hpp> 

struct Message 
{ 
    std::string line; 
    std::string date; 
    std::string time; 
    char id; 
    std::string hex; 
}; 

BOOST_FUSION_ADAPT_STRUCT(
    Message, 
    (std::string, Message::line) 
    (std::string, Message::date) 
    (std::string, Message::time) 
    (char, Message::id) 
    (std::string, Message::hex) 
) 

std::vector<Message> messages; 

namespace qi = boost::spirit::qi; 
namespace repo = boost::spirit::repository; 
namespace ascii = boost::spirit::ascii; 
namespace ph=boost::phoenix; 

void main() 
{ 
    std::ifstream in("C:/temp/log2.txt", std::ios_base::in); 
    in >> std::noskipws;//No white space skipping 

    if (!in) 
    { 
     std::cerr << "Error: Could not open input file: " << std::endl; 
     return; 
    }//if 
    Message msg; 
    boost::spirit::istream_iterator first(in); 
    boost::spirit::istream_iterator last; 
    bool result = qi::phrase_parse(first, last, 
//  *repo::seek[ 
     (+ascii::char_("0-9") 
     >> qi::lexeme[":[" >> +ascii::char_("0-9a-fA-F-")] 
     >> +ascii::char_("0-9.:") 
     >> "] <INFO, RECEIVING_DATA> => PC" 
     >> ascii::char_('1', '2') 
     >> "| Receive msg 6:" 
     >> +ascii::char_("0-9a-fA-F")) 
     % qi::eol, 
/*  >> qi::eol],*/ 
     ascii::blank, 
     messages); 
     for (auto msg : messages) { 
      std::cout << msg.line << ", " << msg.date << ", " << msg.time << ", " << msg.id << ", " << msg.hex << std::endl; 
     } 
    return; 
} 
+0

尼斯和敏捷的答案,但仍不能按需要工作。我需要解析整個日誌文件以檢索msg6中包含的所有信息:我需要行號,日期,時間,CPU ID和十六進制消息。爲了查找所有msg6行,需要* repo :: seek指令,以及第一個qi :: eol和最後一個>> qi :: eol。這樣做,我可以在日誌的第17行和第19行的消息列表中獲得正確的信息,但第14行中的第一個已解析消息包含錯誤的格式化數據,因爲所有先前的字段都已被添加。任何進一步的意見,以解決這個問題? – Pablo

+0

啊。我懂了。有'repo :: seek'文件嗎?這對我來說是全新的。因此我將其刪除。看來,對於id爲2,3,4,5,... 14行的字符串是連接的。導致在這個晦澀的234567 ... 14 id字符串。 – Aleph0

+0

http://www.boost.org/doc/libs/1_61_0/libs/spirit/repository/doc/html/spirit_repository/qi_components/directives/seek.html – llonesmiz