Skip to content

Commit

Permalink
[log_format] reduce piper match quality
Browse files Browse the repository at this point in the history
Sigh, I forget why I set the quality to 100...  Reducing
to 1 for now.

Related to #1339
  • Loading branch information
tstack committed Nov 19, 2024
1 parent 5058e1f commit 99dafd5
Show file tree
Hide file tree
Showing 10 changed files with 99 additions and 49 deletions.
66 changes: 66 additions & 0 deletions src/command_executor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1084,6 +1084,48 @@ sql_callback(exec_context& ec, sqlite3_stmt* stmt)
return retval;
}

int
internal_sql_callback(exec_context& ec, sqlite3_stmt* stmt)
{
if (!sqlite3_stmt_busy(stmt)) {
return 0;
}

const int ncols = sqlite3_column_count(stmt);

auto& vars = ec.ec_local_vars.top();

for (int lpc = 0; lpc < ncols; lpc++) {
const char* column_name = sqlite3_column_name(stmt, lpc);

if (sql_ident_needs_quote(column_name)) {
continue;
}

auto* raw_value = sqlite3_column_value(stmt, lpc);
auto value_type = sqlite3_column_type(stmt, lpc);
scoped_value_t value;
switch (value_type) {
case SQLITE_INTEGER:
value = (int64_t) sqlite3_value_int64(raw_value);
break;
case SQLITE_FLOAT:
value = sqlite3_value_double(raw_value);
break;
case SQLITE_NULL:
value = null_value_t{};
break;
default:
value = std::string((const char*) sqlite3_value_text(raw_value),
sqlite3_value_bytes(raw_value));
break;
}
vars[column_name] = value;
}

return 0;
}

std::future<std::string>
pipe_callback(exec_context& ec, const std::string& cmdline, auto_fd& fd)
{
Expand Down Expand Up @@ -1269,6 +1311,11 @@ exec_context::add_error_context(lnav::console::user_message& um)
}
}

exec_context::sql_callback_guard
exec_context::push_callback(sql_callback_t cb)
{
return sql_callback_guard(*this, cb);
}
exec_context::source_guard
exec_context::enter_source(intern_string_t path,
int line_number,
Expand Down Expand Up @@ -1300,3 +1347,22 @@ output_guard()
this->sg_context.clear_output();
this->sg_context.ec_output_stack.pop_back();
}
exec_context::sql_callback_guard::
sql_callback_guard(exec_context& context, sql_callback_t cb)
: scg_context(context), scg_old_callback(context.ec_sql_callback)
{
context.ec_sql_callback = cb;
}
exec_context::sql_callback_guard::
sql_callback_guard(sql_callback_guard&& other)
: scg_context(other.scg_context),
scg_old_callback(std::exchange(other.scg_old_callback, nullptr))
{
}
exec_context::sql_callback_guard::~
sql_callback_guard()
{
if (this->scg_old_callback != nullptr) {
this->scg_context.ec_sql_callback = this->scg_old_callback;
}
}
16 changes: 16 additions & 0 deletions src/command_executor.hh
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ struct logline_value_vector;

using sql_callback_t = int (*)(exec_context&, sqlite3_stmt*);
int sql_callback(exec_context& ec, sqlite3_stmt* stmt);
int internal_sql_callback(exec_context& ec, sqlite3_stmt* stmt);

using pipe_callback_t
= std::future<std::string> (*)(exec_context&, const std::string&, auto_fd&);
Expand Down Expand Up @@ -191,6 +192,21 @@ struct exec_context {
exec_context& sg_context;
};

struct sql_callback_guard {
sql_callback_guard(exec_context& context, sql_callback_t cb);

sql_callback_guard(const sql_callback_guard&) = delete;

sql_callback_guard(sql_callback_guard&& other);

~sql_callback_guard();

exec_context& scg_context;
sql_callback_t scg_old_callback;
};

sql_callback_guard push_callback(sql_callback_t cb);

source_guard enter_source(intern_string_t path,
int line_number,
const std::string& content);
Expand Down
44 changes: 1 addition & 43 deletions src/hotkeys.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,48 +48,6 @@

using namespace lnav::roles::literals;

static int
key_sql_callback(exec_context& ec, sqlite3_stmt* stmt)
{
if (!sqlite3_stmt_busy(stmt)) {
return 0;
}

int ncols = sqlite3_column_count(stmt);

auto& vars = ec.ec_local_vars.top();

for (int lpc = 0; lpc < ncols; lpc++) {
const char* column_name = sqlite3_column_name(stmt, lpc);

if (sql_ident_needs_quote(column_name)) {
continue;
}

auto* raw_value = sqlite3_column_value(stmt, lpc);
auto value_type = sqlite3_column_type(stmt, lpc);
scoped_value_t value;
switch (value_type) {
case SQLITE_INTEGER:
value = (int64_t) sqlite3_value_int64(raw_value);
break;
case SQLITE_FLOAT:
value = sqlite3_value_double(raw_value);
break;
case SQLITE_NULL:
value = null_value_t{};
break;
default:
value = std::string((const char*) sqlite3_value_text(raw_value),
sqlite3_value_bytes(raw_value));
break;
}
vars[column_name] = value;
}

return 0;
}

bool
handle_keyseq(const char* keyseq)
{
Expand All @@ -100,7 +58,7 @@ handle_keyseq(const char* keyseq)
}

logline_value_vector values;
exec_context ec(&values, key_sql_callback, pipe_callback);
exec_context ec(&values, internal_sql_callback, pipe_callback);
auto& var_stack = ec.ec_local_vars;

ec.ec_label_source_stack.push_back(&lnav_data.ld_db_row_source);
Expand Down
4 changes: 3 additions & 1 deletion src/lnav_commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ com_adjust_log_time(exec_context& ec,
if (args.empty()) {
args.emplace_back("line-time");
} else if (lnav_data.ld_views[LNV_LOG].get_inner_height() == 0) {
return ec.make_error("no log messages");
return ec.make_error("no logp messages");
} else if (args.size() >= 2) {
auto& lss = lnav_data.ld_log_source;
struct timeval top_time, time_diff;
Expand Down Expand Up @@ -3231,6 +3231,8 @@ com_open(exec_context& ec, std::string cmdline, std::vector<std::string>& args)
exec_context::provenance_guard pg(&ec,
exec_context::file_open{fn});

auto cb_guard = ec.push_callback(internal_sql_callback);

auto exec_res = execute_file(ec, path_and_args);
if (exec_res.isErr()) {
return exec_res;
Expand Down
8 changes: 4 additions & 4 deletions src/log_format.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ external_log_format::scan(logfile& lf,
ll.set_time(dst.back().get_timeval());
ll.set_level(LEVEL_INVALID);
dst.emplace_back(ll);
return log_format::scan_match{0};
return scan_match{0};
}

auto& ypc = *(this->jlf_parse_context);
Expand All @@ -1257,7 +1257,7 @@ external_log_format::scan(logfile& lf,
ll.set_level(LEVEL_INVALID);
dst.emplace_back(ll);
}
return log_format::scan_incomplete{};
return scan_incomplete{};
}

const auto* line_data = (const unsigned char*) sbr.get_data();
Expand Down Expand Up @@ -1669,10 +1669,10 @@ external_log_format::scan(logfile& lf,
last_line.get_timeval(),
log_level_t::LEVEL_INVALID);

return log_format::scan_match{0};
return scan_match{0};
}

return log_format::scan_no_match{"no patterns matched"};
return scan_no_match{"no patterns matched"};
}

uint8_t
Expand Down
2 changes: 1 addition & 1 deletion src/log_format_impls.cc
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class piper_log_format : public log_format {
{
dst.emplace_back(
li.li_file_range.fr_offset, li.li_timestamp, li.li_level);
return scan_match{100};
return scan_match{1};
}

return scan_no_match{""};
Expand Down
2 changes: 2 additions & 0 deletions test/expected/expected.am
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ EXPECTED_FILES = \
$(srcdir)/%reldir%/test_json_format.sh_4315a3d6124c14cbe3c474b6dbf4cc8720a9859f.out \
$(srcdir)/%reldir%/test_json_format.sh_469f005b0708d629bc95f0c48a5e390f440c1fef.err \
$(srcdir)/%reldir%/test_json_format.sh_469f005b0708d629bc95f0c48a5e390f440c1fef.out \
$(srcdir)/%reldir%/test_json_format.sh_46d00207e2ffc14da84251af0dd0ef01c0fae4ee.err \
$(srcdir)/%reldir%/test_json_format.sh_46d00207e2ffc14da84251af0dd0ef01c0fae4ee.out \
$(srcdir)/%reldir%/test_json_format.sh_5795c5ffd98ae581b30c6f0983349bf7a6a84501.err \
$(srcdir)/%reldir%/test_json_format.sh_5795c5ffd98ae581b30c6f0983349bf7a6a84501.out \
$(srcdir)/%reldir%/test_json_format.sh_6767b91d715338c24c67e928b59c560c84ddf4be.err \
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
2018-08-21T14:04:21.221373Z 38708007 medusa-GpsLocator.service python[184] FATAL GPS Reference longitude: 7.358143333
2018-08-21T14:04:21.221373Z 38708007 medusa-GpsLocator.service python[184] INFO GPS Reference latitude: 46.908706667
4 changes: 4 additions & 0 deletions test/test_json_format.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ run_cap_test env TZ=UTC ${lnav_test} -n \
-I ${test_dir} \
${test_dir}/logfile_journald.json

cat ${test_dir}/logfile_journald.json | \
run_cap_test env TZ=UTC ${lnav_test} -n \
-c ':test-comment json format on stdin'

# json log format is not working"
run_cap_test ${lnav_test} -n \
-I ${test_dir} \
Expand Down

0 comments on commit 99dafd5

Please sign in to comment.