2011-12-19 18:52:50 +04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <limits.h>
|
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include "compiler.h"
|
|
|
|
#include "types.h"
|
|
|
|
#include "util.h"
|
2012-03-16 17:21:00 +04:00
|
|
|
#include "crtools.h"
|
2011-12-19 18:52:50 +04:00
|
|
|
|
2012-03-01 18:52:42 +04:00
|
|
|
#define DEFAULT_LOGLEVEL LOG_WARN
|
|
|
|
#define DEFAULT_LOGFD STDERR_FILENO
|
2011-12-19 18:52:50 +04:00
|
|
|
|
2012-03-01 18:52:42 +04:00
|
|
|
static unsigned int current_loglevel = DEFAULT_LOGLEVEL;
|
|
|
|
static int current_logfd = DEFAULT_LOGFD;
|
|
|
|
|
|
|
|
int log_get_fd(void)
|
2011-12-19 18:52:50 +04:00
|
|
|
{
|
2012-03-01 18:52:42 +04:00
|
|
|
return current_logfd;
|
2011-12-19 18:52:50 +04:00
|
|
|
}
|
|
|
|
|
2012-03-01 18:52:42 +04:00
|
|
|
int log_init(const char *output)
|
2011-12-19 18:52:50 +04:00
|
|
|
{
|
2012-04-06 16:45:00 +04:00
|
|
|
int new_logfd = DEFAULT_LOGFD, sfd;
|
2011-12-19 18:52:50 +04:00
|
|
|
|
2012-04-06 16:45:00 +04:00
|
|
|
sfd = get_service_fd(LOG_FD_OFF);
|
|
|
|
if (sfd < 0) {
|
2012-03-16 17:21:00 +04:00
|
|
|
pr_msg("Can't obtain logfd");
|
|
|
|
goto err;
|
2012-01-10 16:39:00 +04:00
|
|
|
}
|
|
|
|
|
2012-03-01 18:52:42 +04:00
|
|
|
if (output) {
|
2012-03-28 00:48:13 +04:00
|
|
|
new_logfd = open(output, O_CREAT | O_WRONLY, 0600);
|
2012-03-01 18:52:42 +04:00
|
|
|
if (new_logfd < 0) {
|
|
|
|
pr_perror("Can't create log file %s", output);
|
2011-12-19 18:52:50 +04:00
|
|
|
return -1;
|
|
|
|
}
|
2012-03-04 01:39:00 +04:00
|
|
|
}
|
2012-03-02 15:44:13 +04:00
|
|
|
|
2012-04-06 16:45:00 +04:00
|
|
|
if (reopen_fd_as(sfd, new_logfd) < 0)
|
|
|
|
|
|
|
|
current_logfd = sfd;
|
2011-12-19 18:52:50 +04:00
|
|
|
|
|
|
|
return 0;
|
2012-03-01 18:52:42 +04:00
|
|
|
|
2012-01-10 16:39:00 +04:00
|
|
|
err:
|
2012-03-01 18:52:42 +04:00
|
|
|
pr_perror("Log engine failure, can't duplicate descriptor");
|
2012-01-10 16:39:00 +04:00
|
|
|
return -1;
|
2011-12-19 18:52:50 +04:00
|
|
|
}
|
|
|
|
|
2012-03-01 18:52:42 +04:00
|
|
|
void log_fini(void)
|
2011-12-19 18:52:50 +04:00
|
|
|
{
|
2012-03-01 18:52:42 +04:00
|
|
|
if (current_logfd > 2)
|
|
|
|
close_safe(¤t_logfd);
|
2011-12-19 18:52:50 +04:00
|
|
|
|
2012-03-01 18:52:42 +04:00
|
|
|
current_logfd = DEFAULT_LOGFD;
|
2011-12-19 18:52:50 +04:00
|
|
|
}
|
2012-02-17 12:17:00 +04:00
|
|
|
|
2012-03-01 18:52:42 +04:00
|
|
|
void log_set_loglevel(unsigned int level)
|
2012-02-17 22:51:23 +04:00
|
|
|
{
|
|
|
|
if (!level)
|
2012-03-01 18:52:42 +04:00
|
|
|
current_loglevel = DEFAULT_LOGLEVEL;
|
2012-02-17 22:51:23 +04:00
|
|
|
else
|
2012-03-01 18:52:42 +04:00
|
|
|
current_loglevel = level;
|
2012-02-17 22:51:23 +04:00
|
|
|
}
|
|
|
|
|
2012-03-01 18:52:42 +04:00
|
|
|
void print_on_level(unsigned int loglevel, const char *format, ...)
|
2012-02-17 12:17:00 +04:00
|
|
|
{
|
|
|
|
va_list params;
|
2012-03-01 18:52:42 +04:00
|
|
|
int fd;
|
|
|
|
|
|
|
|
if (unlikely(loglevel == LOG_MSG)) {
|
|
|
|
fd = STDOUT_FILENO;
|
|
|
|
} else {
|
|
|
|
if (loglevel > current_loglevel)
|
|
|
|
return;
|
|
|
|
fd = current_logfd;
|
2012-02-17 22:51:23 +04:00
|
|
|
}
|
2012-03-01 18:52:42 +04:00
|
|
|
|
|
|
|
va_start(params, format);
|
|
|
|
vdprintf(fd, format, params);
|
|
|
|
va_end(params);
|
2012-02-17 12:17:00 +04:00
|
|
|
}
|