xref: /openssh-portable/log.h (revision faf2b86a)
1*faf2b86aSmarkus@openbsd.org /* $OpenBSD: log.h,v 1.33 2021/04/15 16:24:31 markus Exp $ */
236579d3dSBen Lindstrom 
3226cfa03SBen Lindstrom /*
4226cfa03SBen Lindstrom  * Author: Tatu Ylonen <ylo@cs.hut.fi>
5226cfa03SBen Lindstrom  * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6226cfa03SBen Lindstrom  *                    All rights reserved
7226cfa03SBen Lindstrom  *
8226cfa03SBen Lindstrom  * As far as I am concerned, the code I have written for this software
9226cfa03SBen Lindstrom  * can be used freely for any purpose.  Any derived versions of this
10226cfa03SBen Lindstrom  * software must be clearly marked as such, and if the derived work is
11226cfa03SBen Lindstrom  * incompatible with the protocol description in the RFC file, it must be
12226cfa03SBen Lindstrom  * called by a name other than "ssh" or "Secure Shell".
13226cfa03SBen Lindstrom  */
14226cfa03SBen Lindstrom 
15226cfa03SBen Lindstrom #ifndef SSH_LOG_H
16226cfa03SBen Lindstrom #define SSH_LOG_H
17226cfa03SBen Lindstrom 
18be02d7cbSdjm@openbsd.org #include <stdarg.h> /* va_list */
19961af266Sdjm@openbsd.org #include "ssherr.h" /* ssh_err() */
20be02d7cbSdjm@openbsd.org 
21226cfa03SBen Lindstrom /* Supported syslog facilities and levels. */
22226cfa03SBen Lindstrom typedef enum {
23226cfa03SBen Lindstrom 	SYSLOG_FACILITY_DAEMON,
24226cfa03SBen Lindstrom 	SYSLOG_FACILITY_USER,
25226cfa03SBen Lindstrom 	SYSLOG_FACILITY_AUTH,
26226cfa03SBen Lindstrom #ifdef LOG_AUTHPRIV
27226cfa03SBen Lindstrom 	SYSLOG_FACILITY_AUTHPRIV,
28226cfa03SBen Lindstrom #endif
29226cfa03SBen Lindstrom 	SYSLOG_FACILITY_LOCAL0,
30226cfa03SBen Lindstrom 	SYSLOG_FACILITY_LOCAL1,
31226cfa03SBen Lindstrom 	SYSLOG_FACILITY_LOCAL2,
32226cfa03SBen Lindstrom 	SYSLOG_FACILITY_LOCAL3,
33226cfa03SBen Lindstrom 	SYSLOG_FACILITY_LOCAL4,
34226cfa03SBen Lindstrom 	SYSLOG_FACILITY_LOCAL5,
35226cfa03SBen Lindstrom 	SYSLOG_FACILITY_LOCAL6,
36fcd93204SDamien Miller 	SYSLOG_FACILITY_LOCAL7,
3738ed63d7SBen Lindstrom 	SYSLOG_FACILITY_NOT_SET = -1
38226cfa03SBen Lindstrom }       SyslogFacility;
39226cfa03SBen Lindstrom 
40226cfa03SBen Lindstrom typedef enum {
41226cfa03SBen Lindstrom 	SYSLOG_LEVEL_QUIET,
42226cfa03SBen Lindstrom 	SYSLOG_LEVEL_FATAL,
43226cfa03SBen Lindstrom 	SYSLOG_LEVEL_ERROR,
44226cfa03SBen Lindstrom 	SYSLOG_LEVEL_INFO,
45226cfa03SBen Lindstrom 	SYSLOG_LEVEL_VERBOSE,
46226cfa03SBen Lindstrom 	SYSLOG_LEVEL_DEBUG1,
47226cfa03SBen Lindstrom 	SYSLOG_LEVEL_DEBUG2,
48fcd93204SDamien Miller 	SYSLOG_LEVEL_DEBUG3,
4938ed63d7SBen Lindstrom 	SYSLOG_LEVEL_NOT_SET = -1
50226cfa03SBen Lindstrom }       LogLevel;
514cc240daSBen Lindstrom 
52*faf2b86aSmarkus@openbsd.org typedef void (log_handler_fn)(LogLevel, int, const char *, void *);
538f0bf237SDamien Miller 
54ee22db7cSdjm@openbsd.org void     log_init(const char *, LogLevel, SyslogFacility, int);
555d14019bSmarkus@openbsd.org LogLevel log_level_get(void);
5654cd41a4Sdjm@openbsd.org int      log_change_level(LogLevel);
5750a48d02SDarren Tucker int      log_is_on_stderr(void);
5803d4d7e6SDamien Miller void     log_redirect_stderr_to(const char *);
59752250caSdjm@openbsd.org void	 log_verbose_add(const char *);
60752250caSdjm@openbsd.org void	 log_verbose_reset(void);
61226cfa03SBen Lindstrom 
6216ae3d0dSBen Lindstrom SyslogFacility	log_facility_number(char *);
63e7140f20SDarren Tucker const char *	log_facility_name(SyslogFacility);
6416ae3d0dSBen Lindstrom LogLevel	log_level_number(char *);
65e7140f20SDarren Tucker const char *	log_level_name(LogLevel);
66226cfa03SBen Lindstrom 
678f0bf237SDamien Miller void	 set_log_handler(log_handler_fn *, void *);
68f8b7eb7cSDarren Tucker void	 cleanup_exit(int) __attribute__((noreturn));
69752250caSdjm@openbsd.org 
70752250caSdjm@openbsd.org void	 sshlog(const char *, const char *, int, int,
719e2c4f64Sdjm@openbsd.org     LogLevel, const char *, const char *, ...)
729e2c4f64Sdjm@openbsd.org     __attribute__((format(printf, 7, 8)));
73752250caSdjm@openbsd.org void	 sshlogv(const char *, const char *, int, int,
749e2c4f64Sdjm@openbsd.org     LogLevel, const char *, const char *, va_list);
753554b4afSdjm@openbsd.org void	 sshsigdie(const char *, const char *, int, int,
769e2c4f64Sdjm@openbsd.org     LogLevel, const char *, const char *, ...) __attribute__((noreturn))
779e2c4f64Sdjm@openbsd.org     __attribute__((format(printf, 7, 8)));
783554b4afSdjm@openbsd.org void	 sshlogdie(const char *, const char *, int, int,
799e2c4f64Sdjm@openbsd.org     LogLevel, const char *, const char *, ...) __attribute__((noreturn))
809e2c4f64Sdjm@openbsd.org     __attribute__((format(printf, 7, 8)));
813554b4afSdjm@openbsd.org void	 sshfatal(const char *, const char *, int, int,
829e2c4f64Sdjm@openbsd.org     LogLevel, const char *, const char *, ...) __attribute__((noreturn))
839e2c4f64Sdjm@openbsd.org     __attribute__((format(printf, 7, 8)));
84*faf2b86aSmarkus@openbsd.org void	 sshlogdirect(LogLevel, int, const char *, ...)
85*faf2b86aSmarkus@openbsd.org     __attribute__((format(printf, 3, 4)));
86752250caSdjm@openbsd.org 
879e2c4f64Sdjm@openbsd.org #define do_log2(level, ...)	sshlog(__FILE__, __func__, __LINE__, 0, level, NULL, __VA_ARGS__)
889e2c4f64Sdjm@openbsd.org #define debug3(...)		sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG3, NULL, __VA_ARGS__)
899e2c4f64Sdjm@openbsd.org #define debug2(...)		sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG2, NULL, __VA_ARGS__)
909e2c4f64Sdjm@openbsd.org #define debug(...)		sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG1, NULL, __VA_ARGS__)
919e2c4f64Sdjm@openbsd.org #define verbose(...)		sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_VERBOSE, NULL, __VA_ARGS__)
929e2c4f64Sdjm@openbsd.org #define logit(...)		sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_INFO, NULL, __VA_ARGS__)
939e2c4f64Sdjm@openbsd.org #define error(...)		sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
949e2c4f64Sdjm@openbsd.org #define fatal(...)		sshfatal(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_FATAL, NULL, __VA_ARGS__)
959e2c4f64Sdjm@openbsd.org #define logdie(...)		sshlogdie(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
969e2c4f64Sdjm@openbsd.org #define sigdie(...)		sshsigdie(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
979e2c4f64Sdjm@openbsd.org 
989e2c4f64Sdjm@openbsd.org /* Variants that prepend the caller's function */
999e2c4f64Sdjm@openbsd.org #define do_log2_f(level, ...)	sshlog(__FILE__, __func__, __LINE__, 1, level, NULL, __VA_ARGS__)
1009e2c4f64Sdjm@openbsd.org #define debug3_f(...)		sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG3, NULL, __VA_ARGS__)
1019e2c4f64Sdjm@openbsd.org #define debug2_f(...)		sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG2, NULL, __VA_ARGS__)
1029e2c4f64Sdjm@openbsd.org #define debug_f(...)		sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG1, NULL, __VA_ARGS__)
1039e2c4f64Sdjm@openbsd.org #define verbose_f(...)		sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_VERBOSE, NULL, __VA_ARGS__)
1049e2c4f64Sdjm@openbsd.org #define logit_f(...)		sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_INFO, NULL, __VA_ARGS__)
1059e2c4f64Sdjm@openbsd.org #define error_f(...)		sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
1069e2c4f64Sdjm@openbsd.org #define fatal_f(...)		sshfatal(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_FATAL, NULL, __VA_ARGS__)
1079e2c4f64Sdjm@openbsd.org #define logdie_f(...)		sshlogdie(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
1089e2c4f64Sdjm@openbsd.org #define sigdie_f(...)		sshsigdie(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__)
1099e2c4f64Sdjm@openbsd.org 
1109e2c4f64Sdjm@openbsd.org /* Variants that appends a ssh_err message */
1119e2c4f64Sdjm@openbsd.org #define do_log2_r(r, level, ...) sshlog(__FILE__, __func__, __LINE__, 0, level, ssh_err(r), __VA_ARGS__)
1129e2c4f64Sdjm@openbsd.org #define debug3_r(r, ...)	sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG3, ssh_err(r), __VA_ARGS__)
1139e2c4f64Sdjm@openbsd.org #define debug2_r(r, ...)	sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG2, ssh_err(r), __VA_ARGS__)
1149e2c4f64Sdjm@openbsd.org #define debug_r(r, ...)		sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG1, ssh_err(r), __VA_ARGS__)
1159e2c4f64Sdjm@openbsd.org #define verbose_r(r, ...)	sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_VERBOSE, ssh_err(r), __VA_ARGS__)
1169e2c4f64Sdjm@openbsd.org #define logit_r(r, ...)		sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_INFO, ssh_err(r), __VA_ARGS__)
1179e2c4f64Sdjm@openbsd.org #define error_r(r, ...)		sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
1189e2c4f64Sdjm@openbsd.org #define fatal_r(r, ...)		sshfatal(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_FATAL, ssh_err(r), __VA_ARGS__)
1199e2c4f64Sdjm@openbsd.org #define logdie_r(r, ...)	sshlogdie(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
1209e2c4f64Sdjm@openbsd.org #define sigdie_r(r, ...)	sshsigdie(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
1219e2c4f64Sdjm@openbsd.org #define do_log2_fr(r, level, ...) sshlog(__FILE__, __func__, __LINE__, 1, level, ssh_err(r), __VA_ARGS__)
1229e2c4f64Sdjm@openbsd.org #define debug3_fr(r, ...)	sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG3, ssh_err(r), __VA_ARGS__)
1239e2c4f64Sdjm@openbsd.org #define debug2_fr(r, ...)	sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG2, ssh_err(r), __VA_ARGS__)
1249e2c4f64Sdjm@openbsd.org #define debug_fr(r, ...)	sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG1, ssh_err(r), __VA_ARGS__)
1259e2c4f64Sdjm@openbsd.org #define verbose_fr(r, ...)	sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_VERBOSE, ssh_err(r), __VA_ARGS__)
1269e2c4f64Sdjm@openbsd.org #define logit_fr(r, ...)	sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_INFO, ssh_err(r), __VA_ARGS__)
1279e2c4f64Sdjm@openbsd.org #define error_fr(r, ...)	sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
1289e2c4f64Sdjm@openbsd.org #define fatal_fr(r, ...)	sshfatal(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_FATAL, ssh_err(r), __VA_ARGS__)
1299e2c4f64Sdjm@openbsd.org #define logdie_fr(r, ...)	sshlogdie(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
1309e2c4f64Sdjm@openbsd.org #define sigdie_fr(r, ...)	sshsigdie(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__)
131752250caSdjm@openbsd.org 
132226cfa03SBen Lindstrom #endif
133