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