Imported Upstream version 1.0.1
[psensor-pkg-debian.git] / src / lib / slog.c
1 /*
2  * Copyright (C) 2010-2014 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 #define _LARGEFILE_SOURCE 1
20 #include "config.h"
21
22 #include <locale.h>
23 #include <libintl.h>
24 #define _(str) gettext(str)
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <sys/stat.h>
30 #include <sys/time.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33
34 #include "bool.h"
35 #include "config.h"
36 #include <plog.h>
37 #include "ptime.h"
38 #include "slog.h"
39
40 static FILE *file;
41 static double *last_values;
42 static int period;
43 static struct psensor **sensors;
44 static pthread_mutex_t *sensors_mutex;
45 static pthread_t thread;
46 static time_t st;
47
48 static const char *DEFAULT_FILENAME = "sensors.log";
49
50 static char *time_to_str(time_t *t)
51 {
52         struct tm lt;
53         char *str;
54
55         if (!localtime_r(t, &lt))
56                 return NULL;
57
58         str = malloc(64);
59
60         if (strftime(str, 64, "%s", &lt)) {
61                 return str;
62         } else {
63                 free(str);
64                 return NULL;
65         }
66 }
67
68 static char *get_default_path()
69 {
70         char *home, *path, *dir;
71
72         home = getenv("HOME");
73
74         if (home) {
75                 dir = malloc(strlen(home)+1+strlen(".psensor")+1);
76                 sprintf(dir, "%s/%s", home, ".psensor");
77                 mkdir(dir, 0777);
78
79                 path = malloc(strlen(dir)+1+strlen(DEFAULT_FILENAME)+1);
80                 sprintf(path, "%s/%s", dir, DEFAULT_FILENAME);
81
82                 free(dir);
83
84                 return path;
85         } else {
86                 log_warn(_("HOME variable not set."));
87                 return strdup(DEFAULT_FILENAME);
88         }
89 }
90
91 static bool slog_open(const char *path, struct psensor **sensors)
92 {
93         char *lpath, *t;
94
95         if (file) {
96                 log_err(_("Sensor log file already open."));
97                 return 0;
98         }
99
100         lpath = path ? (char *)path : get_default_path();
101
102         file = fopen(lpath, "a");
103
104         if (!file)
105                 log_err(_("Cannot open sensor log file: %s."), lpath);
106
107         if (!path)
108                 free((char *)lpath);
109
110         if (!file)
111                 return 0;
112
113         st = time(NULL);
114         t = time_to_str(&st);
115
116         fprintf(file, "I,%s,%s\n", t, VERSION);
117
118         while (*sensors) {
119                 fprintf(file, "S,%s,%x\n", (*sensors)->id,  (*sensors)->type);
120                 sensors++;
121         }
122
123         fflush(file);
124
125         return 1;
126 }
127
128 static void slog_write_sensors(struct psensor **sensors)
129 {
130         int count, i;
131         double v;
132         struct timeval tv;
133         bool first_call;
134
135         if (!file) {
136                 log_err(_("Sensor log file not open."));
137                 return ;
138         }
139
140         gettimeofday(&tv, NULL);
141
142         count = psensor_list_size(sensors);
143
144         if (last_values) {
145                 first_call = 0;
146         } else {
147                 first_call = 1;
148                 last_values = malloc(count * sizeof(double));
149         }
150
151         fprintf(file, "%ld", (long int)(tv.tv_sec - st));
152         for (i = 0; i < count; i++) {
153                 v = psensor_get_current_value(sensors[i]);
154
155                 if (!first_call && last_values[i] == v)
156                         fputc(',', file);
157                 else
158                         fprintf(file, ",%.1f", v);
159
160                 last_values[i] = v;
161         }
162
163         fputc('\n', file);
164
165         fflush(file);
166 }
167
168 static void *slog_routine(void *data)
169 {
170         while (1) {
171                 pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
172                 pthread_mutex_lock(sensors_mutex);
173                 slog_write_sensors(sensors);
174                 pthread_mutex_unlock(sensors_mutex);
175                 pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
176                 sleep(period);
177         }
178
179         pthread_exit(0);
180 }
181
182 void slog_close()
183 {
184         if (file) {
185                 pthread_cancel(thread);
186
187                 fclose(file);
188                 file = NULL;
189                 free(last_values);
190                 last_values = NULL;
191         } else {
192                 log_debug(_("Sensor log not open, cannot close."));
193         }
194 }
195
196 bool slog_activate(const char *path,
197                    struct psensor **ss,
198                    pthread_mutex_t *mutex,
199                    int p)
200 {
201         bool ret;
202
203         sensors = ss;
204         sensors_mutex = mutex;
205         period = p;
206
207         pthread_mutex_lock(mutex);
208         ret = slog_open(path, sensors);
209         pthread_mutex_unlock(mutex);
210
211         if (ret)
212                 pthread_create(&thread, NULL, slog_routine, NULL);
213
214         return ret;
215 }