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