utils/gprintf.c: Return the correct number of output characters.
[mLib] / trace / trace.3
CommitLineData
d1836466 1.\" -*-nroff-*-
fbf20b5b 2.TH trace 3 "21 October 1999" "Straylight/Edgeware" "mLib utilities library"
d1836466 3.SH "NAME"
4trace \- configurable tracing output
5.\" @trace
6.\" @trace_block
7.\" @trace_on
0680be67 8.\" @trace_custom
d1836466 9.\" @trace_level
10.\" @tracing
11.\" @traceopt
12.\" @NTRACE
13.\" @T
14.\" IF_TRACING
15.SH "SYNOPSIS"
16.nf
17.B "#include <mLib/trace.h>"
18
19.BI "void trace(unsigned " l ", const char *" f ", ...);"
2b1924c2
MW
20.ds mT \fBvoid trace_block(
21.BI "\*(mTunsigned " l ", const char *" s ,
22.BI "\h'\w'\*(mT'u'const void *" b ", size_t " sz );
d1836466 23
24.BI "void trace_on(FILE *" fp ", unsigned " l );
2b1924c2
MW
25.ds mT \fBvoid trace_custom(
26.ds mU \*(mTvoid (*\fIfunc\fB)(
27.BI "\*(mUconst char *" buf ,
28.BI "\h'\w'\*(mU'u'size_t " sz ", void *" v ),
29.BI "\h'\w'\*(mT'u'void *" v );
d1836466 30.BI "void trace_level(unsigned " l );
31.BI "unsigned tracing(void);"
32
2b1924c2
MW
33.ds mT \fBunsigned traceopt(
34.BI "\*(mTconst trace_opt *" t ", const char *" p ,
35.BI "\h'\w'\*(mT'u'unsigned " f ", unsigned " bad );
d1836466 36
37.BI T( statements\fR... )
38.BI "IF_TRACING(unsigned " l ", " statements\fR... )
39.fi
40.SH "DESCRIPTION"
41The
42.B <mLib/trace.h>
43header declares some functions and macros for handling trace output.
44The emphasis for this system is primarily on user configurability of
45what gets traced rather than where the output goes. That's a project
46for later.
47.SS "Trace levels"
48Each trace message is assigned a
49.I level
50by the programmer. A tracing level is set during program
51initialization, usually by the user. A trace message is output if there
52is a trace destination set, and the result of a bitwise AND between the
53message level and the overall tracing level is nonzero. The idea is
54that the programmer assigns a different bit to each group of trace
55messages, and allows a user to select which ones are wanted.
56.SS "Producing trace output"
57The basic function is
58.BR trace .
59It is passed an integer message level and a
60.BR printf (3)-style
61format string together with arguments for the placeholders and emits the
62resulting message.
63.PP
64The function
65.B trace_block
66formats an arbitrary block of memory as a hex dump. The arguments are,
67in order, the message level, a pointer to the header string for the hex
68dump, the base address of the block to dump, and the size of the block
69in bytes.
70.SS "Configuring trace output"
71The tracing destination is set with the function
72.BR trace_on :
73it is passed a
74.B stdio
75stream and a trace level to set. The stream may be null to disable
76tracing completely (which is the default). The trace level may be set
77on its own using
78.BR trace_level ,
79which takes the new level as its single argument. The function
80.B tracing
81returns the current trace level, or zero if there is no trace
82destination set.
0680be67 83.PP
84For more advanced programs, a custom output function may be provided by
85.B trace_custom
86and passing a function which will write a buffer of data somewhere
87sensible.
d1836466 88.SS "Parsing user trace options"
89The function
90.B traceopt
91may be used to allow a user to set the trace level. It is passed a
92table describing the available trace level bits, and some other
93information, and returns a new trace level. The table consists of a
94number of
95.B trace_opt
96structures, each of which describes a bit or selection of bits which may
97be controlled. The structure contains the following members, in order:
98.TP
99.B "char ch;"
100The character used to select this bit or collection of bits.
101.TP
102.B "unsigned f;"
103The level bits for this option.
104.TP
105.B "const char *help;"
106A help string describing this option.
107.PP
108The table is terminated by an entry whose
109.B ch
110member is zero.
111.PP
112The arguments to
113.B traceopt
114are:
115.TP
116.BI "trace_opt *" t
117Pointer to the trace options table.
118.TP
119.BI "const char *" p
120Pointer to the user's option string.
121.TP
122.BI "unsigned " f
123The default trace options, or the previously-set options.
124.TP
125.BI "unsigned " bad
126A bitmask of level bits which should be disallowed.
127.PP
128If the option string
129.I p
130is a null pointer or contains only a
131.RB ` ? '
132character, a help message is printed and the default is returned. Only
133trace options which contain non-bad bits are displayed. Otherwise the
134string contains option characters together with
135.RB ` + '
136and
137.RB ` \- '
138which respectively turn on or off the following options; the default is
139to turn options on. Again, only options which contain non-bad bits are
140allowed.
141.PP
142The `bad bit' mechanism is provided for setuid programs which in their
143normal configuration deal with privileged information which mustn't be
144given out to users. However, if run by someone with appropriate
145privilege such options are safe and may be useful for debugging. The
146program can set the
147.I bad
148mask to prevent access to secret information when running setuid, or to
149zero when not.
150.SS "Macro support for tracing"
151The tracing macros are intended to make insertion of tracing information
152unobtrusive and painless. If the
153.B NTRACE
154macro is defined, all of the tracing macros are disabled and generate no
155code; otherwise they do their normal jobs.
156.PP
157The
158.B T
159macro simply expands to its argument. It may be wrapped around small
160pieces of code which is only needed when compiling with tracing
161enabled. (Larger blocks, of course, should use
76809a64 162.RB ` "#ifndef NTRACE" '/` #endif '
d1836466 163pairs for clarity's sake.)
164.PP
165For slightly larger code chunks which do some processing to generate
166trace output, the
167.B IF_TRACING
168macro is useful. Its first argument is a message level; if the trace
169level is set such that the message will be printed, the code in the
170second argument is executed. If code is being compiled without tracing,
171of course, the entire contents of the macro is ignored.
172.SH "SEE ALSO"
173.BR mLib (3).
174.SH "AUTHOR"
9b5ac6ff 175Mark Wooding, <mdw@distorted.org.uk>