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 const char *DEFAULT_FILENAME = "sensors.log";
42 static char *get_default_path()
44 char *home, *path, *dir;
46 home = getenv("HOME");
49 dir = malloc(strlen(home)+1+strlen(".psensor")+1);
50 sprintf(dir, "%s/%s", home, ".psensor");
53 path = malloc(strlen(dir)+1+strlen(DEFAULT_FILENAME)+1);
54 sprintf(path, "%s/%s", dir, DEFAULT_FILENAME);
60 log_warn(_("HOME variable not set."));
61 return strdup(DEFAULT_FILENAME);
65 int slog_init(const char *path, struct psensor **sensors)
70 log_err(_("Sensor log file already open."));
74 lpath = path ? (char *)path : get_default_path();
76 file = fopen(lpath, "a");
79 log_err(_("Cannot open sensor log file: %s."), lpath);
87 if (gettimeofday(&stv, NULL)) {
88 log_err(_("gettimeofday failed."));
92 fprintf(file, "I,%ld,%s\n", stv.tv_sec, VERSION);
95 fprintf(file, "S,%s,%x\n", (*sensors)->id, (*sensors)->type);
104 void slog_write_sensors(struct psensor **sensors)
112 log_err(_("Sensor log file not open."));
116 if (gettimeofday(&tv, NULL)) {
117 log_err(_("gettimeofday failed."));
121 count = psensor_list_size(sensors);
127 last_values = malloc(count * sizeof(double));
130 fprintf(file, "%ld", tv.tv_sec - stv.tv_sec);
131 for (i = 0; i < count; i++) {
132 v = psensor_get_current_value(sensors[i]);
134 if (!first_call && last_values[i] == v)
137 fprintf(file, ",%.1f", v);
155 log_err(_("Sensor log not open, cannot close."));