aboutsummaryrefslogtreecommitdiffstats
path: root/Sources/Shared/Logging/test_ringlogger.c
blob: ae3f4a93d160fd2a5cd8c00ea4a92484fe82dfd4 (plain) (blame)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include "ringlogger.h"
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <inttypes.h>
#include <sys/wait.h>

static void forkwrite(void)
{
	struct log *log = open_log("/tmp/test_log");
	char c[512];
	int i, base;
	bool in_fork = !fork();

	base = 10000 * in_fork;
	for (i = 0; i < 1024; ++i) {
		snprintf(c, 512, "bla bla bla %d", base + i);
		write_msg_to_log(log, "HMM", c);
	}


	if (in_fork)
		_exit(0);
	wait(NULL);

	write_log_to_file("/dev/stdout", log);
	close_log(log);
}

static void writetext(const char *text)
{
	struct log *log = open_log("/tmp/test_log");
	write_msg_to_log(log, "TXT", text);
	close_log(log);
}

static void show_line(const char *line, uint64_t time_ns)
{
	printf("%" PRIu64 ": %s\n", time_ns, line);
}

static void follow(void)
{
	uint32_t cursor = -1;
	struct log *log = open_log("/tmp/test_log");

	for (;;) {
		cursor = view_lines_from_cursor(log, cursor, show_line);
		usleep(1000 * 300);
	}
}

int main(int argc, char *argv[])
{
	if (!strcmp(argv[1], "fork"))
		forkwrite();
	else if (!strcmp(argv[1], "write"))
		writetext(argv[2]);
	else if (!strcmp(argv[1], "follow"))
		follow();
	return 0;
}