util.c 996 Bytes
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 *	BIRD Client -- Utility Functions
 *
 *	(c) 1999--2000 Martin Mares <mj@ucw.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>

#include "nest/bird.h"
14
#include "lib/string.h"
15 16 17 18
#include "client/client.h"

/* Client versions of logging functions */

19
static void
20
vlog(const char *msg, va_list args)
21 22 23
{
  char buf[1024];

24 25 26
  int n = vsnprintf(buf, sizeof(buf), msg, args);
  if (n < 0)
    snprintf(buf, sizeof(buf), "???");
27
  else if (n >= (int) sizeof(buf))
28
    snprintf(buf + sizeof(buf) - 100, 100, " ... <too long>");
29 30 31
  fputs(buf, stderr);
  fputc('\n', stderr);
}
32

33
void
34
bug(const char *msg, ...)
35 36 37 38
{
  va_list args;

  va_start(args, msg);
39
  cleanup();
40
  fputs("Internal error: ", stderr);
41
  vlog(msg, args);
42
  vfprintf(stderr, msg, args);
43
  va_end(args);
44 45 46 47
  exit(1);
}

void
48
die(const char *msg, ...)
49 50 51 52
{
  va_list args;

  va_start(args, msg);
53
  cleanup();
54
  vlog(msg, args);
55
  va_end(args);
56 57
  exit(1);
}