From cbc54bf559ce257991a4c87001d7a5c41dbe1869 Mon Sep 17 00:00:00 2001 From: Michael Brown Date: Wed, 20 Jun 2012 14:39:33 +0100 Subject: [PATCH] [syslog] Include hostname within syslog messages where possible Signed-off-by: Michael Brown --- src/include/ipxe/syslog.h | 3 +++ src/net/tcp/syslogs.c | 6 ++--- src/net/udp/syslog.c | 56 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/src/include/ipxe/syslog.h b/src/include/ipxe/syslog.h index 035ca6700..131692654 100644 --- a/src/include/ipxe/syslog.h +++ b/src/include/ipxe/syslog.h @@ -35,4 +35,7 @@ FILE_LICENCE ( GPL2_OR_LATER ); /** Syslog priority */ #define SYSLOG_PRIORITY( facility, severity ) ( 8 * (facility) + (severity) ) +extern int syslog_send ( struct interface *xfer, unsigned int severity, + const char *message, const char *terminator ); + #endif /* _IPXE_SYSLOG_H */ diff --git a/src/net/tcp/syslogs.c b/src/net/tcp/syslogs.c index e7480e56a..854e21524 100644 --- a/src/net/tcp/syslogs.c +++ b/src/net/tcp/syslogs.c @@ -162,10 +162,8 @@ static void syslogs_putchar ( int character ) { syslogs_entered = 1; /* Send log message */ - if ( ( rc = xfer_printf ( &syslogs, "<%d>ipxe: %s\n", - SYSLOG_PRIORITY ( SYSLOG_DEFAULT_FACILITY, - syslogs_severity ), - syslogs_buffer ) ) != 0 ) { + if ( ( rc = syslog_send ( &syslogs, syslogs_severity, + syslogs_buffer, "\n" ) ) != 0 ) { DBG ( "SYSLOGS could not send log message: %s\n", strerror ( rc ) ); } diff --git a/src/net/udp/syslog.c b/src/net/udp/syslog.c index 4a2653148..9e55d211b 100644 --- a/src/net/udp/syslog.c +++ b/src/net/udp/syslog.c @@ -25,6 +25,7 @@ FILE_LICENCE ( GPL2_OR_LATER ); */ #include +#include #include #include #include @@ -58,6 +59,41 @@ static struct interface_descriptor syslogger_desc = /** The syslog UDP interface */ static struct interface syslogger = INTF_INIT ( syslogger_desc ); +/****************************************************************************** + * + * Console driver + * + ****************************************************************************** + */ + +/** Host name (for log messages) */ +static char *syslog_hostname; + +/** Domain name (for log messages) */ +static char *syslog_domain; + +/** + * Transmit formatted syslog message + * + * @v xfer Data transfer interface + * @v severity Severity + * @v message Message + * @v terminator Message terminator + * @ret rc Return status code + */ +int syslog_send ( struct interface *xfer, unsigned int severity, + const char *message, const char *terminator ) { + + return xfer_printf ( xfer, "<%d>%s%s%s%sipxe: %s%s", + SYSLOG_PRIORITY ( SYSLOG_DEFAULT_FACILITY, + severity ), + ( syslog_hostname ? syslog_hostname : "" ), + ( syslog_domain ? "." : "" ), + ( syslog_domain ? syslog_domain : "" ), + ( ( syslog_hostname || syslog_domain ) ? " " : ""), + message, terminator ); +} + /****************************************************************************** * * Console driver @@ -124,10 +160,8 @@ static void syslog_putchar ( int character ) { syslog_entered = 1; /* Send log message */ - if ( ( rc = xfer_printf ( &syslogger, "<%d>ipxe: %s", - SYSLOG_PRIORITY ( SYSLOG_DEFAULT_FACILITY, - syslog_severity ), - syslog_buffer ) ) != 0 ) { + if ( ( rc = syslog_send ( &syslogger, syslog_severity, + syslog_buffer, "" ) ) != 0 ) { DBG ( "SYSLOG could not send log message: %s\n", strerror ( rc ) ); } @@ -170,6 +204,20 @@ static int apply_syslog_settings ( void ) { int len; int rc; + /* Fetch hostname and domain name */ + free ( syslog_hostname ); + if ( ( len = fetch_string_setting_copy ( NULL, &hostname_setting, + &syslog_hostname ) ) < 0 ) { + rc = len; + DBG ( "SYSLOG could not fetch hostname: %s\n", strerror ( rc )); + } + free ( syslog_domain ); + if ( ( len = fetch_string_setting_copy ( NULL, &domain_setting, + &syslog_domain ) ) < 0 ) { + rc = len; + DBG ( "SYSLOG could not fetch domain: %s\n", strerror ( rc ) ); + } + /* Fetch log server */ syslog_console.disabled = 1; old_addr.s_addr = sin_logserver->sin_addr.s_addr;