[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-devel] [PATCH 1/3] Add "--timestamp" option to xenconsole.



This patch adds "--timestamp" and "-t" option to xenconsole.
Xenconsole adds date and domid to output message when it's started
with "--timestamp" or "-t" option.

Thanks,
--
Yuji Shimada

Signed-off-by: Yuji Shimada <shimada-yxb@xxxxxxxxxxxxxxx>

diff -r af1d9af1a993 tools/console/client/main.c
--- a/tools/console/client/main.c       Wed Jan 21 14:44:43 2009 +0000
+++ b/tools/console/client/main.c       Wed Jan 28 12:01:28 2009 +0900
@@ -41,6 +41,8 @@
 #define ESCAPE_CHARACTER 0x1d
 
 static volatile sig_atomic_t received_signal = 0;
+static int domid;
+static int log_timestamp = 0;
 
 static void sighandler(int signum)
 {
@@ -50,7 +52,7 @@ static bool write_sync(int fd, const voi
 static bool write_sync(int fd, const void *data, size_t size)
 {
        size_t offset = 0;
-       ssize_t len;
+       ssize_t len = 0;
 
        while (offset < size) {
                len = write(fd, data + offset, size - offset);
@@ -58,6 +60,88 @@ static bool write_sync(int fd, const voi
                        return false;
                }
                offset += len;
+       }
+       return true;
+}
+
+static bool write_timestamp(int fd)
+{
+       time_t current_tm;
+       struct tm time_str;
+       char date_domid[32];
+
+       time(&current_tm);
+       localtime_r(&current_tm, &time_str);
+       sprintf(date_domid, "[%4d-%.2d-%.2d %.2d:%.2d:%.2d %d] ",
+               time_str.tm_year+1900, time_str.tm_mon+1,
+               time_str.tm_mday, time_str.tm_hour, time_str.tm_min,
+               time_str.tm_sec, domid);
+       if (write(fd, &date_domid, strlen(date_domid)) < 1) {
+               return false;
+       }
+
+       return true;
+}
+
+static bool write_sync_with_timestamp(int fd, const void *buf, size_t size)
+{
+       size_t offset = 0;
+       ssize_t len = 0;
+       char *data = (char *)buf;
+       size_t cur_offset;
+       static char last_newline = 0;
+       static int timestamp_f = 1; /* flag for timestamp */
+
+       while (offset < size) {
+               for (cur_offset = offset; cur_offset < size; cur_offset++) {
+                       if (data[cur_offset] == '\r' ||
+                           data[cur_offset] == '\n') {
+                               break;
+                       }
+               }
+               len = cur_offset - offset;
+               if (timestamp_f == 1 && (len > 0 || last_newline == 0 ||
+                   data[cur_offset] == last_newline)) {
+                       if (write_timestamp(fd) == false) {
+                               return false;
+                       }
+                       timestamp_f = 0;
+               }
+               if (len > 0) {
+                       len = write(fd, data + offset, len);
+                       if (len < 1) {
+                               return false;
+                       }
+                       last_newline = 0;
+                       offset += len;
+                       if (size <= offset) {
+                               break;
+                       }
+               }
+               if (data[offset] == '\n' || data[offset] == '\r') {
+                       if (last_newline == 0 ||
+                           data[offset] == last_newline) {
+                               timestamp_f = 1;
+                               last_newline = data[offset];
+                               if (isatty(fd)) {
+                                       if (write(fd, data + offset, 1) != 1) {
+                                               return false;
+                                       }
+                               } else {
+                                       if (write(fd, "\n", 1) != 1) {
+                                               return false;
+                                       }
+                               }
+                       } else {
+                               last_newline = 0;
+                               if (isatty(fd)) {
+                                       if (write(fd, data + offset, 1) != 1) {
+                                               return false;
+                                       }
+                               }
+                       }
+                       offset++;
+               }
        }
 
        return true;
@@ -68,6 +152,7 @@ static void usage(const char *program) {
               "Attaches to a virtual domain console\n"
               "\n"
               "  -h, --help       display this help and exit\n"
+              "  -t, --timestamp       add timestamp to output message\n"
               , program);
 }
 
@@ -199,9 +284,17 @@ static int console_loop(int fd, struct x
                                continue;
                        }
 
-                       if (!write_sync(STDOUT_FILENO, msg, len)) {
-                               perror("write() failed");
-                               return -1;
+                       if (log_timestamp) {
+                               if (!write_sync_with_timestamp(STDOUT_FILENO,
+                                                              msg, len)) {
+                                       perror("write() failed");
+                                       return -1;
+                               }
+                       } else {
+                               if (!write_sync(STDOUT_FILENO, msg, len)) {
+                                       perror("write() failed");
+                                       return -1;
+                               }
                        }
                }
        } while (received_signal == 0);
@@ -212,12 +305,12 @@ int main(int argc, char **argv)
 int main(int argc, char **argv)
 {
        struct termios attr;
-       int domid;
-       char *sopt = "h";
+       char *sopt = "ht";
        int ch;
        int opt_ind=0;
        struct option lopt[] = {
                { "help",    0, 0, 'h' },
+               { "timestamp", 0, 0, 't' },
                { 0 },
 
        };
@@ -231,6 +324,9 @@ int main(int argc, char **argv)
                case 'h':
                        usage(argv[0]);
                        exit(0);
+                       break;
+               case 't':
+                       log_timestamp = 1;
                        break;
                }
        }


_______________________________________________
Xen-devel mailing list
Xen-devel@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-devel


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.