|
Berkeley DB offers programmatic support for displaying error return values. The db_strerror interface returns a pointer to the error message corresponding to any Berkeley DB error return, similar to the ANSI C strerror interface, but able to handle both system error returns and Berkeley DB specific return values.
For example:
int ret; if ((ret = dbenv->set_cachesize(dbenv, 0, 32 * 1024)) != 0) { fprintf(stderr, "set_cachesize failed: %s\n", db_strerror(ret)); return (1); }
There are also two additional error functions, DBENV->err and DBENV->errx. These functions work like the ANSI C printf interface, taking a printf-style format string and argument list, and writing a message constructed from the format string and arguments.
The DBENV->err function appends the standard error string to the constructed message and the DBENV->errx function does not.
Error messages can be configured always to include a prefix (e.g., the program name) using the DBENV->set_errpfx interface.
These functions provide simpler ways of displaying Berkeley DB error messages:
int ret; dbenv->set_errpfx(dbenv, argv0); if ((ret = dbenv->open(dbenv, home, NULL, DB_CREATE | DB_INIT_LOG | DB_INIT_TXN | DB_USE_ENVIRON)) != 0) { dbenv->err(dbenv, ret, "open: %s", home); dbenv->errx(dbenv, "contact your system administrator: session ID was %d", session_id); return (1); }
For example, if the program was called "my_app", attempting to open an environment home directory in "/tmp/home", and the open call returned a permission error, the error messages shown would look like:
my_app: /tmp/home: Permission denied. my_app: contact your system administrator: session ID was 2