From 3773df22f778f86fdb712850dd7ad937cde3bbfa Mon Sep 17 00:00:00 2001 From: Christian Persch Date: Fri, 3 Jan 2014 23:31:56 +0100 Subject: [PATCH] glib: Add API to set a log handler In evince we don't want to have the PDF warnings go to stderr (where they're most likely either getting lost or are annoying, when started from a terminal). --- glib/poppler.cc | 76 +++++++++++++++++++++++++++++++++++++ glib/poppler.h | 30 +++++++++++++++ glib/reference/poppler-sections.txt | 3 ++ 3 files changed, 109 insertions(+) diff --git a/glib/poppler.cc b/glib/poppler.cc index cc2ca4d..31cce05 100644 --- a/glib/poppler.cc +++ b/glib/poppler.cc @@ -19,6 +19,10 @@ #include #include "poppler.h" +#ifndef __GI_SCANNER__ +#include +#endif + GQuark poppler_error_quark (void) { static GQuark q = 0; @@ -56,3 +60,75 @@ poppler_get_version (void) { return poppler_version; } + +static PopplerLogHandler poppler_log_handler; +static gpointer poppler_log_handler_data; +static GDestroyNotify poppler_log_handler_destroy_data; + +/** + * PopplerLogHandler: + * @user_data: the user data passed to poppler_set_log_handler() + * @category: the error category + * @position: the offset in the file + * @message: the log message + * + * Since: 0.26 + */ + +static void +error_cb (void *data G_GNUC_UNUSED, + ErrorCategory category, + Goffset pos, + char *message) +{ + PopplerLogCategory cat; + + switch (category) { + case errSyntaxWarning: cat = POPPLER_LOG_CATEGORY_SYNTAX_WARNING; break; + case errSyntaxError: cat = POPPLER_LOG_CATEGORY_SYNTAX_ERROR; break; + case errIO: cat = POPPLER_LOG_CATEGORY_IO_ERROR; break; + case errUnimplemented: cat = POPPLER_LOG_CATEGORY_UNIMPLEMENTED; break; + case errInternal: cat = POPPLER_LOG_CATEGORY_INTERNAL_ERROR; break; + /* The following will never occur in poppler-glib */ + case errConfig: + case errCommandLine: + case errNotAllowed: + default: + return; + } + + poppler_log_handler (poppler_log_handler_data, cat, (goffset) pos, message); +} + +/** + * poppler_set_log_handler: + * @handler: (scope notified) allow-none): a #PopplerLogHandler, or %NULL + * @user_data: (closure) (allow-none): user data for handler, or %NULL + * @destroy_data: (allow-none): function to free user data, or %NULL + * + * Sets @handler as the poppler log handler. Setting a log handler will + * unset a previously set handler, and free its user data using the + * the previously supplied @destroy callback. Use %NULL handler to unset + * the handler without installing a new handler. + * + * Since: 0.26 + */ +void +poppler_set_log_handler (PopplerLogHandler handler, + gpointer user_data, + GDestroyNotify destroy_data) +{ + g_return_if_fail (destroy_data == NULL || user_data != NULL); + + if (poppler_log_handler_destroy_data && poppler_log_handler_data) + poppler_log_handler_destroy_data (poppler_log_handler_data); + + poppler_log_handler = handler; + poppler_log_handler_data = user_data; + poppler_log_handler_destroy_data = destroy_data; + + if (handler) + setErrorCallback (error_cb, NULL); + else + setErrorCallback (NULL, NULL); +} diff --git a/glib/poppler.h b/glib/poppler.h index 92121f6..dccd472 100644 --- a/glib/poppler.h +++ b/glib/poppler.h @@ -171,6 +171,27 @@ typedef enum /*< flags >*/ POPPLER_FIND_WHOLE_WORDS_ONLY = 1 << 2 } PopplerFindFlags; +/** + * PopplerLogCategory: + * POPPLER_LOG_CATEGORY_SYNTAX_WARNING: A PDF syntax error which can be + * worked around; the output will probably be correct. + * POPPLER_LOG_CATEGORY_SYNTAX_ERROR: A PDF syntax error which can be + * worked around; but the output will probably be incorrect. + * POPPLER_LOG_CATEGORY_IO_ERROR: An error in file I/O. + * POPPLER_LOG_CATEGORY_UNIMPLEMENTED: An unimplemented PDF feature was + * encountered; the display will be incorrect. + * POPPLER_LOG_CATEGORY_INTERNAL_ERROR: An internal error occurred. + * + * Since: 0.26 + */ +typedef enum { + POPPLER_LOG_CATEGORY_UNIMPLEMENTED, + POPPLER_LOG_CATEGORY_SYNTAX_WARNING, + POPPLER_LOG_CATEGORY_SYNTAX_ERROR, + POPPLER_LOG_CATEGORY_IO_ERROR, + POPPLER_LOG_CATEGORY_INTERNAL_ERROR +} PopplerLogCategory; + typedef struct _PopplerDocument PopplerDocument; typedef struct _PopplerIndexIter PopplerIndexIter; typedef struct _PopplerFontsIter PopplerFontsIter; @@ -217,6 +238,15 @@ typedef enum PopplerBackend poppler_get_backend (void); const char * poppler_get_version (void); +typedef void (* PopplerLogHandler) (gpointer user_data, + PopplerLogCategory category, + goffset position, + const char *message); + +void poppler_set_log_handler (PopplerLogHandler handler, + gpointer user_data, + GDestroyNotify destroy_data); + G_END_DECLS #include "poppler-features.h" diff --git a/glib/reference/poppler-sections.txt b/glib/reference/poppler-sections.txt index 9bf2a05..c59c412 100644 --- a/glib/reference/poppler-sections.txt +++ b/glib/reference/poppler-sections.txt @@ -349,6 +349,9 @@ poppler_date_parse poppler_color_new poppler_color_copy poppler_color_free +PopplerLogCategory +PopplerLogHandler +poppler_set_log_handler POPPLER_TYPE_COLOR -- 1.8.3.1