Mercurial > dovecot > core-2.2
changeset 9911:d7bbe0964b0d HEAD
Logging via log service: Added support for changing process's log prefix.
author | Timo Sirainen <tss@iki.fi> |
---|---|
date | Tue, 08 Sep 2009 19:29:54 -0400 |
parents | 5f61efdc9f35 |
children | 37bc3a3d4464 |
files | src/lib/failures.c src/log/log-connection.c |
diffstat | 2 files changed, 45 insertions(+), 26 deletions(-) [+] |
line wrap: on
line diff
--- a/src/lib/failures.c Tue Sep 08 18:45:27 2009 -0400 +++ b/src/lib/failures.c Tue Sep 08 19:29:54 2009 -0400 @@ -406,10 +406,24 @@ i_set_info_handler(NULL); } +static void i_failure_send_option(const char *key, const char *value) +{ + const char *str; + + if (error_handler != i_internal_error_handler) + return; + + str = t_strdup_printf("\001%c%s %s=%s\n", LOG_TYPE_OPTION+1, + my_pid, key, value); + (void)write_full(2, str, strlen(str)); +} + void i_set_failure_prefix(const char *prefix) { i_free(log_prefix); log_prefix = i_strdup(prefix); + + i_failure_send_option("prefix", prefix); } static int internal_send_split(string_t *full_str, unsigned int prefix_len) @@ -556,13 +570,7 @@ void i_set_failure_ip(const struct ip_addr *ip) { - const char *str; - - if (error_handler == i_internal_error_handler) { - str = t_strdup_printf("\001%c%s ip=%s\n", - LOG_TYPE_OPTION, my_pid, net_ip2addr(ip)); - (void)write_full(2, str, strlen(str)); - } + i_failure_send_option("ip", net_ip2addr(ip)); } void i_set_failure_exit_callback(void (*callback)(int *status))
--- a/src/log/log-connection.c Tue Sep 08 18:45:27 2009 -0400 +++ b/src/log/log-connection.c Tue Sep 08 19:29:54 2009 -0400 @@ -17,6 +17,7 @@ struct log_client { struct ip_addr ip; + char *prefix; unsigned int fatal_logged:1; }; @@ -27,7 +28,8 @@ int listen_fd; struct io *io; - char *prefix; + char *default_prefix; + /* pid -> struct log_client* */ struct hash_table *clients; unsigned int master:1; @@ -49,20 +51,27 @@ return client; } -static void log_parse_ip(struct log_connection *log, - const struct failure_line *failure) +static void log_client_free(struct log_connection *log, + struct log_client *client, pid_t pid) { - struct log_client *client; + hash_table_remove(log->clients, POINTER_CAST(pid)); - client = log_client_get(log, failure->pid); - (void)net_addr2ip(failure->text + 3, &client->ip); + i_free(client->prefix); + i_free(client); } static void log_parse_option(struct log_connection *log, const struct failure_line *failure) { + struct log_client *client; + + client = log_client_get(log, failure->pid); if (strncmp(failure->text, "ip=", 3) == 0) - log_parse_ip(log, failure); + (void)net_addr2ip(failure->text + 3, &client->ip); + else if (strncmp(failure->text, "prefix=", 7) == 0) { + i_free(client->prefix); + client->prefix = i_strdup(failure->text + 7); + } } static void log_parse_master_line(const char *line) @@ -98,17 +107,14 @@ it's not an error. */ return; } - hash_table_remove(log->clients, POINTER_CAST(pid)); - i_free(client); + log_client_free(log, client, pid); } else if (strncmp(line, "DEFAULT-FATAL ", 14) == 0) { /* If the client has logged a fatal/panic, don't log this message. */ if (client == NULL || !client->fatal_logged) i_error("%s", line + 14); - else { - hash_table_remove(log->clients, POINTER_CAST(pid)); - i_free(client); - } + else + log_client_free(log, client, pid); } else { i_error("Received unknown command from master: %s", line); } @@ -117,7 +123,8 @@ static void log_it(struct log_connection *log, const char *line) { struct failure_line failure; - struct log_client *client; + struct log_client *client = NULL; + const char *prefix; if (log->master) { log_parse_master_line(line); @@ -135,11 +142,15 @@ log_parse_option(log, &failure); return; default: + client = hash_table_lookup(log->clients, + POINTER_CAST(failure.pid)); break; } i_assert(failure.log_type < LOG_TYPE_COUNT); - i_set_failure_prefix(log->prefix); + prefix = client != NULL && client->prefix != NULL ? + client->prefix : log->default_prefix; + i_set_failure_prefix(prefix); i_log_type(failure.log_type, "%s", failure.text); i_set_failure_prefix("log: "); } @@ -157,11 +168,11 @@ return FALSE; if (handshake.prefix_len <= size - sizeof(handshake)) { - log->prefix = i_strndup(*data + sizeof(handshake), - handshake.prefix_len); + log->default_prefix = i_strndup(*data + sizeof(handshake), + handshake.prefix_len); *data += sizeof(handshake) + handshake.prefix_len; } - if (strcmp(log->prefix, MASTER_LOG_PREFIX_NAME) == 0) { + if (strcmp(log->default_prefix, MASTER_LOG_PREFIX_NAME) == 0) { if (log->listen_fd != MASTER_LISTEN_FD_FIRST) { i_error("Received master prefix in handshake " "from non-master fd %d", log->fd); @@ -239,7 +250,7 @@ io_remove(&log->io); if (close(log->fd) < 0) i_error("close(log connection fd) failed: %m"); - i_free(log->prefix); + i_free(log->default_prefix); i_free(log); master_service_client_connection_destroyed(master_service);