2 * Copyright (c) 1995, 1996, 1997 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the Institute nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 RCSID("$Id: logging.c,v 1.18.2.1 2000/10/13 15:57:34 assar Exp $");
39 struct krb_log_facility {
40 char filename[MaxPathLen];
46 krb_vlogger(struct krb_log_facility *f, const char *format, va_list args)
53 else if (f->filename && f->filename[0])
54 file = fopen(f->filename, "a");
59 ret = f->func(file, format, args);
67 krb_logger(struct krb_log_facility *f, const char *format, ...)
71 va_start(args, format);
72 ret = krb_vlogger(f, format, args);
78 * If FILE * is given log to it, otherwise, log to filename. When
79 * given a file name the file is opened and closed for each log
83 krb_openlog(struct krb_log_facility *f,
88 strlcpy(f->filename, filename, MaxPathLen);
94 /* ------------------------------------------------------------
95 Compatibility functions from warning.c
96 ------------------------------------------------------------ */
99 log_tty(FILE *f, const char *format, va_list args)
101 if (f != NULL && isatty(fileno(f)))
102 vfprintf(f, format, args);
107 static struct krb_log_facility std_log = { "/dev/tty", NULL, log_tty };
115 std_log.file = stderr;
124 krb_set_warnfn (krb_warnfn_t newfunc)
127 std_log.func = newfunc;
134 krb_get_warnfn (void)
141 * Log warnings to stderr if it's a tty.
144 krb_warning (const char *format, ...)
149 va_start(args, format);
150 krb_vlogger(&std_log, format, args);
154 /* ------------------------------------------------------------
155 Compatibility functions from klog.c and log.c
156 ------------------------------------------------------------ */
159 * Used by kerberos and kadmind daemons and in libkrb (rd_req.c).
161 * By default they log to the kerberos server log-file (KRBLOG) to be
162 * backwards compatible.
166 log_with_timestamp_and_nl(FILE *file, const char *format, va_list args)
172 fputs(krb_stime(&now), file);
174 vfprintf(file, format, args);
180 static struct krb_log_facility
181 file_log = { KRBLOG, NULL, log_with_timestamp_and_nl };
184 * kset_logfile() changes the name of the file to which
185 * messages are logged. If kset_logfile() is not called,
186 * the logfile defaults to KRBLOG, defined in "krb.h".
190 kset_logfile(char *filename)
192 krb_openlog(&file_log, filename, NULL, log_with_timestamp_and_nl);
196 * krb_log() and klog() is used to add entries to the logfile.
198 * The log entry consists of a timestamp and the given arguments
199 * printed according to the given "format" string.
201 * The log file is opened and closed for each log entry.
203 * If the given log type "type" is unknown, or if the log file
204 * cannot be opened, no entry is made to the log file.
206 * CHANGE: the type is always ignored
208 * The return value of klog() is always a pointer to the formatted log
209 * text string "logtxt".
212 /* Used in kerberos.c only. */
214 klog(int type, const char *format, ...)
216 static char logtxt[1024];
220 va_start(ap, format);
221 vsnprintf(logtxt, sizeof(logtxt), format, ap);
224 krb_logger(&file_log, "%s", logtxt);
229 /* Used in kadmind and rd_req.c */
231 krb_log(const char *format, ...)
235 va_start(args, format);
236 krb_vlogger(&file_log, format, args);