Merge branch 'pull/collectd-4' into collectd-4
[collectd.git] / src / csv.c
index 268bdbf..5d64fb8 100644 (file)
--- a/src/csv.c
+++ b/src/csv.c
@@ -22,7 +22,6 @@
 #include "collectd.h"
 #include "plugin.h"
 #include "common.h"
-#include "utils_debug.h"
 
 /*
  * Private variables
@@ -114,18 +113,19 @@ static int value_list_to_filename (char *buffer, int buffer_len,
 
        {
                time_t now;
-               struct tm *tm;
+               struct tm stm;
 
-               /* TODO: Find a way to minimize the calls to `localtime', since
-                * they are pretty expensive.. */
+               /* TODO: Find a way to minimize the calls to `localtime_r',
+                * since they are pretty expensive.. */
                now = time (NULL);
-               tm = localtime (&now);
+               if (localtime_r (&now, &stm) == NULL)
+               {
+                       ERROR ("csv plugin: localtime_r failed");
+                       return (1);
+               }
 
                strftime (buffer + offset, buffer_len - offset,
-                               "-%Y-%m-%d", tm);
-
-               /* `localtime(3)' returns a pointer to static data,
-                * therefore the pointer may not be free'd. */
+                               "-%Y-%m-%d", &stm);
        }
 
        return (0);
@@ -142,8 +142,10 @@ static int csv_create_file (const char *filename, const data_set_t *ds)
        csv = fopen (filename, "w");
        if (csv == NULL)
        {
-               syslog (LOG_ERR, "csv plugin: fopen (%s) failed: %s",
-                               filename, strerror(errno));
+               char errbuf[1024];
+               ERROR ("csv plugin: fopen (%s) failed: %s",
+                               filename,
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
                return (-1);
        }
 
@@ -199,7 +201,7 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl)
        if (value_list_to_filename (filename, sizeof (filename), ds, vl) != 0)
                return (-1);
 
-       DBG ("filename = %s;", filename);
+       DEBUG ("csv plugin: csv_write: filename = %s;", filename);
 
        if (value_list_to_string (values, sizeof (values), ds, vl) != 0)
                return (-1);
@@ -213,14 +215,16 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl)
                }
                else
                {
-                       syslog (LOG_ERR, "stat(%s) failed: %s",
-                                       filename, strerror (errno));
+                       char errbuf[1024];
+                       ERROR ("stat(%s) failed: %s", filename,
+                                       sstrerror (errno, errbuf,
+                                               sizeof (errbuf)));
                        return (-1);
                }
        }
        else if (!S_ISREG (statbuf.st_mode))
        {
-               syslog (LOG_ERR, "stat(%s): Not a regular file!",
+               ERROR ("stat(%s): Not a regular file!",
                                filename);
                return (-1);
        }
@@ -228,8 +232,9 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl)
        csv = fopen (filename, "a");
        if (csv == NULL)
        {
-               syslog (LOG_ERR, "csv plugin: fopen (%s) failed: %s",
-                               filename, strerror (errno));
+               char errbuf[1024];
+               ERROR ("csv plugin: fopen (%s) failed: %s", filename,
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
                return (-1);
        }
        csv_fd = fileno (csv);
@@ -244,8 +249,9 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl)
        status = fcntl (csv_fd, F_SETLK, &fl);
        if (status != 0)
        {
-               syslog (LOG_ERR, "csv plugin: flock (%s) failed: %s",
-                               filename, strerror (errno));
+               char errbuf[1024];
+               ERROR ("csv plugin: flock (%s) failed: %s", filename,
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
                fclose (csv);
                return (-1);
        }
@@ -259,9 +265,12 @@ static int csv_write (const data_set_t *ds, const value_list_t *vl)
        return (0);
 } /* int csv_write */
 
-void module_register (void)
+void module_register (modreg_e load)
 {
-       plugin_register_config ("csv", csv_config,
-                       config_keys, config_keys_num);
-       plugin_register_write ("csv", csv_write);
-}
+       if (load & MR_WRITE)
+       {
+               plugin_register_config ("csv", csv_config,
+                               config_keys, config_keys_num);
+               plugin_register_write ("csv", csv_write);
+       }
+} /* void module_register */