~pbatch/patchwerk

patchwerk/print.w -rw-r--r-- 1.8 KiB
9c265356 — paul plan9 additions from Sigrid 3 months ago
                                                                                
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
68
69
70
71
72
73
@** Printing (WIP).
\mkref{printing}

Printing messages to the screen is particularly useful
tool for troubleshooting. An internal printing system has
been designed to accomodate different platforms. This
is done by having an internal printing callback that can
be overridden with a user-supplied callback function.

Most of this functionality has been borrowed from the Runt
system.

@ The core print callback is called |pw_print|. It is
designed to function similarly to printf with a
|pw_patch| argument.

@<Header@>+=
void pw_print(pw_patch *p, const char *fmt, ...);

@ Variadic macro lists are used to set up the internally
stored print callback.

@<Patch Top@>+=
void pw_print(pw_patch *p, const char *fmt, ...)
{
    va_list args;
    va_start(args, fmt);
    p->print(p, fmt, args);
    va_end(args);
}

@ Stored inside of the |pw_patch| struct is the print function
pointer.

@<Variables in Patch Data@>+=
void (*print)(pw_patch *, const char *fmt, va_list);

@ By default, it is set to use |pw_print_default|, via the
function |pw_print_init|.

@<Header@>+=
void pw_print_init(pw_patch *p);

@
@<Patch Top@>+=
@<Default Print Function@>@/
void pw_print_init(pw_patch *p)
{
    p->print = pw_print_default;
}

@ The function |pw_print_default| uses |vprintf|, to get the
equivalent functionality of |printf| with variadic arguments.

@<Default Print Function@>+=
static void pw_print_default(pw_patch *p, const char *str, va_list args)
{
    vprintf(str, args);
}


@ The default print function can be overridden after
patchwerk is initialized using the function |pw_print_set|.

@<Header@>+=
void pw_print_set(pw_patch *p, void (*print)(pw_patch*,const char*, va_list));

@
@<Patch Top@>+=
void pw_print_set(pw_patch *p, void (*print)(pw_patch*,const char*, va_list))
{
    p->print = print;
}