1 /**
2  * Copyright (c) 2021, Timothy Stack
3  *
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * * Neither the name of Timothy Stack nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
19  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21  * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
22  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25  * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  *
29  * @file log_data_helper.cc
30  */
31 
32 #include "config.h"
33 
34 #include "log_data_helper.hh"
35 
clear()36 void log_data_helper::clear()
37 {
38     this->ldh_file = nullptr;
39     this->ldh_msg.disown();
40     this->ldh_parser.reset();
41     this->ldh_scanner.reset();
42     this->ldh_namer.reset();
43     this->ldh_json_pairs.clear();
44     this->ldh_xml_pairs.clear();
45     this->ldh_line_attrs.clear();
46 }
47 
parse_line(content_line_t line,bool allow_middle)48 bool log_data_helper::parse_line(content_line_t line, bool allow_middle)
49 {
50     logfile::iterator ll;
51     bool retval = false;
52 
53     this->ldh_source_line = this->ldh_line_index = line;
54 
55     this->ldh_file = this->ldh_log_source.find(this->ldh_line_index);
56     ll = this->ldh_file->begin() + this->ldh_line_index;
57     this->ldh_y_offset = 0;
58     while (allow_middle && ll->is_continued()) {
59         --ll;
60         this->ldh_y_offset += 1;
61     }
62     this->ldh_line = ll;
63     if (!ll->is_message()) {
64         this->ldh_parser.reset();
65         this->ldh_scanner.reset();
66         this->ldh_namer.reset();
67         this->ldh_json_pairs.clear();
68         this->ldh_xml_pairs.clear();
69         this->ldh_line_attrs.clear();
70     }
71     else {
72         auto format = this->ldh_file->get_format();
73         struct line_range body;
74         auto& sa = this->ldh_line_attrs;
75 
76         this->ldh_line_attrs.clear();
77         this->ldh_line_values.clear();
78         this->ldh_file->read_full_message(ll, this->ldh_msg);
79         format->annotate(this->ldh_line_index, this->ldh_msg, sa, this->ldh_line_values);
80 
81         body = find_string_attr_range(sa, &SA_BODY);
82         if (body.lr_start == -1) {
83             body.lr_start = this->ldh_msg.length();
84             body.lr_end = this->ldh_msg.length();
85         }
86         this->ldh_scanner = std::make_unique<data_scanner>(
87             this->ldh_msg, body.lr_start, body.lr_end);
88         this->ldh_parser = std::make_unique<data_parser>(this->ldh_scanner.get());
89         this->ldh_msg_format.clear();
90         this->ldh_parser->dp_msg_format = &this->ldh_msg_format;
91         this->ldh_parser->parse();
92         this->ldh_namer = std::make_unique<column_namer>();
93         this->ldh_json_pairs.clear();
94         this->ldh_xml_pairs.clear();
95 
96         for (const auto& lv : this->ldh_line_values) {
97             this->ldh_namer->cn_builtin_names.emplace_back(lv.lv_meta.lvm_name.get());
98         }
99 
100         for (auto & ldh_line_value : this->ldh_line_values) {
101             switch (ldh_line_value.lv_meta.lvm_kind) {
102                 case value_kind_t::VALUE_JSON: {
103                     json_ptr_walk jpw;
104 
105                     if (jpw.parse(ldh_line_value.lv_sbr.get_data(), ldh_line_value.lv_sbr.length()) == yajl_status_ok &&
106                         jpw.complete_parse() == yajl_status_ok) {
107                         this->ldh_json_pairs[ldh_line_value.lv_meta.lvm_name] = jpw.jpw_values;
108                     }
109                     break;
110                 }
111                 case value_kind_t::VALUE_XML: {
112                     auto col_name = ldh_line_value.lv_meta.lvm_name;
113                     pugi::xml_document doc;
114 
115                     auto parse_res = doc.load_buffer(
116                         ldh_line_value.lv_sbr.get_data(),
117                         ldh_line_value.lv_sbr.length());
118 
119                     if (parse_res) {
120                         pugi::xpath_query query("//*");
121                         auto node_set = doc.select_nodes(query);
122 
123                         for (auto& xpath_node : node_set) {
124                             auto node_path = lnav::pugixml::get_actual_path(xpath_node.node());
125                             for (auto& attr : xpath_node.node().attributes()) {
126                                 auto attr_path = fmt::format(
127                                     "{}/@{}", node_path, attr.name());
128 
129                                 this->ldh_xml_pairs[
130                                     std::make_pair(col_name, attr_path)] =
131                                     attr.value();
132                             }
133 
134                             if (xpath_node.node().text().empty()) {
135                                 continue;
136                             }
137 
138                             auto text_path = fmt::format(
139                                 "{}/text()", node_path);
140                             this->ldh_xml_pairs[std::make_pair(col_name, text_path)] =
141                                 trim(xpath_node.node().text().get());
142                         }
143                     }
144                     break;
145                 }
146                 default:
147                     break;
148             }
149         }
150 
151         retval = true;
152     }
153 
154     return retval;
155 }
156 
get_line_bounds(size_t & line_index_out,size_t & line_end_index_out) const157 int log_data_helper::get_line_bounds(size_t &line_index_out,
158                                      size_t &line_end_index_out) const
159 {
160     int retval = 0;
161 
162     line_end_index_out = 0;
163     do {
164         const char *line_end;
165 
166         line_index_out = line_end_index_out;
167         line_end = (const char *)memchr(
168             this->ldh_msg.get_data() + line_index_out + 1,
169             '\n',
170             this->ldh_msg.length() - line_index_out - 1);
171         if (line_end != nullptr) {
172             line_end_index_out = line_end - this->ldh_msg.get_data();
173         } else {
174             line_end_index_out = std::string::npos;
175         }
176         retval += 1;
177     } while (retval <= this->ldh_y_offset);
178 
179     if (line_end_index_out == std::string::npos) {
180         line_end_index_out = this->ldh_msg.length();
181     }
182 
183     return retval;
184 }
185 
186 std::string
format_json_getter(const intern_string_t field,int index)187 log_data_helper::format_json_getter(const intern_string_t field, int index)
188 {
189     auto_mem<char, sqlite3_free> qname;
190     auto_mem<char, sqlite3_free> jget;
191     std::string retval;
192 
193     qname = sql_quote_ident(field.get());
194     jget = sqlite3_mprintf("jget(%s,%Q)", qname.in(),
195                            this->ldh_json_pairs[field][index].wt_ptr.c_str());
196     retval = std::string(jget);
197 
198     return retval;
199 }
200