我在用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;
}
當執行代碼,數據在結構中格式不正確。有人可能試圖幫助我解決這個問題嗎?
您是否試過使用'qi :: lexeme'?似乎在結果中附加了id。 – Aleph0