X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;f=src%2Flib%2Fslog.c;h=7e832ef83e3a7540d66585aa2772433bfcf4eef4;hb=cb2b7143c2d307a423f899e30b6b0c1dd056e5b1;hp=effd22367fc0ba488c7ca099c539b0ba43532aa8;hpb=d9eaecbb54ed59e6102dab239ae6ccf819a86fd1;p=psensor.git diff --git a/src/lib/slog.c b/src/lib/slog.c index effd223..7e832ef 100644 --- a/src/lib/slog.c +++ b/src/lib/slog.c @@ -22,7 +22,11 @@ #include #include +#include +#include #include +#include + #include "bool.h" #include "config.h" @@ -33,17 +37,55 @@ static FILE *file; static struct timeval stv; static double *last_values; +static const char *DEFAULT_FILENAME = "sensors.log"; + +static char *get_default_path() +{ + char *home, *path, *dir; + + home = getenv("HOME"); + + if (home) { + dir = malloc(strlen(home)+1+strlen(".psensor")+1); + sprintf(dir, "%s/%s", home, ".psensor"); + mkdir(dir, 0777); + + path = malloc(strlen(dir)+1+strlen(DEFAULT_FILENAME)+1); + sprintf(path, "%s/%s", dir, DEFAULT_FILENAME); + + free(dir); + + return path; + } else { + log_warn(_("HOME variable not set.")); + return strdup(DEFAULT_FILENAME); + } +} + int slog_init(const char *path, struct psensor **sensors) { - file = fopen(path, "a"); + char *lpath; - if (!file) { - log_err(_("slog_init: cannot open sensor log file: %s"), path); + if (file) { + log_err(_("Sensor log file already open.")); return 0; } + lpath = path ? (char *)path : get_default_path(); + + file = fopen(lpath, "a"); + + if (!file) + log_err(_("Cannot open sensor log file: %s."), lpath); + + if (!path) + free((char *)lpath); + + if (!file) + return 0; + if (gettimeofday(&stv, NULL)) { - log_err(_("slog_init: gettimeofday failed.")); + log_err(_("gettimeofday failed.")); return 0; } @@ -66,11 +108,13 @@ void slog_write_sensors(struct psensor **sensors) struct timeval tv; bool first_call; - if (!file) + if (!file) { + log_err(_("Sensor log file not open.")); return ; + } if (gettimeofday(&tv, NULL)) { - log_err(_("slog_write_sensors: gettimeofday failed.")); + log_err(_("gettimeofday failed.")); return ; } @@ -107,5 +151,7 @@ void slog_close() file = NULL; free(last_values); last_values = NULL; + } else { + log_err(_("Sensor log not open, cannot close.")); } }