#include <sys/types.h>
#include <sys/stat.h>
+#include <fcntl.h>
#include <unistd.h>
#include <string.h>
return entries;
}
+static int write_to_logfile(struct rrd_database *rrd, const char *data)
+{
+ time_t t = time(NULL);
+ int fd, ret;
+ int spacing, i;
+ char filename[1024];
+ char logstr[RRD_DATA_MAX_LEN * 2] = { 0 };
+ const char *time_stamp_fmt = "%Y.%m.%d %H:%M ";
+ char *str_ptr;
+
+ if (!rrd->logfile)
+ return 0;
+
+ if (rrd->logfile_timestamp_fmt)
+ time_stamp_fmt = rrd->logfile_timestamp_fmt;
+
+ strftime(filename, sizeof(filename), rrd->logfile, localtime(&t));
+
+ fd = open(filename, O_RDWR | O_APPEND | O_CREAT | O_CLOEXEC, 0644);
+ if (fd < 0) {
+ pr_err("Failed to open file %s for logging: %m\n", filename);
+ return -1;
+ }
+
+ strftime(logstr, sizeof(logstr), time_stamp_fmt, localtime(&t));
+
+ str_ptr = logstr + strlen(logstr);
+
+ data += 2; /* Skip the "N: part */
+ spacing = 12;
+
+ while (*data && str_ptr - logstr < sizeof(logstr) - 1) {
+ if (*data == ':') {
+ *str_ptr++ = ' ';
+ for (i = 0; i < spacing; i++)
+ *str_ptr++ = ' ';
+ spacing = 12;
+ data++;
+ continue;
+ }
+
+ *str_ptr++ = *data++;
+ spacing--;
+ }
+ *str_ptr++ = '\n';
+ *str_ptr++ = 0;
+
+ ret = write(fd, logstr, strlen(logstr));
+ if (ret < 0)
+ pr_err("Failed to write to logfile %s: %m\n", filename);
+
+ close(fd);
+
+ return ret < 0 ? ret : 0;
+}
+
int rrdtool_update_data(struct rrd_database *rrd)
{
int pid;
pr_info("Data: %s\n", data);
sanitize_rrd_update_data(data + l);
+ write_to_logfile(rrd, data);
+
pid = run(cmd, cmdline);
harvest_zombies(pid);
}