ia64/xen-unstable

changeset 17095:28dcae176191

xenconsoled: don't force newline with timestamps

Previously a newline was forced after every call to
write_with_timestamp. This could result in some ugly logs when a
single console line stretched over multiple calls. This change carries
some extra state to determine if we ended the last call to
write_with_timestamp on a newline, and prints the timestamp as
appropriate.

This also fixes a bug where logging hv messages was dependent on the
guest timestamp flag.

Signed-off-by: Cole Robinson <crobinso@redhat.com>
author Keir Fraser <keir.fraser@citrix.com>
date Wed Feb 20 18:00:28 2008 +0000 (2008-02-20)
parents 6e096456b3e1
children ea1dc85d7122
files tools/console/daemon/io.c
line diff
     1.1 --- a/tools/console/daemon/io.c	Wed Feb 20 17:47:58 2008 +0000
     1.2 +++ b/tools/console/daemon/io.c	Wed Feb 20 18:00:28 2008 +0000
     1.3 @@ -64,13 +64,14 @@ extern int log_time_hv;
     1.4  extern int log_time_guest;
     1.5  extern char *log_dir;
     1.6  
     1.7 +static int log_time_hv_needts = 1;
     1.8 +static int log_time_guest_needts = 1;
     1.9  static int log_hv_fd = -1;
    1.10  static evtchn_port_or_error_t log_hv_evtchn = -1;
    1.11  static int xc_handle = -1;
    1.12  static int xce_handle = -1;
    1.13  
    1.14 -struct buffer
    1.15 -{
    1.16 +struct buffer {
    1.17  	char *data;
    1.18  	size_t consumed;
    1.19  	size_t size;
    1.20 @@ -78,8 +79,7 @@ struct buffer
    1.21  	size_t max_capacity;
    1.22  };
    1.23  
    1.24 -struct domain
    1.25 -{
    1.26 +struct domain {
    1.27  	int domid;
    1.28  	int master_fd;
    1.29  	int slave_fd;
    1.30 @@ -116,30 +116,32 @@ static int write_all(int fd, const char*
    1.31  	return 0;
    1.32  }
    1.33  
    1.34 -static int write_with_timestamp(int fd, const char *data, size_t sz)
    1.35 +static int write_with_timestamp(int fd, const char *data, size_t sz,
    1.36 +				int *needts)
    1.37  {
    1.38 -	char buf[sz+1];
    1.39  	char ts[32];
    1.40  	time_t now = time(NULL);
    1.41  	const struct tm *tmnow = localtime(&now);
    1.42  	size_t tslen = strftime(ts, sizeof(ts), "[%Y-%m-%d %H:%M:%S] ", tmnow);
    1.43 -
    1.44 -	memcpy(buf, data, sz);
    1.45 -	while (sz > 0 && buf[sz-1] == '\r')
    1.46 -		sz--;		    // Don't print trailing \r's
    1.47 -	if (sz > 0 && buf[sz-1] != '\n')
    1.48 -		buf[sz++] = '\n';   // Force ending newline
    1.49 -	data = buf;
    1.50 +	const char *last_byte = data + sz - 1;
    1.51  
    1.52 -	while (sz > 0) {
    1.53 -		const char *nl = strchr(data, '\n') + 1;
    1.54 -		size_t towrite = nl - data;
    1.55 -		if (write_all(fd, ts, tslen) < 0)
    1.56 +	while (data <= last_byte) {
    1.57 +		const char *nl = memchr(data, '\n', sz);
    1.58 +		int found_nl = (nl != NULL);
    1.59 +		if (!found_nl)
    1.60 +			nl = last_byte;
    1.61 +
    1.62 +		if ((*needts && write_all(fd, ts, tslen))
    1.63 +		    || write_all(fd, data, nl + 1 - data))
    1.64  			return -1;
    1.65 -		if (write_all(fd, data, towrite))
    1.66 -			return -1;
    1.67 -		sz -= towrite;
    1.68 -		data = nl;
    1.69 +
    1.70 +		*needts = found_nl;
    1.71 +		data = nl + 1;
    1.72 +		if (found_nl) {
    1.73 +			// If we printed a newline, strip all \r following it
    1.74 +			while (data <= last_byte && *data == '\r')
    1.75 +				data++;
    1.76 +		}
    1.77  	}
    1.78  
    1.79  	return 0;
    1.80 @@ -183,12 +185,19 @@ static void buffer_append(struct domain 
    1.81  	if (dom->log_fd != -1) {
    1.82  		int logret;
    1.83  		if (log_time_guest) {
    1.84 -			logret = write_with_timestamp(dom->log_fd, buffer->data + buffer->size - size, size);
    1.85 +			logret = write_with_timestamp(
    1.86 +				dom->log_fd,
    1.87 +				buffer->data + buffer->size - size,
    1.88 +				size, &log_time_guest_needts);
    1.89  		} else {
    1.90 -			logret = write_all(dom->log_fd, buffer->data + buffer->size - size, size);
    1.91 -        }
    1.92 +			logret = write_all(
    1.93 +				dom->log_fd,
    1.94 +				buffer->data + buffer->size - size,
    1.95 +				size);
    1.96 +		}
    1.97  		if (logret < 0)
    1.98 -			dolog(LOG_ERR, "Write to log failed on domain %d: %d (%s)\n",
    1.99 +			dolog(LOG_ERR, "Write to log failed "
   1.100 +			      "on domain %d: %d (%s)\n",
   1.101  			      dom->domid, errno, strerror(errno));
   1.102  	}
   1.103  
   1.104 @@ -246,7 +255,8 @@ static int create_hv_log(void)
   1.105  		      logfile, errno, strerror(errno));
   1.106  	if (fd != -1 && log_time_hv) {
   1.107  		if (write_with_timestamp(fd, "Logfile Opened",
   1.108 -					 strlen("Logfile Opened")) < 0) {
   1.109 +					 strlen("Logfile Opened"),
   1.110 +					 &log_time_hv_needts) < 0) {
   1.111  			dolog(LOG_ERR, "Failed to log opening timestamp "
   1.112  				       "in %s: %d (%s)", logfile, errno,
   1.113  				       strerror(errno));
   1.114 @@ -289,7 +299,8 @@ static int create_domain_log(struct doma
   1.115  		      logfile, errno, strerror(errno));
   1.116  	if (fd != -1 && log_time_guest) {
   1.117  		if (write_with_timestamp(fd, "Logfile Opened",
   1.118 -					 strlen("Logfile Opened")) < 0) {
   1.119 +					 strlen("Logfile Opened"),
   1.120 +					 &log_time_guest_needts) < 0) {
   1.121  			dolog(LOG_ERR, "Failed to log opening timestamp "
   1.122  				       "in %s: %d (%s)", logfile, errno,
   1.123  				       strerror(errno));
   1.124 @@ -314,7 +325,7 @@ static void domain_close_tty(struct doma
   1.125  
   1.126  #ifdef __sun__
   1.127  static int openpty(int *amaster, int *aslave, char *name,
   1.128 -                   struct termios *termp, struct winsize *winp)
   1.129 +		   struct termios *termp, struct winsize *winp)
   1.130  {
   1.131  	const char *slave;
   1.132  	int mfd = -1, sfd = -1;
   1.133 @@ -389,14 +400,16 @@ static int domain_create_tty(struct doma
   1.134  
   1.135  	if (openpty(&dom->master_fd, &dom->slave_fd, NULL, &term, NULL) < 0) {
   1.136  		err = errno;
   1.137 -		dolog(LOG_ERR, "Failed to create tty for domain-%d (errno = %i, %s)",
   1.138 +		dolog(LOG_ERR, "Failed to create tty for domain-%d "
   1.139 +		      "(errno = %i, %s)",
   1.140  		      dom->domid, err, strerror(err));
   1.141  		return 0;
   1.142  	}
   1.143  
   1.144  	if ((slave = ptsname(dom->master_fd)) == NULL) {
   1.145  		err = errno;
   1.146 -		dolog(LOG_ERR, "Failed to get slave name for domain-%d (errno = %i, %s)",
   1.147 +		dolog(LOG_ERR, "Failed to get slave name for domain-%d "
   1.148 +		      "(errno = %i, %s)",
   1.149  		      dom->domid, err, strerror(err));
   1.150  		goto out;
   1.151  	}
   1.152 @@ -886,8 +899,9 @@ static void handle_hv_logs(void)
   1.153  
   1.154  	if (xc_readconsolering(xc_handle, &bufptr, &size, 0, 1, &index) == 0 && size > 0) {
   1.155  		int logret;
   1.156 -		if (log_time_guest)
   1.157 -			logret = write_with_timestamp(log_hv_fd, buffer, size);
   1.158 +		if (log_time_hv)
   1.159 +			logret = write_with_timestamp(log_hv_fd, buffer, size,
   1.160 +						      &log_time_hv_needts);
   1.161  		else
   1.162  			logret = write_all(log_hv_fd, buffer, size);
   1.163  
   1.164 @@ -1048,14 +1062,17 @@ void handle_io(void)
   1.165  			n = d->next;
   1.166  			if (d->event_count < RATE_LIMIT_ALLOWANCE) {
   1.167  				if (d->xce_handle != -1 &&
   1.168 -				    FD_ISSET(xc_evtchn_fd(d->xce_handle), &readfds))
   1.169 +				    FD_ISSET(xc_evtchn_fd(d->xce_handle),
   1.170 +					     &readfds))
   1.171  					handle_ring_read(d);
   1.172  			}
   1.173  
   1.174 -			if (d->master_fd != -1 && FD_ISSET(d->master_fd, &readfds))
   1.175 +			if (d->master_fd != -1 && FD_ISSET(d->master_fd,
   1.176 +							   &readfds))
   1.177  				handle_tty_read(d);
   1.178  
   1.179 -			if (d->master_fd != -1 && FD_ISSET(d->master_fd, &writefds))
   1.180 +			if (d->master_fd != -1 && FD_ISSET(d->master_fd,
   1.181 +							   &writefds))
   1.182  				handle_tty_write(d);
   1.183  
   1.184  			if (d->is_dead)