Git-Url: http://git.frugalware.org/gitweb/gitweb.cgi?p=pacman-g2.git;a=commitdiff;h=cd34fe6df651f3051a0d24c2d0f94bd81149c842
commit cd34fe6df651f3051a0d24c2d0f94bd81149c842 Author: Michel Hermier <[email protected]> Date: Fri Jul 5 07:51:10 2013 +0200 libflib: Add FLogger. diff --git a/lib/libflib/Makefile.am b/lib/libflib/Makefile.am index 2e09632..b78c430 100644 --- a/lib/libflib/Makefile.am +++ b/lib/libflib/Makefile.am @@ -27,6 +27,7 @@ libflib_posix_SOURCES = \ libflib_utils_SOURCES = \ fgraph.c \ flist.c \ + flogger.c \ fstringlist.c libflib_la_SOURCES = \ diff --git a/lib/libflib/flogger.c b/lib/libflib/flogger.c new file mode 100644 index 0000000..c278844 --- /dev/null +++ b/lib/libflib/flogger.c @@ -0,0 +1,87 @@ +/* + * flogger.c + * + * Copyright (c) 2013 by Michel Hermier <[email protected]> + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ + +#include "config.h" + +#include <assert.h> +#include <stdio.h> + +#include "flogger.h" + +#ifdef F_DEBUG +#define F_LOGGER_DEBUG +#endif + +static +FLogger *f_default_logger = NULL; + +void f_logger_init (FLogger *logger, FLoggerOperations *loggeroperations) { + assert (logger != NULL); + assert (loggeroperations != NULL); + + f_object_init (&logger->as_FObject, &loggeroperations->as_FObjectOperations); + logger->flag_mask = ~0; +} + +void f_logger_fini (FLogger *logger) { + assert (logger != NULL); + + f_object_fini (&logger->as_FObject); +} + +void f_logger_log (FLogger *logger, unsigned flag, const char *format, ...) { + va_list ap; + + assert (logger != NULL); + + va_start (ap, format); + f_logger_vlog (logger, flag, format, ap); + va_end (ap); +} + +void f_logger_vlog (FLogger *logger, unsigned flag, const char *format, va_list ap) { + assert (logger != NULL); + + if ((logger->flag_mask & flag) != 0) { + FLoggerOperations *loggeroperations = logger->as_FObject.operations; + +#ifdef F_LOGGER_DEBUG + vfprintf (stderr, format, ap); +#endif + loggeroperations->vlog (logger, flag, format, ap); + } +} + +void f_log (unsigned flag, const char *format, ...) { + va_list ap; + + va_start (ap, format); + f_vlog (flag, format, ap); + va_end (ap); +} + +void f_vlog (unsigned flag, const char *format, va_list ap) { + if (f_default_logger != NULL) { + f_logger_vlog (f_default_logger, flag, format, ap); + } +} + +/* vim: set ts=2 sw=2 noet: */ diff --git a/lib/libflib/flogger.h b/lib/libflib/flogger.h new file mode 100644 index 0000000..7f43ae0 --- /dev/null +++ b/lib/libflib/flogger.h @@ -0,0 +1,53 @@ +/* + * flogger.h + * + * Copyright (c) 2013 by Michel Hermier <[email protected]> + * + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, + * USA. + */ +#ifndef F_LOGGER_H +#define F_LOGGER_H + +#include <stdarg.h> + +#include <fobject.h> + +typedef struct FLogger FLogger; +typedef struct FLoggerOperations FLoggerOperations; + +struct FLoggerOperations { + FObjectOperations as_FObjectOperations; + void (*vlog) (FLogger *logger, unsigned flag, const char *format, va_list ap); +}; + +struct FLogger { + FObject as_FObject; + + unsigned flag_mask; +}; + +void f_logger_init (FLogger *logger, FLoggerOperations *loggeroperations); +void f_logger_fini (FLogger *logger); + +void f_logger_log (FLogger *logger, unsigned flag, const char *format, ...); +void f_logger_vlog (FLogger *logger, unsigned flag, const char *format, va_list ap); + +void f_log (unsigned flag, const char *format, ...); +void f_vlog (unsigned flag, const char *format, va_list ap); + +#endif /* F_LOGGER_H */ + +/* vim: set ts=2 sw=2 noet: */ _______________________________________________ Frugalware-git mailing list [email protected] http://frugalware.org/mailman/listinfo/frugalware-git
