Add a context string to the error system

lumiera_error_set() now takes an optional extra string which can be used
to pass context relevant data along. This string gets copied into the
error state so one can easily create it by the tmpbuf_snprintf() facility.

Also a lot of places which define errors get fixed according to this.
This commit is contained in:
Christian Thaeter 2009-01-13 21:23:37 +01:00
parent c4dd8e465d
commit d37bb17566
15 changed files with 118 additions and 62 deletions

View file

@ -130,7 +130,7 @@ LumieraMMapings
lumiera_file_mmapings (LumieraFile self)
{
if (!self->descriptor->mmapings)
LUMIERA_ERROR_SET (file, FILE_NOCHUNKSIZE);
LUMIERA_ERROR_SET (file, FILE_NOCHUNKSIZE, lumiera_filedescriptor_name (self->descriptor));
return self->descriptor->mmapings;
}

View file

@ -141,7 +141,7 @@ lumiera_filedescriptor_acquire (const char* name, int flags, LList filenode)
INFO (filedescriptor, "try creating dir: %s", dir);
if (mkdir (dir, 0777) == -1 && errno != EEXIST)
{
LUMIERA_ERROR_SET (filedescriptor, ERRNO);
LUMIERA_ERROR_SET (filedescriptor, ERRNO, name);
goto error;
}
*slash = '/';
@ -152,14 +152,14 @@ lumiera_filedescriptor_acquire (const char* name, int flags, LList filenode)
fd = creat (name, 0666);
if (fd == -1)
{
LUMIERA_ERROR_SET (filedescriptor, ERRNO);
LUMIERA_ERROR_SET (filedescriptor, ERRNO, name);
goto error;
}
close (fd);
if (stat (name, &fdesc.stat) != 0)
{
/* finally, no luck */
LUMIERA_ERROR_SET (filedescriptor, ERRNO);
LUMIERA_ERROR_SET (filedescriptor, ERRNO, name);
goto error;
}
}

View file

@ -84,7 +84,7 @@ lumiera_filehandle_handle (LumieraFilehandle self)
fd = open (lumiera_filedescriptor_name (self->descriptor), lumiera_filedescriptor_flags (self->descriptor) & LUMIERA_FILE_MASK);
if (fd == -1)
{
LUMIERA_ERROR_SET (filehandle, ERRNO);
LUMIERA_ERROR_SET (filehandle, ERRNO, lumiera_filedescriptor_name (self->descriptor));
}
else
{
@ -92,13 +92,13 @@ lumiera_filehandle_handle (LumieraFilehandle self)
if (fstat (fd, &st) == -1)
{
close (fd);
LUMIERA_ERROR_SET (filehandle, ERRNO);
LUMIERA_ERROR_SET (filehandle, ERRNO, lumiera_filedescriptor_name (self->descriptor));
}
else if (!lumiera_filedescriptor_samestat (self->descriptor, &st))
{
close (fd);
/* Woops this is not the file we expected to use */
LUMIERA_ERROR_SET (filehandle, FILE_CHANGED);
LUMIERA_ERROR_SET (filehandle, FILE_CHANGED, lumiera_filedescriptor_name (self->descriptor));
}
}
self->fd = fd;

View file

@ -85,7 +85,7 @@ lumiera_filehandlecache_handle_acquire (LumieraFilehandlecache self, LumieraFile
NOTICE (filehandlecache, "overallocating filehandle");
ret = lumiera_filehandle_new (desc);
if (!ret)
LUMIERA_ERROR_SET (filehandlecache, FILEHANDLECACHE_NOHANDLE);
LUMIERA_ERROR_SET (filehandlecache, FILEHANDLECACHE_NOHANDLE, lumiera_filedescriptor_name (desc));
else
--self->available;
}

View file

@ -140,7 +140,7 @@ lumiera_mmap_init (LumieraMMap self, LumieraFile file, off_t start, size_t size)
/* extend file (writable) */
if (ftruncate (fd, begin+length) == -1)
{
LUMIERA_ERROR_SET (mmap, ERRNO);
LUMIERA_ERROR_SET (mmap, ERRNO, lumiera_filedescriptor_name (file->descriptor));
goto etruncate;
};
descriptor->stat.st_size = begin+length;
@ -173,7 +173,7 @@ lumiera_mmap_init (LumieraMMap self, LumieraFile file, off_t start, size_t size)
break;
case GIVE_UP:
LUMIERA_ERROR_SET (mmap, MMAP_SPACE);
LUMIERA_ERROR_SET (mmap, MMAP_SPACE, lumiera_filedescriptor_name (file->descriptor));
goto espace;
}

View file

@ -208,12 +208,12 @@ lumiera_config_get (const char* key, const char** value)
*value = item->delim+1;
}
else
LUMIERA_ERROR_SET (configsys, CONFIG_NO_ENTRY);
LUMIERA_ERROR_SET (configsys, CONFIG_NO_ENTRY, key);
}
}
else
{
LUMIERA_ERROR_SET (configsys, CONFIG_SYNTAX_KEY);
LUMIERA_ERROR_SET (configsys, CONFIG_SYNTAX_KEY, key);
}
return *value;

View file

@ -77,12 +77,12 @@ lumiera_config_number_get (const char* key, long long* value)
/* got it, scan it */
if (sscanf (raw_value, "%Li", value) != 1)
{
LUMIERA_ERROR_SET (config_typed, CONFIG_SYNTAX_VALUE, lumiera_tmpbuf_snprintf (5000, "key '%s', value '%s'", key, raw_value));
raw_value = NULL;
LUMIERA_ERROR_SET (config_typed, CONFIG_SYNTAX_VALUE);
}
}
else
LUMIERA_ERROR_SET (configsys, CONFIG_NO_ENTRY);
LUMIERA_ERROR_SET (configsys, CONFIG_NO_ENTRY, key);
}
}
@ -176,7 +176,7 @@ scan_string (const char* in)
}
else
/* quotes doesnt match */
LUMIERA_ERROR_SET (config_typed, CONFIG_SYNTAX_VALUE);
LUMIERA_ERROR_SET (config_typed, CONFIG_SYNTAX_VALUE, "unmatched quotes");
}
else
{
@ -208,7 +208,7 @@ lumiera_config_string_get (const char* key, const char** value)
*value = scan_string (raw_value);
}
else
LUMIERA_ERROR_SET (configsys, CONFIG_NO_ENTRY);
LUMIERA_ERROR_SET (configsys, CONFIG_NO_ENTRY, key);
}
}
@ -253,7 +253,7 @@ lumiera_config_wordlist_get (const char* key, const char** value)
*value = raw_value;
}
else
LUMIERA_ERROR_SET (configsys, CONFIG_NO_ENTRY);
LUMIERA_ERROR_SET (configsys, CONFIG_NO_ENTRY, key);
TODO ("remove the ERROR_SET because config_get sets it already? also in all other getters in this file");
}
@ -322,7 +322,7 @@ lumiera_config_word_get (const char* key, const char** value)
*value = scan_word (raw_value);
}
else
LUMIERA_ERROR_SET (configsys, CONFIG_NO_ENTRY);
LUMIERA_ERROR_SET (configsys, CONFIG_NO_ENTRY, key);
}
}

View file

@ -250,7 +250,7 @@ parse_directive (LumieraConfigitem self, char* itr)
self->key = NULL;
self->key_size = 0;
LUMIERA_ERROR_SET (config_item, CONFIG_SYNTAX);
LUMIERA_ERROR_SET (config_item, CONFIG_SYNTAX, self->line);
}
}
else
@ -260,7 +260,7 @@ parse_directive (LumieraConfigitem self, char* itr)
self->key = NULL;
self->key_size = 0;
LUMIERA_ERROR_SET (config_item, CONFIG_SYNTAX);
LUMIERA_ERROR_SET (config_item, CONFIG_SYNTAX, self->line);
}
return self;
}
@ -317,7 +317,7 @@ parse_section (LumieraConfigitem self, char* itr)
self->key = NULL;
self->key_size = 0;
LUMIERA_ERROR_SET (config_item, CONFIG_SYNTAX);
LUMIERA_ERROR_SET (config_item, CONFIG_SYNTAX, self->line);
}
}
else
@ -329,7 +329,7 @@ parse_section (LumieraConfigitem self, char* itr)
self->key = NULL;
self->key_size = 0;
LUMIERA_ERROR_SET (config_item, CONFIG_SYNTAX);
LUMIERA_ERROR_SET (config_item, CONFIG_SYNTAX, self->line);
}
return self;
@ -368,7 +368,7 @@ parse_configentry (LumieraConfigitem self, char* itr)
self->key = NULL;
self->key_size = 0;
LUMIERA_ERROR_SET (config_item, CONFIG_SYNTAX);
LUMIERA_ERROR_SET (config_item, CONFIG_SYNTAX, self->line);
}
return self;

View file

@ -283,13 +283,13 @@ lumiera_plugin_register (LumieraPlugin plugin)
}
break;
default:
LUMIERA_ERROR_SET (plugin, PLUGIN_VERSION);
LUMIERA_ERROR_SET (plugin, PLUGIN_VERSION, plugin->name);
}
}
}
else
{
LUMIERA_ERROR_SET (plugin, PLUGIN_REGISTER);
LUMIERA_ERROR_SET (plugin, PLUGIN_REGISTER, plugin->name);
}
}
return !!lumiera_error_peek();

View file

@ -18,6 +18,7 @@
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "lib/safeclib.h"
#include "common/plugin.h"
#include <dlfcn.h>
@ -42,18 +43,10 @@ lumiera_plugin_load_DYNLIB (const char* name)
plugin = (LumieraInterface) dlsym (handle, LUMIERA_INTERFACE_DSTRING (lumieraorg__plugin, 0, lumieraorg_plugin));
if (!plugin)
LUMIERA_ERROR_SET (plugin, PLUGIN_WTF);
LUMIERA_ERROR_SET (plugin, PLUGIN_WTF, name);
}
else
LUMIERA_ERROR_SET (plugin, PLUGIN_OPEN);
#ifdef DEBUG
if (lumiera_error_peek())
{
const char* problem = dlerror();
WARN_IF (problem, plugin, "Problem opening shared object %s : %s", name, problem);
}
#endif
LUMIERA_ERROR_SET (plugin, PLUGIN_OPEN, lumiera_tmpbuf_snprintf (4096, "%s: %s", name, dlerror()));
return lumiera_plugin_init (self, handle, plugin);
}

View file

@ -147,7 +147,7 @@ namespace gui {
, LUMIERA_INTERFACE_INLINE (displayInfo, "\366\075\213\163\207\040\221\233\010\366\174\374\317\126\331\205",
void, (const char* text),
{
if (!_instance) lumiera_error_set(LUMIERA_ERROR_FACADE_LIFECYCLE);
if (!_instance) lumiera_error_set(LUMIERA_ERROR_FACADE_LIFECYCLE, text);
else
_instance->displayInfo(text);
}
@ -155,7 +155,7 @@ namespace gui {
, LUMIERA_INTERFACE_INLINE (triggerGuiShutdown, "\267\043\244\065\107\314\370\175\063\330\264\257\302\146\326\303",
void, (const char* cause),
{
if (!_instance) lumiera_error_set(LUMIERA_ERROR_FACADE_LIFECYCLE);
if (!_instance) lumiera_error_set(LUMIERA_ERROR_FACADE_LIFECYCLE, cause);
else
_instance->triggerGuiShutdown(cause);
}

View file

@ -33,51 +33,103 @@
predefined errors
*/
LUMIERA_ERROR_DEFINE (ERRNO, "errno");
LUMIERA_ERROR_DEFINE (EERROR, "could not initialize error system");
/* Thread local storage */
static pthread_key_t lumiera_error_tls;
static pthread_once_t lumiera_error_initialized = PTHREAD_ONCE_INIT;
/**
* Holding error and some context data.
*/
struct lumiera_errorcontext_struct
{
lumiera_err err;
char* extra;
};
typedef struct lumiera_errorcontext_struct lumiera_errorcontext;
typedef lumiera_errorcontext* LumieraErrorcontext;
static void
lumiera_error_tls_delete (void* err)
{
if (err)
free (((LumieraErrorcontext)err)->extra);
}
static void
lumiera_error_tls_init (void)
{
pthread_key_create (&lumiera_error_tls, NULL);
if (!!pthread_key_create (&lumiera_error_tls, lumiera_error_tls_delete))
LUMIERA_DIE (EERROR);
}
lumiera_err
lumiera_error_set (lumiera_err nerr)
LumieraErrorcontext
lumiera_error_get (void)
{
if (lumiera_error_initialized == PTHREAD_ONCE_INIT)
pthread_once (&lumiera_error_initialized, lumiera_error_tls_init);
lumiera_err err = pthread_getspecific (lumiera_error_tls);
if (!err)
pthread_setspecific (lumiera_error_tls, nerr);
LumieraErrorcontext self = pthread_getspecific (lumiera_error_tls);
if (!self)
{
/* malloc() and not lumiera_malloc() here because nothing else might be initialized when calling this */
self = malloc (sizeof *self);
if (!self)
LUMIERA_DIE (EERROR);
return err;
self->err = NULL;
self->extra = NULL;
pthread_setspecific (lumiera_error_tls, self);
}
return self;
}
lumiera_err
lumiera_error_set (lumiera_err nerr, const char* extra)
{
LumieraErrorcontext self = lumiera_error_get ();
if (!self->err)
{
self->err = nerr;
free (self->extra);
if (extra)
self->extra = strdup (extra);
else
self->extra = NULL;
}
return self->err;
}
lumiera_err
lumiera_error (void)
{
if (lumiera_error_initialized == PTHREAD_ONCE_INIT)
pthread_once (&lumiera_error_initialized, lumiera_error_tls_init);
LumieraErrorcontext self = lumiera_error_get ();
lumiera_err err = self->err;
lumiera_err err = pthread_getspecific (lumiera_error_tls);
if (err)
pthread_setspecific (lumiera_error_tls, NULL);
self->err = NULL;
return err;
}
const char*
lumiera_error_extra (void)
{
return lumiera_error_get ()->extra;
}
lumiera_err
lumiera_error_peek (void)
{
if (lumiera_error_initialized == PTHREAD_ONCE_INIT)
pthread_once (&lumiera_error_initialized, lumiera_error_tls_init);
return pthread_getspecific (lumiera_error_tls);
return lumiera_error_get ()->err;
}

View file

@ -67,19 +67,30 @@ lumiera_err LUMIERA_ERROR_##err = "LUMIERA_ERROR_" #err ":" msg
* @param flag NoBug flag describing the subsystem where the error was raised
* @param err name of the error without the 'LUMIERA_ERROR_' prefix (example: NO_MEMORY)
*/
#define LUMIERA_ERROR_SET(flag, err) \
(({ERROR (flag, "%s", strchr(LUMIERA_ERROR_##err, ':')+1);}), \
lumiera_error_set(LUMIERA_ERROR_##err))
#define LUMIERA_ERROR_SET(flag, err, extra) \
do { \
const char* theextra = extra; \
ERROR (flag, "%s%s%s", strchr(LUMIERA_ERROR_##err, ':')+1, theextra?": ":"", theextra?theextra:""); \
lumiera_error_set(LUMIERA_ERROR_##err, theextra); \
} while (0)
/**
* Set error state for the current thread.
* If the error state of the current thread was cleared, then set it, else preserve the old state.
* @param nerr name of the error with 'LUMIERA_ERROR_' prefix (example: LUMIERA_ERROR_NO_MEMORY)
* @return old state, that is NULL for success, when the state was cleared and a pointer to a pending
* @param extra a string (possibly a constructed tmpbuf) which adds some more context to the error occured this will be copied
* @return old state, that is NULL for success, when the state was cleared and a pointer to a pending
* error when the error state was already set
*/
lumiera_err
lumiera_error_set (lumiera_err err);
lumiera_error_set (lumiera_err nerr, const char* extra);
/**
* Query the extra context for the last error
* @return the extra string from the last error
*/
const char*
lumiera_error_extra (void);
/**
* Get and clear current error state.

View file

@ -79,7 +79,7 @@ namespace lumiera {
desc_ (description),
cause_ ("")
{
lumiera_error_set (this->id_);
lumiera_error_set (this->id_, description.c_str ());
}
@ -91,7 +91,7 @@ namespace lumiera {
desc_ (description),
cause_ (extractCauseMsg(cause))
{
lumiera_error_set (this->id_);
lumiera_error_set (this->id_, description.c_str ());
}

View file

@ -35,7 +35,7 @@ main (int argc, char** argv)
if (!strcmp(argv[1], "set"))
{
lumiera_error_set (LUMIERA_ERROR_TEST);
lumiera_error_set (LUMIERA_ERROR_TEST, NULL);
}
if (!strcmp(argv[1], "get_no"))
@ -47,7 +47,7 @@ main (int argc, char** argv)
if (!strcmp(argv[1], "get"))
{
lumiera_error_set (LUMIERA_ERROR_TEST);
lumiera_error_set (LUMIERA_ERROR_TEST, NULL);
const char* err;
err = lumiera_error ();
printf ("%s\n", err);
@ -55,7 +55,7 @@ main (int argc, char** argv)
if (!strcmp(argv[1], "get2"))
{
lumiera_error_set (LUMIERA_ERROR_TEST);
lumiera_error_set (LUMIERA_ERROR_TEST, NULL);
const char* err;
err = lumiera_error ();
printf ("%s\n", err);