X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Flog.c;h=9df462924030d9587be058f892e00533773b4382;hb=f3b05dae619a7909bd7422b3a82422c9442aa114;hp=f5eb282ce77893b3d5821e7f59d6666394fbc069;hpb=09091abdda7df60d6ef67921a427067ff92bb782;p=psensor.git diff --git a/src/lib/log.c b/src/lib/log.c index f5eb282..9df4629 100644 --- a/src/lib/log.c +++ b/src/lib/log.c @@ -1,21 +1,23 @@ /* - 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-2014 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 + */ +#define _LARGEFILE_SOURCE 1 +#include "config.h" #include #include @@ -23,21 +25,22 @@ #include #include +#include #include +#include #include "log.h" +#include "ptime.h" static FILE *file; -int log_level = LOG_INFO; +int log_level = LOG_WARN; 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,25 +53,19 @@ 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; char buffer[1 + LOG_BUFFER]; - va_list ap; - char *lvl_str; + char *lvl_str, *t; FILE *stdf; 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); switch (lvl) { case LOG_WARN: @@ -87,9 +84,15 @@ void log_printf(int lvl, const char *fmt, ...) lvl_str = "[??]"; } + t = get_time_str(); + if (!t) + return ; + if (file && lvl <= log_level) { - fprintf(file, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer); + fprintf(file, "[%s] %s %s\n", t, lvl_str, buffer); fflush(file); + } else { + t = NULL; } if (lvl <= LOG_INFO) { @@ -98,6 +101,57 @@ void log_printf(int lvl, const char *fmt, ...) else stdf = stdout; - fprintf(stdf, "[%ld] %s %s\n", tv.tv_sec, lvl_str, buffer); + + fprintf(stdf, "[%s] %s %s\n", t, lvl_str, buffer); } + + free(t); +} + +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); +} + +void log_err(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vlogf(LOG_ERR, fmt, ap); + va_end(ap); +} + +void log_warn(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vlogf(LOG_WARN, fmt, ap); + va_end(ap); +} + +void log_info(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + vlogf(LOG_INFO, fmt, ap); + va_end(ap); }