1 /* $OpenBSD: log.h,v 1.34 2024/06/27 22:36:44 djm Exp $ */ 2 3 /* 4 * Author: Tatu Ylonen <ylo@cs.hut.fi> 5 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland 6 * All rights reserved 7 * 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". 13 */ 14 15 #ifndef SSH_LOG_H 16 #define SSH_LOG_H 17 18 #include <stdarg.h> /* va_list */ 19 #include "ssherr.h" /* ssh_err() */ 20 21 /* Supported syslog facilities and levels. */ 22 typedef enum { 23 SYSLOG_FACILITY_DAEMON, 24 SYSLOG_FACILITY_USER, 25 SYSLOG_FACILITY_AUTH, 26 SYSLOG_FACILITY_LOCAL0, 27 SYSLOG_FACILITY_LOCAL1, 28 SYSLOG_FACILITY_LOCAL2, 29 SYSLOG_FACILITY_LOCAL3, 30 SYSLOG_FACILITY_LOCAL4, 31 SYSLOG_FACILITY_LOCAL5, 32 SYSLOG_FACILITY_LOCAL6, 33 SYSLOG_FACILITY_LOCAL7, 34 SYSLOG_FACILITY_NOT_SET = -1 35 } SyslogFacility; 36 37 typedef enum { 38 SYSLOG_LEVEL_QUIET, 39 SYSLOG_LEVEL_FATAL, 40 SYSLOG_LEVEL_ERROR, 41 SYSLOG_LEVEL_INFO, 42 SYSLOG_LEVEL_VERBOSE, 43 SYSLOG_LEVEL_DEBUG1, 44 SYSLOG_LEVEL_DEBUG2, 45 SYSLOG_LEVEL_DEBUG3, 46 SYSLOG_LEVEL_NOT_SET = -1 47 } LogLevel; 48 49 typedef void (log_handler_fn)(LogLevel, int, const char *, void *); 50 51 void log_init(const char *, LogLevel, SyslogFacility, int); 52 LogLevel log_level_get(void); 53 int log_change_level(LogLevel); 54 int log_is_on_stderr(void); 55 void log_redirect_stderr_to(const char *); 56 void log_verbose_add(const char *); 57 void log_verbose_reset(void); 58 59 SyslogFacility log_facility_number(char *); 60 const char * log_facility_name(SyslogFacility); 61 LogLevel log_level_number(char *); 62 const char * log_level_name(LogLevel); 63 64 void set_log_handler(log_handler_fn *, void *); 65 void cleanup_exit(int) __attribute__((noreturn)); 66 67 void sshlog(const char *, const char *, int, int, 68 LogLevel, const char *, const char *, ...) 69 __attribute__((format(printf, 7, 8))); 70 void sshlogv(const char *, const char *, int, int, 71 LogLevel, const char *, const char *, va_list); 72 void sshlogdie(const char *, const char *, int, int, 73 LogLevel, const char *, const char *, ...) __attribute__((noreturn)) 74 __attribute__((format(printf, 7, 8))); 75 void sshfatal(const char *, const char *, int, int, 76 LogLevel, const char *, const char *, ...) __attribute__((noreturn)) 77 __attribute__((format(printf, 7, 8))); 78 void sshlogdirect(LogLevel, int, const char *, ...) 79 __attribute__((format(printf, 3, 4))); 80 81 #define do_log2(level, ...) sshlog(__FILE__, __func__, __LINE__, 0, level, NULL, __VA_ARGS__) 82 #define debug3(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG3, NULL, __VA_ARGS__) 83 #define debug2(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG2, NULL, __VA_ARGS__) 84 #define debug(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG1, NULL, __VA_ARGS__) 85 #define verbose(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_VERBOSE, NULL, __VA_ARGS__) 86 #define logit(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_INFO, NULL, __VA_ARGS__) 87 #define error(...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__) 88 #define fatal(...) sshfatal(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_FATAL, NULL, __VA_ARGS__) 89 #define logdie(...) sshlogdie(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__) 90 91 /* Variants that prepend the caller's function */ 92 #define do_log2_f(level, ...) sshlog(__FILE__, __func__, __LINE__, 1, level, NULL, __VA_ARGS__) 93 #define debug3_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG3, NULL, __VA_ARGS__) 94 #define debug2_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG2, NULL, __VA_ARGS__) 95 #define debug_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG1, NULL, __VA_ARGS__) 96 #define verbose_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_VERBOSE, NULL, __VA_ARGS__) 97 #define logit_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_INFO, NULL, __VA_ARGS__) 98 #define error_f(...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__) 99 #define fatal_f(...) sshfatal(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_FATAL, NULL, __VA_ARGS__) 100 #define logdie_f(...) sshlogdie(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, NULL, __VA_ARGS__) 101 102 /* Variants that appends a ssh_err message */ 103 #define do_log2_r(r, level, ...) sshlog(__FILE__, __func__, __LINE__, 0, level, ssh_err(r), __VA_ARGS__) 104 #define debug3_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG3, ssh_err(r), __VA_ARGS__) 105 #define debug2_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG2, ssh_err(r), __VA_ARGS__) 106 #define debug_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_DEBUG1, ssh_err(r), __VA_ARGS__) 107 #define verbose_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_VERBOSE, ssh_err(r), __VA_ARGS__) 108 #define logit_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_INFO, ssh_err(r), __VA_ARGS__) 109 #define error_r(r, ...) sshlog(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__) 110 #define fatal_r(r, ...) sshfatal(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_FATAL, ssh_err(r), __VA_ARGS__) 111 #define logdie_r(r, ...) sshlogdie(__FILE__, __func__, __LINE__, 0, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__) 112 #define do_log2_fr(r, level, ...) sshlog(__FILE__, __func__, __LINE__, 1, level, ssh_err(r), __VA_ARGS__) 113 #define debug3_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG3, ssh_err(r), __VA_ARGS__) 114 #define debug2_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG2, ssh_err(r), __VA_ARGS__) 115 #define debug_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_DEBUG1, ssh_err(r), __VA_ARGS__) 116 #define verbose_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_VERBOSE, ssh_err(r), __VA_ARGS__) 117 #define logit_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_INFO, ssh_err(r), __VA_ARGS__) 118 #define error_fr(r, ...) sshlog(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__) 119 #define fatal_fr(r, ...) sshfatal(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_FATAL, ssh_err(r), __VA_ARGS__) 120 #define logdie_fr(r, ...) sshlogdie(__FILE__, __func__, __LINE__, 1, SYSLOG_LEVEL_ERROR, ssh_err(r), __VA_ARGS__) 121 122 #endif 123