log.c 1.99 KB
Newer Older
Leigh Stoller's avatar
Leigh Stoller committed
1 2
/*
 * EMULAB-COPYRIGHT
3
 * Copyright (c) 2000-2003 University of Utah and the Flux Group.
Leigh Stoller's avatar
Leigh Stoller committed
4 5 6
 * All rights reserved.
 */

7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Logging and debug routines.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <syslog.h>
#include <assert.h>
#include <errno.h>
#include "decls.h"

20 21 22 23
#ifndef LOG_TESTBED
#define LOG_TESTBED	LOG_USER
#endif

24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
static int usesyslog = 1;

/*
 * There is really no point in the client using syslog, but its nice
 * to use the same log functions either way.
 */
int
ClientLogInit(void)
{
	usesyslog = 0;
	return 0;
}

int
ServerLogInit(void)
{
	if (debug) {
		usesyslog = 0;
		return 1;
	}

45
	openlog("frisbeed", LOG_PID, LOG_TESTBED);
46 47 48 49 50 51 52 53

	return 0;
}

void
log(const char *fmt, ...)
{
	va_list args;
54
	char	buf[BUFSIZ];
55 56

	va_start(args, fmt);
57 58 59
	vsnprintf(buf, sizeof(buf), fmt, args);
	va_end(args);

60
	if (!usesyslog) {
61
		fputs(buf, stderr);
62 63 64
		fputc('\n', stderr);
	}
	else
65
		syslog(LOG_INFO, "%s", buf);
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
}

void
warning(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	if (!usesyslog) {
		vfprintf(stderr, fmt, args);
		fputc('\n', stderr);
		fflush(stderr);
	}
	else
		vsyslog(LOG_WARNING, fmt, args);
	       
	va_end(args);
}

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
void
error(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	if (!usesyslog) {
		vfprintf(stderr, fmt, args);
		fflush(stderr);
	}
	else
		vsyslog(LOG_ERR, fmt, args);
	       
	va_end(args);
}

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 134 135 136 137 138 139 140 141 142 143
void
fatal(const char *fmt, ...)
{
	va_list args;

	va_start(args, fmt);
	if (!usesyslog) {
		vfprintf(stderr, fmt, args);
		fputc('\n', stderr);
		fflush(stderr);
	}
	else
		vsyslog(LOG_ERR, fmt, args);
	       
	va_end(args);
	exit(-1);
}

void
pwarning(const char *fmt, ...)
{
	va_list args;
	char	buf[BUFSIZ];

	va_start(args, fmt);
	vsnprintf(buf, sizeof(buf), fmt, args);
	va_end(args);

	warning("%s : %s", buf, strerror(errno));
}

void
pfatal(const char *fmt, ...)
{
	va_list args;
	char	buf[BUFSIZ];

	va_start(args, fmt);
	vsnprintf(buf, sizeof(buf), fmt, args);
	va_end(args);

	fatal("%s : %s", buf, strerror(errno));
}