/* * * This file is part of bufferpool * * Copyright (C) 2007 by LScube team * See AUTHORS for more details * * bufferpool is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * bufferpool 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with bufferpool; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA * * */ #include #include #include #include #include #include #define LOG_FORMAT "%d/%b/%Y:%H:%M:%S %z" #define ERR_FORMAT "%a %b %d %H:%M:%S %Y" #define MAX_LEN_DATE 30 static int envlevel = 1; /*Prints on standard error or file*/ static void bufferpool_errlog(int level, const char *fmt, va_list args) { time_t now; char date[MAX_LEN_DATE]; const struct tm *tm; if (level > envlevel) return; time(&now); tm = localtime(&now); switch (level) { case FNC_LOG_FATAL: strftime(date, MAX_LEN_DATE, ERR_FORMAT, tm); fprintf(stderr, "[%s] [fatal error] ", date); break; case FNC_LOG_ERR: strftime(date, MAX_LEN_DATE, ERR_FORMAT, tm); fprintf(stderr, "[%s] [error] ", date); break; case FNC_LOG_DEBUG: fprintf(stderr, "[debug] "); break; default: /*FNC_LOG_INFO*/ strftime(date, MAX_LEN_DATE, LOG_FORMAT ,tm); fprintf(stderr, "[%s] ", date); break; } vfprintf(stderr, fmt, args); fprintf(stderr, "\n"); } static void (*bp_vlog)(int, const char*, va_list) = bufferpool_errlog; /** * Set the logger to the current function * @param fn logger function **/ void bp_log_init(bp_log_t fn) { if (fn) bp_vlog = fn; } /** * External logger function * @param level log level * @param fmt as printf format string * @param ... as printf variable argument */ void bp_log(int level, const char *fmt, ...) { va_list vl; va_start(vl, fmt); bp_vlog(level, fmt, vl); va_end(vl); }