2 * Copyright (C) 2010-2013 jeanfi@gmail.com
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 #define _LARGEFILE_SOURCE 1
24 #define _(str) gettext(str)
31 #include <sys/types.h>
41 static double *last_values;
43 static struct psensor **sensors;
44 static pthread_mutex_t *sensors_mutex;
45 static pthread_t thread;
48 static const char *DEFAULT_FILENAME = "sensors.log";
50 static char *get_default_path()
52 char *home, *path, *dir;
54 home = getenv("HOME");
57 dir = malloc(strlen(home)+1+strlen(".psensor")+1);
58 sprintf(dir, "%s/%s", home, ".psensor");
61 path = malloc(strlen(dir)+1+strlen(DEFAULT_FILENAME)+1);
62 sprintf(path, "%s/%s", dir, DEFAULT_FILENAME);
68 log_warn(_("HOME variable not set."));
69 return strdup(DEFAULT_FILENAME);
73 static bool slog_open(const char *path, struct psensor **sensors)
78 log_err(_("Sensor log file already open."));
82 lpath = path ? (char *)path : get_default_path();
84 file = fopen(lpath, "a");
87 log_err(_("Cannot open sensor log file: %s."), lpath);
98 fprintf(file, "I,%s,%s\n", t, VERSION);
101 fprintf(file, "S,%s,%x\n", (*sensors)->id, (*sensors)->type);
110 static void slog_write_sensors(struct psensor **sensors)
118 log_err(_("Sensor log file not open."));
122 gettimeofday(&tv, NULL);
124 count = psensor_list_size(sensors);
130 last_values = malloc(count * sizeof(double));
133 fprintf(file, "%ld", (long int)(tv.tv_sec - st));
134 for (i = 0; i < count; i++) {
135 v = psensor_get_current_value(sensors[i]);
137 if (!first_call && last_values[i] == v)
140 fprintf(file, ",%.1f", v);
150 static void *slog_routine(void *data)
153 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
154 pthread_mutex_lock(sensors_mutex);
155 slog_write_sensors(sensors);
156 pthread_mutex_unlock(sensors_mutex);
157 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
167 pthread_cancel(thread);
174 log_debug(_("Sensor log not open, cannot close."));
178 bool slog_activate(const char *path,
180 pthread_mutex_t *mutex,
186 sensors_mutex = mutex;
189 pthread_mutex_lock(mutex);
190 ret = slog_open(path, sensors);
191 pthread_mutex_unlock(mutex);
194 pthread_create(&thread, NULL, slog_routine, NULL);