logger.c 1.2 KB
#define _ISOC99_SOURCE

#include <syslog.h>
#include <stdio.h>
#include <string.h>

#include "logger.h"

const int priority[] = {
	LOG_USER | LOG_EMERG,
	LOG_USER | LOG_ALERT,
	LOG_USER | LOG_CRIT,
	LOG_USER | LOG_ERR,
	LOG_USER | LOG_WARNING,
	LOG_USER | LOG_NOTICE,
	LOG_USER | LOG_INFO,
	LOG_USER | LOG_DEBUG
};

INIT_CLASS(LOGGER);

static void
logger_syslog(int level, const char * msg)
{
	syslog(priority[level], "%s", msg);
}

__construct(LOGGER)
{
	this->logfncts[0]    = logger_syslog;
	this->logfncts_count = 1;
}

__destruct(LOGGER) {}
__jsonConst(LOGGER) {}
__toJson(LOGGER) {}
__clear(LOGGER) {}

void
logger_log(LOGGER this, int level, const char * message, ...) {
	va_list          args;
	char             buffer[1025];
	logger_logfnct * logfnct;

	int maxBuf = sizeof(buffer)/sizeof(buffer[0]);

	memset(buffer, 0, maxBuf);

	va_start(args, message);
	vsnprintf(buffer, 1024, message, args);
	va_end(args);

	logfnct = this->logfncts;

	while (NULL != *logfnct) {
		(*logfnct)(level, buffer);
		logfnct++;
	}
}

void
logger_add(LOGGER this, logger_logfnct logfunc) {
	if (this->logfncts_count < MAX_LOG_FNCTS) {
		this->logfncts[this->logfncts_count] = logfunc;
		this->logfncts_count++;
	}
}

// vim: set ts=4 sw=4: