log.c 836 Bytes
Newer Older
1 2
#include <stdarg.h>
#include <stdio.h>
Lubos Slovak's avatar
Lubos Slovak committed
3

4 5 6 7 8
#include "log.h"
#include "common.h"

/// Global log-level.
static int _LOG_OPEN = 0;
9
static int _LOG_MASK = 0;
10 11 12

int log_open(int print_mask, int log_mask)
{
Lubos Slovak's avatar
Lubos Slovak committed
13 14 15
	setlogmask(log_mask);

	/// \todo May change to LOG_DAEMON.
16
	openlog(PROJECT_NAME, LOG_CONS | LOG_PID, LOG_LOCAL1);
Lubos Slovak's avatar
Lubos Slovak committed
17 18 19
	_LOG_MASK = print_mask;
	_LOG_OPEN = 1;
	return 0;
20 21 22 23
}

int log_close()
{
Lubos Slovak's avatar
Lubos Slovak committed
24 25 26 27
	_LOG_OPEN = 0;
	_LOG_MASK = 0;
	closelog();
	return 0;
28 29
}

30 31
int log_isopen()
{
Lubos Slovak's avatar
Lubos Slovak committed
32
	return _LOG_OPEN;
33 34
}

Lubos Slovak's avatar
Lubos Slovak committed
35
int print_msg(int level, const char *msg, ...)
36
{
Lubos Slovak's avatar
Lubos Slovak committed
37 38 39 40 41 42 43 44 45
	// Get output stream
	va_list ap;
	FILE *stream = stdout;
	if (level & (LOG_ERR | LOG_WARNING | LOG_CRIT | LOG_ALERT)) {
		stream = stderr;
	}

	// Check mask
	int ret = 0;
46
	if (LOG_MASK(level) & _LOG_MASK) {
Lubos Slovak's avatar
Lubos Slovak committed
47 48 49 50 51 52
		va_start(ap, msg);
		ret = vfprintf(stream, msg, ap);
		va_end(ap);
	}

	return ret;
53
}