thermferm/logger.c

branch
stable
changeset 601
44d41db09466
parent 590
1ff1a95a7614
parent 600
af2383e3f110
child 603
fcff55324b84
equal deleted inserted replaced
590:1ff1a95a7614 601:44d41db09466
1 /*****************************************************************************
2 * Copyright (C) 2014-2019
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 "thermferm.h"
25 #include "futil.h"
26 #include "xutil.h"
27
28
29 void initlog(char *code, char *name)
30 {
31 char buf[128], *filename;
32
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,Event");
34 filename = xstrcpy(code);
35 filename = xstrcat(filename, (char *)" ");
36 filename = xstrcat(filename, name);
37 filename = xstrcat(filename, (char *)".log");
38 logger(filename, buf);
39 free(filename);
40 filename = NULL;
41 }
42
43
44
45 void logger(char *filename, char *data)
46 {
47 struct timeval now;
48 struct tm ptm;
49 char *outstr = NULL, *name = NULL;
50 FILE *logfile;
51
52 if (getenv((char *)"USER") == NULL) {
53 name = xstrcpy((char *)"/root");
54 } else {
55 name = xstrcpy(getenv((char *)"HOME"));
56 }
57 name = xstrcat(name, (char *)"/.thermferm/log/");
58 mkdirs(name, 0755);
59 name = xstrcat(name, filename);
60
61 gettimeofday(&now, NULL);
62 localtime_r(&now.tv_sec, &ptm);
63 outstr = calloc(10240, sizeof(char));
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);
65
66 if ((logfile = fopen(name, "a+"))) {
67 fprintf(logfile, outstr);
68 fclose(logfile);
69 } else {
70 syslog(LOG_NOTICE, "logger: cannot open %s for writing", name);
71 }
72
73 free(outstr);
74 outstr = NULL;
75 free(name);
76 name = NULL;
77 }
78
79

mercurial