From: Florian Forster Date: Fri, 21 Aug 2009 09:23:30 +0000 (+0200) Subject: http plugin: http_value_list_to_string: Clean-up. X-Git-Tag: collectd-4.8.0~26^2~9 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=3cb2eec6754b18cb42be315d07e1a70bdbf40202;p=collectd.git http plugin: http_value_list_to_string: Clean-up. --- diff --git a/src/http.c b/src/http.c index 8add24b3..d5cc2e24 100644 --- a/src/http.c +++ b/src/http.c @@ -103,56 +103,52 @@ static int http_init(void) /* {{{ */ return (0); } /* }}} */ -static int http_value_list_to_string (char *buffer, int buffer_len, /* {{{ */ +static int http_value_list_to_string (char *buffer, /* {{{ */ + size_t buffer_size, const data_set_t *ds, const value_list_t *vl) { - int offset = 0; + size_t offset = 0; int status; int i; assert (0 == strcmp (ds->type, vl->type)); - memset (buffer, '\0', buffer_len); + memset (buffer, 0, buffer_size); - for (i = 0; i < ds->ds_num; i++) - { - if ((ds->ds[i].type != DS_TYPE_COUNTER) - && (ds->ds[i].type != DS_TYPE_GAUGE) - && (ds->ds[i].type != DS_TYPE_DERIVE) - && (ds->ds[i].type != DS_TYPE_ABSOLUTE)) - return (-1); +#define BUFFER_ADD(...) do { \ + status = ssnprintf (buffer + offset, buffer_size - offset, \ + __VA_ARGS__); \ + if (status < 1) \ + return (-1); \ + else if (((size_t) status) >= (buffer_size - offset)) \ + return (-1); \ + else \ + offset += ((size_t) status); \ +} while (0) - status = -1; - if (ds->ds[i].type == DS_TYPE_GAUGE) - { - status = ssnprintf (buffer + offset, buffer_len - offset, - ":%lf", vl->values[i].gauge); - } - else if ((ds->ds[i].type == DS_TYPE_COUNTER) - || (ds->ds[i].type == DS_TYPE_ABSOLUTE)) - { - status = ssnprintf (buffer + offset, - buffer_len - offset, - ":%"PRIu64, - (ds->ds[i].type == DS_TYPE_COUNTER) - ? vl->values[i].counter - : vl->values[i].absolute); - } - else if (ds->ds[i].type == DS_TYPE_DERIVE) - { - status = ssnprintf (buffer + offset, - buffer_len - offset, - ":%"PRIi64, - vl->values[i].derive); - } + BUFFER_ADD ("%lu", (unsigned long) vl->time); - if ((status < 1) || (status >= (buffer_len - offset))) - return (-1); + for (i = 0; i < ds->ds_num; i++) +{ + if (ds->ds[i].type == DS_TYPE_GAUGE) + BUFFER_ADD (":%f", vl->values[i].gauge); + else if (ds->ds[i].type == DS_TYPE_COUNTER) + BUFFER_ADD (":%llu", vl->values[i].counter); + else if (ds->ds[i].type == DS_TYPE_DERIVE) + BUFFER_ADD (":%"PRIi64, vl->values[i].derive); + else if (ds->ds[i].type == DS_TYPE_ABSOLUTE) + BUFFER_ADD (":%"PRIu64, vl->values[i].absolute); + else + { + ERROR ("http plugin: Unknown data source type: %i", + ds->ds[i].type); + return (-1); + } +} /* for ds->ds_num */ - offset += status; - } /* for ds->ds_num */ +#undef BUFFER_ADD - return (0); +return (0); } /* }}} int http_value_list_to_string */ static int http_config (const char *key, const char *value) /* {{{ */