X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Flog.c;h=447a4cf64fb6d00714afd09f33480890a6e9ab5a;hb=45c8086f97097df36ba8cd0552f152be80f79b12;hp=b1a5f3dbcce106ae97a639caae17daaf09713178;hpb=cd194df6ee8cf84f766839f6a95fdd110d3a11a1;p=psensor.git diff --git a/src/lib/log.c b/src/lib/log.c index b1a5f3d..447a4cf 100644 --- a/src/lib/log.c +++ b/src/lib/log.c @@ -1,22 +1,21 @@ /* - Copyright (C) 2010-2011 jeanfi@gmail.com - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301 USA -*/ - + * Copyright (C) 2010-2011 jeanfi@gmail.com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License as + * published by the Free Software Foundation; either version 2 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA + * 02110-1301 USA + */ #include #include #define _(str) gettext(str) @@ -34,10 +33,8 @@ void log_open(const char *path) { file = fopen(path, "a"); - if (file) - log_printf(LOG_INFO, "Start logging"); - else - fprintf(stderr, _("Cannot open log file: %s\n"), path); + if (!file) + log_printf(LOG_ERR, _("Cannot open log file: %s"), path); } void log_close() @@ -50,21 +47,20 @@ void log_close() file = NULL; } + #define LOG_BUFFER 4096 -void log_printf(int lvl, const char *fmt, ...) +static void vlogf(int lvl, const char *fmt, va_list ap) { struct timeval tv; - static char buffer[1 + LOG_BUFFER]; - va_list ap; + char buffer[1 + LOG_BUFFER]; char *lvl_str; + FILE *stdf; - if (!file || lvl > log_level) + if (lvl > LOG_INFO && (!file || lvl > log_level)) return ; - va_start(ap, fmt); vsnprintf(buffer, LOG_BUFFER, fmt, ap); buffer[LOG_BUFFER] = '\0'; - va_end(ap); if (gettimeofday(&tv, NULL) != 0) timerclear(&tv); @@ -86,6 +82,38 @@ void log_printf(int lvl, const char *fmt, ...) lvl_str = "[??]"; } - fprintf(file, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer); - fflush(file); + if (file && lvl <= log_level) { + fprintf(file, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer); + fflush(file); + } + + if (lvl <= LOG_INFO) { + if (lvl == LOG_WARN || lvl == LOG_ERR) + stdf = stderr; + else + stdf = stdout; + + fprintf(stdf, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer); + } +} + +void log_printf(int lvl, const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vlogf(lvl, fmt, ap); + va_end(ap); +} + +void log_debug(const char *fmt, ...) +{ + va_list ap; + + if (log_level < LOG_DEBUG) + return ; + + va_start(ap, fmt); + vlogf(LOG_DEBUG, fmt, ap); + va_end(ap); }