summaryrefslogtreecommitdiff
path: root/lib/table.c
diff options
context:
space:
mode:
authorBen Pfaff <blp@nicira.com>2012-01-27 12:48:21 -0800
committerBen Pfaff <blp@nicira.com>2012-02-02 16:24:51 -0800
commit8f46c9bb842d2564297939aef369b4e4baaaaa43 (patch)
tree92594bc050b6e32e364926002a3d3d3f4be9b2df /lib/table.c
parentc2158145f3c7ea1653e5fbc69389ffdaceecd143 (diff)
downloadopenvswitch-8f46c9bb842d2564297939aef369b4e4baaaaa43.tar.gz
ovsdb-client: Add optional timestamps to "monitor" command output.
Suggestion #9347. Suggested-by: Alan Shieh <ashieh@nicira.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
Diffstat (limited to 'lib/table.c')
-rw-r--r--lib/table.c43
1 files changed, 42 insertions, 1 deletions
diff --git a/lib/table.c b/lib/table.c
index 537fae97c..8141677c3 100644
--- a/lib/table.c
+++ b/lib/table.c
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2009, 2010, 2011 Nicira Networks.
+ * Copyright (c) 2009, 2010, 2011, 2012 Nicira Networks.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,6 +24,7 @@
#include "json.h"
#include "ovsdb-data.h"
#include "ovsdb-error.h"
+#include "timeval.h"
#include "util.h"
struct column {
@@ -123,6 +124,14 @@ table_set_caption(struct table *table, char *caption)
table->caption = caption;
}
+/* Turns printing a timestamp along with 'table' on or off, according to
+ * 'timestamp'. */
+void
+table_set_timestamp(struct table *table, bool timestamp)
+{
+ table->timestamp = timestamp;
+}
+
/* Adds a new column to 'table' just to the right of any existing column, with
* 'heading' as a title for the column. 'heading' must be a valid printf()
* format specifier.
@@ -212,6 +221,24 @@ table_print_table_line__(struct ds *line)
}
static void
+table_format_timestamp__(char *s, size_t size)
+{
+ time_t now = time_wall();
+ strftime(s, size, "%Y-%m-%d %H:%M:%S", localtime(&now));
+}
+
+static void
+table_print_timestamp__(const struct table *table)
+{
+ if (table->timestamp) {
+ char s[32];
+
+ table_format_timestamp__(s, sizeof s);
+ puts(s);
+ }
+}
+
+static void
table_print_table__(const struct table *table, const struct table_style *style)
{
static int n = 0;
@@ -223,6 +250,8 @@ table_print_table__(const struct table *table, const struct table_style *style)
putchar('\n');
}
+ table_print_timestamp__(table);
+
if (table->caption) {
puts(table->caption);
}
@@ -286,6 +315,8 @@ table_print_list__(const struct table *table, const struct table_style *style)
putchar('\n');
}
+ table_print_timestamp__(table);
+
if (table->caption) {
puts(table->caption);
}
@@ -357,6 +388,8 @@ table_print_html__(const struct table *table, const struct table_style *style)
{
size_t x, y;
+ table_print_timestamp__(table);
+
fputs("<table border=1>\n", stdout);
if (table->caption) {
@@ -427,6 +460,8 @@ table_print_csv__(const struct table *table, const struct table_style *style)
putchar('\n');
}
+ table_print_timestamp__(table);
+
if (table->caption) {
puts(table->caption);
}
@@ -465,6 +500,12 @@ table_print_json__(const struct table *table, const struct table_style *style)
if (table->caption) {
json_object_put_string(json, "caption", table->caption);
}
+ if (table->timestamp) {
+ char s[32];
+
+ table_format_timestamp__(s, sizeof s);
+ json_object_put_string(json, "time", s);
+ }
headings = json_array_create_empty();
for (x = 0; x < table->n_columns; x++) {