diff options
author | Keith Bostic <keith@wiredtiger.com> | 2012-05-04 14:38:50 +0000 |
---|---|---|
committer | Keith Bostic <keith@wiredtiger.com> | 2012-05-04 14:38:50 +0000 |
commit | c4dfc3f4d26eccfea8619670e718164a7550d042 (patch) | |
tree | 1e2d5420d209c54596d434635cd87104dc3092d3 /src | |
parent | 38c14648506dbe49d9ff3459baab77b4e04a6754 (diff) | |
download | mongo-c4dfc3f4d26eccfea8619670e718164a7550d042.tar.gz |
Rework WT_EVENT_HANDLERs.
Change the WT_SESSION's WT_EVENT_HANDLER reference to be a real
structure, and copy values into it instead of simply pointing to the
user-specified structure. The argument for using a reference is the
application can swap functions whenever it feels like it, the argument
against using a reference is that if the application allocates the
structure only long enough to call wiredtiger_open, we're going to drop
core. We don't document that the structure needs to persist, and if the
application wants to swap functions, it can do it inside the function
it gives us, so using a real structure doesn't lose any functionality.
Change the semantics of the event-handler error function (used
to return void, now returns int) and the progress function (used to
return int, now returns void). Change the semantics of the erorr and
informational handlers so that if the handler returns failure, we
fallback to the default behavior.
Document the event-handler parameters.
Change the code so that a missing handler calls the default
handler function: that means a NULL event-handler structure does the
right thing, and eliminates the need for __wt_event_handler_default
and some initialization code in a few places, and the need to make
the default error and informational message handlers look like the
application-specified versions.
Delete api/api_event.c, move the default informational and
error message handlers into support/err.c Delete the default progress
message handler entirely, it didn't do anything.
Delete include/progress.i (The only function the file had was
the default progress handler function, __wt_progress, which is only used
by verify and salvage, the compiler can inline it if it wants.) Moved
__wt_progress to support/err.c.
Diffstat (limited to 'src')
-rw-r--r-- | src/api/api_event.c | 71 | ||||
-rw-r--r-- | src/conn/conn_api.c | 22 | ||||
-rw-r--r-- | src/include/api.h | 2 | ||||
-rw-r--r-- | src/include/extern.h | 5 | ||||
-rw-r--r-- | src/include/progress.i | 23 | ||||
-rw-r--r-- | src/include/wiredtiger.in | 34 | ||||
-rw-r--r-- | src/include/wt_internal.in | 1 | ||||
-rw-r--r-- | src/packing/packing_api.c | 3 | ||||
-rw-r--r-- | src/session/session_api.c | 8 | ||||
-rw-r--r-- | src/support/err.c | 88 | ||||
-rw-r--r-- | src/utilities/util_verbose.c | 10 |
11 files changed, 110 insertions, 157 deletions
diff --git a/src/api/api_event.c b/src/api/api_event.c deleted file mode 100644 index 5298767a0c9..00000000000 --- a/src/api/api_event.c +++ /dev/null @@ -1,71 +0,0 @@ -/*- - * Copyright (c) 2008-2012 WiredTiger, Inc. - * All rights reserved. - * - * See the file LICENSE for redistribution information. - */ - -#include "wt_internal.h" - -/* - * __handle_error_default -- - * Default WT_EVENT_HANDLER->handle_error implementation: send to stderr. - */ -static void -__handle_error_default(WT_EVENT_HANDLER *handler, int error, const char *errmsg) -{ - size_t len_err, len_errmsg; - const char *err; - - WT_UNUSED(handler); - - if (error != 0) { - err = wiredtiger_strerror(error); - len_err = strlen(err); - len_errmsg = strlen(errmsg); - if (len_err >= len_errmsg && - strcmp(errmsg + (len_errmsg - len_err), err) != 0) { - fprintf(stderr, - "%s: %s\n", errmsg, wiredtiger_strerror(error)); - return; - } - } - fprintf(stderr, "%s\n", errmsg); -} - -/* - * __handle_message_default -- - * Default WT_EVENT_HANDLER->handle_message implementation: send to stdout. - */ -static int -__handle_message_default(WT_EVENT_HANDLER *handler, const char *message) -{ - WT_UNUSED(handler); - - printf("%s\n", message); - - return (0); -} - -/* - * __handle_progress_default -- - * Default WT_EVENT_HANDLER->handle_progress implementation: ignore. - */ -static int -__handle_progress_default(WT_EVENT_HANDLER *handler, - const char *operation, uint64_t progress) -{ - WT_UNUSED(handler); - WT_UNUSED(operation); - WT_UNUSED(progress); - - return (0); -} - -static WT_EVENT_HANDLER __event_handler_default = { - __handle_error_default, - __handle_message_default, - __handle_progress_default -}; - -WT_EVENT_HANDLER *__wt_event_handler_default = &__event_handler_default; diff --git a/src/conn/conn_api.c b/src/conn/conn_api.c index dfad036de4f..f0cef278b5d 100644 --- a/src/conn/conn_api.c +++ b/src/conn/conn_api.c @@ -728,25 +728,11 @@ wiredtiger_open(const char *home, WT_EVENT_HANDLER *event_handler, session->name = "wiredtiger_open"; /* - * Configure event handling as soon as possible so errors are handled - * correctly. If the application didn't configure an event handler, - * use the default one, and use default entries for any entries not - * set by the application. + * Configure any event handlers provided by the application as soon as + * possible so errors are handled correctly. */ - if (event_handler == NULL) - event_handler = __wt_event_handler_default; - else { - if (event_handler->handle_error == NULL) - event_handler->handle_error = - __wt_event_handler_default->handle_error; - if (event_handler->handle_message == NULL) - event_handler->handle_message = - __wt_event_handler_default->handle_message; - if (event_handler->handle_progress == NULL) - event_handler->handle_progress = - __wt_event_handler_default->handle_progress; - } - session->event_handler = event_handler; + if (event_handler != NULL) + session->event_handler = *event_handler; /* Remaining basic initialization of the connection structure. */ WT_ERR(__wt_connection_init(conn)); diff --git a/src/include/api.h b/src/include/api.h index ea451e2720e..b1ea0f81367 100644 --- a/src/include/api.h +++ b/src/include/api.h @@ -61,7 +61,7 @@ struct __wt_session_impl { WT_CONDVAR *cond; /* Condition variable */ const char *name; /* Name */ - WT_EVENT_HANDLER *event_handler; + WT_EVENT_HANDLER event_handler; WT_BTREE *btree; /* Current file */ TAILQ_HEAD(__btrees, __wt_btree_session) btrees; diff --git a/src/include/extern.h b/src/include/extern.h index bc4150cb0af..eb3d3ec7ad9 100644 --- a/src/include/extern.h +++ b/src/include/extern.h @@ -891,12 +891,13 @@ extern void __wt_errx(WT_SESSION_IMPL *session, 2, 3))); extern void __wt_msgv(WT_SESSION_IMPL *session, const char *fmt, va_list ap); -extern void __wt_verbose(WT_SESSION_IMPL *session, +extern void __wt_msg(WT_SESSION_IMPL *session, const char *fmt, ...) WT_GCC_ATTRIBUTE((format (printf, 2, 3))); -extern void __wt_msg(WT_SESSION_IMPL *session, +extern void __wt_progress(WT_SESSION_IMPL *session, const char *s, uint64_t v); +extern void __wt_verbose(WT_SESSION_IMPL *session, const char *fmt, ...) WT_GCC_ATTRIBUTE((format (printf, 2, diff --git a/src/include/progress.i b/src/include/progress.i deleted file mode 100644 index 315eeabc6cd..00000000000 --- a/src/include/progress.i +++ /dev/null @@ -1,23 +0,0 @@ -/*- - * Copyright (c) 2008-2012 WiredTiger, Inc. - * All rights reserved. - * - * See the file LICENSE for redistribution information. - */ - -/* - * __wt_progress -- - * Send a progress message to stdout. - */ -static inline void -__wt_progress(WT_SESSION_IMPL *session, const char *s, uint64_t v) -{ - WT_EVENT_HANDLER *handler; - - if (s == NULL) - s = session->name; - - handler = session->event_handler; - if (handler->handle_progress != NULL) - (void)handler->handle_progress(handler, s, v); -} diff --git a/src/include/wiredtiger.in b/src/include/wiredtiger.in index 168bad61b61..68f4b596ba6 100644 --- a/src/include/wiredtiger.in +++ b/src/include/wiredtiger.in @@ -1119,29 +1119,41 @@ int wiredtiger_open(const char *home, const char *wiredtiger_strerror(int err); /*! - * The interface implemented by applications in order to handle error messages, - * information messages and progress. Entries set to NULL are ignored and will - * continue to use the default handlers. + * The interface implemented by applications to handle error, informational and + * progress messages. Entries set to NULL are ignored and the default handlers + * will continue to be used. If a handler returns failure, the default handler + * will be called. */ struct __wt_event_handler { /*! * Callback to handle error messages; by default, error messages are - * written to the stderr stream. + * written to the stderr stream. If a handler returns non-zero, the + * message will be written to the stderr stream. + * + * @param error a WiredTiger, C99 or POSIX error code, which can + * be converted to a string using ::wiredtiger_strerror + * @param message an error string */ - void (*handle_error)(WT_EVENT_HANDLER *handler, - int error, const char *errmsg); + int (*handle_error)(WT_EVENT_HANDLER *handler, + int error, const char *message); /*! * Callback to handle informational messages; by default, informational - * messages are written to the stdout stream. + * messages are written to the stdout stream. If a handler returns + * non-zero, the message will be written to the stdout stream. + * + * @param message an informational string */ int (*handle_message)(WT_EVENT_HANDLER *handler, const char *message); /*! * Callback to handle progress messages; by default, progress messages * are ignored. + * + * @param operation a string representation of the operation + * @param progress a counter */ - int (*handle_progress)(WT_EVENT_HANDLER *handler, + void (*handle_progress)(WT_EVENT_HANDLER *handler, const char *operation, uint64_t progress); }; @@ -1219,9 +1231,9 @@ const char *wiredtiger_version(int *majorp, int *minorp, int *patchp); * whether the operation succeeded or failed. A return of zero indicates * success, all non-zero return values indicate some kind of failure. * - * WiredTiger reserves all values from -31,800 to -31,999 to itself as possible - * error values. In addition, C99/POSIX error codes such as \c ENOMEM, - * \c EINVAL and \c ENOTSUP may also be returned, with the usual meanings. + * WiredTiger reserves all values from -31,800 to -31,999 as possible error + * return values. WiredTiger may also return C99/POSIX error codes such as + * \c ENOMEM, \c EINVAL and \c ENOTSUP, with the usual meanings. * * The following are all of the WiredTiger-specific error returns: * @{ diff --git a/src/include/wt_internal.in b/src/include/wt_internal.in index 82c08091925..1085f0e65ff 100644 --- a/src/include/wt_internal.in +++ b/src/include/wt_internal.in @@ -193,7 +193,6 @@ struct __wt_update; #include "log.i" #include "mutex.i" #include "packing.i" -#include "progress.i" #include "serial.i" #include "serial_funcs.i" diff --git a/src/packing/packing_api.c b/src/packing/packing_api.c index 0b06c996f4b..0ee1def8925 100644 --- a/src/packing/packing_api.c +++ b/src/packing/packing_api.c @@ -19,7 +19,6 @@ wiredtiger_struct_size(const char *fmt, ...) size_t size; WT_CLEAR(session); - session.event_handler = __wt_event_handler_default; va_start(ap, fmt); size = __wt_struct_sizev(&session, fmt, ap); @@ -40,7 +39,6 @@ wiredtiger_struct_pack(void *buffer, size_t size, const char *fmt, ...) va_list ap; WT_CLEAR(session); - session.event_handler = __wt_event_handler_default; va_start(ap, fmt); ret = __wt_struct_packv(&session, buffer, size, fmt, ap); @@ -61,7 +59,6 @@ wiredtiger_struct_unpack(const void *buffer, size_t size, const char *fmt, ...) va_list ap; WT_CLEAR(session); - session.event_handler = __wt_event_handler_default; va_start(ap, fmt); ret = __wt_struct_unpackv(&session, buffer, size, fmt, ap); diff --git a/src/session/session_api.c b/src/session/session_api.c index 8e226c6d027..126febea6b3 100644 --- a/src/session/session_api.c +++ b/src/session/session_api.c @@ -497,14 +497,14 @@ __wt_open_session(WT_CONNECTION_IMPL *conn, int internal, WT_ERR(__wt_cond_alloc(session, "session", 1, &session_ret->cond)); session_ret->iface = stds; session_ret->iface.connection = &conn->iface; - WT_ASSERT(session, session->event_handler != NULL); - session_ret->event_handler = session->event_handler; + if (event_handler == NULL) + session_ret->event_handler = session->event_handler; + else + session_ret->event_handler = *event_handler; session_ret->hazard = conn->hazard + slot * conn->hazard_size; TAILQ_INIT(&session_ret->cursors); TAILQ_INIT(&session_ret->btrees); - if (event_handler != NULL) - session_ret->event_handler = event_handler; /* * Public sessions are automatically closed during WT_CONNECTION->close. diff --git a/src/support/err.c b/src/support/err.c index c9582ea4b3a..bac65b9a3a3 100644 --- a/src/support/err.c +++ b/src/support/err.c @@ -8,6 +8,39 @@ #include "wt_internal.h" /* + * __handle_error_default -- + * Default WT_EVENT_HANDLER->handle_error implementation: send to stderr. + */ +static void +__handle_error_default(int error, const char *errmsg) +{ + size_t len_err, len_errmsg; + const char *err; + + if (error != 0) { + err = wiredtiger_strerror(error); + len_err = strlen(err); + len_errmsg = strlen(errmsg); + if (len_err >= len_errmsg && + strcmp(errmsg + (len_errmsg - len_err), err) != 0) { + (void)fprintf(stderr, + "%s: %s\n", errmsg, wiredtiger_strerror(error)); + } + } + (void)fprintf(stderr, "%s\n", errmsg); +} + +/* + * __handle_message_default -- + * Default WT_EVENT_HANDLER->handle_message implementation: send to stdout. + */ +static void +__handle_message_default(const char *message) +{ + (void)printf("%s\n", message); +} + +/* * __wt_eventv -- * Report a message to an event handler. */ @@ -49,11 +82,15 @@ __wt_eventv(WT_SESSION_IMPL *session, int msg_event, p += snprintf(p, (size_t)(end - p), ": %s", wiredtiger_strerror(error)); - handler = session->event_handler; - if (msg_event) - (void)handler->handle_message(handler, s); - else - handler->handle_error(handler, error, s); + handler = &session->event_handler; + if (msg_event) { + if (handler->handle_message == NULL || + handler->handle_message(handler, s)) + __handle_message_default(s); + } else + if (handler->handle_error == NULL || + handler->handle_error(handler, error, s)) + __handle_error_default(error, s); } /* @@ -87,8 +124,8 @@ __wt_errx(WT_SESSION_IMPL *session, const char *fmt, ...) } /* - * __wt_msg_call -- - * Pass a message to an event handler. + * __wt_msgv -- + * Informational message. */ void __wt_msgv(WT_SESSION_IMPL *session, const char *fmt, va_list ap) @@ -104,37 +141,54 @@ __wt_msgv(WT_SESSION_IMPL *session, const char *fmt, va_list ap) (void)vsnprintf(s, sizeof(s), fmt, ap); - handler = session->event_handler; - (void)handler->handle_message(handler, s); + handler = &session->event_handler; + if (handler->handle_message == NULL || + handler->handle_message(handler, s)) + __handle_message_default(s); } /* - * __wt_verbose -- - * Verbose message. + * __wt_msg -- + * Informational message. */ void -__wt_verbose(WT_SESSION_IMPL *session, const char *fmt, ...) +__wt_msg(WT_SESSION_IMPL *session, const char *fmt, ...) WT_GCC_FUNC_ATTRIBUTE((format (printf, 2, 3))) { va_list ap; va_start(ap, fmt); - __wt_eventv(session, 1, 0, NULL, 0, fmt, ap); + __wt_msgv(session, fmt, ap); va_end(ap); } /* - * __wt_msg -- - * Report a message. + * __wt_progress -- + * Progress message. */ void -__wt_msg(WT_SESSION_IMPL *session, const char *fmt, ...) +__wt_progress(WT_SESSION_IMPL *session, const char *s, uint64_t v) +{ + WT_EVENT_HANDLER *handler; + + handler = &session->event_handler; + if (handler->handle_progress != NULL) + handler->handle_progress( + handler, s == NULL ? session->name : s, v); +} + +/* + * __wt_verbose -- + * Verbose message. + */ +void +__wt_verbose(WT_SESSION_IMPL *session, const char *fmt, ...) WT_GCC_FUNC_ATTRIBUTE((format (printf, 2, 3))) { va_list ap; va_start(ap, fmt); - __wt_msgv(session, fmt, ap); + __wt_eventv(session, 1, 0, NULL, 0, fmt, ap); va_end(ap); } diff --git a/src/utilities/util_verbose.c b/src/utilities/util_verbose.c index 6e3703201e5..3fd98190973 100644 --- a/src/utilities/util_verbose.c +++ b/src/utilities/util_verbose.c @@ -11,13 +11,13 @@ * __handle_error_verbose -- * Verbose WT_EVENT_HANDLER->handle_error implementation: send to stderr. */ -static void +static int __handle_error_verbose(WT_EVENT_HANDLER *handler, int error, const char *errmsg) { WT_UNUSED(handler); WT_UNUSED(error); - fprintf(stderr, "%s\n", errmsg); + return (fprintf(stderr, "%s\n", errmsg) < 0 ? -1 : 0); } /* @@ -29,22 +29,20 @@ __handle_message_verbose(WT_EVENT_HANDLER *handler, const char *message) { WT_UNUSED(handler); - (void)printf("%s\n", message); - return (0); + return (printf("%s\n", message) < 0 ? -1 : 0); } /* * __handle_progress_verbose -- * Default WT_EVENT_HANDLER->handle_progress implementation: ignore. */ -static int +static void __handle_progress_verbose(WT_EVENT_HANDLER *handler, const char *operation, uint64_t progress) { WT_UNUSED(handler); (void)printf("\r\t%s %-20" PRIu64, operation, progress); - return (0); } static WT_EVENT_HANDLER __event_handler_verbose = { |