#include <fmtmsg.h> int fmtmsg(long classification, const char *label, int severity, const char *text, const char *action, const char *tag);
The label argument identifies the source of the message. The string must consist of two colon separated parts where the first part has not more than 10 and the second part not more than 14 characters.
The text argument describes the condition of the error.
The action argument describes possible steps to recover from the error. If it is printed, it is prefixed by "TO FIX: ".
The tag argument is a reference to the online documentation where more information can be found. It should contain the label value and a unique identification number.
The first value defines the output channel.
The second value is the source of the error:
The third value encodes the detector of the problem:
The fourth value shows the severity of the incident:
The numeric values are between 0 and 4. Using addseverity(3) or the environment variable SEV_LEVEL you can add more levels and strings to print.
The environment variable SEV_LEVEL can be used to introduce new severity levels. By default, only the five severity levels described above are available. Any other numeric value would make fmtmsg() print nothing. If the user puts SEV_LEVEL with a format like
in the environment of the process before the first call to fmtmsg(), where each description is of the form
then fmtmsg() will also accept the indicated values for the level (in addition to the standard levels 0-4), and use the indicated printstring when such a level occurs.
The severity-keyword part is not used by fmtmsg() but it has to be present. The level part is a string representation of a number. The numeric value must be a number greater than 4. This value must be used in the severity argument of fmtmsg() to select this class. It is not possible to overwrite any of the predefined classes. The printstring is the string printed when a message of this class is processed by fmtmsg().
Interface | Attribute | Value |
fmtmsg() | Thread safety |
glibc >= 2.16: MT-Safe
glibc < 2.16: MT-Unsafe |
Before glibc 2.16, the fmtmsg() function uses a static variable that is not protected, so it is not thread-safe.
Since glibc 2.16, the fmtmsg() function uses a lock to protect the static variable, so it is thread-safe.
The function fmtmsg() and the environment variable MSGVERB are described in POSIX.1-2001 and POSIX.1-2008.
int
main(void)
{
long class = MM_PRINT | MM_SOFT | MM_OPSYS | MM_RECOVER;
int err;
err = fmtmsg(class, "util-linux:mount", MM_ERROR,
"unknown mount option", "See mount(8).",
"util-linux:mount:017");
switch (err) {
case MM_OK:
break;
case MM_NOTOK:
printf("Nothing printed\n");
break;
case MM_NOMSG:
printf("Nothing printed to stderr\n");
break;
case MM_NOCON:
printf("No console output\n");
break;
default:
printf("Unknown error from fmtmsg()\n");
}
exit(EXIT_SUCCESS);
}
The output should be:
util-linux:mount: ERROR: unknown mount option TO FIX: See mount(8). util-linux:mount:017
and after
MSGVERB=text:action; export MSGVERB
the output becomes:
unknown mount option TO FIX: See mount(8).