2 /* $OpenBSD: log.c,v 1.41 2008/06/10 04:50:25 dtucker Exp $ */
4 * Author: Tatu Ylonen <ylo@cs.hut.fi>
5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8 * As far as I am concerned, the code I have written for this software
9 * can be used freely for any purpose. Any derived versions of this
10 * software must be clearly marked as such, and if the derived work is
11 * incompatible with the protocol description in the RFC file, it must be
12 * called by a name other than "ssh" or "Secure Shell".
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("$NetBSD: log.c,v 1.11 2009/02/16 20:53:54 christos Exp $");
40 #include <sys/types.h>
54 static LogLevel log_level
= SYSLOG_LEVEL_INFO
;
55 static int log_on_stderr
= 1;
56 static int log_facility
= LOG_AUTH
;
59 extern char *__progname
;
61 /* textual representation of log-facilities/levels */
66 } log_facilities
[] = {
67 { "DAEMON", SYSLOG_FACILITY_DAEMON
},
68 { "USER", SYSLOG_FACILITY_USER
},
69 { "AUTH", SYSLOG_FACILITY_AUTH
},
70 { "LOCAL0", SYSLOG_FACILITY_LOCAL0
},
71 { "LOCAL1", SYSLOG_FACILITY_LOCAL1
},
72 { "LOCAL2", SYSLOG_FACILITY_LOCAL2
},
73 { "LOCAL3", SYSLOG_FACILITY_LOCAL3
},
74 { "LOCAL4", SYSLOG_FACILITY_LOCAL4
},
75 { "LOCAL5", SYSLOG_FACILITY_LOCAL5
},
76 { "LOCAL6", SYSLOG_FACILITY_LOCAL6
},
77 { "LOCAL7", SYSLOG_FACILITY_LOCAL7
},
78 { NULL
, SYSLOG_FACILITY_NOT_SET
}
86 { "QUIET", SYSLOG_LEVEL_QUIET
},
87 { "FATAL", SYSLOG_LEVEL_FATAL
},
88 { "ERROR", SYSLOG_LEVEL_ERROR
},
89 { "INFO", SYSLOG_LEVEL_INFO
},
90 { "VERBOSE", SYSLOG_LEVEL_VERBOSE
},
91 { "DEBUG", SYSLOG_LEVEL_DEBUG1
},
92 { "DEBUG1", SYSLOG_LEVEL_DEBUG1
},
93 { "DEBUG2", SYSLOG_LEVEL_DEBUG2
},
94 { "DEBUG3", SYSLOG_LEVEL_DEBUG3
},
95 { NULL
, SYSLOG_LEVEL_NOT_SET
}
99 log_facility_number(char *name
)
104 for (i
= 0; log_facilities
[i
].name
; i
++)
105 if (strcasecmp(log_facilities
[i
].name
, name
) == 0)
106 return log_facilities
[i
].val
;
107 return SYSLOG_FACILITY_NOT_SET
;
111 log_facility_name(SyslogFacility facility
)
115 for (i
= 0; log_facilities
[i
].name
; i
++)
116 if (log_facilities
[i
].val
== facility
)
117 return log_facilities
[i
].name
;
122 log_level_number(char *name
)
127 for (i
= 0; log_levels
[i
].name
; i
++)
128 if (strcasecmp(log_levels
[i
].name
, name
) == 0)
129 return log_levels
[i
].val
;
130 return SYSLOG_LEVEL_NOT_SET
;
134 log_level_name(LogLevel level
)
138 for (i
= 0; log_levels
[i
].name
!= NULL
; i
++)
139 if (log_levels
[i
].val
== level
)
140 return log_levels
[i
].name
;
144 /* Error messages that should be logged. */
147 error(const char *fmt
,...)
152 do_log(SYSLOG_LEVEL_ERROR
, fmt
, args
);
157 sigdie(const char *fmt
,...)
162 do_log(SYSLOG_LEVEL_FATAL
, fmt
, args
);
168 /* Log this message (information that usually should go to the log). */
171 logit(const char *fmt
,...)
176 do_log(SYSLOG_LEVEL_INFO
, fmt
, args
);
180 /* More detailed messages (information that does not need to go to the log). */
183 verbose(const char *fmt
,...)
188 do_log(SYSLOG_LEVEL_VERBOSE
, fmt
, args
);
192 /* Debugging messages that should not be logged during normal operation. */
195 debug(const char *fmt
,...)
200 do_log(SYSLOG_LEVEL_DEBUG1
, fmt
, args
);
205 debug2(const char *fmt
,...)
210 do_log(SYSLOG_LEVEL_DEBUG2
, fmt
, args
);
215 debug3(const char *fmt
,...)
220 do_log(SYSLOG_LEVEL_DEBUG3
, fmt
, args
);
225 * Initialize the log.
229 log_init(char *av0
, LogLevel level
, SyslogFacility facility
, int on_stderr
)
234 case SYSLOG_LEVEL_QUIET
:
235 case SYSLOG_LEVEL_FATAL
:
236 case SYSLOG_LEVEL_ERROR
:
237 case SYSLOG_LEVEL_INFO
:
238 case SYSLOG_LEVEL_VERBOSE
:
239 case SYSLOG_LEVEL_DEBUG1
:
240 case SYSLOG_LEVEL_DEBUG2
:
241 case SYSLOG_LEVEL_DEBUG3
:
245 fprintf(stderr
, "Unrecognized internal syslog level code %d\n",
250 log_on_stderr
= on_stderr
;
255 case SYSLOG_FACILITY_DAEMON
:
256 log_facility
= LOG_DAEMON
;
258 case SYSLOG_FACILITY_USER
:
259 log_facility
= LOG_USER
;
261 case SYSLOG_FACILITY_AUTH
:
262 log_facility
= LOG_AUTH
;
264 case SYSLOG_FACILITY_LOCAL0
:
265 log_facility
= LOG_LOCAL0
;
267 case SYSLOG_FACILITY_LOCAL1
:
268 log_facility
= LOG_LOCAL1
;
270 case SYSLOG_FACILITY_LOCAL2
:
271 log_facility
= LOG_LOCAL2
;
273 case SYSLOG_FACILITY_LOCAL3
:
274 log_facility
= LOG_LOCAL3
;
276 case SYSLOG_FACILITY_LOCAL4
:
277 log_facility
= LOG_LOCAL4
;
279 case SYSLOG_FACILITY_LOCAL5
:
280 log_facility
= LOG_LOCAL5
;
282 case SYSLOG_FACILITY_LOCAL6
:
283 log_facility
= LOG_LOCAL6
;
285 case SYSLOG_FACILITY_LOCAL7
:
286 log_facility
= LOG_LOCAL7
;
290 "Unrecognized internal syslog facility code %d\n",
296 #define MSGBUFSIZ 1024
299 do_log(LogLevel level
, const char *fmt
, va_list args
)
301 #ifdef SYSLOG_DATA_INIT
302 struct syslog_data sdata
= SYSLOG_DATA_INIT
;
304 char msgbuf
[MSGBUFSIZ
];
305 char fmtbuf
[4 * sizeof(msgbuf
) + 1];
308 int saved_errno
= errno
;
310 if (level
> log_level
)
314 case SYSLOG_LEVEL_FATAL
:
319 case SYSLOG_LEVEL_ERROR
:
324 case SYSLOG_LEVEL_INFO
:
327 case SYSLOG_LEVEL_VERBOSE
:
330 case SYSLOG_LEVEL_DEBUG1
:
334 case SYSLOG_LEVEL_DEBUG2
:
338 case SYSLOG_LEVEL_DEBUG3
:
343 txt
= "internal error";
348 snprintf(fmtbuf
, sizeof(fmtbuf
), "%s: %s", txt
, fmt
);
349 vsnprintf(msgbuf
, sizeof(msgbuf
), fmtbuf
, args
);
351 vsnprintf(msgbuf
, sizeof(msgbuf
), fmt
, args
);
353 strvis(fmtbuf
, msgbuf
, VIS_SAFE
|VIS_OCTAL
);
355 snprintf(msgbuf
, sizeof msgbuf
, "%s\r\n", fmtbuf
);
356 write(STDERR_FILENO
, msgbuf
, strlen(msgbuf
));
358 #ifdef SYSLOG_DATA_INIT
359 openlog_r(argv0
? argv0
: __progname
, LOG_PID
, log_facility
, &sdata
);
360 syslog_r(pri
, &sdata
, "%.500s", fmtbuf
);
363 openlog(argv0
? argv0
: __progname
, LOG_PID
, log_facility
);
364 syslog(pri
, "%.500s", fmtbuf
);