diff options
author | Kieran Bingham <kieran.bingham@ideasonboard.com> | 2020-09-17 15:04:49 +0100 |
---|---|---|
committer | Laurent Pinchart <laurent.pinchart@ideasonboard.com> | 2022-02-28 13:12:36 +0200 |
commit | 56d5364fced642aa203bd74b9d92798985d3ca5c (patch) | |
tree | f966ed80edf43004772d215bd3833cbee63da958 /src/monotonic-ts.c | |
parent | d797d5cc5fef90a9fc913b81087308815a126ce5 (diff) |
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 <kieran.bingham@ideasonboard.com>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Diffstat (limited to 'src/monotonic-ts.c')
-rw-r--r-- | src/monotonic-ts.c | 36 |
1 files changed, 36 insertions, 0 deletions
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 <kieran.bingham@ideasonboard.com> */ + +#include <stdio.h> +#include <stdlib.h> +#include <time.h> + +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; +} |