2 * $Id: log.c,v 1.11 2008/01/24 17:08:46 psavola Exp $
5 * Lars Fenneberg <lf@elemental.net>
7 * This software is Copyright 1996,1997 by the above mentioned author(s),
10 * The license which is distributed with this software in the file
11 * COPYRIGHT applies to this software. If your distribution is missing
12 * this file, you may request it from <pekkas@netcore.fi>.
20 static int log_method
= L_NONE
;
21 static char *log_ident
;
22 static char *log_file
;
23 static FILE *log_file_fd
;
24 static int log_facility
;
25 static int debug_level
= 0;
28 log_open(int method
, char *ident
, char *log
, int facility
)
41 log_facility
= LOG_DAEMON
;
43 log_facility
= facility
;
45 openlog(log_ident
, LOG_PID
, log_facility
);
50 fprintf(stderr
, "%s: no logfile specified\n", log_ident
);
54 if ((log_file_fd
= fopen(log_file
, "a")) == NULL
)
56 fprintf(stderr
, "%s: can't open %s: %s\n", log_ident
, log_file
, strerror(errno
));
61 fprintf(stderr
, "%s: unknown logging method: %d\n", log_ident
, log_method
);
68 /* note: [dfv]log() is also called from root context */
70 vlog(int prio
, char *format
, va_list ap
)
72 char tstamp
[64], buff
[1024];
76 vsnprintf(buff
, sizeof(buff
), format
, ap
);
82 syslog(prio
, "%s", buff
);
85 syslog(prio
, "%s", buff
);
86 if (prio
> LOG_ERR
) /* fall through for messages with high priority */
90 tm
= localtime(¤t
);
91 (void) strftime(tstamp
, sizeof(tstamp
), LOG_TIME_FORMAT
, tm
);
93 fprintf(stderr
, "[%s] %s: %s\n", tstamp
, log_ident
, buff
);
98 tm
= localtime(¤t
);
99 (void) strftime(tstamp
, sizeof(tstamp
), LOG_TIME_FORMAT
, tm
);
101 fprintf(log_file_fd
, "[%s] %s: %s\n", tstamp
, log_ident
, buff
);
105 fprintf(stderr
, "%s: unknown logging method: %d\n", log_ident
, log_method
);
113 dlog(int prio
, int level
, char *format
, ...)
118 if (debug_level
< level
)
121 va_start(ap
, format
);
122 res
= vlog(prio
, format
, ap
);
125 /* XXX: should we do something if res < 0.. */
129 flog(int prio
, char *format
, ...)
134 va_start(ap
, format
);
135 res
= vlog(prio
, format
, ap
);
138 /* XXX: should we do something if res < 0.. */
144 switch (log_method
) {
148 case L_STDERR_SYSLOG
:
156 fprintf(stderr
, "%s: unknown logging method: %d\n", log_ident
, log_method
);
167 return log_open(log_method
, log_ident
, log_file
, log_facility
);
171 set_debuglevel(int level
)