summaryrefslogtreecommitdiff
path: root/src/monotonic-ts.c
blob: 69a1809de1d18f79352f09c4713753f473739c06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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;
}