From c47718b2da10f3546b54b4e1001bb5ebe76227df Mon Sep 17 00:00:00 2001 From: Hans-Peter Deifel Date: Fri, 12 Dec 2014 13:09:51 +0100 Subject: [PATCH] cpp: New API to set debug output function Adds the global function set_debug_error_function, that allows users to install their own function to print internal poppler errors. --- cpp/poppler-global.cpp | 28 ++++++++++++++++++++++++++++ cpp/poppler-global.h | 4 ++++ cpp/poppler-private.cpp | 14 +++++++++++--- cpp/poppler-private.h | 2 ++ 4 files changed, 45 insertions(+), 3 deletions(-) diff --git a/cpp/poppler-global.cpp b/cpp/poppler-global.cpp index 176c0c8..e95c428 100644 --- a/cpp/poppler-global.cpp +++ b/cpp/poppler-global.cpp @@ -334,3 +334,31 @@ std::ostream& poppler::operator<<(std::ostream& stream, const byte_array &array) stream << "]"; return stream; } + +/** + \typedef poppler::debug_func + + Debug/error function. + + This function type is used for debugging & error output; + the first parameter is the actual message, the second is the unaltered + closure argument which was passed to the set_debug_error_function() call. + + \since 0.30.0 + */ + +/** + Set a new debug/error output function. + + If not set, by default error and debug messages will be sent to stderr. + + \param debug_function the new debug function + \param closure user data which will be passed as-is to the debug function + + \since 0.30.0 + */ +void poppler::set_debug_error_function(debug_func debug_function, void *closure) +{ + poppler::detail::user_debug_function = debug_function; + poppler::detail::debug_closure = closure; +} diff --git a/cpp/poppler-global.h b/cpp/poppler-global.h index 5650182..157aaf9 100644 --- a/cpp/poppler-global.h +++ b/cpp/poppler-global.h @@ -106,6 +106,10 @@ POPPLER_CPP_EXPORT time_type convert_date(const std::string &date); POPPLER_CPP_EXPORT std::ostream& operator<<(std::ostream& stream, const byte_array &array); +typedef void(*debug_func)(const std::string &, void *); + +POPPLER_CPP_EXPORT void set_debug_error_function(debug_func debug_function, void *closure); + } #endif diff --git a/cpp/poppler-private.cpp b/cpp/poppler-private.cpp index 3bee619..ed8d381 100644 --- a/cpp/poppler-private.cpp +++ b/cpp/poppler-private.cpp @@ -29,16 +29,24 @@ using namespace poppler; +static void stderr_debug_function(const std::string &msg, void * /*data*/) +{ + std::cerr << "poppler/" << msg; +} + +debug_func detail::user_debug_function = stderr_debug_function; +void *detail::debug_closure; + void detail::error_function(void * /*data*/, ErrorCategory /*category*/, Goffset pos, char *msg) { std::ostringstream oss; if (pos >= 0) { - oss << "poppler/error (" << pos << "): "; + oss << "error (" << pos << "): "; } else { - oss << "poppler/error: "; + oss << "error: "; } oss << msg; - std::cerr << oss.str(); + detail::user_debug_function(oss.str(), detail::debug_closure); } rectf detail::pdfrectangle_to_rectf(const PDFRectangle &pdfrect) diff --git a/cpp/poppler-private.h b/cpp/poppler-private.h index 3e8873f..6dfdd61 100644 --- a/cpp/poppler-private.h +++ b/cpp/poppler-private.h @@ -39,6 +39,8 @@ namespace poppler namespace detail { +extern debug_func user_debug_function; +extern void *debug_closure; void error_function(void *data, ErrorCategory category, Goffset pos, char *msg); rectf pdfrectangle_to_rectf(const PDFRectangle &pdfrect); -- 2.0.4