aboutsummaryrefslogtreecommitdiffstats
path: root/util.c
blob: 521f73ade71f3ec23757c077d568d49396978586 (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
64
65
66
67
/* See LICENSE file for copyright and license details. */
#include "common.h"


static FILE *trace_fp;
static char last_char = '\n';
static pid_t last_pid = 0;
static int multiproctrace;


void
setup_trace_output(FILE *fp, int multiprocess)
{
	multiproctrace = multiprocess;
	trace_fp = fp;
}


void
tprintf(struct process *proc, const char *fmt, ...)
{
	va_list ap;
	if (fmt[0] == '\n' && fmt[1]) {
		last_pid = 0;
		fmt = &fmt[1];
	}
	if (multiproctrace) {
		if (proc->thread_group_leader) {
			if (last_char == '\n')
				fprintf(trace_fp, "[%ju, %ju] ", (uintmax_t)proc->thread_group_leader, (uintmax_t)proc->pid);
			else if (proc->pid != last_pid)
				fprintf(trace_fp, "\n[%ju, %ju] ", (uintmax_t)proc->thread_group_leader, (uintmax_t)proc->pid);
		} else {
			if (last_char == '\n')
				fprintf(trace_fp, "[%ju] ", (uintmax_t)proc->pid);
			else if (proc->pid != last_pid)
				fprintf(trace_fp, "\n[%ju] ", (uintmax_t)proc->pid);
		}
	}
	va_start(ap, fmt);
	vfprintf(trace_fp, fmt, ap);
	last_pid = proc->pid;
	last_char = strchr(fmt, '\0')[-1];
	va_end(ap);
}


_Noreturn void
eprintf(const char *fmt, ...)
{
	va_list ap;
	va_start(ap, fmt);
	fprintf(stderr, "%s%s: ", last_char == '\n' ? "" : "\n", argv0);
	vfprintf(stderr, fmt, ap);
	switch (strchr(fmt, '\0')[-1]) {
	case ':':
		fprintf(stderr, " %s\n", strerror(errno));
		break;
	case '\n':
		break;
	default:
		fprintf(stderr, "\n");
		break;
	}
	va_end(ap);
	exit(1);
}