summaryrefslogtreecommitdiff
path: root/src/common/log.c
blob: 8c0a0625275e27fdc527ee7830b0868c114d7be6 (plain)
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
 * log.c
 * Logging facilities.
 *
 * Matej Pfajfar <mp292@cam.ac.uk>
 */

/*
 * Changes :
 * $Log$
 * Revision 1.3  2002/09/04 00:39:33  arma
 * the logs now include a timestamp and severity
 *
 * the implementation is sort of a kludge..you're welcome to fix it up
 *
 * Revision 1.2  2002/07/12 18:14:16  montrose
 * removed loglevel from global namespace. severity level is set using log() with a NULL format argument now. example: log(LOG_ERR,NULL);
 *
 * Revision 1.1.1.1  2002/06/26 22:45:50  arma
 * initial commit: current code
 *
 * Revision 1.11  2002/06/14 20:44:57  mp292
 * *** empty log message ***
 *
 * Revision 1.10  2002/03/12 23:31:36  mp292
 * *** empty log message ***
 *
 * Revision 1.9  2002/03/02 18:55:50  mp292
 * LOG_DEBUG messages don't print the last errno error anymore.
 *
 * Revision 1.8  2002/01/26 22:46:48  mp292
 * Reviewd according to Secure-Programs-HOWTO.
 *
 * Revision 1.7  2002/01/17 15:00:43  mp292
 * Fixed a bug which caused malloc() generate a seg fault.
 *
 * Revision 1.6  2001/12/12 16:02:55  badbytes
 * Minor changes in output format.
 *
 * Revision 1.5  2001/12/12 06:48:07  badbytes
 * Correction - last error message now only shown if severity==LOG_DEBUG.
 *
 * Revision 1.4  2001/12/12 06:28:46  badbytes
 * Modified log() to print error message for last error in addition to the user-specified message.
 *
 * Revision 1.3  2001/12/07 09:38:03  badbytes
 * Tested.
 *
 * Revision 1.2  2001/12/06 15:43:50  badbytes
 * config.c compiles. Proceeding to test it.
 *
 * Revision 1.1  2001/11/21 23:03:41  mp292
 * log function coded and tested.
 * Top-level makefile.
 *
 */

#include <stdio.h>
#include <stdarg.h>
#include <syslog.h>
#include <string.h>
#include <errno.h>
#include <assert.h>
#include <time.h>
#include "log.h"

/* 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;
}

/* Outputs a message to stdout */
void log(int severity, const char *format, ...)
{
  static int loglevel = LOG_DEBUG;
  char buf[201];
  time_t t;
  va_list ap;

  if ( format )
  {

    va_start(ap,format);
  
    if (severity <= loglevel)
    {
      t = time(NULL);
      strftime(buf, 200, "%b %d %H:%M:%S", localtime(&t));
      printf("%s ", buf);
      sev_to_string(buf, 200, severity);
      printf("[%s] ", buf);
      vprintf(format,ap);
      printf("\n");
    }
    
    va_end(ap);
  }
  else
    loglevel = severity;
}