From: Florian Forster Date: Sun, 22 Feb 2009 22:03:00 +0000 (+0100) Subject: src/plugin.c: Add a user_data_t pointer to log callbacks. X-Git-Tag: collectd-4.7.0~127^2~17 X-Git-Url: https://git.octo.it/?p=collectd.git;a=commitdiff_plain;h=3f9f3d0f8454337c3095f6c7bc2358a50df47876 src/plugin.c: Add a user_data_t pointer to log callbacks. --- diff --git a/src/logfile.c b/src/logfile.c index 382386b7..26d805a4 100644 --- a/src/logfile.c +++ b/src/logfile.c @@ -143,7 +143,8 @@ static void logfile_print (const char *msg, time_t timestamp_time) return; } /* void logfile_print */ -static void logfile_log (int severity, const char *msg) +static void logfile_log (int severity, const char *msg, + user_data_t __attribute__((unused)) *user_data) { if (severity > log_level) return; @@ -194,7 +195,7 @@ void module_register (void) { plugin_register_config ("logfile", logfile_config, config_keys, config_keys_num); - plugin_register_log ("logfile", logfile_log); + plugin_register_log ("logfile", logfile_log, /* user_data = */ NULL); plugin_register_notification ("logfile", logfile_notification); } /* void module_register (void) */ diff --git a/src/perl.c b/src/perl.c index b600ca1d..e6b7c460 100644 --- a/src/perl.c +++ b/src/perl.c @@ -1933,7 +1933,8 @@ static int perl_write (const data_set_t *ds, const value_list_t *vl, return pplugin_call_all (aTHX_ PLUGIN_WRITE, ds, vl); } /* static int perl_write (const data_set_t *, const value_list_t *) */ -static void perl_log (int level, const char *msg) +static void perl_log (int level, const char *msg, + user_data_t __attribute__((unused)) *user_data) { dTHX; @@ -2220,7 +2221,7 @@ static int init_pi (int argc, char **argv) perl_run (aTHX); - plugin_register_log ("perl", perl_log); + plugin_register_log ("perl", perl_log, /* user_data = */ NULL); plugin_register_notification ("perl", perl_notify); plugin_register_init ("perl", perl_init); diff --git a/src/plugin.c b/src/plugin.c index 49793dca..41a816f8 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -72,6 +72,13 @@ struct flush_func_s }; typedef struct flush_func_s flush_func_t; +struct log_func_s +{ + plugin_log_cb callback; + user_data_t udata; +}; +typedef struct log_func_s log_func_t; + /* * Private variables */ @@ -648,10 +655,31 @@ int plugin_register_data_set (const data_set_t *ds) return (c_avl_insert (data_sets, (void *) ds_copy->type, (void *) ds_copy)); } /* int plugin_register_data_set */ -int plugin_register_log (char *name, - void (*callback) (int priority, const char *msg)) +int plugin_register_log (const char *name, + plugin_log_cb callback, user_data_t *user_data) { - return (register_callback (&list_log, name, (void *) callback)); + log_func_t *lf; + + lf = (log_func_t *) malloc (sizeof (*lf)); + if (lf == NULL) + { + ERROR ("plugin_register_log: malloc failed."); + return (-1); + } + memset (lf, 0, sizeof (*lf)); + + lf->callback = callback; + if (user_data == NULL) + { + lf->udata.data = NULL; + lf->udata.free_func = NULL; + } + else + { + lf->udata = *user_data; + } + + return (register_callback (&list_log, name, (void *) lf)); } /* int plugin_register_log */ int plugin_register_notification (const char *name, @@ -766,7 +794,24 @@ int plugin_unregister_data_set (const char *name) int plugin_unregister_log (const char *name) { - return (plugin_unregister (list_log, name)); + llentry_t *e; + log_func_t *lf; + + e = llist_search (list_log, name); + + if (e == NULL) + return (-1); + + llist_remove (list_log, e); + + lf = (log_func_t *) e->value; + plugin_user_data_destroy (&lf->udata); + free (lf); + free (e->key); + + llentry_destroy (e); + + return (0); } int plugin_unregister_notification (const char *name) @@ -1172,8 +1217,6 @@ void plugin_log (int level, const char *format, ...) { char msg[1024]; va_list ap; - - void (*callback) (int, const char *); llentry_t *le; if (list_log == NULL) @@ -1192,8 +1235,11 @@ void plugin_log (int level, const char *format, ...) le = llist_head (list_log); while (le != NULL) { - callback = (void (*) (int, const char *)) le->value; - (*callback) (level, msg); + log_func_t *lf; + + lf = (log_func_t *) le->value; + + lf->callback (level, msg, &lf->udata); le = le->next; } diff --git a/src/plugin.h b/src/plugin.h index ed9fdab7..b4e5cb37 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -151,6 +151,8 @@ typedef int (*plugin_write_cb) (const data_set_t *, const value_list_t *, user_data_t *); typedef int (*plugin_flush_cb) (int timeout, const char *identifier, user_data_t *); +typedef void (*plugin_log_cb) (int severity, const char *message, + user_data_t *); /* * NAME @@ -250,8 +252,8 @@ int plugin_register_flush (const char *name, int plugin_register_shutdown (char *name, int (*callback) (void)); int plugin_register_data_set (const data_set_t *ds); -int plugin_register_log (char *name, - void (*callback) (int, const char *)); +int plugin_register_log (const char *name, + plugin_log_cb callback, user_data_t *user_data); int plugin_register_notification (const char *name, int (*callback) (const notification_t *notif)); diff --git a/src/syslog.c b/src/syslog.c index a21bef18..ace9dc6f 100644 --- a/src/syslog.c +++ b/src/syslog.c @@ -68,7 +68,8 @@ static int sl_config (const char *key, const char *value) return (0); } /* int sl_config */ -static void sl_log (int severity, const char *msg) +static void sl_log (int severity, const char *msg, + user_data_t __attribute__((unused)) *user_data) { if (severity > log_level) return; @@ -88,6 +89,6 @@ void module_register (void) openlog ("collectd", LOG_CONS | LOG_PID, LOG_DAEMON); plugin_register_config ("syslog", sl_config, config_keys, config_keys_num); - plugin_register_log ("syslog", sl_log); + plugin_register_log ("syslog", sl_log, /* user_data = */ NULL); plugin_register_shutdown ("syslog", sl_shutdown); } /* void module_register(void) */