thermferm/logger.c

Wed, 01 Apr 2015 23:13:08 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Wed, 01 Apr 2015 23:13:08 +0200
changeset 355
8f946f9d125a
parent 298
b88c60e8369e
child 397
00ca08f5a6f8
permissions
-rw-r--r--

Added logging of the room temperature.

51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /*****************************************************************************
355
8f946f9d125a Added logging of the room temperature.
Michiel Broek <mbroek@mbse.eu>
parents: 298
diff changeset
2 * Copyright (C) 2014-2015
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 *
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 * Michiel Broek <mbroek at mbse dot eu>
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 *
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 * This file is part of the mbsePi-apps
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 *
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 * This is free software; you can redistribute it and/or modify it
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9 * under the terms of the GNU General Public License as published by the
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 * Free Software Foundation; either version 2, or (at your option) any
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 * later version.
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 *
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 * mbsePi-apps is distributed in the hope that it will be useful, but
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 * General Public License for more details.
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 *
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
213
2317b8d644fa Code cleanup, streamlined error messages.
Michiel Broek <mbroek@mbse.eu>
parents: 155
diff changeset
19 * along with ThermFerm; see the file COPYING. If not, write to the Free
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 *****************************************************************************/
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22
106
1bd9a16f5061 Added .h files
Michiel Broek <mbroek@mbse.eu>
parents: 104
diff changeset
23 #include "logger.h"
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 #include "thermferm.h"
106
1bd9a16f5061 Added .h files
Michiel Broek <mbroek@mbse.eu>
parents: 104
diff changeset
25 #include "futil.h"
1bd9a16f5061 Added .h files
Michiel Broek <mbroek@mbse.eu>
parents: 104
diff changeset
26 #include "xutil.h"
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28
155
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
29 void initlog(char *unit)
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
30 {
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
31 char buf[128], *filename;
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
32
355
8f946f9d125a Added logging of the room temperature.
Michiel Broek <mbroek@mbse.eu>
parents: 298
diff changeset
33 snprintf(buf, 127, "Mode,Air,Beer,Target,S_Heater,S_Cooler,S_Fan,S_Door,U_Heater,U_Cooler,U_Fan,Room");
155
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
34 filename = xstrcpy(unit);
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
35 filename = xstrcat(filename, (char *)".log");
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
36 logger(filename, buf);
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
37 free(filename);
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
38 filename = NULL;
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
39 }
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
40
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
41
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
42
104
5e538c4e1ecb Added units logging
Michiel Broek <mbroek@mbse.eu>
parents: 86
diff changeset
43 void logger(char *filename, char *data)
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44 {
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 struct timeval now;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 struct tm ptm;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47 char *outstr = NULL, *name = NULL;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48 FILE *logfile;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49
86
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
50 if (getenv((char *)"USER") == NULL) {
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
51 name = xstrcpy((char *)"/root");
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
52 } else {
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
53 name = xstrcpy(getenv((char *)"HOME"));
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
54 }
104
5e538c4e1ecb Added units logging
Michiel Broek <mbroek@mbse.eu>
parents: 86
diff changeset
55 name = xstrcat(name, (char *)"/.thermferm/log/");
86
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
56 mkdirs(name, 0755);
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57 name = xstrcat(name, filename);
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
58
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 gettimeofday(&now, NULL);
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60 localtime_r(&now.tv_sec, &ptm);
61
2810e55ac99d Simple detection for the same log lines
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
61 outstr = calloc(10240, sizeof(char));
2810e55ac99d Simple detection for the same log lines
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
62 snprintf(outstr, 10239, "%04d-%02d-%02d %02d:%02d,%s\n", ptm.tm_year + 1900, ptm.tm_mon + 1, ptm.tm_mday, ptm.tm_hour, ptm.tm_min, data);
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
63
86
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
64 if ((logfile = fopen(name, "a+"))) {
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
65 fprintf(logfile, outstr);
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
66 fclose(logfile);
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
67 } else {
268
dda91dfa4aa8 All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents: 235
diff changeset
68 syslog(LOG_NOTICE, "logger: cannot open %s for writing", name);
86
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
69 }
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
70
62
dc22cb54babd Fixed possible memory leaks
Michiel Broek <mbroek@mbse.eu>
parents: 61
diff changeset
71 free(outstr);
dc22cb54babd Fixed possible memory leaks
Michiel Broek <mbroek@mbse.eu>
parents: 61
diff changeset
72 outstr = NULL;
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
73 free(name);
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
74 name = NULL;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
75 }
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
76
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
77

mercurial