2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
13 * Shared versions of debug(), log(), etc.
15 * Copyright (c) 2000 Markus Friedl. All rights reserved.
17 * Redistribution and use in source and binary forms, with or without
18 * modification, are permitted provided that the following conditions
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 * 2. Redistributions in binary form must reproduce the above copyright
23 * notice, this list of conditions and the following disclaimer in the
24 * documentation and/or other materials provided with the distribution.
26 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
27 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
28 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
30 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
31 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
35 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 RCSID("$OpenBSD: log.c,v 1.10 2000/09/12 20:53:10 markus Exp $");
44 /* Fatal messages. This function never returns. */
47 fatal(const char *fmt,...)
51 do_log(SYSLOG_LEVEL_FATAL, fmt, args);
56 /* Error messages that should be logged. */
59 error(const char *fmt,...)
63 do_log(SYSLOG_LEVEL_ERROR, fmt, args);
67 /* Log this message (information that usually should go to the log). */
70 log(const char *fmt,...)
74 do_log(SYSLOG_LEVEL_INFO, fmt, args);
78 /* More detailed messages (information that does not need to go to the log). */
81 verbose(const char *fmt,...)
85 do_log(SYSLOG_LEVEL_VERBOSE, fmt, args);
89 /* Debugging messages that should not be logged during normal operation. */
92 debug(const char *fmt,...)
96 do_log(SYSLOG_LEVEL_DEBUG1, fmt, args);
101 debug2(const char *fmt,...)
105 do_log(SYSLOG_LEVEL_DEBUG2, fmt, args);
110 debug3(const char *fmt,...)
114 do_log(SYSLOG_LEVEL_DEBUG3, fmt, args);
120 struct fatal_cleanup {
121 struct fatal_cleanup *next;
122 void (*proc) (void *);
126 static struct fatal_cleanup *fatal_cleanups = NULL;
128 /* Registers a cleanup function to be called by fatal() before exiting. */
131 fatal_add_cleanup(void (*proc) (void *), void *context)
133 struct fatal_cleanup *cu;
135 cu = xmalloc(sizeof(*cu));
137 cu->context = context;
138 cu->next = fatal_cleanups;
142 /* Removes a cleanup frunction to be called at fatal(). */
145 fatal_remove_cleanup(void (*proc) (void *context), void *context)
147 struct fatal_cleanup **cup, *cu;
149 for (cup = &fatal_cleanups; *cup; cup = &cu->next) {
151 if (cu->proc == proc && cu->context == context) {
157 fatal("fatal_remove_cleanup: no such cleanup function: 0x%lx 0x%lx\n",
158 (unsigned long) proc, (unsigned long) context);
161 /* Cleanup and exit */
165 struct fatal_cleanup *cu, *next_cu;
166 static int called = 0;
171 /* Call cleanup functions. */
172 for (cu = fatal_cleanups; cu; cu = next_cu) {
174 debug("Calling cleanup 0x%lx(0x%lx)",
175 (unsigned long) cu->proc, (unsigned long) cu->context);
176 (*cu->proc) (cu->context);
181 /* textual representation of log-facilities/levels */
186 } log_facilities[] = {
187 { "DAEMON", SYSLOG_FACILITY_DAEMON },
188 { "USER", SYSLOG_FACILITY_USER },
189 { "AUTH", SYSLOG_FACILITY_AUTH },
190 { "LOCAL0", SYSLOG_FACILITY_LOCAL0 },
191 { "LOCAL1", SYSLOG_FACILITY_LOCAL1 },
192 { "LOCAL2", SYSLOG_FACILITY_LOCAL2 },
193 { "LOCAL3", SYSLOG_FACILITY_LOCAL3 },
194 { "LOCAL4", SYSLOG_FACILITY_LOCAL4 },
195 { "LOCAL5", SYSLOG_FACILITY_LOCAL5 },
196 { "LOCAL6", SYSLOG_FACILITY_LOCAL6 },
197 { "LOCAL7", SYSLOG_FACILITY_LOCAL7 },
206 { "QUIET", SYSLOG_LEVEL_QUIET },
207 { "FATAL", SYSLOG_LEVEL_FATAL },
208 { "ERROR", SYSLOG_LEVEL_ERROR },
209 { "INFO", SYSLOG_LEVEL_INFO },
210 { "VERBOSE", SYSLOG_LEVEL_VERBOSE },
211 { "DEBUG1", SYSLOG_LEVEL_DEBUG1 },
212 { "DEBUG2", SYSLOG_LEVEL_DEBUG2 },
213 { "DEBUG3", SYSLOG_LEVEL_DEBUG3 },
218 log_facility_number(char *name)
222 for (i = 0; log_facilities[i].name; i++)
223 if (strcasecmp(log_facilities[i].name, name) == 0)
224 return log_facilities[i].val;
225 return (SyslogFacility) - 1;
229 log_level_number(char *name)
233 for (i = 0; log_levels[i].name; i++)
234 if (strcasecmp(log_levels[i].name, name) == 0)
235 return log_levels[i].val;
236 return (LogLevel) - 1;