From 56d5364fced642aa203bd74b9d92798985d3ca5c Mon Sep 17 00:00:00 2001 From: Kieran Bingham Date: Thu, 17 Sep 2020 15:04:49 +0100 Subject: src: monotonic-ts: Monotonic timestamp logging Introduce a new utility which prefixes a monotonic timestamp rendered in the same format as the kernel logs to all lines fed in through stdin. Signed-off-by: Kieran Bingham Reviewed-by: Laurent Pinchart Signed-off-by: Laurent Pinchart --- src/.gitignore | 1 + src/Makefile | 9 +++++++-- src/monotonic-ts.c | 36 ++++++++++++++++++++++++++++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) create mode 100644 src/monotonic-ts.c (limited to 'src') diff --git a/src/.gitignore b/src/.gitignore index d42c924..817142c 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -2,3 +2,4 @@ *.o gen-image +monotonic-ts diff --git a/src/Makefile b/src/Makefile index d7f901f..c40e95a 100644 --- a/src/Makefile +++ b/src/Makefile @@ -7,18 +7,23 @@ CFLAGS ?= -O0 -g -W -Wall -Wno-unused-parameter -Iinclude LDFLAGS ?= LIBS := -lm GEN-IMAGE := gen-image +MONOTONIC-TS := monotonic-ts %.o : %.c $(CC) $(CFLAGS) -c -o $@ $< -all: $(GEN-IMAGE) +all: $(GEN-IMAGE) $(MONOTONIC-TS) $(GEN-IMAGE): gen-image.o $(CC) $(LDFLAGS) -o $@ $^ $(LIBS) +$(MONOTONIC-TS): monotonic-ts.o + $(CC) $(LDFLAGS) -o $@ $^ $(LIBS) + clean: -rm -f *.o - -rm -f $(GEN-IMAGE) + -rm -f $(GEN-IMAGE) $(MONOTONIC_TS) install: cp $(GEN-IMAGE) $(INSTALL_DIR)/ + cp $(MONOTONIC-TS) $(INSTALL_DIR)/ diff --git a/src/monotonic-ts.c b/src/monotonic-ts.c new file mode 100644 index 0000000..69a1809 --- /dev/null +++ b/src/monotonic-ts.c @@ -0,0 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ +/* SPDX-FileCopyrightText: 2020 Kieran Bingham */ + +#include +#include +#include + +int main(int argc, char ** argv) +{ + struct timespec tp; + char *line = NULL; + size_t size = 0; + const char *label = ""; + + if (argc > 1) + label = argv[1]; + + /* + * Explicitly set line buffering on stdin to be sure it is delivered + * in a timely fashion for our timestamping purposes when data is fed + * through a pipe. + */ + setlinebuf(stdin); + + do { + if (getline(&line, &size, stdin) <= 0) + break; + + clock_gettime(CLOCK_MONOTONIC, &tp); + printf("[%ld.%.9ld]%s %s", tp.tv_sec, tp.tv_nsec, label, line); + } while (!feof(stdin)); + + free(line); + + return 0; +} -- cgit v1.2.3