2 * Copyright (C) 2010-2012 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
21 #define _(str) gettext(str)
28 #include <sys/types.h>
37 static struct timeval stv;
38 static double *last_values;
40 static struct psensor **sensors;
41 static pthread_mutex_t *sensors_mutex;
42 static pthread_t thread;
44 static const char *DEFAULT_FILENAME = "sensors.log";
46 static char *get_default_path()
48 char *home, *path, *dir;
50 home = getenv("HOME");
53 dir = malloc(strlen(home)+1+strlen(".psensor")+1);
54 sprintf(dir, "%s/%s", home, ".psensor");
57 path = malloc(strlen(dir)+1+strlen(DEFAULT_FILENAME)+1);
58 sprintf(path, "%s/%s", dir, DEFAULT_FILENAME);
64 log_warn(_("HOME variable not set."));
65 return strdup(DEFAULT_FILENAME);
69 static bool slog_open(const char *path, struct psensor **sensors)
74 log_err(_("Sensor log file already open."));
78 lpath = path ? (char *)path : get_default_path();
80 file = fopen(lpath, "a");
83 log_err(_("Cannot open sensor log file: %s."), lpath);
91 if (gettimeofday(&stv, NULL)) {
92 log_err(_("gettimeofday failed."));
96 fprintf(file, "I,%ld,%s\n", stv.tv_sec, VERSION);
99 fprintf(file, "S,%s,%x\n", (*sensors)->id, (*sensors)->type);
108 static void slog_write_sensors(struct psensor **sensors)
116 log_err(_("Sensor log file not open."));
120 gettimeofday(&tv, NULL);
122 count = psensor_list_size(sensors);
128 last_values = malloc(count * sizeof(double));
131 fprintf(file, "%ld", tv.tv_sec - stv.tv_sec);
132 for (i = 0; i < count; i++) {
133 v = psensor_get_current_value(sensors[i]);
135 if (!first_call && last_values[i] == v)
138 fprintf(file, ",%.1f", v);
148 static void *slog_routine(void *data)
151 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
152 pthread_mutex_lock(sensors_mutex);
153 slog_write_sensors(sensors);
154 pthread_mutex_unlock(sensors_mutex);
155 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
165 pthread_cancel(thread);
172 log_err(_("Sensor log not open, cannot close."));
176 bool slog_activate(const char *path,
178 pthread_mutex_t *mutex,
184 sensors_mutex = mutex;
187 pthread_mutex_lock(mutex);
188 ret = slog_open(path, sensors);
189 pthread_mutex_unlock(mutex);
192 pthread_create(&thread, NULL, slog_routine, NULL);