log.c 7.11 KB
Newer Older
1 2 3
/*
 *	BIRD Library -- Logging Functions
 *
4
 *	(c) 1998--2000 Martin Mares <mj@ucw.cz>
5 6 7 8
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

9 10 11 12
/**
 * DOC: Logging
 *
 * The Logging module offers a simple set of functions for writing
13 14 15
 * messages to system logs and to the debug output. Message classes
 * used by this module are described in |birdlib.h| and also in the
 * user's manual.
16 17
 */

18 19 20
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
21
#include <time.h>
22
#include <unistd.h>
23
#include <errno.h>
24 25

#include "nest/bird.h"
26
#include "nest/cli.h"
27
#include "nest/mrtdump.h"
28
#include "lib/string.h"
29 30
#include "lib/lists.h"
#include "lib/unix.h"
31

32
static FILE *dbgf;
33
static list *current_log_list;
34
static char *current_syslog_name; /* NULL -> syslog closed */
35

36

37
#ifdef USE_PTHREADS
38 39

#include <pthread.h>
40

41 42 43 44
static pthread_mutex_t log_mutex;
static inline void log_lock(void) { pthread_mutex_lock(&log_mutex); }
static inline void log_unlock(void) { pthread_mutex_unlock(&log_mutex); }

45 46 47 48
static pthread_t main_thread;
void main_thread_init(void) { main_thread = pthread_self(); }
static int main_thread_self(void) { return pthread_equal(pthread_self(), main_thread); }

49 50 51 52
#else

static inline void log_lock(void) {  }
static inline void log_unlock(void) {  }
53 54
void main_thread_init(void) { }
static int main_thread_self(void) { return 1; }
55 56 57

#endif

58

59
#ifdef HAVE_SYSLOG_H
60 61 62
#include <sys/syslog.h>

static int syslog_priorities[] = {
63 64
  LOG_DEBUG,
  LOG_DEBUG,
65 66
  LOG_DEBUG,
  LOG_INFO,
67
  LOG_ERR,
68 69
  LOG_WARNING,
  LOG_ERR,
70 71
  LOG_ERR,
  LOG_CRIT,
72 73 74 75 76 77 78
  LOG_CRIT
};
#endif

static char *class_names[] = {
  "???",
  "DBG",
79
  "TRACE",
80
  "INFO",
81
  "RMT",
82 83 84
  "WARN",
  "ERR",
  "AUTH",
85 86
  "FATAL",
  "BUG"
87 88
};

89

90 91 92
/**
 * log_commit - commit a log message
 * @class: message class information (%L_DEBUG to %L_BUG, see |lib/birdlib.h|)
93
 * @buf: message to write
94 95 96 97 98 99 100 101 102 103
 *
 * This function writes a message prepared in the log buffer to the
 * log file (as specified in the configuration). The log buffer is
 * reset after that. The log message is a full line, log_commit()
 * terminates it.
 *
 * The message class is an integer, not a first char of a string like
 * in log(), so it should be written like *L_INFO.
 */
void
104
log_commit(int class, buffer *buf)
105 106
{
  struct log_config *l;
107

108 109 110 111
  if (buf->pos == buf->end)
    strcpy(buf->end - 100, " ... <too long>");

  log_lock();
112
  WALK_LIST(l, *current_log_list)
113
    {
114 115 116 117 118 119 120 121
      if (!(l->mask & (1 << class)))
	continue;
      if (l->fh)
	{
	  if (l->terminal_flag)
	    fputs("bird: ", l->fh);
	  else
	    {
122 123 124
	      byte tbuf[TM_DATETIME_BUFFER_SIZE];
	      tm_format_datetime(tbuf, &config->tf_log, now);
	      fprintf(l->fh, "%s <%s> ", tbuf, class_names[class]);
125
	    }
126
	  fputs(buf->start, l->fh);
127 128 129
	  fputc('\n', l->fh);
	  fflush(l->fh);
	}
130
#ifdef HAVE_SYSLOG_H
131
      else
132
	syslog(syslog_priorities[class], "%s", buf->start);
133 134
#endif
    }
135
  log_unlock();
136

137 138 139
  /* cli_echo is not thread-safe, so call it just from the main thread */
  if (main_thread_self())
    cli_echo(class, buf->start);
140 141

  buf->pos = buf->start;
142 143
}

144
int buffer_vprint(buffer *buf, const char *fmt, va_list args);
145 146 147 148

static void
vlog(int class, const char *msg, va_list args)
{
149 150 151 152
  buffer buf;
  LOG_BUFFER_INIT(buf);
  buffer_vprint(&buf, msg, args);
  log_commit(class, &buf);
153 154 155
}


156 157 158 159 160 161
/**
 * log - log a message
 * @msg: printf-like formatting string with message class information
 * prepended (%L_DEBUG to %L_BUG, see |lib/birdlib.h|)
 *
 * This function formats a message according to the format string @msg
162
 * and writes it to the corresponding log file (as specified in the
163 164
 * configuration). Please note that the message is automatically
 * formatted as a full line, no need to include |\n| inside.
165
 * It is essentially a sequence of log_reset(), logn() and log_commit().
166
 */
167
void
168
log_msg(const char *msg, ...)
169 170 171 172 173
{
  int class = 1;
  va_list args;

  va_start(args, msg);
174
  if (*msg >= 1 && *msg <= 8)
175 176 177 178 179
    class = *msg++;
  vlog(class, msg, args);
  va_end(args);
}

180
void
181
log_rl(struct tbf *f, const char *msg, ...)
182
{
183
  int last_hit = f->mark;
184 185 186
  int class = 1;
  va_list args;

187 188
  /* Rate limiting is a bit tricky here as it also logs '...' during the first hit */
  if (tbf_limit(f) && last_hit)
189 190 191 192
    return;

  if (*msg >= 1 && *msg <= 8)
    class = *msg++;
193 194 195

  va_start(args, msg);
  vlog(class, (f->mark ? "..." : msg), args);
196 197 198
  va_end(args);
}

199 200 201 202 203 204 205
/**
 * bug - report an internal error
 * @msg: a printf-like error message
 *
 * This function logs an internal error and aborts execution
 * of the program.
 */
206
void
207
bug(const char *msg, ...)
208 209 210 211 212
{
  va_list args;

  va_start(args, msg);
  vlog(L_BUG[0], msg, args);
213
  va_end(args);
214
  abort();
215 216
}

217 218 219 220 221 222 223
/**
 * bug - report a fatal error
 * @msg: a printf-like error message
 *
 * This function logs a fatal error and aborts execution
 * of the program.
 */
224
void
225
die(const char *msg, ...)
226 227 228 229
{
  va_list args;

  va_start(args, msg);
230
  vlog(L_FATAL[0], msg, args);
231
  va_end(args);
232 233 234
  exit(1);
}

235 236 237 238 239 240 241
/**
 * debug - write to debug output
 * @msg: a printf-like message
 *
 * This function formats the message @msg and prints it out
 * to the debugging output. No newline character is appended.
 */
242
void
243
debug(const char *msg, ...)
244 245
{
  va_list args;
246
  char buf[1024];
247 248 249

  va_start(args, msg);
  if (dbgf)
250 251 252 253 254
    {
      if (bvsnprintf(buf, sizeof(buf), msg, args) < 0)
	bsprintf(buf + sizeof(buf) - 100, " ... <too long>\n");
      fputs(buf, dbgf);
    }
255 256 257
  va_end(args);
}

258 259
static list *
default_log_list(int debug, int init, char **syslog_name)
260
{
261
  static list init_log_list;
262
  init_list(&init_log_list);
263
  *syslog_name = NULL;
264

265
#ifdef HAVE_SYSLOG_H
266
  if (!debug)
267
    {
268
      static struct log_config lc_syslog = { .mask = ~0 };
269 270
      add_tail(&init_log_list, &lc_syslog.n);
      *syslog_name = bird_name;
271
      if (!init)
272
	return &init_log_list;
273
    }
274 275
#endif

276
  static struct log_config lc_stderr = { .mask = ~0, .terminal_flag = 1 };
277
  lc_stderr.fh = stderr;
278 279
  add_tail(&init_log_list, &lc_stderr.n);
  return &init_log_list;
280 281 282
}

void
283
log_switch(int debug, list *l, char *new_syslog_name)
284
{
285 286 287 288 289
  if (!l || EMPTY_LIST(*l))
    l = default_log_list(debug, !l, &new_syslog_name);

  current_log_list = l;

290
#ifdef HAVE_SYSLOG_H
291 292
  if (current_syslog_name && new_syslog_name &&
      !strcmp(current_syslog_name, new_syslog_name))
293 294
    return;

295 296
  if (current_syslog_name)
  {
297
    closelog();
298 299 300
    xfree(current_syslog_name);
    current_syslog_name = NULL;
  }
301 302

  if (new_syslog_name)
303 304 305 306
  {
    current_syslog_name = xstrdup(new_syslog_name);
    openlog(current_syslog_name, LOG_CONS | LOG_NDELAY, LOG_DAEMON);
  }
307
#endif
308 309
}

310 311


312 313 314 315 316 317 318
void
log_init_debug(char *f)
{
  if (dbgf && dbgf != stderr)
    fclose(dbgf);
  if (!f)
    dbgf = NULL;
319 320
  else if (!*f)
    dbgf = stderr;
321
  else if (!(dbgf = fopen(f, "a")))
322 323 324 325 326
  {
    /* Cannot use die() nor log() here, logging is not yet initialized */
    fprintf(stderr, "bird: Unable to open debug file %s: %s\n", f, strerror(errno));
    exit(1);
  }
327 328
  if (dbgf)
    setvbuf(dbgf, NULL, _IONBF, 0);
329
}
330 331 332 333 334 335 336 337 338 339 340 341 342

void
mrt_dump_message(struct proto *p, u16 type, u16 subtype, byte *buf, u32 len)
{
  /* Prepare header */
  put_u32(buf+0, now_real);
  put_u16(buf+4, type);
  put_u16(buf+6, subtype);
  put_u32(buf+8, len - MRTDUMP_HDR_LENGTH);

  if (p->cf->global->mrtdump_file != -1)
    write(p->cf->global->mrtdump_file, buf, len);
}