#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;
static const char *DEFAULT_FILENAME = "sensors.log";
}
}
-int slog_init(const char *path, struct psensor **sensors)
+static bool slog_open(const char *path, struct psensor **sensors)
{
char *lpath;
return 1;
}
-void slog_write_sensors(struct psensor **sensors)
+static void slog_write_sensors(struct psensor **sensors)
{
int count, i;
double v;
return ;
}
- if (gettimeofday(&tv, NULL)) {
- log_err(_("gettimeofday failed."));
- return ;
- }
+ gettimeofday(&tv, NULL);
count = psensor_list_size(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);
log_err(_("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;
+}