aboutsummaryrefslogtreecommitdiff
path: root/share/man/man9/printf.9
blob: 4d3a6fb8b2a51c13c166eabb34499537a0f24794 (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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
.\"
.\" Copyright (c) 2001 Andrew R. Reiter
.\" Copyright (c) 2004 Joerg Wunsch
.\" All rights reserved.
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.\" are met:
.\" 1. Redistributions of source code must retain the above copyright
.\"    notice, this list of conditions and the following disclaimer.
.\" 2. Redistributions in binary form must reproduce the above copyright
.\"    notice, this list of conditions and the following disclaimer in the
.\"    documentation and/or other materials provided with the distribution.
.\"
.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
.\" IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
.\" OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
.\" IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
.\" INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
.\" BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
.\" LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
.\" AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
.\" OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $FreeBSD$
.\"
.Dd May 9, 2020
.Dt PRINTF 9
.Os
.Sh NAME
.Nm printf ,
.Nm uprintf ,
.Nm tprintf ,
.Nm log
.Nd formatted output conversion
.Sh SYNOPSIS
.In sys/types.h
.In sys/systm.h
.Ft int
.Fn printf "const char *fmt" ...
.Ft void
.Fn tprintf "struct proc *p" "int pri" "const char *fmt" ...
.Ft int
.Fn uprintf "const char *fmt" ...
.Ft int
.Fn vprintf "const char *fmt" "va_list ap"
.In sys/syslog.h
.Ft void
.Fn log "int pri" "const char *fmt" ...
.Ft void
.Fn vlog "int pri" "const char *fmt" "va_list ap"
.Sh DESCRIPTION
The
.Nm
family of functions are similar to the
.Xr printf 3
family of functions.
The different functions each use a different output stream.
The
.Fn uprintf
function outputs to the current process' controlling tty, while
.Fn printf
writes to the console as well as to the logging facility.
The
.Fn tprintf
function outputs to the tty associated with the process
.Fa p
and the logging facility if
.Fa pri
is not \-1.
The
.Fn log
function sends the message to the kernel logging facility, using
the log level as indicated by
.Fa pri ,
and to the console if no process is yet reading the log.
.Pp
Each of these related functions use the
.Fa fmt
parameter in the same manner as
.Xr printf 3 .
However,
.Nm
adds two other conversion specifiers and omits one.
.Pp
The
.Cm \&%b
identifier expects two arguments: an
.Vt int
and a
.Vt "char *" .
These are used as a register value and a print mask for decoding bitmasks.
The print mask is made up of two parts: the base and the
arguments.
The base value is the output base expressed as an integer value;
for example, \e10 gives octal and \e20 gives hexadecimal.
The arguments are made up of a sequence of bit identifiers.
Each bit identifier begins with an integer value which is the number of the
bit (starting from 1) this identifier describes.
The rest of the identifier is a string of characters containing the name of
the bit.
The string is terminated by either the bit number at the start of the next
bit identifier or
.Dv NUL
for the last bit identifier.
.Pp
The
.Cm \&%D
identifier is meant to assist in hexdumps.
It requires two arguments: a
.Vt "u_char *"
pointer and a
.Vt "char *"
string.
The memory pointed to by the pointer is output in hexadecimal one byte at
a time.
The string is used as a delimiter between individual bytes.
If present, a width directive will specify the number of bytes to display.
By default, 16 bytes of data are output.
.Pp
The
.Cm \&%n
conversion specifier is not supported.
.Pp
The
.Fn log
function uses
.Xr syslog 3
level values
.Dv LOG_DEBUG
through
.Dv LOG_EMERG
for its
.Fa pri
parameter (mistakenly called
.Sq priority
here).
Alternatively, if a
.Fa pri
of \-1 is given, the message will be appended to the last log message
started by a previous call to
.Fn log .
As these messages are generated by the kernel itself, the facility will
always be
.Dv LOG_KERN .
.Sh RETURN VALUES
The
.Fn printf
and the
.Fn uprintf
functions return the number of characters displayed.
.Sh EXAMPLES
This example demonstrates the use of the
.Cm \&%b
and
.Cm \&%D
conversion specifiers.
The function
.Bd -literal -offset indent
void
printf_test(void)
{

	printf("reg=%b\en", 3, "\e10\e2BITTWO\e1BITONE");
	printf("out: %4D\en", "AAAA", ":");
}
.Ed
.Pp
will produce the following output:
.Bd -literal -offset indent
reg=3<BITTWO,BITONE>
out: 41:41:41:41
.Ed
.Pp
The call
.Bd -literal -offset indent
log(LOG_DEBUG, "%s%d: been there.\en", sc->sc_name, sc->sc_unit);
.Ed
.Pp
will add the appropriate debug message at priority
.Dq Li kern.debug
to the system log.
.Sh SEE ALSO
.Xr printf 3 ,
.Xr syslog 3