brewco/logger.c

Sat, 26 Dec 2015 21:45:44 +0100

author
Michiel Broek <mbroek@mbse.eu>
date
Sat, 26 Dec 2015 21:45:44 +0100
changeset 473
fdd30e935079
permissions
-rw-r--r--

The brew state machine is complete, works but is not bugfree

473
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
1 /*****************************************************************************
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
2 * Copyright (C) 2014-2015
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
3 *
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
4 * Michiel Broek <mbroek at mbse dot eu>
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
5 *
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
6 * This file is part of the mbsePi-apps
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
7 *
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
8 * This is free software; you can redistribute it and/or modify it
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
9 * under the terms of the GNU General Public License as published by the
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
10 * Free Software Foundation; either version 2, or (at your option) any
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
11 * later version.
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
12 *
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
13 * mbsePi-apps is distributed in the hope that it will be useful, but
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
16 * General Public License for more details.
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
17 *
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
18 * You should have received a copy of the GNU General Public License
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
19 * along with ThermFerm; see the file COPYING. If not, write to the Free
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
20 * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
21 *****************************************************************************/
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
22
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
23 #include "logger.h"
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
24 #include "brewco.h"
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
25 #include "util.h"
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
26 #include "xutil.h"
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
27
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
28 extern char *varpath;
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
29
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
30
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
31 void initlog(char *name)
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
32 {
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
33 char buf[128];
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
34
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
35 snprintf(buf, 127, "Fase,hltInput,hltOutput,hltSetpoint,mltInput,mltOutput,mltSetpoint");
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
36 logger(name, buf);
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
37 }
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
38
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
39
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
40
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
41 void logger(char *name, char *data)
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
42 {
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
43 struct timeval now;
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
44 struct tm ptm;
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
45 char *outstr = NULL, *logfile = NULL;
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
46 FILE *fp;
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
47
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
48 logfile = xstrcpy(varpath);
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
49 logfile = xstrcat(logfile, (char *)"log/brew-");
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
50 logfile = xstrcat(logfile, name);
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
51 mkdirs(logfile, 0755);
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
52 logfile = xstrcat(logfile, (char *)".log");
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
53
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
54 gettimeofday(&now, NULL);
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
55 localtime_r(&now.tv_sec, &ptm);
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
56 outstr = calloc(10240, sizeof(char));
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
57 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);
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
58
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
59 if ((fp = fopen(logfile, "a+"))) {
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
60 fprintf(fp, outstr);
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
61 fclose(fp);
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
62 } else {
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
63 syslog(LOG_NOTICE, "logger: cannot open %s for writing", logfile);
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
64 }
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
65
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
66 free(outstr);
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
67 outstr = NULL;
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
68 free(logfile);
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
69 logfile = NULL;
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
70 }
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
71
fdd30e935079 The brew state machine is complete, works but is not bugfree
Michiel Broek <mbroek@mbse.eu>
parents:
diff changeset
72

mercurial