From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by passt.top (Postfix, from userid 1000) id 09D765A0054; Mon, 17 Jun 2024 14:03:20 +0200 (CEST) From: Stefano Brivio To: passt-dev@passt.top Subject: [PATCH 4/6] log: Add _perror() logging function variants Date: Mon, 17 Jun 2024 14:03:17 +0200 Message-ID: <20240617120319.1206857-5-sbrivio@redhat.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240617120319.1206857-1-sbrivio@redhat.com> References: <20240617120319.1206857-1-sbrivio@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: 7GWS5JEMG4DMTSWX3SYD2SJYNZ655GNU X-Message-ID-Hash: 7GWS5JEMG4DMTSWX3SYD2SJYNZ655GNU X-MailFrom: sbrivio@passt.top X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Yalan Zhang X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: In many places, we have direct perror() calls, which completely bypass logging functions and log files. They are definitely convenient: offer similar convenience with _perror() logging variants, so that we can drop those direct perror() calls. Signed-off-by: Stefano Brivio --- log.c | 21 +++++++++++++++++++++ log.h | 21 +++++++++++++++++---- 2 files changed, 38 insertions(+), 4 deletions(-) diff --git a/log.c b/log.c index 939bb93..6764b2b 100644 --- a/log.c +++ b/log.c @@ -80,6 +80,11 @@ void vlogmsg(int pri, const char *format, va_list ap) } } +/** + * logmsg() - vlogmsg() wrapper for variable argument lists + * @pri: Facility and level map, same as priority for vsyslog() + * @format: Message + */ void logmsg(int pri, const char *format, ...) { va_list ap; @@ -89,6 +94,22 @@ void logmsg(int pri, const char *format, ...) va_end(ap); } +/** + * logmsg_perror() - vlogmsg() wrapper with perror()-like functionality + * @pri: Facility and level map, same as priority for vsyslog() + * @format: Message + */ +void logmsg_perror(int pri, const char *format, ...) +{ + va_list ap; + + logmsg(pri, "%s: ", strerror(errno)); + + va_start(ap, format); + vlogmsg(pri, format, ap); + va_end(ap); +} + /* Prefixes for log file messages, indexed by priority */ const char *logfile_prefix[] = { NULL, NULL, NULL, /* Unused: LOG_EMERG, LOG_ALERT, LOG_CRIT */ diff --git a/log.h b/log.h index 680baab..5be0be9 100644 --- a/log.h +++ b/log.h @@ -19,11 +19,18 @@ void vlogmsg(int pri, const char *format, va_list ap); void logmsg(int pri, const char *format, ...) __attribute__((format(printf, 2, 3))); +void logmsg_perror(int pri, const char *format, ...) + __attribute__((format(printf, 2, 3))); + +#define err(...) logmsg( LOG_ERR, __VA_ARGS__) +#define warn(...) logmsg( LOG_WARNING, __VA_ARGS__) +#define info(...) logmsg( LOG_INFO, __VA_ARGS__) +#define debug(...) logmsg( LOG_DEBUG, __VA_ARGS__) -#define err(...) logmsg(LOG_ERR, __VA_ARGS__) -#define warn(...) logmsg(LOG_WARNING, __VA_ARGS__) -#define info(...) logmsg(LOG_INFO, __VA_ARGS__) -#define debug(...) logmsg(LOG_DEBUG, __VA_ARGS__) +#define err_perror(...) logmsg_perror( LOG_ERR, __VA_ARGS__) +#define warn_perror(...) logmsg_perror( LOG_WARNING, __VA_ARGS__) +#define info_perror(...) logmsg_perror( LOG_INFO, __VA_ARGS__) +#define debug_perror(...) logmsg_perror( LOG_DEBUG, __VA_ARGS__) #define die(...) \ do { \ @@ -31,6 +38,12 @@ void logmsg(int pri, const char *format, ...) exit(EXIT_FAILURE); \ } while (0) +#define die_perror(...) \ + do { \ + err_perror(__VA_ARGS__); \ + exit(EXIT_FAILURE); \ + } while (0) + extern int log_trace; void trace_init(int enable); #define trace(...) \ -- 2.43.0