X-Git-Url: https://git.wpitchoune.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=src%2Flib%2Fslog.c;h=d6c4bc2e305cef23e1833857e4f1abc709568e15;hb=c1e20f2631a1249720e9c75d753eacfcb0f6c7b9;hp=7e832ef83e3a7540d66585aa2772433bfcf4eef4;hpb=cb2b7143c2d307a423f899e30b6b0c1dd056e5b1;p=psensor.git diff --git a/src/lib/slog.c b/src/lib/slog.c index 7e832ef..d6c4bc2 100644 --- a/src/lib/slog.c +++ b/src/lib/slog.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2010-2012 jeanfi@gmail.com + * Copyright (C) 2010-2014 jeanfi@gmail.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as @@ -16,6 +16,9 @@ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA * 02110-1301 USA */ +#define _LARGEFILE_SOURCE 1 +#include "config.h" + #include #include #define _(str) gettext(str) @@ -26,20 +29,43 @@ #include #include #include - +#include #include "bool.h" #include "config.h" -#include "log.h" +#include +#include +#include "ptime.h" #include "slog.h" static FILE *file; -static struct timeval stv; static double *last_values; +static int period; +static struct psensor **sensors; +static pthread_mutex_t *sensors_mutex; +static pthread_t thread; +static time_t st; static const char *DEFAULT_FILENAME = "sensors.log"; -static char *get_default_path() +static char *time_to_str(time_t *t) +{ + struct tm lt; + char *str; + + if (!localtime_r(t, <)) + return NULL; + + str = malloc(64); + + if (strftime(str, 64, "%s", <)) + return str; + + free(str); + return NULL; +} + +static char *get_default_path(void) { char *home, *path, *dir; @@ -56,15 +82,15 @@ static char *get_default_path() free(dir); return path; - } else { - log_warn(_("HOME variable not set.")); - return strdup(DEFAULT_FILENAME); } + + log_warn(_("HOME variable not set.")); + return strdup(DEFAULT_FILENAME); } -int slog_init(const char *path, struct psensor **sensors) +static bool slog_open(const char *path, struct psensor **sensors) { - char *lpath; + char *lpath, *t; if (file) { log_err(_("Sensor log file already open.")); @@ -84,12 +110,10 @@ int slog_init(const char *path, struct psensor **sensors) if (!file) return 0; - if (gettimeofday(&stv, NULL)) { - log_err(_("gettimeofday failed.")); - return 0; - } + st = time(NULL); + t = time_to_str(&st); - fprintf(file, "I,%ld,%s\n", stv.tv_sec, VERSION); + fprintf(file, "I,%s,%s\n", t, VERSION); while (*sensors) { fprintf(file, "S,%s,%x\n", (*sensors)->id, (*sensors)->type); @@ -101,7 +125,7 @@ int slog_init(const char *path, struct psensor **sensors) return 1; } -void slog_write_sensors(struct psensor **sensors) +static void slog_write_sensors(struct psensor **sensors) { int count, i; double v; @@ -109,14 +133,11 @@ void slog_write_sensors(struct psensor **sensors) bool first_call; if (!file) { - log_err(_("Sensor log file not open.")); - return ; + log_debug(_("Sensor log file not open.")); + return; } - if (gettimeofday(&tv, NULL)) { - log_err(_("gettimeofday failed.")); - return ; - } + gettimeofday(&tv, NULL); count = psensor_list_size(sensors); @@ -127,7 +148,7 @@ void slog_write_sensors(struct psensor **sensors) last_values = malloc(count * sizeof(double)); } - fprintf(file, "%ld", tv.tv_sec - stv.tv_sec); + fprintf(file, "%ld", (long int)(tv.tv_sec - st)); for (i = 0; i < count; i++) { v = psensor_get_current_value(sensors[i]); @@ -144,14 +165,51 @@ void slog_write_sensors(struct psensor **sensors) fflush(file); } -void slog_close() +static void *slog_routine(void *data) +{ + while (1) { + pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL); + pmutex_lock(sensors_mutex); + slog_write_sensors(sensors); + pmutex_unlock(sensors_mutex); + pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL); + sleep(period); + } + + pthread_exit(NULL); +} + +void slog_close(void) { if (file) { + pthread_cancel(thread); + fclose(file); file = NULL; free(last_values); last_values = NULL; } else { - log_err(_("Sensor log not open, cannot close.")); + log_debug(_("Sensor log not open, cannot close.")); } } + +bool slog_activate(const char *path, + struct psensor **ss, + pthread_mutex_t *mutex, + int p) +{ + bool ret; + + sensors = ss; + sensors_mutex = mutex; + period = p; + + pthread_mutex_lock(mutex); + ret = slog_open(path, sensors); + pthread_mutex_unlock(mutex); + + if (ret) + pthread_create(&thread, NULL, slog_routine, NULL); + + return ret; +}