thermferm/logger.c

Sat, 19 Jan 2019 11:38:09 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 19 Jan 2019 11:38:09 +0100
changeset 571
6f8eda55ec2c
parent 554
ab9f22ab57b5
child 576
fbc6012405f1
permissions
-rw-r--r--

Version 0.9.1. Added fermentation profiles via MQTT. Added uuid field for loaded product. Removed some debug messages in the simulator. Removed dead code and debug messages from the lcd driver.

51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /*****************************************************************************
536
e833bbd5e733 Versie 0.6.5. LIST LOG <unit> commando verwijderd, alleen ARCHIVE LOG <unit> is nog in gebruik. De chiller temperatuur zit nu ook in de log en grafiek.
Michiel Broek <mbroek@mbse.eu>
parents: 397
diff changeset
2 * Copyright (C) 2014-2018
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
554
ab9f22ab57b5 Versie 0.8.5. Implementatie product code en product naam. Implementatie DLOG MQTT berichten. Verminderde server rotocol debug berichten.
Michiel Broek <mbroek@mbse.eu>
parents: 536
diff changeset
29 void initlog(char *code, char *name)
155
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
536
e833bbd5e733 Versie 0.6.5. LIST LOG <unit> commando verwijderd, alleen ARCHIVE LOG <unit> is nog in gebruik. De chiller temperatuur zit nu ook in de log en grafiek.
Michiel Broek <mbroek@mbse.eu>
parents: 397
diff changeset
33 snprintf(buf, 127, "Mode,Air,Beer,Target_L,S_Heater,S_Cooler,S_Fan,S_Door,U_Heater,U_Cooler,U_Fan,Room,Target_H,Chiller");
554
ab9f22ab57b5 Versie 0.8.5. Implementatie product code en product naam. Implementatie DLOG MQTT berichten. Verminderde server rotocol debug berichten.
Michiel Broek <mbroek@mbse.eu>
parents: 536
diff changeset
34 filename = xstrcpy(code);
ab9f22ab57b5 Versie 0.8.5. Implementatie product code en product naam. Implementatie DLOG MQTT berichten. Verminderde server rotocol debug berichten.
Michiel Broek <mbroek@mbse.eu>
parents: 536
diff changeset
35 filename = xstrcat(filename, (char *)" ");
ab9f22ab57b5 Versie 0.8.5. Implementatie product code en product naam. Implementatie DLOG MQTT berichten. Verminderde server rotocol debug berichten.
Michiel Broek <mbroek@mbse.eu>
parents: 536
diff changeset
36 filename = xstrcat(filename, name);
155
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
37 filename = xstrcat(filename, (char *)".log");
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
38 logger(filename, buf);
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
39 free(filename);
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
40 filename = NULL;
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
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
43
0d86f3c0a07b Unit mode can switch between OFF and NONE.
Michiel Broek <mbroek@mbse.eu>
parents: 106
diff changeset
44
104
5e538c4e1ecb Added units logging
Michiel Broek <mbroek@mbse.eu>
parents: 86
diff changeset
45 void logger(char *filename, char *data)
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 {
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47 struct timeval now;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48 struct tm ptm;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49 char *outstr = NULL, *name = NULL;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
50 FILE *logfile;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
51
86
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
52 if (getenv((char *)"USER") == NULL) {
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
53 name = xstrcpy((char *)"/root");
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
54 } else {
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
55 name = xstrcpy(getenv((char *)"HOME"));
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
56 }
104
5e538c4e1ecb Added units logging
Michiel Broek <mbroek@mbse.eu>
parents: 86
diff changeset
57 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
58 mkdirs(name, 0755);
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 name = xstrcat(name, filename);
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
61 gettimeofday(&now, NULL);
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
62 localtime_r(&now.tv_sec, &ptm);
61
2810e55ac99d Simple detection for the same log lines
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
63 outstr = calloc(10240, sizeof(char));
2810e55ac99d Simple detection for the same log lines
Michiel Broek <mbroek@mbse.eu>
parents: 51
diff changeset
64 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
65
86
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
66 if ((logfile = fopen(name, "a+"))) {
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
67 fprintf(logfile, outstr);
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
68 fclose(logfile);
3d7a241329e2 Using final user paths for log, configuration and profiles
Michiel Broek <mbroek@mbse.eu>
parents: 65
diff changeset
69 } else {
268
dda91dfa4aa8 All syslog logging only to LOG_NOTICE
Michiel Broek <mbroek@mbse.eu>
parents: 235
diff changeset
70 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
71 }
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
72
62
dc22cb54babd Fixed possible memory leaks
Michiel Broek <mbroek@mbse.eu>
parents: 61
diff changeset
73 free(outstr);
dc22cb54babd Fixed possible memory leaks
Michiel Broek <mbroek@mbse.eu>
parents: 61
diff changeset
74 outstr = NULL;
51
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
75 free(name);
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
76 name = NULL;
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
77 }
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
78
a03b6dac5398 Removed library, bumped to version 0.0.7
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
79

mercurial