summaryrefslogtreecommitdiff
path: root/timeadd.c
blob: 061a0ae82ca54f5ae0d383dca4210bf08c3f5a5f (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
/*
 * Adds a timestamp before every line.
 *
 * Author: Peter Wu <lekensteyn@gmail.com>
 * Date: 2013-10-04
 */
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <stdlib.h>

int main() {
	char buf[4096];

	while (fgets(buf, sizeof(buf), stdin) != NULL) {
		char *newline = strrchr(buf, '\n');
		struct timespec tp;

		clock_gettime(CLOCK_MONOTONIC, &tp);

		if (newline)
			*newline = '\0';

		printf("[%5llu.%06lu] %s\n",
			(long long) tp.tv_sec,
			tp.tv_nsec / 1000,
			buf);
	}

	return 0;
}