zeek/src/DebugLogger.cc
Robin Sommer 68062e87f1 Lots of infracstructure for the new logging framework.
This pretty much follows the proposal on the projects page.

It includes:

    - A new LogMgr, maintaining the set of writers.

    - The abstract LogWriter API.

    - An initial implementation in the form of LogWriterAscii
      producing tab-separated columns.

Note that things are only partially working right now, things are
subject to change, and it's all not much tested at all. That's why I'm
creating separate branch for now.

Example:

     bro -B logging test-logging && cat debug.log
    1298063168.409852/1298063168.410368 [logging] Created new logging stream 'SSH::LOG_SSH'
    1298063168.409852/1298063168.410547 [logging] Created new filter 'default' for stream 'SSH::LOG_SSH'
    1298063168.409852/1298063168.410564 [logging]    writer    : Ascii
    1298063168.409852/1298063168.410574 [logging]    path      : ssh_log_ssh
    1298063168.409852/1298063168.410584 [logging]    path_func : not set
    1298063168.409852/1298063168.410594 [logging]    event     : not set
    1298063168.409852/1298063168.410604 [logging]    pred      : not set
    1298063168.409852/1298063168.410614 [logging]    field          t: time
    1298063168.409852/1298063168.410625 [logging]    field  id.orig_h: addr
    1298063168.409852/1298063168.410635 [logging]    field  id.orig_p: port
    1298063168.409852/1298063168.410645 [logging]    field  id.resp_h: addr
    1298063168.409852/1298063168.410655 [logging]    field  id.resp_p: port
    1298063168.409852/1298063168.410665 [logging]    field     status: string
    1298063168.409852/1298063168.410675 [logging]    field    country: string
    1298063168.409852/1298063168.410817 [logging] Wrote record to filter 'default' on stream 'SSH::LOG_SSH'
    1298063168.409852/1298063168.410865 [logging] Wrote record to filter 'default' on stream 'SSH::LOG_SSH'
    1298063168.409852/1298063168.410906 [logging] Wrote record to filter 'default' on stream 'SSH::LOG_SSH'
    1298063168.409852/1298063168.410945 [logging] Wrote record to filter 'default' on stream 'SSH::LOG_SSH'
    1298063168.409852/1298063168.411044 [logging] Wrote record to filter 'default' on stream 'SSH::LOG_SSH

> cat ssh_log_ssh.log
1298063168.40985        1.2.3.4 66770   2.3.4.5 65616   success unknown
1298063168.40985        1.2.3.4 66770   2.3.4.5 65616   failure US
1298063168.40985        1.2.3.4 66770   2.3.4.5 65616   failure UK
1298063168.40985        1.2.3.4 66770   2.3.4.5 65616   success BR
1298063168.40985        1.2.3.4 66770   2.3.4.5 65616   failure MX
2011-02-18 13:03:46 -08:00

100 lines
1.8 KiB
C++

// $Id: DebugLogger.cc 4771 2007-08-11 05:50:24Z vern $
#ifdef DEBUG
#include <stdlib.h>
#include <unistd.h>
#include "DebugLogger.h"
#include "Net.h"
DebugLogger debug_logger("debug");
// Same order here as in DebugStream.
DebugLogger::Stream DebugLogger::streams[NUM_DBGS] = {
{ "serial", 0, false }, { "rules", 0, false }, { "comm", 0, false },
{ "state", 0, false }, { "chunkedio", 0, false },
{ "compressor", 0, false }, {"string", 0, false },
{ "notifiers", 0, false }, { "main-loop", 0, false },
{ "dpd", 0, false }, { "tm", 0, false },
{ "logging", 0, false }
};
DebugLogger::DebugLogger(const char* filename)
{
if ( filename )
{
filename = log_file_name(filename);
file = fopen(filename, "w");
if ( ! file )
{
fprintf(stderr, "Can't open '%s' for debugging output.", filename);
exit(1);
}
setvbuf(file, NULL, _IOLBF, 0);
}
else
file = stderr;
verbose = false;
}
DebugLogger::~DebugLogger()
{
if ( file != stderr )
fclose(file);
}
void DebugLogger::EnableStreams(const char* s)
{
char* tmp = copy_string(s);
char* brkt;
char* tok = strtok(tmp, ",");
while ( tok )
{
int i;
for ( i = 0; i < NUM_DBGS; ++i )
if ( strcasecmp(streams[i].prefix, tok) == 0 )
{
streams[i].enabled = true;
break;
}
if ( i == NUM_DBGS )
{
if ( strcasecmp("verbose", tok) == 0 )
verbose = true;
else
internal_error("unknown debug stream %s\n", tok);
}
tok = strtok(0, ",");
}
}
void DebugLogger::Log(DebugStream stream, const char* fmt, ...)
{
Stream* g = &streams[int(stream)];
if ( ! g->enabled )
return;
fprintf(file, "%17.06f/%17.06f [%s] ",
network_time, current_time(true), g->prefix);
for ( int i = g->indent; i > 0; --i )
fputs(" ", file);
va_list ap;
va_start(ap, fmt);
vfprintf(file, fmt, ap);
va_end(ap);
fputc('\n', file);
fflush(file);
}
#endif