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