log.cc 1.73 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
// 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.
31
static void logPrefix(int which)
32
{
33
  switch(which)
34
  {
35
  case ERROR:
36
    fprintf(logFile, "ERROR ");
37
38
    break;
  case EXCEPTION:
39
    fprintf(logFile, "EXCEPTION ");
40
41
    break;
  case PEER_CYCLE:
42
    fprintf(logFile, "PEER_CYCLE ");
43
44
    break;
  case SENSOR:
45
    fprintf(logFile, "SENSOR ");
46
47
    break;
  case CONNECTION_MODEL:
48
    fprintf(logFile, "CONNECTION_MODEL ");
49
50
    break;
  case ROBUST:
51
    fprintf(logFile, "ROBUST ");
52
53
    break;
  case MAIN_LOOP:
54
    fprintf(logFile, "MAIN_LOOP ");
55
56
    break;
  case COMMAND_INPUT:
57
    fprintf(logFile, "COMMAND_INPUT ");
58
59
    break;
  case CONNECTION:
60
    fprintf(logFile, "CONNECTION ");
61
62
    break;
  case PCAP:
63
    fprintf(logFile, "PCAP ");
64
65
    break;
  case COMMAND_OUTPUT:
66
    fprintf(logFile, "COMMAND_OUTPUT ");
67
68
69
70
    break;
  default:
    fprintf(logFile, "LOG_ERROR ");
    break;
71
  }
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
  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);
}