xref: /freebsd/share/man/man9/printf.9 (revision e2afbc45258f2fa4bdcf126e959ac660e76fc802)
1.\"
2.\" Copyright (c) 2001 Andrew R. Reiter
3.\" Copyright (c) 2004 Joerg Wunsch
4.\" All rights reserved.
5.\"
6.\" Redistribution and use in source and binary forms, with or without
7.\" modification, are permitted provided that the following conditions
8.\" are met:
9.\" 1. Redistributions of source code must retain the above copyright
10.\"    notice, this list of conditions and the following disclaimer.
11.\" 2. Redistributions in binary form must reproduce the above copyright
12.\"    notice, this list of conditions and the following disclaimer in the
13.\"    documentation and/or other materials provided with the distribution.
14.\"
15.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25.\" SUCH DAMAGE.
26.\"
27.Dd December 19, 2025
28.Dt PRINTF 9
29.Os
30.Sh NAME
31.Nm printf ,
32.Nm uprintf ,
33.Nm tprintf ,
34.Nm log
35.Nd formatted output conversion
36.Sh SYNOPSIS
37.In sys/types.h
38.In sys/systm.h
39.Ft int
40.Fn printf "const char *fmt" ...
41.Ft void
42.Fn tprintf "struct proc *p" "int pri" "const char *fmt" ...
43.Ft int
44.Fn uprintf "const char *fmt" ...
45.Ft int
46.Fn vprintf "const char *fmt" "va_list ap"
47.In sys/syslog.h
48.Ft void
49.Fn log "int pri" "const char *fmt" ...
50.Ft void
51.Fn vlog "int pri" "const char *fmt" "va_list ap"
52.Sh DESCRIPTION
53The
54.Nm
55family of functions are similar to the
56.Xr printf 3
57family of functions.
58The different functions each use a different output stream.
59The
60.Fn uprintf
61function outputs to the current process' controlling tty, while
62.Fn printf
63writes to the console as well as to the logging facility.
64The
65.Fn tprintf
66function outputs to the tty associated with the process
67.Fa p
68and the logging facility if
69.Fa pri
70is not \-1.
71The
72.Fn log
73function sends the message to the kernel logging facility, using
74the log level as indicated by
75.Fa pri ,
76and to the console if no process is yet reading the log.
77.Pp
78Each of these related functions use the
79.Fa fmt
80parameter in the same manner as
81.Xr printf 3 .
82However,
83.Nm
84adds two other conversion specifiers and omits one.
85.Pp
86The
87.Cm \&%b
88identifier expects two arguments: an
89.Vt int
90and a
91.Vt "char *" .
92These are used as a register value and a print mask for decoding bitmasks.
93The print mask is made up of two parts: the base and the
94arguments.
95The base value is the output base (radix) expressed as an octal value;
96for example, \e10 gives octal and \e20 gives hexadecimal.
97The arguments are made up of a sequence of bit identifiers.
98Each bit identifier begins with a character specifying the number of the bit
99(starting from 1) this identifier describes.
100The characters from \e01 to \e40 can be used to specify bit numbers in the
101range from 1 to 32 and characters from \e200 to \e377 to specify bit numbers
102in the range from 1 to 128.
103The rest of the identifier is a string of characters containing the name of
104the bit.
105The identifier is terminated by either the bit number at the start of the next
106bit identifier or by
107.Dv NUL
108for the last bit identifier.
109.Pp
110The
111.Cm \&%D
112identifier is meant to assist in hexdumps.
113It requires two arguments: a
114.Vt "u_char *"
115pointer and a
116.Vt "char *"
117string.
118The memory pointed to by the pointer is output in hexadecimal one byte at
119a time.
120The string is used as a delimiter between individual bytes.
121If present, a width directive will specify the number of bytes to display.
122By default, 16 bytes of data are output.
123.Pp
124The
125.Cm \&%n
126conversion specifier is not supported.
127.Pp
128The
129.Fn log
130function uses
131.Xr syslog 3
132level values
133.Dv LOG_DEBUG
134through
135.Dv LOG_EMERG
136for its
137.Fa pri
138parameter (mistakenly called
139.Sq priority
140here).
141Alternatively, if a
142.Fa pri
143of \-1 is given, the message will be appended to the last log message
144started by a previous call to
145.Fn log .
146As these messages are generated by the kernel itself, the facility will
147always be
148.Dv LOG_KERN .
149.Sh RETURN VALUES
150The
151.Fn printf
152and the
153.Fn uprintf
154functions return the number of characters displayed.
155.Sh EXAMPLES
156This example demonstrates the use of the
157.Cm \&%b
158and
159.Cm \&%D
160conversion specifiers.
161The function
162.Bd -literal -offset indent
163void
164printf_test(void)
165{
166	printf("reg=%b\en", 3, "\e10\e2BITTWO\e1BITONE");
167	printf("out: %4D\en", "AAZZ", ":");
168}
169.Ed
170.Pp
171will produce the following output:
172.Bd -literal -offset indent
173reg=3<BITTWO,BITONE>
174out: 41:41:5a:5a
175.Ed
176.Pp
177The same output will be generated by the following function:
178.Bd -literal -offset indent
179void
180printf_test(void)
181{
182	printf("reg=%b\en", 3, "\e10\e201BITTWO\e200BITONE");
183	printf("out: %4D\en", "AAZZ", ":");
184}
185.Ed
186.Pp
187The call
188.Bd -literal -offset indent
189log(LOG_DEBUG, "%s%d: been there.\en", sc->sc_name, sc->sc_unit);
190.Ed
191.Pp
192will add the appropriate debug message at priority
193.Dq Li kern.debug
194to the system log.
195.Sh SEE ALSO
196.Xr printf 3 ,
197.Xr syslog 3
198