log.cc 1.69 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
// log.c

#include "lib.h"
#include "log.h"

static FILE * logFile;
static int logFlags;
static int logTimestamp;

void logInit(FILE * destFile, int flags, int useTimestamp)
{
  if (destFile == NULL || flags == LOG_NOTHING)
  {
    logFile = NULL;
    logFlags = LOG_NOTHING;
    logTimestamp = 0;
  }
  else
  {
    logFile = destFile;
    logFlags = flags;
    logTimestamp = useTimestamp;
  }
}

void logCleanup(void)
{
}

// Print the timestamp and type of logging to the logFile.
static void logPrefix(int flags)
{
  if (flags & ERROR)
  {
    fprintf(logFile, "ERROR ");
  }
  if (flags & EXCEPTION)
  {
    fprintf(logFile, "EXCEPTION ");
  }
  if (flags & PEER_CYCLE)
  {
    fprintf(logFile, "PEER_CYCLE ");
  }
45 46 47 48
  if (flags & SENSOR)
  {
    fprintf(logFile, "SENSOR ");
  }
49 50 51 52 53 54 55 56
  if (flags & CONNECTION_MODEL)
  {
    fprintf(logFile, "CONNECTION_MODEL ");
  }
  if (flags & ROBUST)
  {
    fprintf(logFile, "ROBUST ");
  }
57 58 59 60
  if (flags & MAIN_LOOP)
  {
    fprintf(logFile, "MAIN_LOOP ");
  }
61 62 63 64 65 66 67 68
  if (flags & COMMAND_INPUT)
  {
    fprintf(logFile, "COMMAND_INPUT ");
  }
  if (flags & CONNECTION)
  {
    fprintf(logFile, "CONNECTION ");
  }
69 70 71 72
  if (flags & PCAP)
  {
    fprintf(logFile, "PCAP ");
  }
73 74 75 76
  if (flags & COMMAND_OUTPUT)
  {
    fprintf(logFile, "COMMAND_OUTPUT ");
  }
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
  if (logTimestamp)
  {
    struct timeval now;
    gettimeofday(&now, NULL);
    fprintf(logFile, "%f ", (double)(now.tv_sec) +
            ((now.tv_usec)/1000)/1000.0);
  }
  fprintf(logFile, ": ");
}

void logWrite(int flags, char const * format, ...)
{
  va_list va;
  va_start(va, format);
  if ((flags & logFlags) != 0)
  {
    logPrefix(flags & logFlags);
    vfprintf(logFile, format, va);
    fprintf(logFile, "\n");
  }
  va_end(va);
}