not an error
[psensor.git] / src / lib / slog.c
index aed2a5b..b1c98ad 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2012 jeanfi@gmail.com
+ * Copyright (C) 2010-2013 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
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
+#include <sys/stat.h>
 #include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #include "bool.h"
 #include "config.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;
 
-int slog_init(const char *path, struct psensor **sensors)
+static const char *DEFAULT_FILENAME = "sensors.log";
+
+static char *get_default_path()
 {
-       file = fopen(path, "a");
+       char *home, *path, *dir;
 
-       if (!file) {
-               log_err(_("Cannot open sensor log file: %s"), path);
+       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);
+       }
+}
+
+static bool slog_open(const char *path, struct psensor **sensors)
+{
+       char *lpath;
+
+       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;
        }
 
@@ -59,23 +105,20 @@ 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;
        struct timeval tv;
        bool first_call;
 
-       if (!file)
-               return ;
-
-       if (gettimeofday(&tv, NULL)) {
-               log_err(_("slog_write_sensors: gettimeofday failed."));
+       if (!file) {
+               log_err(_("Sensor log file not open."));
                return ;
        }
 
+       gettimeofday(&tv, NULL);
+
        count = psensor_list_size(sensors);
 
        if (last_values) {
@@ -102,12 +145,51 @@ void slog_write_sensors(struct psensor **sensors)
        fflush(file);
 }
 
+static void *slog_routine(void *data)
+{
+       while (1) {
+               pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
+               pthread_mutex_lock(sensors_mutex);
+               slog_write_sensors(sensors);
+               pthread_mutex_unlock(sensors_mutex);
+               pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
+               sleep(period);
+       }
+
+       pthread_exit(0);
+}
+
 void slog_close()
 {
        if (file) {
+               pthread_cancel(thread);
+
                fclose(file);
                file = NULL;
                free(last_values);
                last_values = NULL;
+       } else {
+               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;
+}