summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorKeith Bostic <keith@wiredtiger.com>2016-02-21 12:41:04 -0500
committerKeith Bostic <keith@wiredtiger.com>2016-02-21 12:41:04 -0500
commitc4b6096c81a502e5f5d345e3af92817f49afe229 (patch)
treeabff121b4e7e4bfa8d21bc1d08139189b7818bf3 /examples
parent2c98d0b51854f827c6cfd837eb550581f8b68a8b (diff)
downloadmongo-c4b6096c81a502e5f5d345e3af92817f49afe229.tar.gz
WT-2107: Add example code including an event handler
Add an event handler example to the error handling docs.
Diffstat (limited to 'examples')
-rw-r--r--examples/c/ex_all.c66
1 files changed, 66 insertions, 0 deletions
diff --git a/examples/c/ex_all.c b/examples/c/ex_all.c
index 418c99ad6a3..ef77f10f6de 100644
--- a/examples/c/ex_all.c
+++ b/examples/c/ex_all.c
@@ -36,6 +36,7 @@
#include <sys/stat.h>
#include <inttypes.h>
+#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -1044,6 +1045,57 @@ backup(WT_SESSION *session)
return (ret);
}
+#define APPLICATION_ERROR 1
+#define APPLICATION_INFO 2
+static int
+application_logging(int which, const char *message)
+{
+ (void)which;
+ (void)message;
+ return (0);
+}
+
+/*! [Function event_handler] */
+
+/*
+ * handle_error --
+ * WiredTiger error handler.
+ */
+int
+wiredtiger_handle_error(
+ WT_EVENT_HANDLER *handler, WT_SESSION *session, const char *error)
+{
+ int ret;
+
+ (void)(handler); /* Unused variables */
+
+ /* Timestamp and log the error message. */
+ ret = application_logging(APPLICATION_ERROR, error);
+
+ /* Copy and flush the message to stderr. */
+ if (fprintf(stderr, "%p: %s\n", session, error) < 0 && ret == 0)
+ ret = EIO;
+ if (fflush(stderr) != 0 && ret == 0)
+ ret = errno;
+ return (ret);
+}
+
+/*
+ * handle_message --
+ * WiredTiger message handler.
+ */
+int
+wiredtiger_handle_message(
+ WT_EVENT_HANDLER *handler, WT_SESSION *session, const char *message)
+{
+ (void)(handler); /* Unused variables */
+ (void)(session); /* Unused variables */
+
+ /* Timestamp and log the informational message. */
+ return (application_logging(APPLICATION_INFO, message));
+}
+/*! [Function event_handler] */
+
int
main(void)
{
@@ -1113,6 +1165,20 @@ main(void)
if (ret == 0)
(void)conn->close(conn, NULL);
#endif
+ {
+ /*! [Configure event_handler] */
+ WT_EVENT_HANDLER event_handler;
+
+ event_handler.handle_error = wiredtiger_handle_error;
+ event_handler.handle_message = wiredtiger_handle_message;
+ event_handler.handle_progress = NULL;
+ event_handler.handle_close = NULL;
+
+ ret = wiredtiger_open(home, &event_handler, "create", &conn);
+ /*! [Configure event_handler] */
+ if (ret == 0)
+ (void)conn->close(conn, NULL);
+ }
/*! [Configure file_extend] */
ret = wiredtiger_open(