mirror of
https://github.com/zeek/zeek.git
synced 2025-10-02 14:48:21 +00:00
Change reporter messages to more reliably print to stderr.
Moved this functionality to be internal instead of in the script-layer event handlers. The issue with the later is that bad things can happen between the time a reporter event handler is dispatched and the time it is executed, and if bro crashes in that time, the message may never be seen/logged. Addressed #930 (and revisits #836).
This commit is contained in:
parent
2823744ea5
commit
fdd11428c1
11 changed files with 90 additions and 64 deletions
|
@ -1,10 +1,15 @@
|
|||
##! This framework is intended to create an output and filtering path for
|
||||
##! internal messages/warnings/errors. It should typically be loaded to
|
||||
##! avoid Bro spewing internal messages to standard error and instead log
|
||||
##! them to a file in a standard way. Note that this framework deals with
|
||||
##! the handling of internally-generated reporter messages, for the
|
||||
##! interface into actually creating reporter messages from the scripting
|
||||
##! layer, use the built-in functions in :doc:`/scripts/base/reporter.bif`.
|
||||
##! log such messages to a file in a standard way. For the options to
|
||||
##! toggle whether messages are additionally written to STDERR, see
|
||||
##! :bro:see:`Reporter::info_to_stderr`,
|
||||
##! :bro:see:`Reporter::warnings_to_stderr`, and
|
||||
##! :bro:see:`Reporter::errors_to_stderr`.
|
||||
##!
|
||||
##! Note that this framework deals with the handling of internally generated
|
||||
##! reporter messages, for the interface in to actually creating interface
|
||||
##! into actually creating reporter messages from the scripting layer, use
|
||||
##! the built-in functions in :doc:`/scripts/base/reporter.bif`.
|
||||
|
||||
module Reporter;
|
||||
|
||||
|
@ -36,26 +41,11 @@ export {
|
|||
## Not all reporter messages will have locations in them though.
|
||||
location: string &log &optional;
|
||||
};
|
||||
|
||||
## Tunable for sending reporter warning messages to STDERR. The option to
|
||||
## turn it off is presented here in case Bro is being run by some
|
||||
## external harness and shouldn't output anything to the console.
|
||||
const warnings_to_stderr = T &redef;
|
||||
|
||||
## Tunable for sending reporter error messages to STDERR. The option to
|
||||
## turn it off is presented here in case Bro is being run by some
|
||||
## external harness and shouldn't output anything to the console.
|
||||
const errors_to_stderr = T &redef;
|
||||
}
|
||||
|
||||
global stderr: file;
|
||||
|
||||
event bro_init() &priority=5
|
||||
{
|
||||
Log::create_stream(Reporter::LOG, [$columns=Info]);
|
||||
|
||||
if ( errors_to_stderr || warnings_to_stderr )
|
||||
stderr = open("/dev/stderr");
|
||||
}
|
||||
|
||||
event reporter_info(t: time, msg: string, location: string) &priority=-5
|
||||
|
@ -65,26 +55,10 @@ event reporter_info(t: time, msg: string, location: string) &priority=-5
|
|||
|
||||
event reporter_warning(t: time, msg: string, location: string) &priority=-5
|
||||
{
|
||||
if ( warnings_to_stderr )
|
||||
{
|
||||
if ( t > double_to_time(0.0) )
|
||||
print stderr, fmt("WARNING: %.6f %s (%s)", t, msg, location);
|
||||
else
|
||||
print stderr, fmt("WARNING: %s (%s)", msg, location);
|
||||
}
|
||||
|
||||
Log::write(Reporter::LOG, [$ts=t, $level=WARNING, $message=msg, $location=location]);
|
||||
}
|
||||
|
||||
event reporter_error(t: time, msg: string, location: string) &priority=-5
|
||||
{
|
||||
if ( errors_to_stderr )
|
||||
{
|
||||
if ( t > double_to_time(0.0) )
|
||||
print stderr, fmt("ERROR: %.6f %s (%s)", t, msg, location);
|
||||
else
|
||||
print stderr, fmt("ERROR: %s (%s)", msg, location);
|
||||
}
|
||||
|
||||
Log::write(Reporter::LOG, [$ts=t, $level=ERROR, $message=msg, $location=location]);
|
||||
}
|
||||
|
|
|
@ -2857,6 +2857,25 @@ export {
|
|||
} # end export
|
||||
module GLOBAL;
|
||||
|
||||
module Reporter;
|
||||
export {
|
||||
## Tunable for sending reporter info messages to STDERR. The option to
|
||||
## turn it off is presented here in case Bro is being run by some
|
||||
## external harness and shouldn't output anything to the console.
|
||||
const info_to_stderr = T &redef;
|
||||
|
||||
## Tunable for sending reporter warning messages to STDERR. The option to
|
||||
## turn it off is presented here in case Bro is being run by some
|
||||
## external harness and shouldn't output anything to the console.
|
||||
const warnings_to_stderr = T &redef;
|
||||
|
||||
## Tunable for sending reporter error messages to STDERR. The option to
|
||||
## turn it off is presented here in case Bro is being run by some
|
||||
## external harness and shouldn't output anything to the console.
|
||||
const errors_to_stderr = T &redef;
|
||||
}
|
||||
module GLOBAL;
|
||||
|
||||
## Number of bytes per packet to capture from live interfaces.
|
||||
const snaplen = 8192 &redef;
|
||||
|
||||
|
|
|
@ -27,6 +27,13 @@ Reporter::Reporter()
|
|||
via_events = false;
|
||||
in_error_handler = 0;
|
||||
|
||||
// Always use stderr at startup/init before scripts have been fully parsed.
|
||||
// Messages may otherwise be missed if an error occurs that prevents events
|
||||
// from ever being dispatched.
|
||||
info_to_stderr = true;
|
||||
warnings_to_stderr = true;
|
||||
errors_to_stderr = true;
|
||||
|
||||
openlog("bro", 0, LOG_LOCAL5);
|
||||
}
|
||||
|
||||
|
@ -35,11 +42,19 @@ Reporter::~Reporter()
|
|||
closelog();
|
||||
}
|
||||
|
||||
void Reporter::InitOptions()
|
||||
{
|
||||
info_to_stderr = internal_const_val("Reporter::info_to_stderr")->AsBool();
|
||||
warnings_to_stderr = internal_const_val("Reporter::warnings_to_stderr")->AsBool();
|
||||
errors_to_stderr = internal_const_val("Reporter::errors_to_stderr")->AsBool();
|
||||
}
|
||||
|
||||
void Reporter::Info(const char* fmt, ...)
|
||||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
DoLog("", reporter_info, stderr, 0, 0, true, true, 0, fmt, ap);
|
||||
FILE* out = info_to_stderr ? stderr : 0;
|
||||
DoLog("", reporter_info, out, 0, 0, true, true, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
|
@ -47,7 +62,8 @@ void Reporter::Warning(const char* fmt, ...)
|
|||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
DoLog("warning", reporter_warning, stderr, 0, 0, true, true, 0, fmt, ap);
|
||||
FILE* out = warnings_to_stderr ? stderr : 0;
|
||||
DoLog("warning", reporter_warning, out, 0, 0, true, true, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
|
@ -56,7 +72,8 @@ void Reporter::Error(const char* fmt, ...)
|
|||
++errors;
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
DoLog("error", reporter_error, stderr, 0, 0, true, true, 0, fmt, ap);
|
||||
FILE* out = errors_to_stderr ? stderr : 0;
|
||||
DoLog("error", reporter_error, out, 0, 0, true, true, 0, fmt, ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
|
@ -98,7 +115,9 @@ void Reporter::ExprRuntimeError(const Expr* expr, const char* fmt, ...)
|
|||
PushLocation(expr->GetLocationInfo());
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
DoLog("expression error", reporter_error, stderr, 0, 0, true, true, d.Description(), fmt, ap);
|
||||
FILE* out = errors_to_stderr ? stderr : 0;
|
||||
DoLog("expression error", reporter_error, out, 0, 0, true, true,
|
||||
d.Description(), fmt, ap);
|
||||
va_end(ap);
|
||||
PopLocation();
|
||||
throw InterpreterException();
|
||||
|
@ -122,7 +141,9 @@ void Reporter::InternalWarning(const char* fmt, ...)
|
|||
{
|
||||
va_list ap;
|
||||
va_start(ap, fmt);
|
||||
DoLog("internal warning", reporter_warning, stderr, 0, 0, true, true, 0, fmt, ap);
|
||||
FILE* out = warnings_to_stderr ? stderr : 0;
|
||||
DoLog("internal warning", reporter_warning, out, 0, 0, true, true, 0, fmt,
|
||||
ap);
|
||||
va_end(ap);
|
||||
}
|
||||
|
||||
|
@ -189,7 +210,9 @@ void Reporter::Weird(const IPAddr& orig, const IPAddr& resp, const char* name)
|
|||
WeirdFlowHelper(orig, resp, "%s", name);
|
||||
}
|
||||
|
||||
void Reporter::DoLog(const char* prefix, EventHandlerPtr event, FILE* out, Connection* conn, val_list* addl, bool location, bool time, const char* postfix, const char* fmt, va_list ap)
|
||||
void Reporter::DoLog(const char* prefix, EventHandlerPtr event, FILE* out,
|
||||
Connection* conn, val_list* addl, bool location, bool time,
|
||||
const char* postfix, const char* fmt, va_list ap)
|
||||
{
|
||||
static char tmp[512];
|
||||
|
||||
|
@ -298,7 +321,7 @@ void Reporter::DoLog(const char* prefix, EventHandlerPtr event, FILE* out, Conne
|
|||
mgr.QueueEvent(event, vl);
|
||||
}
|
||||
|
||||
else
|
||||
if ( out )
|
||||
{
|
||||
string s = "";
|
||||
|
||||
|
|
|
@ -43,6 +43,9 @@ public:
|
|||
Reporter();
|
||||
~Reporter();
|
||||
|
||||
// Initialize reporter-sepcific options that are defined in script-layer.
|
||||
void InitOptions();
|
||||
|
||||
// Report an informational message, nothing that needs specific
|
||||
// attention.
|
||||
void Info(const char* fmt, ...) FMT_ATTR;
|
||||
|
@ -126,6 +129,9 @@ private:
|
|||
int errors;
|
||||
bool via_events;
|
||||
int in_error_handler;
|
||||
bool info_to_stderr;
|
||||
bool warnings_to_stderr;
|
||||
bool errors_to_stderr;
|
||||
|
||||
std::list<std::pair<const Location*, const Location*> > locations;
|
||||
};
|
||||
|
|
|
@ -824,6 +824,8 @@ int main(int argc, char** argv)
|
|||
exit(1);
|
||||
}
|
||||
|
||||
reporter->InitOptions();
|
||||
|
||||
init_general_global_var();
|
||||
|
||||
if ( user_pcap_filter )
|
||||
|
|
|
@ -1,3 +1,3 @@
|
|||
error in /home/jsiwek/bro/testing/btest/.tmp/core.reporter-error-in-handler/reporter-error-in-handler.bro, line 22: no such index (a[2])
|
||||
ERROR: no such index (a[1]) (/home/jsiwek/bro/testing/btest/.tmp/core.reporter-error-in-handler/reporter-error-in-handler.bro, line 28)
|
||||
error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter-error-in-handler/reporter-error-in-handler.bro, line 28: no such index (a[1])
|
||||
error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter-error-in-handler/reporter-error-in-handler.bro, line 22: no such index (a[2])
|
||||
1st error printed on script level
|
||||
|
|
|
@ -1,2 +1,2 @@
|
|||
error in /home/jsiwek/bro/testing/btest/.tmp/core.reporter-runtime-error/reporter-runtime-error.bro, line 12: no such index (a[1])
|
||||
ERROR: no such index (a[2]) (/home/jsiwek/bro/testing/btest/.tmp/core.reporter-runtime-error/reporter-runtime-error.bro, line 9)
|
||||
error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter-runtime-error/reporter-runtime-error.bro, line 12: no such index (a[1])
|
||||
error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter-runtime-error/reporter-runtime-error.bro, line 9: no such index (a[2])
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
reporter_info|init test-info|/home/jsiwek/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 8|0.000000
|
||||
reporter_warning|init test-warning|/home/jsiwek/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 9|0.000000
|
||||
reporter_error|init test-error|/home/jsiwek/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 10|0.000000
|
||||
reporter_info|done test-info|/home/jsiwek/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 15|0.000000
|
||||
reporter_warning|done test-warning|/home/jsiwek/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 16|0.000000
|
||||
reporter_error|done test-error|/home/jsiwek/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 17|0.000000
|
||||
reporter_info|init test-info|/Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 8|0.000000
|
||||
reporter_warning|init test-warning|/Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 9|0.000000
|
||||
reporter_error|init test-error|/Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 10|0.000000
|
||||
reporter_info|done test-info|/Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 15|0.000000
|
||||
reporter_warning|done test-warning|/Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 16|0.000000
|
||||
reporter_error|done test-error|/Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 17|0.000000
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
/home/jsiwek/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 52: pre test-info
|
||||
warning in /home/jsiwek/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 53: pre test-warning
|
||||
error in /home/jsiwek/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 54: pre test-error
|
||||
WARNING: init test-warning (/home/jsiwek/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 9)
|
||||
ERROR: init test-error (/home/jsiwek/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 10)
|
||||
WARNING: done test-warning (/home/jsiwek/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 16)
|
||||
ERROR: done test-error (/home/jsiwek/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 17)
|
||||
/Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 52: pre test-info
|
||||
warning in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 53: pre test-warning
|
||||
error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 54: pre test-error
|
||||
/Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 8: init test-info
|
||||
warning in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 9: init test-warning
|
||||
error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 10: init test-error
|
||||
/Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 15: done test-info
|
||||
warning in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 16: done test-warning
|
||||
error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/core.reporter/reporter.bro, line 17: done test-error
|
||||
|
|
|
@ -1 +1 @@
|
|||
ERROR: no such index (test[3]) (/blah/testing/btest/.tmp/scripts.base.frameworks.reporter.stderr/stderr.bro, line 9)
|
||||
error in /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/scripts.base.frameworks.reporter.stderr/stderr.bro, line 9: no such index (test[3])
|
||||
|
|
|
@ -3,8 +3,8 @@
|
|||
#empty_field (empty)
|
||||
#unset_field -
|
||||
#path reporter
|
||||
#open 2012-08-10-20-09-23
|
||||
#open 2013-01-18-18-29-30
|
||||
#fields ts level message location
|
||||
#types time enum string string
|
||||
0.000000 Reporter::ERROR no such index (test[3]) /da/home/robin/bro/master/testing/btest/.tmp/scripts.base.frameworks.reporter.stderr/stderr.bro, line 9
|
||||
#close 2012-08-10-20-09-23
|
||||
0.000000 Reporter::ERROR no such index (test[3]) /Users/jsiwek/Projects/bro/bro/testing/btest/.tmp/scripts.base.frameworks.reporter.stderr/stderr.bro, line 9
|
||||
#close 2013-01-18-18-29-30
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue