/*
- * Copyright (c) 2009, 2010, 2011 Nicira Networks.
+ * Copyright (c) 2009, 2010, 2011, 2012 Nicira, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
#include "table.h"
-#include <assert.h>
-
#include "dynamic-string.h"
#include "json.h"
#include "ovsdb-data.h"
#include "ovsdb-error.h"
+#include "timeval.h"
#include "util.h"
struct column {
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.
struct column *column;
va_list args;
- assert(!table->n_rows);
+ ovs_assert(!table->n_rows);
if (table->n_columns >= table->allocated_columns) {
table->columns = x2nrealloc(table->columns, &table->allocated_columns,
sizeof *table->columns);
{
size_t x, y;
- assert(table->n_rows > 0);
- assert(table->current_column < table->n_columns);
+ ovs_assert(table->n_rows > 0);
+ ovs_assert(table->current_column < table->n_columns);
x = table->current_column++;
y = table->n_rows - 1;
ds_clear(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", gmtime(&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)
{
putchar('\n');
}
+ table_print_timestamp__(table);
+
if (table->caption) {
puts(table->caption);
}
putchar('\n');
}
+ table_print_timestamp__(table);
+
if (table->caption) {
puts(table->caption);
}
{
size_t x, y;
+ table_print_timestamp__(table);
+
fputs("<table border=1>\n", stdout);
if (table->caption) {
putchar('\n');
}
+ table_print_timestamp__(table);
+
if (table->caption) {
puts(table->caption);
}
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++) {
const struct cell *cell = table_cell__(table, y, x);
if (cell->text) {
json_array_add(row, json_string_create(cell->text));
- } else {
+ } else if (cell->json) {
json_array_add(row, json_clone(cell->json));
+ } else {
+ json_array_add(row, json_null_create());
}
}
json_array_add(data, row);