From 0f6d23757ffff00bfdd678be25b63010cc216d99 Mon Sep 17 00:00:00 2001 From: Jef Driesen Date: Tue, 22 May 2012 21:22:37 +0200 Subject: [PATCH] Add a new library context object. With the introduction of a context object, library initialization and shutdown can be performed without requiring any global state. A single process can use multiple independent contexts without any problems. The lack of a global state also improves the thread-safety of the library. At the moment, the new context object is primary used to implement an improved logging system. --- include/libdivecomputer/Makefile.am | 1 + include/libdivecomputer/context.h | 54 ++++++++++++++++++ src/Makefile.am | 1 + src/context-private.h | 42 ++++++++++++++ src/context.c | 87 +++++++++++++++++++++++++++++ src/libdivecomputer.symbols | 4 ++ 6 files changed, 189 insertions(+) create mode 100644 include/libdivecomputer/context.h create mode 100644 src/context-private.h create mode 100644 src/context.c diff --git a/include/libdivecomputer/Makefile.am b/include/libdivecomputer/Makefile.am index 2184a52..2505889 100644 --- a/include/libdivecomputer/Makefile.am +++ b/include/libdivecomputer/Makefile.am @@ -2,6 +2,7 @@ libdivecomputerdir = $(includedir)/libdivecomputer libdivecomputer_HEADERS = \ version.h \ common.h \ + context.h \ utils.h \ buffer.h \ descriptor.h \ diff --git a/include/libdivecomputer/context.h b/include/libdivecomputer/context.h new file mode 100644 index 0000000..10f21b4 --- /dev/null +++ b/include/libdivecomputer/context.h @@ -0,0 +1,54 @@ +/* + * libdivecomputer + * + * Copyright (C) 2012 Jef Driesen + * + * This library 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. + * + * This library 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 this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#ifndef DC_CONTEXT_H +#define DC_CONTEXT_H + +#include "common.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +typedef struct dc_context_t dc_context_t; + +typedef enum dc_loglevel_t { + DC_LOGLEVEL_NONE, + DC_LOGLEVEL_ERROR, + DC_LOGLEVEL_WARNING, + DC_LOGLEVEL_INFO, + DC_LOGLEVEL_DEBUG, + DC_LOGLEVEL_ALL +} dc_loglevel_t; + +dc_status_t +dc_context_new (dc_context_t **context); + +dc_status_t +dc_context_free (dc_context_t *context); + +dc_status_t +dc_context_set_loglevel (dc_context_t *context, dc_loglevel_t loglevel); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* DC_CONTEXT_H */ diff --git a/src/Makefile.am b/src/Makefile.am index b215312..9fcfb14 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -13,6 +13,7 @@ libdivecomputer_la_SOURCES = \ version.c \ descriptor.c \ iterator-private.h iterator.c \ + context-private.h context.c \ device-private.h device.c \ parser-private.h parser.c \ datetime.c \ diff --git a/src/context-private.h b/src/context-private.h new file mode 100644 index 0000000..677eb09 --- /dev/null +++ b/src/context-private.h @@ -0,0 +1,42 @@ +/* + * libdivecomputer + * + * Copyright (C) 2012 Jef Driesen + * + * This library 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. + * + * This library 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 this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#ifndef DC_CONTEXT_PRIVATE_H +#define DC_CONTEXT_PRIVATE_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define ERROR(context, ...) dc_context_log (context, DC_LOGLEVEL_ERROR, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__) +#define WARNING(context, ...) dc_context_log (context, DC_LOGLEVEL_WARNING, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__) +#define INFO(context, ...) dc_context_log (context, DC_LOGLEVEL_INFO, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__) +#define DEBUG(context, ...) dc_context_log (context, DC_LOGLEVEL_DEBUG, __FILE__, __LINE__, __FUNCTION__, __VA_ARGS__) + +dc_status_t +dc_context_log (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *format, ...); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* DC_CONTEXT_PRIVATE_H */ diff --git a/src/context.c b/src/context.c new file mode 100644 index 0000000..f9d720a --- /dev/null +++ b/src/context.c @@ -0,0 +1,87 @@ +/* + * libdivecomputer + * + * Copyright (C) 2012 Jef Driesen + * + * This library 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. + * + * This library 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 this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ + +#include +#include +#include +#include + +#include "context-private.h" + +struct dc_context_t { + dc_loglevel_t loglevel; +}; + +dc_status_t +dc_context_new (dc_context_t **out) +{ + dc_context_t *context = NULL; + + if (out == NULL) + return DC_STATUS_INVALIDARGS; + + context = (dc_context_t *) malloc (sizeof (dc_context_t)); + if (context == NULL) + return DC_STATUS_NOMEMORY; + + context->loglevel = DC_LOGLEVEL_WARNING; + + *out = context; + + return DC_STATUS_SUCCESS; +} + +dc_status_t +dc_context_free (dc_context_t *context) +{ + free (context); + + return DC_STATUS_SUCCESS; +} + +dc_status_t +dc_context_set_loglevel (dc_context_t *context, dc_loglevel_t loglevel) +{ + if (context == NULL) + return DC_STATUS_INVALIDARGS; + + context->loglevel = loglevel; + + return DC_STATUS_SUCCESS; +} + +dc_status_t +dc_context_log (dc_context_t *context, dc_loglevel_t loglevel, const char *file, unsigned int line, const char *function, const char *format, ...) +{ + va_list ap; + + if (context == NULL) + return DC_STATUS_INVALIDARGS; + + if (loglevel > context->loglevel) + return DC_STATUS_SUCCESS; + + va_start (ap, format); + vfprintf (stderr, format, ap); + va_end (ap); + + return DC_STATUS_SUCCESS; +} diff --git a/src/libdivecomputer.symbols b/src/libdivecomputer.symbols index 0d5a25e..fbc599d 100644 --- a/src/libdivecomputer.symbols +++ b/src/libdivecomputer.symbols @@ -17,6 +17,10 @@ dc_datetime_localtime dc_datetime_gmtime dc_datetime_mktime +dc_context_new +dc_context_free +dc_context_set_loglevel + dc_iterator_next dc_iterator_free