#include <string.h>
#include <getopt.h>
+#include <json_writer.h>
#include "lnstat.h"
static struct option opts[] = {
{ "keys", 1, NULL, 'k' },
{ "subject", 1, NULL, 's' },
{ "width", 1, NULL, 'w' },
+ { "oneline", 0, NULL, 0 },
};
static int usage(char *name, int exit_code)
{
fprintf(stderr, "%s Version %s\n", name, LNSTAT_VERSION);
- fprintf(stderr, "Copyright (C) 2004 by Harald Welte "
- "<laforge@gnumonks.org>\n");
- fprintf(stderr, "This program is free software licensed under GNU GPLv2"
- "\nwith ABSOLUTELY NO WARRANTY.\n\n");
+ fprintf(stderr, "Copyright (C) 2004 by Harald Welte <laforge@gnumonks.org>\n");
+ fprintf(stderr, "This program is free software licensed under GNU GPLv2\nwith ABSOLUTELY NO WARRANTY.\n\n");
fprintf(stderr, "Parameters:\n");
fprintf(stderr, "\t-V --version\t\tPrint Version of Program\n");
fprintf(stderr, "\t-c --count <count>\t"
fprintf(stderr, "\t-i --interval <intv>\t"
"Set interval to 'intv' seconds\n");
fprintf(stderr, "\t-k --keys k,k,k,...\tDisplay only keys specified\n");
- fprintf(stderr, "\t-s --subject [0-2]\t?\n");
+ fprintf(stderr, "\t-s --subject [0-2]\tControl header printing:\n");
+ fprintf(stderr, "\t\t\t\t0 = never\n");
+ fprintf(stderr, "\t\t\t\t1 = once\n");
+ fprintf(stderr, "\t\t\t\t2 = every 20 lines (default))\n");
fprintf(stderr, "\t-w --width n,n,n,...\tWidth for each field\n");
fprintf(stderr, "\n");
static void print_json(FILE *of, const struct lnstat_file *lnstat_files,
const struct field_params *fp)
{
+ json_writer_t *jw = jsonw_new(of);
int i;
- const char *sep;
- const char *base = NULL;
- fputs("{\n", of);
+ jsonw_start_object(jw);
for (i = 0; i < fp->num; i++) {
const struct lnstat_field *lf = fp->params[i].lf;
- if (!base || lf->file->basename != base) {
- if (base) fputs("},\n", of);
- base = lf->file->basename;
- sep = "\n\t";
- fprintf(of, " \"%s\":{", base);
- }
- fprintf(of, "%s\"%s\":%lu", sep,
- lf->name, lf->result);
- sep = ",\n\t";
+ jsonw_uint_field(jw, lf->name, lf->result);
}
- fputs("}\n}\n", of);
+ jsonw_end_object(jw);
+ jsonw_destroy(&jw);
}
/* find lnstat_field according to user specification */
if (++j >= MAX_FIELDS - 1) {
fprintf(stderr,
- "WARN: MAX_FIELDS (%d) reached,"
- " truncating number of keys\n",
+ "WARN: MAX_FIELDS (%d) reached, truncating number of keys\n",
MAX_FIELDS);
goto full;
}
}
}
- full:
+full:
fps->num = j;
return 1;
}
struct field_params *fps,
int linewidth)
{
- int h,i;
+ int h, i;
static struct table_hdr th;
int ofs = 0;
- for (i = 0; i < HDR_LINES; i++) {
- th.hdr[i] = malloc(HDR_LINE_LENGTH);
- memset(th.hdr[i], 0, HDR_LINE_LENGTH);
- }
+ for (i = 0; i < HDR_LINES; i++)
+ th.hdr[i] = calloc(1, HDR_LINE_LENGTH);
for (i = 0; i < fps->num; i++) {
char *cname, *fname = fps->params[i].lf->name;
MODE_JSON,
MODE_NORMAL,
} mode = MODE_NORMAL;
- unsigned long count = 1;
+ unsigned long count = 0;
struct table_hdr *header;
static struct field_params fp;
int num_req_files = 0;
num_req_files = 1;
}
- while ((c = getopt_long(argc, argv,"Vc:djf:h?i:k:s:w:",
+ while ((c = getopt_long(argc, argv, "Vc:djpf:h?i:k:s:w:",
opts, NULL)) != -1) {
int len = 0;
char *tmp, *tok;
tok = strtok(NULL, ",")) {
if (fp.num >= MAX_FIELDS) {
fprintf(stderr,
- "WARN: too many keys"
- " requested: (%d max)\n",
+ "WARN: too many keys requested: (%d max)\n",
MAX_FIELDS);
break;
}
switch (mode) {
case MODE_DUMP:
- lnstat_dump(stderr, lnstat_files);
+ lnstat_dump(stdout, lnstat_files);
break;
case MODE_NORMAL:
if (!header)
exit(1);
- if (interval < 1 )
+ if (interval < 1)
interval = 1;
- for (i = 0; i < count; i++) {
+ for (i = 0; i < count || !count; i++) {
lnstat_update(lnstat_files);
if (mode == MODE_JSON)
print_json(stdout, lnstat_files, &fp);
else {
- if ((hdr > 1 &&
- (! (i % 20))) || (hdr == 1 && i == 0))
+ if ((hdr > 1 && !(i % 20)) ||
+ (hdr == 1 && i == 0))
print_hdr(stdout, header);
print_line(stdout, lnstat_files, &fp);
}
fflush(stdout);
- if (i < count - 1)
+ if (i < count - 1 || !count)
sleep(interval);
}
break;
return 1;
}
-