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