mirror of https://github.com/tstack/lnav
[data-parser] fix some issues with parsing hierarchical data and improve the debugger
parent
1477c2ae51
commit
3167302722
@ -0,0 +1,210 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2016, Timothy Stack
|
||||||
|
*
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of Timothy Stack nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||||
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||||
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "config.h"
|
||||||
|
|
||||||
|
#include "elem_to_json.hh"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
static
|
||||||
|
void element_to_json(yajl_gen gen, data_parser &dp, const data_parser::element &elem)
|
||||||
|
{
|
||||||
|
size_t value_len;
|
||||||
|
const char *value_str = dp.get_element_string(elem, value_len);
|
||||||
|
|
||||||
|
switch (elem.value_token()) {
|
||||||
|
case DT_NUMBER: {
|
||||||
|
yajl_gen_number(gen, value_str, value_len);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case DNT_GROUP: {
|
||||||
|
elements_to_json(gen, dp, elem.get_value_elem().e_sub_elements, false);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case DNT_PAIR: {
|
||||||
|
const data_parser::element &pair_elem = elem.get_pair_elem();
|
||||||
|
string key_str = dp.get_element_string(pair_elem.e_sub_elements->front());
|
||||||
|
|
||||||
|
if (!key_str.empty()) {
|
||||||
|
yajlpp_map singleton_map(gen);
|
||||||
|
|
||||||
|
singleton_map.gen(key_str);
|
||||||
|
element_to_json(gen, dp, pair_elem.get_pair_value());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
element_to_json(gen, dp, pair_elem.get_pair_value());
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case DT_CONSTANT: {
|
||||||
|
if (strncasecmp("true", value_str, value_len) == 0) {
|
||||||
|
yajl_gen_bool(gen, true);
|
||||||
|
}
|
||||||
|
else if (strncasecmp("false", value_str, value_len) == 0) {
|
||||||
|
yajl_gen_bool(gen, false);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
yajl_gen_null(gen);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
yajl_gen_pstring(gen, value_str, value_len);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
void map_elements_to_json2(yajl_gen gen, data_parser &dp, data_parser::element_list_t *el)
|
||||||
|
{
|
||||||
|
yajlpp_map root_map(gen);
|
||||||
|
int col = 0;
|
||||||
|
|
||||||
|
for (data_parser::element_list_t::iterator iter = el->begin();
|
||||||
|
iter != el->end();
|
||||||
|
++iter) {
|
||||||
|
const data_parser::element &pvalue = iter->get_pair_value();
|
||||||
|
|
||||||
|
if (pvalue.value_token() == DT_INVALID) {
|
||||||
|
log_debug("invalid!!");
|
||||||
|
// continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string key_str = dp.get_element_string(
|
||||||
|
iter->e_sub_elements->front());
|
||||||
|
|
||||||
|
if (key_str.empty()) {
|
||||||
|
char buffer[32];
|
||||||
|
|
||||||
|
snprintf(buffer, sizeof(buffer), "col_%d", col);
|
||||||
|
key_str = buffer;
|
||||||
|
col += 1;
|
||||||
|
}
|
||||||
|
root_map.gen(key_str);
|
||||||
|
element_to_json(gen, dp, pvalue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
void list_body_elements_to_json(yajl_gen gen, data_parser &dp, data_parser::element_list_t *el)
|
||||||
|
{
|
||||||
|
for (data_parser::element_list_t::iterator iter = el->begin();
|
||||||
|
iter != el->end();
|
||||||
|
++iter) {
|
||||||
|
element_to_json(gen, dp, *iter);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
void list_elements_to_json(yajl_gen gen, data_parser &dp, data_parser::element_list_t *el)
|
||||||
|
{
|
||||||
|
yajlpp_array root_array(gen);
|
||||||
|
|
||||||
|
list_body_elements_to_json(gen, dp, el);
|
||||||
|
}
|
||||||
|
|
||||||
|
static
|
||||||
|
void map_elements_to_json(yajl_gen gen, data_parser &dp, data_parser::element_list_t *el)
|
||||||
|
{
|
||||||
|
bool unique_names = el->size() > 1;
|
||||||
|
vector<string> names;
|
||||||
|
|
||||||
|
for (data_parser::element_list_t::iterator iter = el->begin();
|
||||||
|
iter != el->end();
|
||||||
|
++iter) {
|
||||||
|
const data_parser::element &pvalue = iter->get_pair_value();
|
||||||
|
|
||||||
|
if (pvalue.value_token() == DT_INVALID) {
|
||||||
|
log_debug("invalid!!");
|
||||||
|
// continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string key_str = dp.get_element_string(
|
||||||
|
iter->e_sub_elements->front());
|
||||||
|
if (key_str.empty()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if (find(names.begin(), names.end(), key_str) != names.end()) {
|
||||||
|
unique_names = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
names.push_back(key_str);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
names.clear();
|
||||||
|
|
||||||
|
if (unique_names) {
|
||||||
|
map_elements_to_json2(gen, dp, el);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
list_elements_to_json(gen, dp, el);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void elements_to_json(yajl_gen gen, data_parser &dp, data_parser::element_list_t *el, bool root)
|
||||||
|
{
|
||||||
|
if (el->empty()) {
|
||||||
|
yajl_gen_null(gen);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
switch (el->front().e_token) {
|
||||||
|
case DNT_PAIR: {
|
||||||
|
if (root && el->size() == 1) {
|
||||||
|
const data_parser::element &pair_elem = el->front().get_pair_elem();
|
||||||
|
std::string key_str = dp.get_element_string(
|
||||||
|
pair_elem.e_sub_elements->front());
|
||||||
|
|
||||||
|
if (key_str.empty() &&
|
||||||
|
el->front().get_pair_value().value_token() == DNT_GROUP) {
|
||||||
|
element_to_json(gen, dp, el->front().get_pair_value());
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
yajlpp_map singleton_map(gen);
|
||||||
|
|
||||||
|
if (key_str.empty()) {
|
||||||
|
key_str = "col_0";
|
||||||
|
}
|
||||||
|
singleton_map.gen(key_str);
|
||||||
|
element_to_json(gen, dp, pair_elem.get_pair_value());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
map_elements_to_json(gen, dp, el);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
list_elements_to_json(gen, dp, el);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
/**
|
||||||
|
* Copyright (c) 2016, Timothy Stack
|
||||||
|
*
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice, this
|
||||||
|
* list of conditions and the following disclaimer.
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer in the documentation
|
||||||
|
* and/or other materials provided with the distribution.
|
||||||
|
* * Neither the name of Timothy Stack nor the names of its contributors
|
||||||
|
* may be used to endorse or promote products derived from this software
|
||||||
|
* without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
|
||||||
|
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||||
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||||
|
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
|
||||||
|
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||||
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||||
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||||
|
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||||
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||||
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef __elem_to_json_hh
|
||||||
|
#define __elem_to_json_hh
|
||||||
|
|
||||||
|
#include "yajl/api/yajl_gen.h"
|
||||||
|
#include "data_parser.hh"
|
||||||
|
|
||||||
|
void elements_to_json(yajl_gen gen, data_parser &dp, data_parser::element_list_t *el, bool root = true);
|
||||||
|
|
||||||
|
#endif
|
@ -0,0 +1,23 @@
|
|||||||
|
list [foo(bar=1)]
|
||||||
|
key 6:6 ^
|
||||||
|
key 6:9 ^-^ foo
|
||||||
|
key 10:13 ^-^ bar
|
||||||
|
num 14:15 ^ 1
|
||||||
|
val 14:15 ^ 1
|
||||||
|
pair 10:15 ^---^ bar=1
|
||||||
|
grp 10:15 ^---^ bar=1
|
||||||
|
val 10:15 ^---^ bar=1
|
||||||
|
pair 6:15 ^-------^ foo(bar=1
|
||||||
|
grp 6:15 ^-------^ foo(bar=1
|
||||||
|
pair 6:15 ^-------^ foo(bar=1
|
||||||
|
msg :list [foo(bar=1)]
|
||||||
|
format :list [#)]
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"foo": [
|
||||||
|
{
|
||||||
|
"bar": 1
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
@ -0,0 +1,53 @@
|
|||||||
|
list [foo(bar=1), foo(bar=2), foo(bar=3)]
|
||||||
|
key 6:6 ^
|
||||||
|
key 6:9 ^-^ foo
|
||||||
|
key 10:13 ^-^ bar
|
||||||
|
num 14:15 ^ 1
|
||||||
|
val 14:15 ^ 1
|
||||||
|
pair 10:15 ^---^ bar=1
|
||||||
|
grp 10:15 ^---^ bar=1
|
||||||
|
val 10:15 ^---^ bar=1
|
||||||
|
pair 6:15 ^-------^ foo(bar=1
|
||||||
|
key 18:21 ^-^ foo
|
||||||
|
key 22:25 ^-^ bar
|
||||||
|
num 26:27 ^ 2
|
||||||
|
val 26:27 ^ 2
|
||||||
|
pair 22:27 ^---^ bar=2
|
||||||
|
grp 22:27 ^---^ bar=2
|
||||||
|
val 22:27 ^---^ bar=2
|
||||||
|
pair 18:27 ^-------^ foo(bar=2
|
||||||
|
key 30:33 ^-^ foo
|
||||||
|
key 34:37 ^-^ bar
|
||||||
|
num 38:39 ^ 3
|
||||||
|
val 38:39 ^ 3
|
||||||
|
pair 34:39 ^---^ bar=3
|
||||||
|
grp 34:39 ^---^ bar=3
|
||||||
|
val 34:39 ^---^ bar=3
|
||||||
|
pair 30:39 ^-------^ foo(bar=3
|
||||||
|
grp 6:39 ^-------------------------------^ foo(bar=1), foo(bar=2), foo(bar=3
|
||||||
|
pair 6:39 ^-------------------------------^ foo(bar=1), foo(bar=2), foo(bar=3
|
||||||
|
msg :list [foo(bar=1), foo(bar=2), foo(bar=3)]
|
||||||
|
format :list [#)]
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"foo": [
|
||||||
|
{
|
||||||
|
"bar": 1
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"foo": [
|
||||||
|
{
|
||||||
|
"bar": 2
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"foo": [
|
||||||
|
{
|
||||||
|
"bar": 3
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
]
|
@ -0,0 +1,17 @@
|
|||||||
|
list ["abc", "def", "ghi"]
|
||||||
|
key 7:7 ^
|
||||||
|
quot 7:10 ^-^ abc
|
||||||
|
val 7:10 ^-^ abc
|
||||||
|
quot 14:17 ^-^ def
|
||||||
|
val 14:17 ^-^ def
|
||||||
|
quot 21:24 ^-^ ghi
|
||||||
|
val 21:24 ^-^ ghi
|
||||||
|
grp 7:24 ^---------------^ abc", "def", "ghi
|
||||||
|
pair 7:24 ^---------------^ abc", "def", "ghi
|
||||||
|
msg :list ["abc", "def", "ghi"]
|
||||||
|
format :list [#]
|
||||||
|
[
|
||||||
|
"abc",
|
||||||
|
"def",
|
||||||
|
"ghi"
|
||||||
|
]
|
@ -0,0 +1,13 @@
|
|||||||
|
#!/usr/bin/env bash
|
||||||
|
|
||||||
|
test_dir=`dirname $0`
|
||||||
|
|
||||||
|
for fn in ${test_dir}/datafile_simple.*; do
|
||||||
|
echo "Checking $fn"
|
||||||
|
./drive_data_scanner -p $fn
|
||||||
|
done
|
||||||
|
|
||||||
|
for fn in ${test_dir}/log-samples/sample-*; do
|
||||||
|
echo "Checking $fn"
|
||||||
|
./drive_data_scanner -p -l $fn
|
||||||
|
done
|
Loading…
Reference in New Issue