brewco/logger.c

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

mercurial