summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 6944190)
raw | patch | inline | side by side (parent: 6944190)
author | Florian Forster <octo@collectd.org> | |
Fri, 12 Jul 2013 16:17:19 +0000 (18:17 +0200) | ||
committer | Florian Forster <octo@collectd.org> | |
Fri, 12 Jul 2013 16:17:19 +0000 (18:17 +0200) |
The new implementation is faster, because it doesn't use snprintf().
src/csv.c | patch | blob | history |
diff --git a/src/csv.c b/src/csv.c
index 02d62c1e014e89f98becd7993ea14a646287b31b..afb4f8338c5d7e2611e489338eda5a642f3c56b8 100644 (file)
--- a/src/csv.c
+++ b/src/csv.c
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);
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);