Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sort streams in "-B help" output, and match case-insensitively throug… #52

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 27 additions & 15 deletions src/DebugLogger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ DebugLogger::Stream DebugLogger::streams[NUM_DBGS] =
{"scripts", 0, false}, {"supervisor", 0, false}, {"hashkey", 0, false}, {"spicy", 0, false}};

DebugLogger::DebugLogger() {
verbose = false;
file = nullptr;
verbose = false;
all = false;
}

DebugLogger::~DebugLogger() {
Expand Down Expand Up @@ -53,21 +54,28 @@ void DebugLogger::OpenDebugLog(const char* filename) {
}

void DebugLogger::ShowStreamsHelp() {
fprintf(stderr, "\n");
fprintf(stderr, "Enable debug output into debug.log with -B <streams>.\n");
fprintf(stderr, "<streams> is a comma-separated list of streams to enable.\n");
fprintf(stderr, "<streams> is a case-insensitive, comma-separated list of streams to enable:\n");
fprintf(stderr, "\n");
fprintf(stderr, "Available streams:\n");

for ( int i = 0; i < NUM_DBGS; ++i )
fprintf(stderr, " %s\n", streams[i].prefix);
std::vector<std::string> prefixes;

for ( const auto& stream : streams )
prefixes.emplace_back(stream.prefix);
std::sort(prefixes.begin(), prefixes.end());

for ( const auto& prefix : prefixes )
fprintf(stderr, " %s\n", prefix.c_str());

fprintf(stderr, "\n");
fprintf(stderr, "Every plugin (see -N) also has its own debug stream:\n");
fprintf(stderr, "\n");
fprintf(stderr,
" plugin-<plugin-name> (replace '::' in name with '-'; e.g., '-B "
"plugin-Zeek-Netmap')\n");
"plugin-Zeek-JavaScript')\n");
fprintf(stderr, "\n");
fprintf(stderr, "Pseudo streams:\n");
fprintf(stderr, "\n");
fprintf(stderr, "Pseudo streams\n");
fprintf(stderr, " verbose Increase verbosity.\n");
fprintf(stderr, " all Enable all streams at maximum verbosity.\n");
fprintf(stderr, "\n");
Expand All @@ -79,12 +87,15 @@ void DebugLogger::EnableStreams(const char* s) {
char* tok = strtok(tmp, ",");

while ( tok ) {
std::string ltok{util::strtolower(tok)};

if ( strcasecmp("all", tok) == 0 ) {
for ( int i = 0; i < NUM_DBGS; ++i ) {
streams[i].enabled = true;
enabled_streams.insert(streams[i].prefix);
}

all = true;
verbose = true;
goto next;
}
Expand All @@ -99,10 +110,10 @@ void DebugLogger::EnableStreams(const char* s) {
exit(0);
}

if ( util::starts_with(tok, "plugin-") ) {
if ( util::starts_with(ltok, "plugin-") ) {
// Cannot verify this at this time, plugins may not
// have been loaded.
enabled_streams.insert(tok);
enabled_streams.insert(ltok);
goto next;
}

Expand All @@ -111,7 +122,7 @@ void DebugLogger::EnableStreams(const char* s) {
for ( i = 0; i < NUM_DBGS; ++i ) {
if ( strcasecmp(streams[i].prefix, tok) == 0 ) {
streams[i].enabled = true;
enabled_streams.insert(tok);
enabled_streams.insert(ltok);
goto next;
}
}
Expand Down Expand Up @@ -166,10 +177,11 @@ void DebugLogger::Log(DebugStream stream, const char* fmt, ...) {
}

void DebugLogger::Log(const plugin::Plugin& plugin, const char* fmt, ...) {
std::string tok = PluginStreamName(plugin.Name());

if ( enabled_streams.find(tok) == enabled_streams.end() )
return;
if ( ! all ) {
std::string tok = PluginStreamName(plugin.Name());
if ( enabled_streams.find(tok) == enabled_streams.end() )
return;
}

fprintf(file, "%17.06f/%17.06f [plugin %s] ", run_state::network_time, util::current_time(true),
plugin.Name().c_str());
Expand Down
3 changes: 2 additions & 1 deletion src/DebugLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class DebugLogger {

private:
FILE* file;
bool all;
bool verbose;

struct Stream {
Expand All @@ -108,7 +109,7 @@ class DebugLogger {
static Stream streams[NUM_DBGS];

const std::string PluginStreamName(const std::string& plugin_name) {
return "plugin-" + util::strreplace(plugin_name, "::", "-");
return "plugin-" + util::strtolower(util::strreplace(plugin_name, "::", "-"));
}
};

Expand Down
2 changes: 1 addition & 1 deletion testing/btest/Baseline/plugins.debug-streams/zeek.stderr
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
### BTest baseline data generated by btest-diff. Do not edit. Use "btest -U/-u" to update. Requires BTest >= 0.63.
error in <command line>, line 3: No plugin debug stream 'plugin-zeek-http' found
error in <command line>, line 3: No plugin debug stream 'plugin-notaplugin' found
3 changes: 2 additions & 1 deletion testing/btest/plugins/debug-streams.zeek
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
# @TEST-REQUIRES: test "$($BUILD/zeek-config --build_type)" = "debug"

# @TEST-EXEC: zeek -B plugin-Zeek-HTTP -e 'event zeek_init() { print "zeek_init"; }' 2>zeek.stderr
# @TEST-EXEC-FAIL: zeek -B plugin-zeek-http -e 'event zeek_init() { print "zeek_init"; }' 2>zeek.stderr
# @TEST-EXEC: zeek -B PLUGIN-zeek-http -e 'event zeek_init() { print "zeek_init"; }' 2>zeek.stderr
# @TEST-EXEC-FAIL: zeek -B plugin-notaplugin -e 'event zeek_init() { print "zeek_init"; }' 2>zeek.stderr

# @TEST-EXEC: btest-diff zeek.stderr
Loading