From a1d5fe0eed54535f23ac4df39a633f714f6085f1 Mon Sep 17 00:00:00 2001 From: Stefano Zacchiroli Date: Thu, 13 Mar 2003 21:45:37 +0000 Subject: [PATCH] added support for setting debug and error callbacks --- .../gdome_xslt/C/gdome_xslt/gdome_xslt.c | 85 +++++++++++++++++++ .../gdome_xslt/C/gdome_xslt/gdome_xslt.h | 23 +++++ .../gdome_xslt/ocaml/gdome_xslt/gdome_xslt.ml | 13 +++ .../ocaml/gdome_xslt/gdome_xslt.mli | 8 ++ .../ocaml/gdome_xslt/i_gdome_xslt.ml | 5 ++ .../ocaml/gdome_xslt/ml_gdome_xslt.c | 41 +++++++++ .../ocaml/gdome_xslt/ml_gdome_xslt.h | 5 ++ 7 files changed, 180 insertions(+) diff --git a/helm/DEVEL/gdome_xslt/C/gdome_xslt/gdome_xslt.c b/helm/DEVEL/gdome_xslt/C/gdome_xslt/gdome_xslt.c index 87d3d4324..2c34f9cb5 100644 --- a/helm/DEVEL/gdome_xslt/C/gdome_xslt/gdome_xslt.c +++ b/helm/DEVEL/gdome_xslt/C/gdome_xslt/gdome_xslt.c @@ -27,6 +27,8 @@ */ #include +#include +#include #include #include #include @@ -152,3 +154,86 @@ int saveResultToFd (int fd, GdomeDocument* result, xsltStylesheetPtr return xsltSaveResultToFd(fd, result_libxml, style_libxslt); } + /**********************************************/ + /* Error and Debugging Callbacks Registration */ + /**********************************************/ + + /* max size of a single message passed to callbacks */ +#define MAX_MSG_SIZE 1024 +#define TRUNCATED_MSG "... TRUNCATED ..." +#define TRUNCATED_MSG_LEN strlen(TRUNCATED_MSG) + + /* ERROR callbacks */ + + /* user provided error callback, needs a string input */ +static gdomeXsltMsgCallback errorUserCallback = NULL; + + /* libxslt like error callback, ignore context, builds a string + * input for user provided error callback and invoke it */ +void gdomeXsltErrorCallback (void *ctx, const char *msg, ...) { + va_list args; + char buf[MAX_MSG_SIZE]; + + if (errorUserCallback == NULL) + return; + + va_start(args, msg); + if (vsnprintf(buf, MAX_MSG_SIZE, msg, args) > MAX_MSG_SIZE - 1) + { /* message truncated; write TRUNCATED_MSG on it */ + strncpy(buf+(strlen(buf) - TRUNCATED_MSG_LEN), + TRUNCATED_MSG, TRUNCATED_MSG_LEN); + } + va_end(args); + + (*errorUserCallback) (buf); + + return; +} + + /* set user provided error callback */ +void setErrorCallback (gdomeXsltMsgCallback callback) +{ + errorUserCallback = callback; + xsltSetGenericErrorFunc(NULL, + (callback == NULL ? NULL : gdomeXsltErrorCallback)); + + return; +} + + /* DEBUG callbacks */ + + /* user provided debug callback, needs a string input */ +static gdomeXsltMsgCallback debugUserCallback = NULL; + + /* libxslt like debug callback, ignore context, builds a string + * input for user provided debug callback and invoke it */ +void gdomeXsltDebugCallback (void *ctx, const char *msg, ...) { + va_list args; + char buf[MAX_MSG_SIZE]; + + if (debugUserCallback == NULL) + return; + + va_start(args, msg); + if (vsnprintf(buf, MAX_MSG_SIZE, msg, args) > MAX_MSG_SIZE - 1) + { /* message truncated; write TRUNCATED_MSG on it */ + strncpy(buf+(strlen(buf) - TRUNCATED_MSG_LEN), + TRUNCATED_MSG, TRUNCATED_MSG_LEN); + } + va_end(args); + + (*debugUserCallback) (buf); + + return; +} + + /* set user provided debug callback */ +void setDebugCallback (gdomeXsltMsgCallback callback) +{ + debugUserCallback = callback; + xsltSetGenericDebugFunc(NULL, + (callback == NULL ? NULL : gdomeXsltDebugCallback)); + + return; +} + diff --git a/helm/DEVEL/gdome_xslt/C/gdome_xslt/gdome_xslt.h b/helm/DEVEL/gdome_xslt/C/gdome_xslt/gdome_xslt.h index 685c7afb4..383208770 100644 --- a/helm/DEVEL/gdome_xslt/C/gdome_xslt/gdome_xslt.h +++ b/helm/DEVEL/gdome_xslt/C/gdome_xslt/gdome_xslt.h @@ -41,12 +41,25 @@ extern "C" { #endif /* __cplusplus */ + /******************************/ + /* XSLT stylesheet Processing */ + /******************************/ + xsltStylesheetPtr processStylesheet (GdomeDocument* style); + + /*******************************/ + /* XSLT stylesheet Application */ + /*******************************/ + GdomeDocument* applyStylesheet (GdomeDocument* source, xsltStylesheetPtr style_libxslt, const char** params); + /******************/ + /* Results Output */ + /******************/ + int saveResultToFilename (const char* name, GdomeDocument* result, xsltStylesheetPtr style_libxslt, @@ -57,6 +70,16 @@ int saveResultToFile (FILE* file, int saveResultToFd (int fd, GdomeDocument* result, xsltStylesheetPtr style_libxslt); + + /**********************************************/ + /* Error and Debugging Callbacks Registration */ + /**********************************************/ + +typedef void(*gdomeXsltMsgCallback)(const char *); + +void setErrorCallback (gdomeXsltMsgCallback callback); +void setDebugCallback (gdomeXsltMsgCallback callback); + #ifdef __cplusplus } #endif /* __cplusplus */ diff --git a/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/gdome_xslt.ml b/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/gdome_xslt.ml index aa05777ab..b64a006b4 100644 --- a/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/gdome_xslt.ml +++ b/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/gdome_xslt.ml @@ -44,3 +44,16 @@ let saveResultToChannel ~outchan ~result ~stylesheet = ~stylesheet ;; +let setErrorCallback = function + | None -> I_gdome_xslt.disableErrorCallback () + | Some (callback: string -> unit) -> + Callback.register "error_callback" callback; + I_gdome_xslt.enableErrorCallback () +;; +let setDebugCallback = function + | None -> I_gdome_xslt.disableDebugCallback () + | Some (callback: string -> unit) -> + Callback.register "debug_callback" callback; + I_gdome_xslt.enableDebugCallback () +;; + diff --git a/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/gdome_xslt.mli b/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/gdome_xslt.mli index 3c7c232ec..8115457f3 100644 --- a/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/gdome_xslt.mli +++ b/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/gdome_xslt.mli @@ -49,3 +49,11 @@ val saveResultToChannel: stylesheet: I_gdome_xslt.processed_stylesheet -> unit +(** set error callback, that is a function invoked each time an error message is +generated. If None is passed, libxslt default error callback is used *) +val setErrorCallback: (string -> unit) option -> unit + +(** set debug callback, that is a function invoked each time a debugging message +is generated. If None is passed, libxslt default error callback is used *) +val setDebugCallback: (string -> unit) option -> unit + diff --git a/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/i_gdome_xslt.ml b/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/i_gdome_xslt.ml index 92f74da39..6f686c482 100644 --- a/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/i_gdome_xslt.ml +++ b/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/i_gdome_xslt.ml @@ -51,3 +51,8 @@ external saveResultToChannel: unit = "ml_saveResultToChannel" +external enableErrorCallback : unit -> unit = "ml_enableErrorCallback" +external disableErrorCallback : unit -> unit = "ml_disableErrorCallback" +external enableDebugCallback : unit -> unit = "ml_enableDebugCallback" +external disableDebugCallback : unit -> unit = "ml_disableDebugCallback" + diff --git a/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/ml_gdome_xslt.c b/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/ml_gdome_xslt.c index b6330868f..bc2854a9b 100644 --- a/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/ml_gdome_xslt.c +++ b/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/ml_gdome_xslt.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -141,3 +142,43 @@ value ml_saveResultToChannel(value channel, CAMLreturn0; } + /* error callback handling */ + +static void ml_gdomeXsltErrorCallback(const char *msg) { + callback(*caml_named_value("error_callback"), copy_string(msg)); + + return; +} + +value ml_enableErrorCallback(value unit) { + CAMLparam1(unit); + setErrorCallback(ml_gdomeXsltErrorCallback); + CAMLreturn(Val_unit); +} + +value ml_disableErrorCallback(value unit) { + CAMLparam1(unit); + setErrorCallback(NULL); + CAMLreturn(Val_unit); +} + + /* debug callback handling */ + +static void ml_gdomeXsltDebugCallback(const char *msg) { + callback(*caml_named_value("debug_callback"), copy_string(msg)); + + return; +} + +value ml_enableDebugCallback(value unit) { + CAMLparam1(unit); + setDebugCallback(ml_gdomeXsltDebugCallback); + CAMLreturn(Val_unit); +} + +value ml_disableDebugCallback(value unit) { + CAMLparam1(unit); + setDebugCallback(NULL); + CAMLreturn(Val_unit); +} + diff --git a/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/ml_gdome_xslt.h b/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/ml_gdome_xslt.h index 974ad7d14..a173bc97f 100644 --- a/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/ml_gdome_xslt.h +++ b/helm/DEVEL/gdome_xslt/ocaml/gdome_xslt/ml_gdome_xslt.h @@ -33,3 +33,8 @@ value ml_applyStylesheet(value source, value style, value params); value ml_saveResultToChannel(value channel, value result, value stylesheet); +value ml_enableErrorCallback(value unit); +value ml_disableErrorCallback(value unit); +value ml_enableDebugCallback(value unit); +value ml_disableDebugCallback(value unit); + -- 2.39.2