From: Florian Forster Date: Fri, 12 Jul 2013 16:17:19 +0000 (+0200) Subject: csv plugin: Re-implement value_list_to_filename(). X-Git-Tag: collectd-5.3.1~2^2~1 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=7f90e30;p=collectd.git csv plugin: Re-implement value_list_to_filename(). The new implementation is faster, because it doesn't use snprintf(). --- diff --git a/src/csv.c b/src/csv.c index 02d62c1e..afb4f833 100644 --- a/src/csv.c +++ b/src/csv.c @@ -121,65 +121,49 @@ static int value_list_to_string (char *buffer, int buffer_len, return (0); } /* int value_list_to_string */ -static int value_list_to_filename (char *buffer, int buffer_len, - const data_set_t *ds, const value_list_t *vl) +static int value_list_to_filename (char *buffer, size_t buffer_size, + value_list_t const *vl) { - int offset = 0; int status; - assert (0 == strcmp (ds->type, vl->type)); - - if (datadir != NULL) - { - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s/", datadir); - if ((status < 1) || (status >= buffer_len - offset)) - return (-1); - offset += status; - } + char *ptr; + size_t ptr_size; + time_t now; + struct tm struct_tm; - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s/", vl->host); - if ((status < 1) || (status >= buffer_len - offset)) - return (-1); - offset += status; + status = FORMAT_VL (buffer, buffer_size, vl); + if (status != 0) + return (status); - if (strlen (vl->plugin_instance) > 0) - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s-%s/", vl->plugin, vl->plugin_instance); - else - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s/", vl->plugin); - if ((status < 1) || (status >= buffer_len - offset)) - return (-1); - offset += status; + /* Skip all the time formatting stuff when printing to STDOUT or + * STDERR. */ + if (use_stdio) + return (0); - if (strlen (vl->type_instance) > 0) - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s-%s", vl->type, vl->type_instance); - else - status = ssnprintf (buffer + offset, buffer_len - offset, - "%s", vl->type); - if ((status < 1) || (status >= buffer_len - offset)) - return (-1); - offset += status; + ptr_size = buffer_size - strlen (buffer); + ptr = buffer + strlen (buffer); - if (!use_stdio) + /* "-2013-07-12" => 11 bytes */ + if (ptr_size < 12) { - time_t now; - struct tm stm; + ERROR ("csv plugin: Buffer too small."); + return (ENOMEM); + } - /* TODO: Find a way to minimize the calls to `localtime_r', - * since they are pretty expensive.. */ - now = time (NULL); - if (localtime_r (&now, &stm) == NULL) - { - ERROR ("csv plugin: localtime_r failed"); - return (1); - } + /* TODO: Find a way to minimize the calls to `localtime_r', + * since they are pretty expensive.. */ + now = time (NULL); + if (localtime_r (&now, &struct_tm) == NULL) + { + ERROR ("csv plugin: localtime_r failed"); + return (-1); + } - strftime (buffer + offset, buffer_len - offset, - "-%Y-%m-%d", &stm); + status = strftime (ptr, ptr_size, "-%Y-%m-%d", &struct_tm); + if (status == 0) /* yep, it returns zero on error. */ + { + ERROR ("csv plugin: strftime failed"); + return (-1); } return (0); @@ -275,7 +259,8 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl, return -1; } - if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0) + status = value_list_to_filename (filename, sizeof (filename), vl); + if (status != 0) return (-1); DEBUG ("csv plugin: csv_write: filename = %s;", filename);