X-Git-Url: http://git.itanic.dy.fi/?p=log-plotter;a=blobdiff_plain;f=data.c;fp=data.c;h=31113706da0211dd4079fc45f82c7ff2afa8ae40;hp=b663cd50f5daed521764504e38f1f9646a00635f;hb=3870f4e94d04c2f900ab07100d2f2a4b577c851a;hpb=fbe43f1acc6fc7280b0efd230c3e86009f5a1c0d diff --git a/data.c b/data.c index b663cd5..3111370 100644 --- a/data.c +++ b/data.c @@ -3,6 +3,8 @@ #include #include #include +#include +#include #include #include #include @@ -14,9 +16,11 @@ #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; @@ -212,6 +216,8 @@ static int read_log_line(int infd, char *buf, size_t bufsize, 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; @@ -243,6 +249,27 @@ static int read_log_line(int infd, char *buf, size_t bufsize, int *offset) 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; @@ -274,6 +301,8 @@ static int read_data(struct eventhandler_entry *h) 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; @@ -283,9 +312,15 @@ static int read_data(struct eventhandler_entry *h) 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;" @@ -328,13 +363,13 @@ static struct dataparser_struct dataparser = { .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)