2 * IPRT - Message Formatting.
6 * Copyright (C) 2009-2024 Oracle and/or its affiliates.
8 * This file is part of VirtualBox base platform packages, as
9 * available from https://www.virtualbox.org.
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
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.
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>.
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.
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.
33 * SPDX-License-Identifier: GPL-3.0-only OR CDDL-1.0
36 #ifndef IPRT_INCLUDED_message_h
37 #define IPRT_INCLUDED_message_h
38 #ifndef RT_WITHOUT_PRAGMA_ONCE
42 #include <iprt/cdefs.h>
43 #include <iprt/types.h>
44 #include <iprt/stdarg.h>
48 /** @defgroup grp_rt_msg RTMsg - Message Formatting
54 * Sets the program name to use.
56 * @returns IPRT status code.
57 * @param pszFormat The program name format string.
58 * @param ... Format arguments.
60 RTDECL(int) RTMsgSetProgName(const char *pszFormat
, ...) RT_IPRT_FORMAT_ATTR(1, 2);
63 * Print error message to standard error.
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.
70 * @returns IPRT status code.
71 * @param pszFormat The message format string.
72 * @param ... Format arguments.
74 RTDECL(int) RTMsgError(const char *pszFormat
, ...) RT_IPRT_FORMAT_ATTR(1, 2);
77 * Print error message to standard error.
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.
84 * @returns IPRT status code.
85 * @param pszFormat The message format string.
86 * @param va Format arguments.
88 RTDECL(int) RTMsgErrorV(const char *pszFormat
, va_list va
) RT_IPRT_FORMAT_ATTR(1, 0);
91 * Same as RTMsgError() except for the return value.
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.
99 RTDECL(RTEXITCODE
) RTMsgErrorExit(RTEXITCODE enmExitCode
, const char *pszFormat
, ...) RT_IPRT_FORMAT_ATTR(2, 3);
102 * Same as RTMsgErrorV() except for the return value.
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.
110 RTDECL(RTEXITCODE
) RTMsgErrorExitV(RTEXITCODE enmExitCode
, const char *pszFormat
, va_list va
) RT_IPRT_FORMAT_ATTR(2, 0);
113 * Same as RTMsgError() except for always returning RTEXITCODE_FAILURE.
115 * @returns RTEXITCODE_FAILURE
116 * @param pszFormat The message format string.
117 * @param ... Format arguments.
119 RTDECL(RTEXITCODE
) RTMsgErrorExitFailure(const char *pszFormat
, ...) RT_IPRT_FORMAT_ATTR(1, 2);
122 * Same as RTMsgErrorV() except for always returning RTEXITCODE_FAILURE.
124 * @returns RTEXITCODE_FAILURE
125 * @param pszFormat The message format string.
126 * @param va Format arguments.
128 RTDECL(RTEXITCODE
) RTMsgErrorExitFailureV(const char *pszFormat
, va_list va
) RT_IPRT_FORMAT_ATTR(1, 0);
131 * Same as RTMsgError() except for the return value.
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.
139 RTDECL(int) RTMsgErrorRc(int rcRet
, const char *pszFormat
, ...) RT_IPRT_FORMAT_ATTR(2, 3);
142 * Same as RTMsgErrorV() except for the return value.
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.
150 RTDECL(int) RTMsgErrorRcV(int rcRet
, const char *pszFormat
, va_list va
) RT_IPRT_FORMAT_ATTR(2, 0);
153 * For reporting syntax errors.
155 * @returns RTEXITCODE_SYNTAX
156 * @param pszFormat The message format string. Newline not needed.
157 * @param ... Format arguments.
159 RTDECL(RTEXITCODE
) RTMsgSyntax(const char *pszFormat
, ...) RT_IPRT_FORMAT_ATTR(1, 2);
162 * For reporting syntax errors.
164 * @returns RTEXITCODE_SYNTAX
165 * @param pszFormat The message format string. Newline not needed.
166 * @param va Format arguments.
168 RTDECL(RTEXITCODE
) RTMsgSyntaxV(const char *pszFormat
, va_list va
) RT_IPRT_FORMAT_ATTR(1, 0);
171 * Print an error message for a RTR3Init failure and suggest an exit code.
175 * int rc = RTR3Init();
176 * if (RT_FAILURE(rc))
177 * return RTMsgInitFailure(rc);
181 * @returns Appropriate exit code.
182 * @param rcRTR3Init The status code returned by RTR3Init.
184 RTDECL(RTEXITCODE
) RTMsgInitFailure(int rcRTR3Init
);
187 * Print informational message to standard error.
189 * The message will be prefixed with the file name part of process image name
190 * (i.e. no path) and "warning: ". If the message doesn't end with a new line,
191 * one will be added. The caller should call this with an empty string if
192 * unsure whether the cursor is currently position at the start of a new line.
194 * @returns IPRT status code.
195 * @param pszFormat The message format string.
196 * @param ... Format arguments.
198 RTDECL(int) RTMsgWarning(const char *pszFormat
, ...) RT_IPRT_FORMAT_ATTR(1, 2);
201 * Print informational message to standard error.
203 * The message will be prefixed with the file name part of process image name
204 * (i.e. no path) and "warning: ". If the message doesn't end with a new line,
205 * one will be added. The caller should call this with an empty string if
206 * unsure whether the cursor is currently position at the start of a new line.
208 * @returns IPRT status code.
209 * @param pszFormat The message format string.
210 * @param va Format arguments.
212 RTDECL(int) RTMsgWarningV(const char *pszFormat
, va_list va
) RT_IPRT_FORMAT_ATTR(1, 0);
215 * Print informational message to standard output.
217 * The message will be prefixed with the file name part of process image name
218 * (i.e. no path) and "info: ". If the message doesn't end with a new line,
219 * one will be added. The caller should call this with an empty string if
220 * unsure whether the cursor is currently position at the start of a new line.
222 * @returns IPRT status code.
223 * @param pszFormat The message format string.
224 * @param ... Format arguments.
226 RTDECL(int) RTMsgInfo(const char *pszFormat
, ...) RT_IPRT_FORMAT_ATTR(1, 2);
229 * Print informational message to standard output.
231 * The message will be prefixed with the file name part of process image name
232 * (i.e. no path) and "info: ". If the message doesn't end with a new line,
233 * one will be added. The caller should call this with an empty string if
234 * unsure whether the cursor is currently position at the start of a new line.
236 * @returns IPRT status code.
237 * @param pszFormat The message format string.
238 * @param va Format arguments.
240 RTDECL(int) RTMsgInfoV(const char *pszFormat
, va_list va
) RT_IPRT_FORMAT_ATTR(1, 0);
244 /** @defgroup grp_rt_msg_refentry Help generated from refentry/manpage.
246 * The refentry/manpage docbook source in doc/manual/en_US/man_* is processed by
247 * doc/manual/docbook-refentry-to-C-help.xsl and turned a set of the structures
253 /** The non-breaking space character.
254 * @remarks We could've used U+00A0, but it is easier both to encode and to
255 * search and replace a single ASCII character. */
256 #define RTMSGREFENTRY_NBSP '\b'
258 /** @name REFENTRYSTR_SCOPE_XXX - Common string scoping and flags.
260 /** Same scope as previous string table entry, flags are reset and can be
262 #define RTMSGREFENTRYSTR_SCOPE_SAME UINT64_C(0)
264 #define RTMSGREFENTRYSTR_SCOPE_GLOBAL UINT64_C(0x0fffffffffffffff)
266 #define RTMSGREFENTRYSTR_SCOPE_MASK UINT64_C(0x0fffffffffffffff)
268 #define RTMSGREFENTRYSTR_FLAGS_MASK UINT64_C(0xf000000000000000)
269 /** Command synopsis, special hanging indent rules applies. */
270 #define RTMSGREFENTRYSTR_FLAGS_SYNOPSIS RT_BIT_64(63)
273 /** String table entry for a refentry. */
274 typedef struct RTMSGREFENTRYSTR
276 /** The scope of the string. There are two predefined scopes,
277 * REFENTRYSTR_SCOPE_SAME and REFENTRYSTR_SCOPE_GLOBAL. The rest are
278 * reference entry specific. */
280 /** The string. Non-breaking space is represented by the char
281 * REFENTRY_NBSP defines, just in case the string needs wrapping. There is
282 * no trailing newline, that's implicit. */
285 /** Pointer to a read-only string table entry. */
286 typedef const RTMSGREFENTRYSTR
*PCRTMSGREFENTRYSTR
;
288 /** Refentry string table. */
289 typedef struct RTMSGREFENTRYSTRTAB
291 /** Number of strings. */
293 /** Reserved for future use. */
295 /** Pointer to the string table. */
296 PCRTMSGREFENTRYSTR paStrings
;
297 } RTMSGREFENTRYSTRTAB
;
298 /** Pointer to a read-only string table. */
299 typedef RTMSGREFENTRYSTRTAB
const *PCRTMSGREFENTRYSTRTAB
;
302 * Help extracted from a docbook refentry document.
304 typedef struct RTMSGREFENTRY
306 /** Internal reference entry identifier. */
308 /** Usage synopsis. */
309 RTMSGREFENTRYSTRTAB Synopsis
;
311 RTMSGREFENTRYSTRTAB Help
;
312 /** Brief command description. */
313 const char *pszBrief
;
315 /** Pointer to a read-only refentry help extract structure. */
316 typedef RTMSGREFENTRY
const *PCRTMSGREFENTRY
;
319 #ifndef IPRT_INCLUDED_stream_h
320 typedef struct RTSTREAM
*PRTSTREAM
;
325 * Print the synopsis to the given stream.
327 * @returns Current number of pending blank lines.
328 * @param pStrm The output stream.
329 * @param pEntry The refentry to print the help for.
331 RTDECL(int) RTMsgRefEntrySynopsis(PRTSTREAM pStrm
, PCRTMSGREFENTRY pEntry
);
335 * Print the synopsis to the given stream.
337 * @returns Current number of pending blank lines.
338 * @param pStrm The output stream.
339 * @param pEntry The refentry to print the help for.
340 * @param fScope The scope inclusion mask.
341 * @param fFlags RTMSGREFENTRY_SYNOPSIS_F_XXX.
343 RTDECL(int) RTMsgRefEntrySynopsisEx(PRTSTREAM pStrm
, PCRTMSGREFENTRY pEntry
, uint64_t fScope
, uint32_t fFlags
);
344 /** @name RTMSGREFENTRY_SYNOPSIS_F_XXX - Flags for RTMsgRefEntrySynopsisEx.
346 /** Prefix the output with 'Usage:'. */
347 #define RTMSGREFENTRY_SYNOPSIS_F_USAGE RT_BIT_32(0)
352 * Print the help text to the given stream.
354 * @returns Current number of pending blank lines.
355 * @param pStrm The output stream.
356 * @param pEntry The refentry to print the help for.
358 RTDECL(int) RTMsgRefEntryHelp(PRTSTREAM pStrm
, PCRTMSGREFENTRY pEntry
);
361 * Print the help text to the given stream, extended version.
363 * @returns Current number of pending blank lines.
364 * @param pStrm The output stream.
365 * @param pEntry The refentry to print the help for.
366 * @param fScope The scope inclusion mask.
367 * @param fFlags Reserved, MBZ.
369 RTDECL(int) RTMsgRefEntryHelpEx(PRTSTREAM pStrm
, PCRTMSGREFENTRY pEntry
, uint64_t fScope
, uint32_t fFlags
);
372 * Prints a string table.
374 * @returns Current number of pending blank lines.
375 * @param pStrm The output stream.
376 * @param pStrTab The string table.
377 * @param fScope The selection scope.
378 * @param pcPendingBlankLines In: Pending blank lines from previous string
379 * table. Out: Pending blank lines.
380 * @param pcLinesWritten Pointer to variable that should be incremented
381 * by the number of lines written. Optional.
383 RTDECL(int) RTMsgRefEntryPrintStringTable(PRTSTREAM pStrm
, PCRTMSGREFENTRYSTRTAB pStrTab
, uint64_t fScope
,
384 uint32_t *pcPendingBlankLines
, uint32_t *pcLinesWritten
);
393 #endif /* !IPRT_INCLUDED_message_h */