thermferm/logger.c

changeset 51
a03b6dac5398
child 61
2810e55ac99d
equal deleted inserted replaced
50:8b5e8f1e172d 51:a03b6dac5398
1 /*****************************************************************************
2 * Copyright (C) 2014
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 EC-65K; 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 "thermferm.h"
24
25
26 void logger(char *filename, char *progname, char *data)
27 {
28 struct timeval now;
29 struct tm ptm;
30 char *outstr = NULL, *name = NULL;
31 FILE *logfile;
32
33 name = xstrcpy((char *)"/var/local/log/");
34 name = xstrcat(name, progname);
35 name = xstrcat(name, (char *)"/");
36 name = xstrcat(name, filename);
37
38 gettimeofday(&now, NULL);
39 localtime_r(&now.tv_sec, &ptm);
40
41 if ((logfile = fopen(name, "a+"))) {
42 outstr = calloc(10240, sizeof(char));
43 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);
44 fprintf(logfile, outstr);
45 fclose(logfile);
46 free(outstr);
47 outstr = NULL;
48 } else {
49 syslog(LOG_NOTICE, "logger: cannot open %s for writing", name);
50 }
51
52 free(name);
53 name = NULL;
54 }
55
56

mercurial