From d36a38a56eeacc7f7130b304638bc5c23fefd6ac Mon Sep 17 00:00:00 2001 From: Christian Thaeter Date: Tue, 14 Aug 2007 04:40:13 +0200 Subject: [PATCH] errorhandling finished for now, as proposed --- Makefile.am | 1 + src/lib/Makefile.am | 4 +-- src/lib/error.c | 56 ++++++++++++++++++++++++++++++++ src/lib/error.h | 14 ++++++-- tests/10errorhandling.tests | 27 +++++++++++++++ tests/Makefile.am | 6 +++- tests/errortest.c | 65 +++++++++++++++++++++++++++++++++++++ 7 files changed, 167 insertions(+), 6 deletions(-) create mode 100644 src/lib/error.c create mode 100644 tests/10errorhandling.tests create mode 100644 tests/errortest.c diff --git a/Makefile.am b/Makefile.am index df66c2437..09e1593c9 100644 --- a/Makefile.am +++ b/Makefile.am @@ -18,6 +18,7 @@ bin_PROGRAMS = lib_LTLIBRARIES = noinst_PROGRAMS = +check_PROGRAMS = noinst_LIBRARIES = noinst_LTLIBRARIES = noinst_HEADERS = diff --git a/src/lib/Makefile.am b/src/lib/Makefile.am index b6c493a18..7419bedf9 100644 --- a/src/lib/Makefile.am +++ b/src/lib/Makefile.am @@ -20,5 +20,5 @@ noinst_LIBRARIES += libcin3.a libcin3_a_CFLAGS = $(CFLAGS) -std=gnu99 -Wall -Werror -libcin3_a_SOURCES = $(libcin3_a_srcdir)/plugin.c -noinst_HEADERS += $(libcin3_a_srcdir)/plugin.h +libcin3_a_SOURCES = $(libcin3_a_srcdir)/plugin.c $(libcin3_a_srcdir)/error.c +noinst_HEADERS += $(libcin3_a_srcdir)/plugin.h $(libcin3_a_srcdir)/error.h diff --git a/src/lib/error.c b/src/lib/error.c new file mode 100644 index 000000000..d05696c06 --- /dev/null +++ b/src/lib/error.c @@ -0,0 +1,56 @@ +/* + error.c - Cinelerra Error handling + + Copyright (C) CinelerraCV + 2007, Christian Thaeter + + 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., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include + +/* Thread local storage */ +static pthread_key_t cinelerra_error_tls; +static pthread_once_t cinelerra_error_initialized = PTHREAD_ONCE_INIT; + +static void +cinelerra_error_tls_init (void) +{ + pthread_key_create (&cinelerra_error_tls, NULL); +} + +const char* +cinelerra_error_set (const char * nerr) +{ + pthread_once (&cinelerra_error_initialized, cinelerra_error_tls_init); + + const char* err = pthread_getspecific (cinelerra_error_tls); + if (!err) + pthread_setspecific (cinelerra_error_tls, nerr); + + return err; +} + + +const char* +cinelerra_error () +{ + pthread_once (&cinelerra_error_initialized, cinelerra_error_tls_init); + + const char* err = pthread_getspecific (cinelerra_error_tls); + if (err) + pthread_setspecific (cinelerra_error_tls, NULL); + return err; +} diff --git a/src/lib/error.h b/src/lib/error.h index 09cd31c8d..6b8aaf753 100644 --- a/src/lib/error.h +++ b/src/lib/error.h @@ -26,9 +26,17 @@ #define CINELERRA_DIE(message) do { NOBUG_ERROR(NOBUG_ON, message); abort(); } while(0) -#define CINELERRA_ERROR_DECLARE(err) extern const cinelerra_error err -#define CINELERRA_ERROR_DEFINE(err, msg) const cinelerra_error err = #err ":" msg +#define CINELERRA_ERROR_DECLARE(err) \ +extern const char* CINELERRA_ERROR_##err + +#define CINELERRA_ERROR_DEFINE(err, msg) \ +const char* CINELERRA_ERROR_##err = "CINELERRA_ERROR_" #err ":" msg + +const char* +cinelerra_error_set (const char * err); + +const char* +cinelerra_error (); -typedef const char* cinelerra_error; #endif /* CINELERRA_ERROR_H */ diff --git a/tests/10errorhandling.tests b/tests/10errorhandling.tests new file mode 100644 index 000000000..220e1da3d --- /dev/null +++ b/tests/10errorhandling.tests @@ -0,0 +1,27 @@ + +TESTING "Error handling" ./errortest + +TEST "no error" < + + 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., 675 Mass Ave, Cambridge, MA 02139, USA. +*/ + +#include +#include +#include + +CINELERRA_ERROR_DEFINE(TEST, "test error"); + +int +main (int argc, char** argv) +{ + if (argc == 1) + return 0; + + if (!strcmp(argv[1], "set")) + { + cinelerra_error_set (CINELERRA_ERROR_TEST); + } + + if (!strcmp(argv[1], "get_no")) + { + const char* err; + err = cinelerra_error (); + printf ("%p\n", err); + } + + if (!strcmp(argv[1], "get")) + { + cinelerra_error_set (CINELERRA_ERROR_TEST); + const char* err; + err = cinelerra_error (); + printf ("%s\n", err); + } + + if (!strcmp(argv[1], "get2")) + { + cinelerra_error_set (CINELERRA_ERROR_TEST); + const char* err; + err = cinelerra_error (); + printf ("%s\n", err); + err = cinelerra_error (); + printf ("%p\n", err); + } + + return 0; +}