/*
- * 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
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301 USA
*/
+#define _LARGEFILE_SOURCE 1
+#include "config.h"
+
#include <locale.h>
#include <libintl.h>
#define _(str) gettext(str)
#include "bool.h"
#include "config.h"
-#include "log.h"
+#include <plog.h>
+#include <pmutex.h>
+#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 *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()
{
char *home, *path, *dir;
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);
}
static bool slog_open(const char *path, struct psensor **sensors)
{
- char *lpath;
+ char *lpath, *t;
if (file) {
log_err(_("Sensor log file already open."));
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);
bool first_call;
if (!file) {
- log_err(_("Sensor log file not open."));
- return ;
+ log_debug(_("Sensor log file not open."));
+ return;
}
gettimeofday(&tv, NULL);
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]);
{
while (1) {
pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
- pthread_mutex_lock(sensors_mutex);
+ pmutex_lock(sensors_mutex);
slog_write_sensors(sensors);
- pthread_mutex_unlock(sensors_mutex);
+ pmutex_unlock(sensors_mutex);
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
sleep(period);
}
free(last_values);
last_values = NULL;
} else {
- log_err(_("Sensor log not open, cannot close."));
+ log_debug(_("Sensor log not open, cannot close."));
}
}