From: Florian Forster Date: Wed, 14 Mar 2007 08:55:05 +0000 (+0100) Subject: src/plugin.[ch]: Add `log' callbacks. X-Git-Tag: collectd-4.0.0~156 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=d9e0b91f78a3063551908d3a268d2b020509970b;p=collectd.git src/plugin.[ch]: Add `log' callbacks. --- diff --git a/src/plugin.c b/src/plugin.c index 852a693e..f2e205af 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -47,6 +47,7 @@ static llist_t *list_read; static llist_t *list_write; static llist_t *list_shutdown; static llist_t *list_data_set; +static llist_t *list_log; static char *plugindir = NULL; @@ -274,6 +275,12 @@ int plugin_register_data_set (const data_set_t *ds) return (register_callback (&list_data_set, ds->type, (void *) ds)); } /* int plugin_register_data_set */ +int plugin_register_log (char *name, + void (*callback) (int priority, const char *msg)) +{ + return (register_callback (&list_log, name, (void *) callback)); +} /* int plugin_register_log */ + int plugin_unregister_init (const char *name) { return (plugin_unregister (list_init, name)); @@ -311,6 +318,11 @@ int plugin_unregister_data_set (const char *name) return (plugin_unregister (list_data_set, name)); } +int plugin_unregister_log (const char *name) +{ + return (plugin_unregister (list_log, name)); +} + void plugin_init_all (void) { int (*callback) (void); @@ -441,6 +453,34 @@ int plugin_dispatch_values (const char *name, const value_list_t *vl) return (0); } +void plugin_log (int level, const char *format, ...) +{ + char msg[512]; + va_list ap; + + void (*callback) (int, const char *); + llentry_t *le; + +#if !COLLECT_DEBUG + if (level >= LOG_DEBUG) + return; +#endif + + va_start (ap, format); + vsnprintf (msg, 512, format, ap); + msg[511] = '\0'; + va_end (ap); + + le = llist_head (list_log); + while (le != NULL) + { + callback = le->value; + (*callback) (level, msg); + + le = le->next; + } +} /* void plugin_log */ + void plugin_complain (int level, complain_t *c, const char *format, ...) { char message[512]; diff --git a/src/plugin.h b/src/plugin.h index ef84a46f..cf2c3f77 100644 --- a/src/plugin.h +++ b/src/plugin.h @@ -27,6 +27,22 @@ #define DS_TYPE_COUNTER 0 #define DS_TYPE_GAUGE 1 +#ifndef LOG_ERR +# define LOG_ERR 3 +#endif +#ifndef LOG_WARNING +# define LOG_WARNING 4 +#endif +#ifndef LOG_NOTICE +# define LOG_NOTICE 5 +#endif +#ifndef LOG_INFO +# define LOG_INFO 6 +#endif +#ifndef LOG_DEBUG +# define LOG_DEBUG 7 +#endif + /* * Public data types */ @@ -135,12 +151,15 @@ int plugin_register_write (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_unregister_init (const char *name); int plugin_unregister_read (const char *name); int plugin_unregister_write (const char *name); int plugin_unregister_shutdown (const char *name); int plugin_unregister_data_set (const char *name); +int plugin_unregister_log (const char *name); /* * NAME @@ -159,6 +178,13 @@ int plugin_unregister_data_set (const char *name); */ int plugin_dispatch_values (const char *name, const value_list_t *vl); +void plugin_log (int level, const char *format, ...); +#define ERROR(...) plugin_log (LOG_ERR, __VA_ARGS__) +#define WARNING(...) plugin_log (LOG_WARNING, __VA_ARGS__) +#define NOTICE(...) plugin_log (LOG_NOTICE, __VA_ARGS__) +#define INFO(...) plugin_log (LOG_INFO, __VA_ARGS__) +#define DEBUG(...) plugin_log (LOG_DEBUG, __VA_ARGS__) + /* TODO: Move plugin_{complain,relief} into `utils_complain.[ch]'. -octo */ void plugin_complain (int level, complain_t *c, const char *format, ...); void plugin_relief (int level, complain_t *c, const char *format, ...);