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-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
|
|
|
{
|
|
|
|
struct rlimit rlimit;
|
2012-03-04 01:39:00 +04:00
|
|
|
int new_logfd = DEFAULT_LOGFD;
|
2011-12-19 18:52:50 +04:00
|
|
|
|
2012-01-10 16:39:00 +04:00
|
|
|
if (getrlimit(RLIMIT_NOFILE, &rlimit)) {
|
2012-03-01 18:52:42 +04:00
|
|
|
pr_perror("Can't get rlimit");
|
2012-01-10 16:39:00 +04:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2012-03-01 18:52:42 +04:00
|
|
|
/*
|
|
|
|
* We might need to transfer this descriptors
|
|
|
|
* to another process' address space (and file
|
|
|
|
* descriptors space) so we try to minimize
|
|
|
|
* potential conflict between descriptors and
|
|
|
|
* try to reopen them somewhere near a limit.
|
|
|
|
*
|
|
|
|
* Still an explicit output file might be
|
|
|
|
* requested.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (output) {
|
|
|
|
new_logfd = open(output, O_CREAT | O_WRONLY);
|
|
|
|
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
|
|
|
|
|
|
|
current_logfd = rlimit.rlim_cur - 1;
|
|
|
|
if (reopen_fd_as(current_logfd, new_logfd) < 0)
|
|
|
|
goto err;
|
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
|
|
|
}
|