log.c 1.49 KB
Newer Older
1
2
3
/* Copyright 2001,2002 Roger Dingledine, Matej Pfajfar. */
/* See LICENSE for licensing information */
/* $Id$ */
Roger Dingledine's avatar
Roger Dingledine committed
4

5
#include "../or/or.h"
Roger Dingledine's avatar
Roger Dingledine committed
6
7
#include "log.h"

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
/* FIXME this whole thing is hacked together. feel free to make it clean. */
size_t sev_to_string(char *buf, int max, int severity) {
  assert(max > 20);

  switch(severity) {
    case LOG_DEBUG:
      strcpy(buf,"debug");  
      break;
    case LOG_INFO:
      strcpy(buf,"info");
      break;
    case LOG_NOTICE:
      strcpy(buf,"notice");
      break;
    case LOG_WARNING:
      strcpy(buf,"warn");
      break;
    case LOG_ERR:
      strcpy(buf,"err");
      break;
    case LOG_CRIT:
      strcpy(buf,"crit");
      break;
    case LOG_ALERT:
      strcpy(buf,"alert");
      break;
    case LOG_EMERG: 
      strcpy(buf,"emerg");
      break;
    default:
      strcpy(buf,"UNKNOWN");
      break;
  }

  return strlen(buf)+1;
}

Roger Dingledine's avatar
Roger Dingledine committed
45
46
47
/* Outputs a message to stdout */
void log(int severity, const char *format, ...)
{
48
  static int loglevel = LOG_DEBUG;
49
50
  char buf[201];
  time_t t;
Roger Dingledine's avatar
Roger Dingledine committed
51
  va_list ap;
52
53
54
  struct timeval now;

  if (format) {
Roger Dingledine's avatar
Roger Dingledine committed
55

56
57
    if(gettimeofday(&now,NULL) < 0)
      return;
58
59

    va_start(ap,format);
Roger Dingledine's avatar
Roger Dingledine committed
60
  
61
62
    if (severity <= loglevel)
    {
63
64
      t = time(NULL);
      strftime(buf, 200, "%b %d %H:%M:%S", localtime(&t));
65
      printf("%s.%.3ld ", buf, now.tv_usec / 1000);
66
67
      sev_to_string(buf, 200, severity);
      printf("[%s] ", buf);
68
69
70
71
72
73
74
75
      vprintf(format,ap);
      printf("\n");
    }
    
    va_end(ap);
  }
  else
    loglevel = severity;
Roger Dingledine's avatar
Roger Dingledine committed
76
}
77