lib/logger.c

changeset 33
2357e8636a60
child 50
8b5e8f1e172d
equal deleted inserted replaced
32:3bac8fd4173d 33:2357e8636a60
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 "../config.h"
24 #include "mbselib.h"
25
26 static int lastmin = 70;
27
28 void logger(char *filename, char *progname, char *data)
29 {
30 struct timeval now;
31 struct tm ptm;
32 char *outstr = NULL, *name = NULL;
33 FILE *logfile;
34
35 name = xstrcpy((char *)"/var/local/log/");
36 name = xstrcat(name, progname);
37 name = xstrcat(name, (char *)"/");
38 name = xstrcat(name, filename);
39
40 gettimeofday(&now, NULL);
41 localtime_r(&now.tv_sec, &ptm);
42
43 /*
44 * Log no more then once per minute
45 */
46 if (lastmin != ptm.tm_min) {
47 if ((logfile = fopen(name, "a+"))) {
48 outstr = calloc(10240, sizeof(char));
49 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);
50
51 fprintf(logfile, outstr);
52 fclose(logfile);
53 free(outstr);
54 outstr = NULL;
55 } else {
56 syslog(LOG_NOTICE, "logger: cannot open %s for writing", name);
57 }
58 lastmin = ptm.tm_min;
59 }
60
61 free(name);
62 name = NULL;
63 }
64
65

mercurial