implemented sensor logging
[psensor.git] / src / lib / slog.c
1 /*
2  * Copyright (C) 2010-2012 jeanfi@gmail.com
3  *
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.
8  *
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.
13  *
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
17  * 02110-1301 USA
18  */
19 #include <locale.h>
20 #include <libintl.h>
21 #define _(str) gettext(str)
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/stat.h>
27 #include <sys/time.h>
28 #include <sys/types.h>
29
30
31 #include "bool.h"
32 #include "config.h"
33 #include "log.h"
34 #include "slog.h"
35
36 static FILE *file;
37 static struct timeval stv;
38 static double *last_values;
39
40 static const char *DEFAULT_FILENAME = "sensors.log";
41
42 static char *get_default_path()
43 {
44         char *home, *path, *dir;
45
46         home = getenv("HOME");
47
48         if (home) {
49                 dir = malloc(strlen(home)+1+strlen(".psensor")+1);
50                 sprintf(dir, "%s/%s", home, ".psensor");
51                 mkdir(dir, 0777);
52
53                 path = malloc(strlen(dir)+1+strlen(DEFAULT_FILENAME)+1);
54                 sprintf(path, "%s/%s", dir, DEFAULT_FILENAME);
55
56                 free(dir);
57
58                 return path;
59         } else {
60                 log_warn(_("HOME variable not set."));
61                 return strdup(DEFAULT_FILENAME);
62         }
63 }
64
65 int slog_init(const char *path, struct psensor **sensors)
66 {
67         char *lpath;
68
69         if (file) {
70                 log_err(_("Sensor log file already open."));
71                 return 0;
72         }
73
74         lpath = path ? (char *)path : get_default_path();
75
76         file = fopen(lpath, "a");
77
78         if (!file)
79                 log_err(_("Cannot open sensor log file: %s."), lpath);
80
81         if (!path)
82                 free((char *)lpath);
83
84         if (!file)
85                 return 0;
86
87         if (gettimeofday(&stv, NULL)) {
88                 log_err(_("gettimeofday failed."));
89                 return 0;
90         }
91
92         fprintf(file, "I,%ld,%s\n", stv.tv_sec, VERSION);
93
94         while (*sensors) {
95                 fprintf(file, "S,%s,%x\n", (*sensors)->id,  (*sensors)->type);
96                 sensors++;
97         }
98
99         fflush(file);
100
101         return 1;
102 }
103
104 void slog_write_sensors(struct psensor **sensors)
105 {
106         int count, i;
107         double v;
108         struct timeval tv;
109         bool first_call;
110
111         if (!file) {
112                 log_err(_("Sensor log file not open."));
113                 return ;
114         }
115
116         if (gettimeofday(&tv, NULL)) {
117                 log_err(_("gettimeofday failed."));
118                 return ;
119         }
120
121         count = psensor_list_size(sensors);
122
123         if (last_values) {
124                 first_call = 0;
125         } else {
126                 first_call = 1;
127                 last_values = malloc(count * sizeof(double));
128         }
129
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]);
133
134                 if (!first_call && last_values[i] == v)
135                         fputc(',', file);
136                 else
137                         fprintf(file, ",%.1f", v);
138
139                 last_values[i] = v;
140         }
141
142         fputc('\n', file);
143
144         fflush(file);
145 }
146
147 void slog_close()
148 {
149         if (file) {
150                 fclose(file);
151                 file = NULL;
152                 free(last_values);
153                 last_values = NULL;
154         } else {
155                 log_err(_("Sensor log not open, cannot close."));
156         }
157 }