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

7 8 9 10 11 12 13
/*
 * Logging and debug routines.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
14
#include <fcntl.h>
15 16 17 18
#include <unistd.h>
#include <syslog.h>
#include <assert.h>
#include <errno.h>
19
#include "config.h"
20 21
#include "log.h"

22 23
static int	usesyslog = 0;
static char    *filename;
24 25 26 27 28

/*
 * Init.
 */
int
29
loginit(int slog, char *name)
30
{
31 32
	if (slog) {
		usesyslog = 1;
33 34
		if (! name)
			name = "Testbed";
35
		openlog(name, LOG_PID, LOG_TESTBED);
36
		return 0;
37 38
	}

39 40 41 42 43
	usesyslog = 0;

	if (name) {
		int	fd;

44
		if ((fd = open(name, O_RDWR|O_CREAT|O_APPEND, 0640)) != -1) {
45 46 47 48 49 50 51
			(void)dup2(fd, STDOUT_FILENO);
			(void)dup2(fd, STDERR_FILENO);
			if (fd > 2)
				(void)close(fd);
		}
		filename = name;
	}
52 53 54
	return 0;
}

55 56 57 58 59 60 61 62 63
/*
 * Switch to syslog; caller has already opened syslog connection.
 */
void
logsyslog(void)
{
	usesyslog = 1;
}

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
void
info(const char *fmt, ...)
{
	va_list args;
	char	buf[BUFSIZ];

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

	if (!usesyslog) {
		fputs(buf, stderr);
	}
	else
		syslog(LOG_INFO, "%s", buf);
}

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

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

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);
}

113 114 115 116 117 118 119 120 121 122 123 124 125
void
errorc(const char *fmt, ...)
{
	va_list args;
	char	buf[BUFSIZ];

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

	error("%s : %s\n", buf, strerror(errno));
}

126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
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);

154
	warning("%s : %s\n", buf, strerror(errno));
155 156 157 158 159 160 161 162 163 164 165 166 167 168
}

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));
}