1 | /** @file
|
---|
2 | * IPRT - Message Formatting.
|
---|
3 | */
|
---|
4 |
|
---|
5 | /*
|
---|
6 | * Copyright (C) 2009-2022 Oracle and/or its affiliates.
|
---|
7 | *
|
---|
8 | * This file is part of VirtualBox base platform packages, as
|
---|
9 | * available from https://www.virtualbox.org.
|
---|
10 | *
|
---|
11 | * This program is free software; you can redistribute it and/or
|
---|
12 | * modify it under the terms of the GNU General Public License
|
---|
13 | * as published by the Free Software Foundation, in version 3 of the
|
---|
14 | * License.
|
---|
15 | *
|
---|
16 | * This program is distributed in the hope that it will be useful, but
|
---|
17 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
19 | * General Public License for more details.
|
---|
20 | *
|
---|
21 | * You should have received a copy of the GNU General Public License
|
---|
22 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
23 | *
|
---|
24 | * The contents of this file may alternatively be used under the terms
|
---|
25 | * of the Common Development and Distribution License Version 1.0
|
---|
26 | * (CDDL), a copy of it is provided in the "COPYING.CDDL" file included
|
---|
27 | * in the VirtualBox distribution, in which case the provisions of the
|
---|
28 | * CDDL are applicable instead of those of the GPL.
|
---|
29 | *
|
---|
30 | * You may elect to license modified versions of this file under the
|
---|
31 | * terms and conditions of either the GPL or the CDDL or both.
|
---|
32 | *
|
---|
33 | * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
|
---|
34 | */
|
---|
35 |
|
---|
36 | #ifndef IPRT_INCLUDED_message_h
|
---|
37 | #define IPRT_INCLUDED_message_h
|
---|
38 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
39 | # pragma once
|
---|
40 | #endif
|
---|
41 |
|
---|
42 | #include <iprt/cdefs.h>
|
---|
43 | #include <iprt/types.h>
|
---|
44 | #include <iprt/stdarg.h>
|
---|
45 |
|
---|
46 | RT_C_DECLS_BEGIN
|
---|
47 |
|
---|
48 | /** @defgroup grp_rt_msg RTMsg - Message Formatting
|
---|
49 | * @ingroup grp_rt
|
---|
50 | * @{
|
---|
51 | */
|
---|
52 |
|
---|
53 | /**
|
---|
54 | * Sets the program name to use.
|
---|
55 | *
|
---|
56 | * @returns IPRT status code.
|
---|
57 | * @param pszFormat The program name format string.
|
---|
58 | * @param ... Format arguments.
|
---|
59 | */
|
---|
60 | RTDECL(int) RTMsgSetProgName(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Print error message to standard error.
|
---|
64 | *
|
---|
65 | * The message will be prefixed with the file name part of process image name
|
---|
66 | * (i.e. no path) and "error: ". If the message doesn't end with a new line,
|
---|
67 | * one will be added. The caller should call this with an empty string if
|
---|
68 | * unsure whether the cursor is currently position at the start of a new line.
|
---|
69 | *
|
---|
70 | * @returns IPRT status code.
|
---|
71 | * @param pszFormat The message format string.
|
---|
72 | * @param ... Format arguments.
|
---|
73 | */
|
---|
74 | RTDECL(int) RTMsgError(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Print error message to standard error.
|
---|
78 | *
|
---|
79 | * The message will be prefixed with the file name part of process image name
|
---|
80 | * (i.e. no path) and "error: ". If the message doesn't end with a new line,
|
---|
81 | * one will be added. The caller should call this with an empty string if
|
---|
82 | * unsure whether the cursor is currently position at the start of a new line.
|
---|
83 | *
|
---|
84 | * @returns IPRT status code.
|
---|
85 | * @param pszFormat The message format string.
|
---|
86 | * @param va Format arguments.
|
---|
87 | */
|
---|
88 | RTDECL(int) RTMsgErrorV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * Same as RTMsgError() except for the return value.
|
---|
92 | *
|
---|
93 | * @returns @a enmExitCode
|
---|
94 | * @param enmExitCode What to exit code to return. This is mainly for
|
---|
95 | * saving some vertical space in the source file.
|
---|
96 | * @param pszFormat The message format string.
|
---|
97 | * @param ... Format arguments.
|
---|
98 | */
|
---|
99 | RTDECL(RTEXITCODE) RTMsgErrorExit(RTEXITCODE enmExitCode, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3);
|
---|
100 |
|
---|
101 | /**
|
---|
102 | * Same as RTMsgErrorV() except for the return value.
|
---|
103 | *
|
---|
104 | * @returns @a enmExitCode
|
---|
105 | * @param enmExitCode What to exit code to return. This is mainly for
|
---|
106 | * saving some vertical space in the source file.
|
---|
107 | * @param pszFormat The message format string.
|
---|
108 | * @param va Format arguments.
|
---|
109 | */
|
---|
110 | RTDECL(RTEXITCODE) RTMsgErrorExitV(RTEXITCODE enmExitCode, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0);
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Same as RTMsgError() except for always returning RTEXITCODE_FAILURE.
|
---|
114 | *
|
---|
115 | * @returns RTEXITCODE_FAILURE
|
---|
116 | * @param pszFormat The message format string.
|
---|
117 | * @param ... Format arguments.
|
---|
118 | */
|
---|
119 | RTDECL(RTEXITCODE) RTMsgErrorExitFailure(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
|
---|
120 |
|
---|
121 | /**
|
---|
122 | * Same as RTMsgErrorV() except for always returning RTEXITCODE_FAILURE.
|
---|
123 | *
|
---|
124 | * @returns RTEXITCODE_FAILURE
|
---|
125 | * @param pszFormat The message format string.
|
---|
126 | * @param va Format arguments.
|
---|
127 | */
|
---|
128 | RTDECL(RTEXITCODE) RTMsgErrorExitFailureV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Same as RTMsgError() except for the return value.
|
---|
132 | *
|
---|
133 | * @returns @a rcRet
|
---|
134 | * @param rcRet What IPRT status to return. This is mainly for
|
---|
135 | * saving some vertical space in the source file.
|
---|
136 | * @param pszFormat The message format string.
|
---|
137 | * @param ... Format arguments.
|
---|
138 | */
|
---|
139 | RTDECL(int) RTMsgErrorRc(int rcRet, const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(2, 3);
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Same as RTMsgErrorV() except for the return value.
|
---|
143 | *
|
---|
144 | * @returns @a rcRet
|
---|
145 | * @param rcRet What IPRT status to return. This is mainly for
|
---|
146 | * saving some vertical space in the source file.
|
---|
147 | * @param pszFormat The message format string.
|
---|
148 | * @param va Format arguments.
|
---|
149 | */
|
---|
150 | RTDECL(int) RTMsgErrorRcV(int rcRet, const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(2, 0);
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Print an error message for a RTR3Init failure and suggest an exit code.
|
---|
154 | *
|
---|
155 | * @code
|
---|
156 | *
|
---|
157 | * int rc = RTR3Init();
|
---|
158 | * if (RT_FAILURE(rc))
|
---|
159 | * return RTMsgInitFailure(rc);
|
---|
160 | *
|
---|
161 | * @endcode
|
---|
162 | *
|
---|
163 | * @returns Appropriate exit code.
|
---|
164 | * @param rcRTR3Init The status code returned by RTR3Init.
|
---|
165 | */
|
---|
166 | RTDECL(RTEXITCODE) RTMsgInitFailure(int rcRTR3Init);
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Print informational message to standard error.
|
---|
170 | *
|
---|
171 | * The message will be prefixed with the file name part of process image name
|
---|
172 | * (i.e. no path) and "warning: ". If the message doesn't end with a new line,
|
---|
173 | * one will be added. The caller should call this with an empty string if
|
---|
174 | * unsure whether the cursor is currently position at the start of a new line.
|
---|
175 | *
|
---|
176 | * @returns IPRT status code.
|
---|
177 | * @param pszFormat The message format string.
|
---|
178 | * @param ... Format arguments.
|
---|
179 | */
|
---|
180 | RTDECL(int) RTMsgWarning(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Print informational message to standard error.
|
---|
184 | *
|
---|
185 | * The message will be prefixed with the file name part of process image name
|
---|
186 | * (i.e. no path) and "warning: ". If the message doesn't end with a new line,
|
---|
187 | * one will be added. The caller should call this with an empty string if
|
---|
188 | * unsure whether the cursor is currently position at the start of a new line.
|
---|
189 | *
|
---|
190 | * @returns IPRT status code.
|
---|
191 | * @param pszFormat The message format string.
|
---|
192 | * @param va Format arguments.
|
---|
193 | */
|
---|
194 | RTDECL(int) RTMsgWarningV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Print informational message to standard output.
|
---|
198 | *
|
---|
199 | * The message will be prefixed with the file name part of process image name
|
---|
200 | * (i.e. no path) and "info: ". If the message doesn't end with a new line,
|
---|
201 | * one will be added. The caller should call this with an empty string if
|
---|
202 | * unsure whether the cursor is currently position at the start of a new line.
|
---|
203 | *
|
---|
204 | * @returns IPRT status code.
|
---|
205 | * @param pszFormat The message format string.
|
---|
206 | * @param ... Format arguments.
|
---|
207 | */
|
---|
208 | RTDECL(int) RTMsgInfo(const char *pszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Print informational message to standard output.
|
---|
212 | *
|
---|
213 | * The message will be prefixed with the file name part of process image name
|
---|
214 | * (i.e. no path) and "info: ". If the message doesn't end with a new line,
|
---|
215 | * one will be added. The caller should call this with an empty string if
|
---|
216 | * unsure whether the cursor is currently position at the start of a new line.
|
---|
217 | *
|
---|
218 | * @returns IPRT status code.
|
---|
219 | * @param pszFormat The message format string.
|
---|
220 | * @param va Format arguments.
|
---|
221 | */
|
---|
222 | RTDECL(int) RTMsgInfoV(const char *pszFormat, va_list va) RT_IPRT_FORMAT_ATTR(1, 0);
|
---|
223 |
|
---|
224 |
|
---|
225 |
|
---|
226 | /** @defgroup grp_rt_msg_refentry Help generated from refentry/manpage.
|
---|
227 | *
|
---|
228 | * The refentry/manpage docbook source in doc/manual/en_US/man_* is processed by
|
---|
229 | * doc/manual/docbook-refentry-to-C-help.xsl and turned a set of the structures
|
---|
230 | * defined here.
|
---|
231 | *
|
---|
232 | * @{
|
---|
233 | */
|
---|
234 |
|
---|
235 | /** The non-breaking space character.
|
---|
236 | * @remarks We could've used U+00A0, but it is easier both to encode and to
|
---|
237 | * search and replace a single ASCII character. */
|
---|
238 | #define RTMSGREFENTRY_NBSP '\b'
|
---|
239 |
|
---|
240 | /** @name REFENTRYSTR_SCOPE_XXX - Common string scoping and flags.
|
---|
241 | * @{ */
|
---|
242 | /** Same scope as previous string table entry, flags are reset and can be
|
---|
243 | * ORed in. */
|
---|
244 | #define RTMSGREFENTRYSTR_SCOPE_SAME UINT64_C(0)
|
---|
245 | /** Global scope. */
|
---|
246 | #define RTMSGREFENTRYSTR_SCOPE_GLOBAL UINT64_C(0x0fffffffffffffff)
|
---|
247 | /** Scope mask. */
|
---|
248 | #define RTMSGREFENTRYSTR_SCOPE_MASK UINT64_C(0x0fffffffffffffff)
|
---|
249 | /** Flags mask. */
|
---|
250 | #define RTMSGREFENTRYSTR_FLAGS_MASK UINT64_C(0xf000000000000000)
|
---|
251 | /** Command synopsis, special hanging indent rules applies. */
|
---|
252 | #define RTMSGREFENTRYSTR_FLAGS_SYNOPSIS RT_BIT_64(63)
|
---|
253 | /** @} */
|
---|
254 |
|
---|
255 | /** String table entry for a refentry. */
|
---|
256 | typedef struct RTMSGREFENTRYSTR
|
---|
257 | {
|
---|
258 | /** The scope of the string. There are two predefined scopes,
|
---|
259 | * REFENTRYSTR_SCOPE_SAME and REFENTRYSTR_SCOPE_GLOBAL. The rest are
|
---|
260 | * reference entry specific. */
|
---|
261 | uint64_t fScope;
|
---|
262 | /** The string. Non-breaking space is represented by the char
|
---|
263 | * REFENTRY_NBSP defines, just in case the string needs wrapping. There is
|
---|
264 | * no trailing newline, that's implicit. */
|
---|
265 | const char *psz;
|
---|
266 | } RTMSGREFENTRYSTR;
|
---|
267 | /** Pointer to a read-only string table entry. */
|
---|
268 | typedef const RTMSGREFENTRYSTR *PCRTMSGREFENTRYSTR;
|
---|
269 |
|
---|
270 | /** Refentry string table. */
|
---|
271 | typedef struct RTMSGREFENTRYSTRTAB
|
---|
272 | {
|
---|
273 | /** Number of strings. */
|
---|
274 | uint16_t cStrings;
|
---|
275 | /** Reserved for future use. */
|
---|
276 | uint16_t fReserved;
|
---|
277 | /** Pointer to the string table. */
|
---|
278 | PCRTMSGREFENTRYSTR paStrings;
|
---|
279 | } RTMSGREFENTRYSTRTAB;
|
---|
280 | /** Pointer to a read-only string table. */
|
---|
281 | typedef RTMSGREFENTRYSTRTAB const *PCRTMSGREFENTRYSTRTAB;
|
---|
282 |
|
---|
283 | /**
|
---|
284 | * Help extracted from a docbook refentry document.
|
---|
285 | */
|
---|
286 | typedef struct RTMSGREFENTRY
|
---|
287 | {
|
---|
288 | /** Internal reference entry identifier. */
|
---|
289 | int64_t idInternal;
|
---|
290 | /** Usage synopsis. */
|
---|
291 | RTMSGREFENTRYSTRTAB Synopsis;
|
---|
292 | /** Full help. */
|
---|
293 | RTMSGREFENTRYSTRTAB Help;
|
---|
294 | /** Brief command description. */
|
---|
295 | const char *pszBrief;
|
---|
296 | } RTMSGREFENTRY;
|
---|
297 | /** Pointer to a read-only refentry help extract structure. */
|
---|
298 | typedef RTMSGREFENTRY const *PCRTMSGREFENTRY;
|
---|
299 |
|
---|
300 |
|
---|
301 | #ifndef IPRT_INCLUDED_stream_h
|
---|
302 | typedef struct RTSTREAM *PRTSTREAM;
|
---|
303 | #endif
|
---|
304 |
|
---|
305 |
|
---|
306 | /**
|
---|
307 | * Print the synopsis to the given stream.
|
---|
308 | *
|
---|
309 | * @returns Current number of pending blank lines.
|
---|
310 | * @param pStrm The output stream.
|
---|
311 | * @param pEntry The refentry to print the help for.
|
---|
312 | */
|
---|
313 | RTDECL(int) RTMsgRefEntrySynopsis(PRTSTREAM pStrm, PCRTMSGREFENTRY pEntry);
|
---|
314 |
|
---|
315 |
|
---|
316 | /**
|
---|
317 | * Print the synopsis to the given stream.
|
---|
318 | *
|
---|
319 | * @returns Current number of pending blank lines.
|
---|
320 | * @param pStrm The output stream.
|
---|
321 | * @param pEntry The refentry to print the help for.
|
---|
322 | * @param fScope The scope inclusion mask.
|
---|
323 | * @param fFlags RTMSGREFENTRY_SYNOPSIS_F_XXX.
|
---|
324 | */
|
---|
325 | RTDECL(int) RTMsgRefEntrySynopsisEx(PRTSTREAM pStrm, PCRTMSGREFENTRY pEntry, uint64_t fScope, uint32_t fFlags);
|
---|
326 | /** @name RTMSGREFENTRY_SYNOPSIS_F_XXX - Flags for RTMsgRefEntrySynopsisEx.
|
---|
327 | * @{ */
|
---|
328 | /** Prefix the output with 'Usage:'. */
|
---|
329 | #define RTMSGREFENTRY_SYNOPSIS_F_USAGE RT_BIT_32(0)
|
---|
330 | /** @} */
|
---|
331 |
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * Print the help text to the given stream.
|
---|
335 | *
|
---|
336 | * @returns Current number of pending blank lines.
|
---|
337 | * @param pStrm The output stream.
|
---|
338 | * @param pEntry The refentry to print the help for.
|
---|
339 | */
|
---|
340 | RTDECL(int) RTMsgRefEntryHelp(PRTSTREAM pStrm, PCRTMSGREFENTRY pEntry);
|
---|
341 |
|
---|
342 | /**
|
---|
343 | * Print the help text to the given stream, extended version.
|
---|
344 | *
|
---|
345 | * @returns Current number of pending blank lines.
|
---|
346 | * @param pStrm The output stream.
|
---|
347 | * @param pEntry The refentry to print the help for.
|
---|
348 | * @param fScope The scope inclusion mask.
|
---|
349 | * @param fFlags Reserved, MBZ.
|
---|
350 | */
|
---|
351 | RTDECL(int) RTMsgRefEntryHelpEx(PRTSTREAM pStrm, PCRTMSGREFENTRY pEntry, uint64_t fScope, uint32_t fFlags);
|
---|
352 |
|
---|
353 | /**
|
---|
354 | * Prints a string table.
|
---|
355 | *
|
---|
356 | * @returns Current number of pending blank lines.
|
---|
357 | * @param pStrm The output stream.
|
---|
358 | * @param pStrTab The string table.
|
---|
359 | * @param fScope The selection scope.
|
---|
360 | * @param pcPendingBlankLines In: Pending blank lines from previous string
|
---|
361 | * table. Out: Pending blank lines.
|
---|
362 | * @param pcLinesWritten Pointer to variable that should be incremented
|
---|
363 | * by the number of lines written. Optional.
|
---|
364 | */
|
---|
365 | RTDECL(int) RTMsgRefEntryPrintStringTable(PRTSTREAM pStrm, PCRTMSGREFENTRYSTRTAB pStrTab, uint64_t fScope,
|
---|
366 | uint32_t *pcPendingBlankLines, uint32_t *pcLinesWritten);
|
---|
367 |
|
---|
368 | /** @} */
|
---|
369 |
|
---|
370 |
|
---|
371 | /** @} */
|
---|
372 |
|
---|
373 | RT_C_DECLS_END
|
---|
374 |
|
---|
375 | #endif /* !IPRT_INCLUDED_message_h */
|
---|
376 |
|
---|