【问题标题】:Parsing complex logs with boost spirit以提振精神解析复杂的日志
【发布时间】:2017-02-22 17:42:01
【问题描述】:

我在使用 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

我正在使用下一个代码将其解析为 boost fusion 适应结构:

#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。

标签: boost boost-spirit


【解决方案1】:

添加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;
}

【讨论】:

  • 不错且准确的答案,但仍无法按需要工作。我需要解析整个日志文件以检索 msg6 中包含的所有信息:我需要行号、日期、时间、cpu id 和十六进制消息。为了找到所有 msg6 行,需要 *repo::seek 指令,以及第一个 qi::eol 和最后一个 >> qi::eol。这样做,我在日志的第 17 行和第 19 行的消息列表中获得了正确的信息,但是第 14 行中的第一个解析消息包含格式错误的数据,因为所有先前的字段都已附加。任何进一步的想法来解决这个问题?up
  • 啊。我知道了。 repo::seek 有一些文档吗?这对我来说是全新的。因此我将其删除。似乎对于 id 行 2,3,4,5,...14 的字符串是串联的。导致这个不起眼的 234567...14 id 字符串。
  • 似乎强制“有趣”行的属性为Message(通过使用额外的规则)解决了不同字符串合并为一个的问题。 Example.
  • 很抱歉,但仍无法按预期工作。在您的精简版日志中,第一个非 msg6 行从一开始就缺少 eol,因此下一行是 msg 6,它被正确解析。但是,如果您在 std::string 的开头添加“12:[2017-Feb-18 01:58:29.706065] => PC1 | Receive msg 4: 04010000F8B8C1A7”,程序会一直以错误的方式运行和以前一样。
猜你喜欢
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多