Overhauling the internal reporting of messages to the user.

The Logger class is now in charge of reporting all errors, warnings,
informational messages, weirds, and syslogs. All other components
route their messages through the global bro_logger singleton.

The Logger class comes with these reporting methods:

    void Message(const char* fmt, ...);
    void Warning(const char* fmt, ...);
    void Error(const char* fmt, ...);
    void FatalError(const char* fmt, ...); // Terminate Bro.
    void Weird(const char* name);
    [ .. some more Weird() variants ... ]
    void Syslog(const char* fmt, ...);
    void InternalWarning(const char* fmt, ...);
    void InternalError(const char* fmt, ...); // Terminates Bro.

See Logger.h for more information on these.

Generally, the reporting now works as follows:

    - All non-fatal message are reported in one of two ways:

        (1) At startup (i.e., before we start processing packets),
            they are logged to stderr.

        (2) During processing, they turn into events:

            event log_message%(msg: string, location: string%);
            event log_warning%(msg: string, location: string%);
            event log_error%(msg: string, location: string%);

            The script level can then handle them as desired.

            If we don't have an event handler, we fall back to
            reporting on stderr.

    - All fatal errors are logged to stderr and Bro terminates
      immediately.

    - Syslog(msg) directly syslogs, but doesn't do anything else.

The three main types of messages can also be generated on the
scripting layer via new Log::* bifs:

    Log::error(msg: string);
    Log::warning(msg: string);
    Log::message(msg: string);

These pass through the bro_logger as well and thus are handled in the
same way. Their output includes location information.

More changes:

    - Removed the alarm statement and the alarm_hook event.

    - Adapted lots of locations to use the bro_logger, including some
      of the messages that were previously either just written to
      stdout, or even funneled through the alarm mechanism.

    - No distinction anymore between Error() and RunTime(). There's
      now only one class of errors; the line was quite blurred already
      anyway.

    - util.h: all the error()/warn()/message()/run_time()/pinpoint()
      functions are gone. Use the bro_logger instead now.

    - Script errors are formatted a bit differently due to the
      changes. What I've seen so far looks ok to me, but let me know
      if there's something odd.

Notes:

    - The default handlers for the new log_* events are just dummy
      implementations for now since we need to integrate all this into
      the new scripts anyway.

    - I'm not too happy with the names of the Logger class and its
      instance bro_logger. We now have a LogMgr as well, which makes
      this all a bit confusing. But I didn't have a good idea for
      better names so I stuck with them for now.

      Perhaps we should merge Logger and LogMgr?
This commit is contained in:
Robin Sommer 2011-06-24 21:33:05 -07:00
parent ff7b92ffc8
commit 93894eed9b
140 changed files with 2453 additions and 1054 deletions

View file

@ -35,6 +35,7 @@
#include "Event.h"
#include "Net.h"
#include "Var.h"
#include "Logger.h"
extern "C" {
extern int select(int, fd_set *, fd_set *, fd_set *, struct timeval *);
@ -352,7 +353,7 @@ DNS_Mgr::DNS_Mgr(DNS_MgrMode arg_mode)
nb_dns = nb_dns_init(err);
if ( ! nb_dns )
warn(fmt("problem initializing NB-DNS: %s", err));
bro_logger->Warning(fmt("problem initializing NB-DNS: %s", err));
dns_mapping_valid = dns_mapping_unverified = dns_mapping_new_name =
dns_mapping_lost_name = dns_mapping_name_changed =
@ -439,7 +440,7 @@ TableVal* DNS_Mgr::LookupHost(const char* name)
return d->Addrs()->ConvertToSet();
else
{
warn("no such host:", name);
bro_logger->Warning("no such host:", name);
return empty_addr_set();
}
}
@ -452,7 +453,7 @@ TableVal* DNS_Mgr::LookupHost(const char* name)
return empty_addr_set();
case DNS_FORCE:
internal_error("can't find DNS entry for %s in cache", name);
bro_logger->InternalError("can't find DNS entry for %s in cache", name);
return 0;
case DNS_DEFAULT:
@ -461,7 +462,7 @@ TableVal* DNS_Mgr::LookupHost(const char* name)
return LookupHost(name);
default:
internal_error("bad mode in DNS_Mgr::LookupHost");
bro_logger->InternalError("bad mode in DNS_Mgr::LookupHost");
return 0;
}
}
@ -482,7 +483,7 @@ Val* DNS_Mgr::LookupAddr(uint32 addr)
return d->Host();
else
{
warn("can't resolve IP address:", dotted_addr(addr));
bro_logger->Warning("can't resolve IP address:", dotted_addr(addr));
return new StringVal(dotted_addr(addr));
}
}
@ -495,7 +496,7 @@ Val* DNS_Mgr::LookupAddr(uint32 addr)
return new StringVal("<none>");
case DNS_FORCE:
internal_error("can't find DNS entry for %s in cache",
bro_logger->InternalError("can't find DNS entry for %s in cache",
dotted_addr(addr));
return 0;
@ -505,7 +506,7 @@ Val* DNS_Mgr::LookupAddr(uint32 addr)
return LookupAddr(addr);
default:
internal_error("bad mode in DNS_Mgr::LookupHost");
bro_logger->InternalError("bad mode in DNS_Mgr::LookupHost");
return 0;
}
}
@ -566,7 +567,7 @@ void DNS_Mgr::Resolve()
struct nb_dns_result r;
status = nb_dns_activity(nb_dns, &r, err);
if ( status < 0 )
internal_error(
bro_logger->InternalError(
"NB-DNS error in DNS_Mgr::WaitForReplies (%s)",
err);
else if ( status > 0 )
@ -744,7 +745,7 @@ void DNS_Mgr::CompareMappings(DNS_Mapping* prev_dm, DNS_Mapping* new_dm)
ListVal* new_a = new_dm->Addrs();
if ( ! prev_a || ! new_a )
internal_error("confused in DNS_Mgr::CompareMappings");
bro_logger->InternalError("confused in DNS_Mgr::CompareMappings");
ListVal* prev_delta = AddrListDelta(prev_a, new_a);
ListVal* new_delta = AddrListDelta(new_a, prev_a);
@ -814,7 +815,7 @@ void DNS_Mgr::LoadCache(FILE* f)
}
if ( ! m->NoMapping() )
internal_error("DNS cache corrupted");
bro_logger->InternalError("DNS cache corrupted");
delete m;
fclose(f);
@ -934,7 +935,7 @@ void DNS_Mgr::IssueAsyncRequests()
if ( ! dr->MakeRequest(nb_dns) )
{
run_time("can't issue DNS request");
bro_logger->Error("can't issue DNS request");
req->Timeout();
continue;
}
@ -1047,7 +1048,7 @@ void DNS_Mgr::Process()
int status = nb_dns_activity(nb_dns, &r, err);
if ( status < 0 )
internal_error("NB-DNS error in DNS_Mgr::Process (%s)", err);
bro_logger->InternalError("NB-DNS error in DNS_Mgr::Process (%s)", err);
else if ( status > 0 )
{
@ -1071,7 +1072,7 @@ int DNS_Mgr::AnswerAvailable(int timeout)
{
int fd = nb_dns_fd(nb_dns);
if ( fd < 0 )
internal_error("nb_dns_fd() failed in DNS_Mgr::WaitForReplies");
bro_logger->InternalError("nb_dns_fd() failed in DNS_Mgr::WaitForReplies");
fd_set read_fds;
@ -1088,11 +1089,11 @@ int DNS_Mgr::AnswerAvailable(int timeout)
{
if ( errno == EINTR )
return -1;
internal_error("problem with DNS select");
bro_logger->InternalError("problem with DNS select");
}
if ( status > 1 )
internal_error("strange return from DNS select");
bro_logger->InternalError("strange return from DNS select");
return status;
}