ia64/xen-unstable

changeset 17038:181578832957

Add timestamp option to xenconsoled

Similar to the --log option, --timestamp or -t takes:
- none : No timestamping
- hv : Timestamp hypervisor logs
- guest: Timestamp guest logs
- all : Timestamp guest and hypervisor logs

From: Cole Robinson <crobinso@redhat.com>
Signed-off-by: Keir Fraser <keir.fraser@citrix.com>
author Keir Fraser <keir.fraser@citrix.com>
date Tue Feb 12 10:16:20 2008 +0000 (2008-02-12)
parents 2a3111016f88
children c3fd43049492
files tools/console/daemon/io.c tools/console/daemon/main.c
line diff
     1.1 --- a/tools/console/daemon/io.c	Mon Feb 11 15:59:49 2008 +0000
     1.2 +++ b/tools/console/daemon/io.c	Tue Feb 12 10:16:20 2008 +0000
     1.3 @@ -60,6 +60,8 @@
     1.4  extern int log_reload;
     1.5  extern int log_guest;
     1.6  extern int log_hv;
     1.7 +extern int log_time_hv;
     1.8 +extern int log_time_guest;
     1.9  extern char *log_dir;
    1.10  
    1.11  static int log_hv_fd = -1;
    1.12 @@ -99,6 +101,50 @@ struct domain
    1.13  
    1.14  static struct domain *dom_head;
    1.15  
    1.16 +static int write_all(int fd, const char* buf, size_t len)
    1.17 +{
    1.18 +	while (len) {
    1.19 +		ssize_t ret = write(fd, buf, len);
    1.20 +		if (ret == -1 && errno == EINTR)
    1.21 +			continue;
    1.22 +		if (ret <= 0)
    1.23 +			return -1;
    1.24 +		len -= ret;
    1.25 +		buf += ret;
    1.26 +	}
    1.27 +
    1.28 +	return 0;
    1.29 +}
    1.30 +
    1.31 +static int write_with_timestamp(int fd, const char *data, size_t sz)
    1.32 +{
    1.33 +	char buf[sz+1];
    1.34 +	char ts[32];
    1.35 +	time_t now = time(NULL);
    1.36 +	const struct tm *tmnow = localtime(&now);
    1.37 +	size_t tslen = strftime(ts, sizeof(ts), "[%d-%m-%Y %H:%M:%S] ", tmnow);
    1.38 +
    1.39 +	memcpy(buf, data, sz);
    1.40 +	while (sz > 0 && buf[sz-1] == '\r')
    1.41 +		sz--;		    // Don't print trailing \r's
    1.42 +	if (sz > 0 && buf[sz-1] != '\n')
    1.43 +		buf[sz++] = '\n';   // Force ending newline
    1.44 +	data = buf;
    1.45 +
    1.46 +	while (sz > 0) {
    1.47 +		const char *nl = strchr(data, '\n') + 1;
    1.48 +		size_t towrite = nl - data;
    1.49 +		if (write_all(fd, ts, tslen) < 0)
    1.50 +			return -1;
    1.51 +		if (write_all(fd, data, towrite))
    1.52 +			return -1;
    1.53 +		sz -= towrite;
    1.54 +		data = nl;
    1.55 +	}
    1.56 +
    1.57 +	return 0;
    1.58 +}
    1.59 +
    1.60  static void buffer_append(struct domain *dom)
    1.61  {
    1.62  	struct buffer *buffer = &dom->buffer;
    1.63 @@ -135,10 +181,13 @@ static void buffer_append(struct domain 
    1.64  	 * and handle_tty_write will stop being called.
    1.65  	 */
    1.66  	if (dom->log_fd != -1) {
    1.67 -		int len = write(dom->log_fd,
    1.68 -				buffer->data + buffer->size - size,
    1.69 -				size);
    1.70 -		if (len < 0)
    1.71 +		int logret;
    1.72 +		if (log_time_guest) {
    1.73 +			logret = write_with_timestamp(dom->log_fd, buffer->data + buffer->size - size, size);
    1.74 +		} else {
    1.75 +			logret = write_all(dom->log_fd, buffer->data + buffer->size - size, size);
    1.76 +        }
    1.77 +		if (logret < 0)
    1.78  			dolog(LOG_ERR, "Write to log failed on domain %d: %d (%s)\n",
    1.79  			      dom->domid, errno, strerror(errno));
    1.80  	}
    1.81 @@ -195,6 +244,15 @@ static int create_hv_log(void)
    1.82  	if (fd == -1)
    1.83  		dolog(LOG_ERR, "Failed to open log %s: %d (%s)",
    1.84  		      logfile, errno, strerror(errno));
    1.85 +	if (fd != -1 && log_time_hv) {
    1.86 +		if (write_with_timestamp(fd, "Logfile Opened",
    1.87 +					 strlen("Logfile Opened")) < 0) {
    1.88 +			dolog(LOG_ERR, "Failed to log opening timestamp "
    1.89 +				       "in %s: %d (%s)", logfile, errno,
    1.90 +				       strerror(errno));
    1.91 +			return -1;
    1.92 +		}
    1.93 +	}
    1.94  	return fd;
    1.95  }
    1.96  
    1.97 @@ -229,6 +287,15 @@ static int create_domain_log(struct doma
    1.98  	if (fd == -1)
    1.99  		dolog(LOG_ERR, "Failed to open log %s: %d (%s)",
   1.100  		      logfile, errno, strerror(errno));
   1.101 +	if (fd != -1 && log_time_guest) {
   1.102 +		if (write_with_timestamp(fd, "Logfile Opened",
   1.103 +					 strlen("Logfile Opened")) < 0) {
   1.104 +			dolog(LOG_ERR, "Failed to log opening timestamp "
   1.105 +				       "in %s: %d (%s)", logfile, errno,
   1.106 +				       strerror(errno));
   1.107 +			return -1;
   1.108 +		}
   1.109 +	}
   1.110  	return fd;
   1.111  }
   1.112  
   1.113 @@ -817,11 +884,16 @@ static void handle_hv_logs(void)
   1.114  	if ((port = xc_evtchn_pending(xce_handle)) == -1)
   1.115  		return;
   1.116  
   1.117 -	if (xc_readconsolering(xc_handle, &bufptr, &size, 0, 1, &index) == 0) {
   1.118 -		int len = write(log_hv_fd, buffer, size);
   1.119 -		if (len < 0)
   1.120 -			dolog(LOG_ERR, "Failed to write hypervisor log: %d (%s)",
   1.121 -			      errno, strerror(errno));
   1.122 +	if (xc_readconsolering(xc_handle, &bufptr, &size, 0, 1, &index) == 0 && size > 0) {
   1.123 +		int logret;
   1.124 +		if (log_time_guest)
   1.125 +			logret = write_with_timestamp(log_hv_fd, buffer, size);
   1.126 +		else
   1.127 +			logret = write_all(log_hv_fd, buffer, size);
   1.128 +
   1.129 +		if (logret < 0)
   1.130 +			dolog(LOG_ERR, "Failed to write hypervisor log: "
   1.131 +				       "%d (%s)", errno, strerror(errno));
   1.132  	}
   1.133  
   1.134  	(void)xc_evtchn_unmask(xce_handle, port);
     2.1 --- a/tools/console/daemon/main.c	Mon Feb 11 15:59:49 2008 +0000
     2.2 +++ b/tools/console/daemon/main.c	Tue Feb 12 10:16:20 2008 +0000
     2.3 @@ -35,6 +35,8 @@
     2.4  int log_reload = 0;
     2.5  int log_guest = 0;
     2.6  int log_hv = 0;
     2.7 +int log_time_hv = 0;
     2.8 +int log_time_guest = 0;
     2.9  char *log_dir = NULL;
    2.10  
    2.11  static void handle_hup(int sig)
    2.12 @@ -44,7 +46,7 @@ static void handle_hup(int sig)
    2.13  
    2.14  static void usage(char *name)
    2.15  {
    2.16 -	printf("Usage: %s [-h] [-V] [-v] [-i] [--log=none|guest|hv|all] [--log-dir=DIR] [--pid-file=PATH]\n", name);
    2.17 +	printf("Usage: %s [-h] [-V] [-v] [-i] [--log=none|guest|hv|all] [--log-dir=DIR] [--pid-file=PATH] [-t, --timestamp=none|guest|hv|all]\n", name);
    2.18  }
    2.19  
    2.20  static void version(char *name)
    2.21 @@ -54,7 +56,7 @@ static void version(char *name)
    2.22  
    2.23  int main(int argc, char **argv)
    2.24  {
    2.25 -	const char *sopts = "hVvi";
    2.26 +	const char *sopts = "hVvit:";
    2.27  	struct option lopts[] = {
    2.28  		{ "help", 0, 0, 'h' },
    2.29  		{ "version", 0, 0, 'V' },
    2.30 @@ -63,6 +65,7 @@ int main(int argc, char **argv)
    2.31  		{ "log", 1, 0, 'l' },
    2.32  		{ "log-dir", 1, 0, 'r' },
    2.33  		{ "pid-file", 1, 0, 'p' },
    2.34 +		{ "timestamp", 1, 0, 't' },
    2.35  		{ 0 },
    2.36  	};
    2.37  	bool is_interactive = false;
    2.38 @@ -103,6 +106,19 @@ int main(int argc, char **argv)
    2.39  		case 'p':
    2.40  		        pidfile = strdup(optarg);
    2.41  			break;
    2.42 +		case 't':
    2.43 +			if (!strcmp(optarg, "all")) {
    2.44 +				log_time_hv = 1;
    2.45 +				log_time_guest = 1;
    2.46 +			} else if (!strcmp(optarg, "hv")) {
    2.47 +				log_time_hv = 1;
    2.48 +			} else if (!strcmp(optarg, "guest")) {
    2.49 +				log_time_guest = 1;
    2.50 +			} else if (!strcmp(optarg, "none")) {
    2.51 +				log_time_guest = 0;
    2.52 +				log_time_hv = 0;
    2.53 +			}
    2.54 +			break;
    2.55  		case '?':
    2.56  			fprintf(stderr,
    2.57  				"Try `%s --help' for more information\n",