Go to the first, previous, next, last section, table of contents.


How to use fmtmsg and addseverity

Here is a simple example program to illustrate the use of the both functions described in this section.

#include <fmtmsg.h>

int
main (void)
{
  addseverity (5, "NOTE2");
  fmtmsg (MM_PRINT, "only1field", MM_INFO, "text2", "action2", "tag2");
  fmtmsg (MM_PRINT, "UX:cat", 5, "invalid syntax", "refer to manual",
          "UX:cat:001");
  fmtmsg (MM_PRINT, "label:foo", 6, "text", "action", "tag");
  return 0;
}

The second call to fmtmsg illustrates a use of this function how it usually happens on System V systems which heavily use this function. It might be worth a thought to follow the scheme used in System V systems so we give a short explanation here. The value of the label field (UX:cat) says that the error occurred in the Unix program cat. The explanation of the error follows and the value for the action parameter is "refer to manual". One could me more specific here, if needed. The tag field contains, as proposed above, the value of the string given for the label parameter, and additionally a unique ID (001 in this case). For a GNU environment this string could contain a reference to the corresponding node in the Info page for the program.

Running this program without specifying the MSGVERB and SEV_LEVEL function produces the following output:

UX:cat: NOTE2: invalid syntax
TO FIX: refer to manual UX:cat:001

We see the different fields of the message and how the extra glue (the colons and the TO FIX string) are printed. But only one of the three calls to fmtmsg produced output. The first call does not print anything because the label parameter is not in the correct form. The string must contain two fields, separated by a colon (see section Printing Formatted Messages). The third fmtmsg call produced no output since the class with the numeric value 6 is not defined. Although a class with numeric value 5 is also not defined by default, the call the addseverity introduces it and the second call to fmtmsg produces the above output.

When we change the environment of the program to contain SEV_LEVEL=XXX,6,NOTE when running it we get a different result:

UX:cat: NOTE2: invalid syntax
TO FIX: refer to manual UX:cat:001
label:foo: NOTE: text
TO FIX: action tag

Now the third call the fmtmsg produced some output and we see how the string NOTE from the environment variable appears in the message.

Now we can reduce the output by specifying in which fields we are interested in. If we additionally set the environment variable MSGVERB to the value severity:label:action we get the following output:

UX:cat: NOTE2
TO FIX: refer to manual
label:foo: NOTE
TO FIX: action

I.e., the output produced by the text and the tag parameters to fmtmsg vanished. Please also note that now there is no colon after the NOTE and NOTE2 strings in the output. This is not necessary since there is no more output on this line since the text is missing.


Go to the first, previous, next, last section, table of contents.