#include <string.h>
#include <time.h>
#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
#include <sys/epoll.h>
#include <sys/types.h>
#include <sys/stat.h>
#include "event.h"
#include "utils.h"
#include "event.h"
+#include "plotter_status.h"
struct dataparser_struct {
struct eventhandler_entry evhandler;
+ char *log_path;
int infd;
int outfd;
int offset;
if (ret == 0) {
pr_err("Read EOF, stopping\n");
+
+ set_plotter_system_status(SYSTEM_STATUS_NO_USB);
return -1;
}
buf[*offset + ret] = 0;
return 0;
}
+static int open_new_logfile(struct dataparser_struct *dt)
+{
+ char path[2048];
+ struct tm *tm;
+ time_t cur_time = time(NULL);
+
+ tm = localtime(&cur_time);
+ strftime(path, sizeof(path), dt->log_path, tm);
+
+ pr_debug("Opening %s for writing the log file\n", path);
+
+ dt->outfd = open(path, O_CREAT | O_APPEND | O_WRONLY, 0664);
+ if (dt->outfd < 0) {
+ pr_err("Failed to open file %s for writing: %m\n",
+ path);
+ return -1;
+ }
+
+ return 0;
+}
+
static int read_data(struct eventhandler_entry *h)
{
struct dataparser_struct *dt;
parse_logline(dt->buf, &data);
+ set_plotter_system_status(data.state);
+
/* Fill in possibly missing timestamp */
if (isnan(data.timestamp) || data.timestamp == 0)
data.timestamp = cur_time - dt->start_time;
if (0)
dump_data(&data);
- if (!dt->outfd)
+ if (!dt->log_path)
return 0;
+ if (state_has_changed()) {
+ ret = open_new_logfile(dt);
+ if (ret < 0)
+ return ret;
+ }
+
len = snprintf(str, sizeof(str),
"%d;%d;%.1f;"
"%.3f;%.3f;%.3f;"
.evhandler.handle_event = read_data,
};
-int init_data_parser(int infd, int outfd)
+int init_data_parser(int infd, struct plotter_config *cfg)
{
int ret;
dataparser.evhandler.fd = infd;
dataparser.infd = infd;
- dataparser.outfd = outfd;
+ dataparser.log_path = cfg->log_path;
ret = register_event_handler(&dataparser.evhandler);
if (ret < 0)