dovecot-2.0: Added debug_log_path setting and i_debug() call.

dovecot at dovecot.org dovecot at dovecot.org
Thu Oct 1 00:48:46 EEST 2009


details:   http://hg.dovecot.org/dovecot-2.0/rev/86a39e326ee7
changeset: 9959:86a39e326ee7
user:      Timo Sirainen <tss at iki.fi>
date:      Wed Sep 30 17:48:38 2009 -0400
description:
Added debug_log_path setting and i_debug() call.

diffstat:

8 files changed, 111 insertions(+), 15 deletions(-)
doc/example-config/conf.d/logging.conf   |    5 -
src/lib-master/master-service-settings.c |    2 
src/lib-master/master-service-settings.h |    1 
src/lib-master/master-service.c          |    4 +
src/lib/failures.c                       |   96 +++++++++++++++++++++++++++---
src/lib/failures.h                       |   12 +++
src/master/main.c                        |    5 -
src/master/service-process.c             |    1 

diffs (truncated from 363 to 300 lines):

diff -r e139e62cfa22 -r 86a39e326ee7 doc/example-config/conf.d/logging.conf
--- a/doc/example-config/conf.d/logging.conf	Wed Sep 30 17:25:29 2009 -0400
+++ b/doc/example-config/conf.d/logging.conf	Wed Sep 30 17:48:38 2009 -0400
@@ -6,9 +6,10 @@
 # /dev/stderr can be used to log into stderr.
 #log_path = 
 
-# Log file to use for informational and debug messages.
-# Default is the same as log_path.
+# Log file to use for informational messages. Defaults to log_path.
 #info_log_path = 
+# Log file to use for debug messages. Defaults to info_log_path.
+#debug_log_path = 
 
 # Syslog facility to use if you're logging to syslog. Usually if you don't
 # want to use "mail", you'll use local0..local7. Also other standard
diff -r e139e62cfa22 -r 86a39e326ee7 src/lib-master/master-service-settings.c
--- a/src/lib-master/master-service-settings.c	Wed Sep 30 17:25:29 2009 -0400
+++ b/src/lib-master/master-service-settings.c	Wed Sep 30 17:48:38 2009 -0400
@@ -25,6 +25,7 @@ static struct setting_define master_serv
 static struct setting_define master_service_setting_defines[] = {
 	DEF(SET_STR, log_path),
 	DEF(SET_STR, info_log_path),
+	DEF(SET_STR, debug_log_path),
 	DEF(SET_STR, log_timestamp),
 	DEF(SET_STR, syslog_facility),
 	DEF(SET_BOOL, version_ignore),
@@ -35,6 +36,7 @@ static struct master_service_settings ma
 static struct master_service_settings master_service_default_settings = {
 	MEMBER(log_path) "",
 	MEMBER(info_log_path) "",
+	MEMBER(debug_log_path) "",
 	MEMBER(log_timestamp) DEFAULT_FAILURE_STAMP_FORMAT,
 	MEMBER(syslog_facility) "mail",
 	MEMBER(version_ignore) FALSE
diff -r e139e62cfa22 -r 86a39e326ee7 src/lib-master/master-service-settings.h
--- a/src/lib-master/master-service-settings.h	Wed Sep 30 17:25:29 2009 -0400
+++ b/src/lib-master/master-service-settings.h	Wed Sep 30 17:48:38 2009 -0400
@@ -10,6 +10,7 @@ struct master_service_settings {
 struct master_service_settings {
 	const char *log_path;
 	const char *info_log_path;
+	const char *debug_log_path;
 	const char *log_timestamp;
 	const char *syslog_facility;
 	bool version_ignore;
diff -r e139e62cfa22 -r 86a39e326ee7 src/lib-master/master-service.c
--- a/src/lib-master/master-service.c	Wed Sep 30 17:25:29 2009 -0400
+++ b/src/lib-master/master-service.c	Wed Sep 30 17:48:38 2009 -0400
@@ -174,6 +174,10 @@ void master_service_init_log(struct mast
 	path = home_expand(service->set->info_log_path);
 	if (*path != '\0')
 		i_set_info_file(path);
+
+	path = home_expand(service->set->debug_log_path);
+	if (*path != '\0')
+		i_set_debug_file(path);
 	i_set_failure_timestamp_format(service->set->log_timestamp);
 }
 
diff -r e139e62cfa22 -r 86a39e326ee7 src/lib/failures.c
--- a/src/lib/failures.c	Wed Sep 30 17:25:29 2009 -0400
+++ b/src/lib/failures.c	Wed Sep 30 17:48:38 2009 -0400
@@ -17,6 +17,7 @@
 #include <time.h>
 
 const char *failure_log_type_prefixes[LOG_TYPE_COUNT] = {
+	"Debug: ",
 	"Info: ",
 	"Warning: ",
 	"Error: ",
@@ -29,9 +30,11 @@ static fatal_failure_callback_t *fatal_h
 	default_fatal_handler;
 static failure_callback_t *error_handler = default_error_handler;
 static failure_callback_t *info_handler = default_error_handler;
+static failure_callback_t *debug_handler = default_error_handler;
 static void (*failure_exit_callback)(int *) = NULL;
 
-static int log_fd = STDERR_FILENO, log_info_fd = STDERR_FILENO;
+static int log_fd = STDERR_FILENO, log_info_fd = STDERR_FILENO,
+	   log_debug_fd = STDERR_FILENO;
 static char *log_prefix = NULL, *log_stamp_format = NULL;
 static bool failure_ignore_errors = FALSE;
 
@@ -175,16 +178,27 @@ void default_fatal_handler(enum log_type
 
 void default_error_handler(enum log_type type, const char *format, va_list args)
 {
-	int fd = type == LOG_TYPE_INFO ? log_info_fd : log_fd;
+	int fd;
+
+	switch (type) {
+	case LOG_TYPE_DEBUG:
+		fd = log_debug_fd;
+		break;
+	case LOG_TYPE_INFO:
+		fd = log_info_fd;
+		break;
+	default:
+		fd = log_fd;
+	}
 
 	if (default_handler(failure_log_type_prefixes[type],
 			    fd, format, args) < 0) {
 		if (fd == log_fd)
 			failure_exit(FATAL_LOGWRITE);
-		/* we failed to log to info log, try to log the write error
-		   to error log - maybe that'll work. */
-		i_fatal_status(FATAL_LOGWRITE,
-			       "write() failed to info log: %m");
+		/* we failed to log to info/debug log, try to log the
+		   write error to error log - maybe that'll work. */
+		i_fatal_status(FATAL_LOGWRITE, "write() failed to %s log: %m",
+			       fd == log_info_fd ? "info" : "debug");
 	}
 }
 
@@ -194,10 +208,17 @@ void i_log_type(enum log_type type, cons
 
 	va_start(args, format);
 
-	if (type == LOG_TYPE_INFO)
+	switch (type) {
+	case LOG_TYPE_DEBUG:
+		debug_handler(type, format, args);
+		break;
+	case LOG_TYPE_INFO:
 		info_handler(type, format, args);
-	else
+		break;
+	default:
 		error_handler(type, format, args);
+	}
+
 	va_end(args);
 }
 
@@ -259,6 +280,18 @@ void i_info(const char *format, ...)
 
 	va_start(args, format);
 	info_handler(LOG_TYPE_INFO, format, args);
+	va_end(args);
+
+	errno = old_errno;
+}
+
+void i_debug(const char *format, ...)
+{
+	int old_errno = errno;
+	va_list args;
+
+	va_start(args, format);
+	debug_handler(LOG_TYPE_DEBUG, format, args);
 	va_end(args);
 
 	errno = old_errno;
@@ -285,13 +318,22 @@ void i_set_info_handler(failure_callback
 	info_handler = callback;
 }
 
+void i_set_debug_handler(failure_callback_t *callback)
+{
+	if (callback == NULL)
+		callback = default_error_handler;
+	debug_handler = callback;
+}
+
 void i_get_failure_handlers(fatal_failure_callback_t **fatal_callback_r,
 			    failure_callback_t **error_callback_r,
-			    failure_callback_t **info_callback_r)
+			    failure_callback_t **info_callback_r,
+			    failure_callback_t **debug_callback_r)
 {
 	*fatal_callback_r = fatal_handler;
 	*error_callback_r = error_handler;
 	*info_callback_r = info_handler;
+	*debug_callback_r = debug_handler;
 }
 
 static int ATTR_FORMAT(3, 0)
@@ -331,6 +373,9 @@ void i_syslog_error_handler(enum log_typ
 	int level = LOG_ERR;
 
 	switch (type) {
+	case LOG_TYPE_DEBUG:
+		level = LOG_DEBUG;
+		break;
 	case LOG_TYPE_INFO:
 		level = LOG_INFO;
 		break;
@@ -360,6 +405,7 @@ void i_set_failure_syslog(const char *id
 	i_set_fatal_handler(i_syslog_fatal_handler);
 	i_set_error_handler(i_syslog_error_handler);
 	i_set_info_handler(i_syslog_error_handler);
+	i_set_debug_handler(i_syslog_error_handler);
 }
 
 static void open_log_file(int *fd, const char *path)
@@ -398,12 +444,22 @@ void i_set_failure_file(const char *path
 			i_error("close(%d) failed: %m", log_info_fd);
 	}
 
+	if (log_debug_fd != STDERR_FILENO && log_debug_fd != log_info_fd &&
+	    log_debug_fd != log_fd) {
+		if (close(log_debug_fd) < 0)
+			i_error("close(%d) failed: %m", log_debug_fd);
+	}
+
 	open_log_file(&log_fd, path);
+	/* if info/debug logs are elsewhere, i_set_info/debug_file()
+	   overrides these later. */
 	log_info_fd = log_fd;
+	log_debug_fd = log_fd;
 
 	i_set_fatal_handler(NULL);
 	i_set_error_handler(NULL);
 	i_set_info_handler(NULL);
+	i_set_debug_handler(NULL);
 }
 
 static void i_failure_send_option(const char *key, const char *value)
@@ -546,6 +602,7 @@ void i_set_failure_internal(void)
 	i_set_fatal_handler(i_internal_fatal_handler);
 	i_set_error_handler(i_internal_error_handler);
 	i_set_info_handler(i_internal_error_handler);
+	i_set_debug_handler(i_internal_error_handler);
 }
 
 void i_set_failure_ignore_errors(bool ignore)
@@ -560,6 +617,19 @@ void i_set_info_file(const char *path)
 
 	open_log_file(&log_info_fd, path);
         info_handler = default_error_handler;
+	/* write debug-level messages to the info_log_path,
+	  until i_set_debug_file() was called */
+	log_debug_fd = log_info_fd;
+	i_set_debug_handler(NULL);
+}
+
+void i_set_debug_file(const char *path)
+{
+	if (log_debug_fd == log_fd || log_debug_fd == log_info_fd)
+		log_debug_fd = STDERR_FILENO;
+
+	open_log_file(&log_debug_fd, path);
+	debug_handler = default_error_handler;
 }
 
 void i_set_failure_timestamp_format(const char *fmt)
@@ -580,6 +650,9 @@ void i_set_failure_exit_callback(void (*
 
 void failures_deinit(void)
 {
+	if (log_debug_fd == log_info_fd || log_debug_fd == log_fd)
+		log_debug_fd = STDERR_FILENO;
+
 	if (log_info_fd == log_fd)
 		log_info_fd = STDERR_FILENO;
 
@@ -593,6 +666,11 @@ void failures_deinit(void)
 		log_info_fd = STDERR_FILENO;
 	}
 
+	if (log_debug_fd != STDERR_FILENO) {
+		(void)close(log_debug_fd);
+		log_debug_fd = STDERR_FILENO;
+	}
+
 	i_free_and_null(log_prefix);
 	i_free_and_null(log_stamp_format);
 }
diff -r e139e62cfa22 -r 86a39e326ee7 src/lib/failures.h
--- a/src/lib/failures.h	Wed Sep 30 17:25:29 2009 -0400
+++ b/src/lib/failures.h	Wed Sep 30 17:48:38 2009 -0400
@@ -15,6 +15,7 @@ enum fatal_exit_status {
 };
 
 enum log_type {
+	LOG_TYPE_DEBUG,
 	LOG_TYPE_INFO,
 	LOG_TYPE_WARNING,
 	LOG_TYPE_ERROR,
@@ -46,6 +47,7 @@ void i_error(const char *format, ...) AT
 void i_error(const char *format, ...) ATTR_FORMAT(1, 2) ATTR_COLD;
 void i_warning(const char *format, ...) ATTR_FORMAT(1, 2);
 void i_info(const char *format, ...) ATTR_FORMAT(1, 2);
+void i_debug(const char *format, ...) ATTR_FORMAT(1, 2);
 
 void i_fatal_status(int status, const char *format, ...)
 	ATTR_FORMAT(2, 3) ATTR_NORETURN ATTR_COLD;
@@ -59,9 +61,11 @@ void i_set_fatal_handler(fatal_failure_c
 #endif
 void i_set_error_handler(failure_callback_t *callback);
 void i_set_info_handler(failure_callback_t *callback);
+void i_set_debug_handler(failure_callback_t *callback);
 void i_get_failure_handlers(fatal_failure_callback_t **fatal_callback_r,
 			    failure_callback_t **error_callback_r,
-			    failure_callback_t **info_callback_r);
+			    failure_callback_t **info_callback_r,
+			    failure_callback_t **debug_callback_r);


More information about the dovecot-cvs mailing list