Skip to content

Commit

Permalink
Add debug logs, including query plans (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
msm-cert authored Oct 1, 2024
1 parent 90d706d commit 9f2a149
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
2 changes: 2 additions & 0 deletions libursa/OnDiskDataset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "DatabaseName.h"
#include "Json.h"
#include "Query.h"
#include "spdlog/fmt/ostr.h"
#include "spdlog/spdlog.h"

void OnDiskDataset::save() {
Expand Down Expand Up @@ -91,6 +92,7 @@ void OnDiskDataset::execute(const Query &query, ResultWriter *out,
types_to_query.emplace(ndx.index_type());
}
const Query plan = query.plan(types_to_query);
spdlog::debug("PLAN: {}", plan);

QueryResult result = this->query(plan, counters);
if (result.is_everything()) {
Expand Down
10 changes: 6 additions & 4 deletions libursa/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ std::ostream &operator<<(std::ostream &os, const Query &query) {
if (type == QueryType::AND || type == QueryType::OR ||
type == QueryType::MIN_OF) {
if (type == QueryType::AND) {
os << "AND(";
os << "and(";
} else if (type == QueryType::OR) {
os << "OR(";
os << "or(";
} else if (type == QueryType::MIN_OF) {
os << "MIN_OF(" << query.as_count() << ", ";
os << "min_of(" << query.as_count() << ", ";
}

bool is_first = true;
Expand Down Expand Up @@ -81,7 +81,9 @@ std::string Query::as_string_repr() const {
std::string out = "";
if (value.empty()) {
// Query is already after planning stage. Show low-level representation.
return fmt::format("{}:[{:06x}]", (int)ngram.itype, ngram.trigram);
// First digit is the index type and the rest is the raw trigram.
// Underscore was picked to make copying from terminal easier.
return fmt::format("{}_{:06x}", (int)ngram.itype, ngram.trigram);
}
// No query plan yet. Show stringlike representation.
for (const auto &token : value) {
Expand Down
2 changes: 1 addition & 1 deletion libursa/Version.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ constexpr std::string_view ursadb_format_version = "1.5.0";
// Project version.
// Consider updating the version tag when doing PRs.
constexpr std::string_view ursadb_version_string =
"@PROJECT_VERSION@+primitives";
"@PROJECT_VERSION@+debuglogs";
41 changes: 32 additions & 9 deletions src/NetworkService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -188,12 +188,17 @@ void NetworkService::poll_frontend() {
s_send(&backend, request, ZMQTRACE);
}

void print_usage_and_exit() {
puts("Usage:");
puts(" ursadb database-file [bind-address]");
puts(" -v Enable verbose output (debug messages)");
exit(1);
}

// Main entry point of the system!
int main(int argc, char *argv[]) {
if (argc < 2) {
printf("Usage:\n");
printf(" %s database-file [bind-address]\n", argv[0]);
return 1;
print_usage_and_exit();
}

spdlog::info("UrsaDB v{}", get_version_string());
Expand All @@ -202,15 +207,33 @@ int main(int argc, char *argv[]) {
migrate_version(argv[1]);

try {
Database db(argv[1]);
std::string bind_address = "tcp://127.0.0.1:9281";
bool enable_debug = false;
const char *db_file = NULL;
const char *bind_address = NULL;
for (int i = 1; i < argc; i++) {
if (std::string("-v") == argv[i]) {
enable_debug = true;
} else if (db_file == NULL) {
db_file = argv[i]; // first positional arg
} else if (bind_address == NULL) {
bind_address = argv[i];
} else {
spdlog::error("Too many positional arguments.");
}
}

if (db_file == NULL) {
print_usage_and_exit();
} else if (bind_address == NULL) {
bind_address = "tcp://127.0.0.1:9281";
}

if (argc > 3) {
spdlog::error("Too many command line arguments.");
} else if (argc == 3) {
bind_address = std::string(argv[2]);
if (enable_debug) {
spdlog::set_level(spdlog::level::debug);
spdlog::debug("Debug logging enabled");
}

Database db(db_file);
spdlog::info("BIND: {}", bind_address);
NetworkService service(db, bind_address);
service.run();
Expand Down

0 comments on commit 9f2a149

Please sign in to comment.