10. Msgno

The msgno(3m) module provides a mechanism for managing error codes (or more generically message numbers) and associated messages across separate C libraries. This is very similar to the com_err library but with runtime message registration and stack-trace-like output.

Each participating library registers a table of messages at runtime with the msgno_add_codes function. Several macros are provided to dispatch messages (e.g. print to stderr). When a library is compiled with other packages that use also use msgno and GNU cpp is used (for variatic macros), the following stack trace like output may be generated:

  src/expatls.c:97:utf8tods: Character encoding error
    src/expatls.c:449:start_fn: 
    src/dom.c:405:DOM_Element_normalize: 
    dump.c:30:main: Failed to process sample.xml
  
The MSGNO macro must be set to activate msgno(3m). Currently the GNU C preprocessor(cpp) is required to support full stack-trace-like output because of the variatic macros used by this package. Note currently this code is not reentrant. A future version will accept a context object or arbitrary buffer to enable the module to be used in a threaded environment.

10.1. MSG, MNO, MNF, PMSG, PMNO, PMNF, AMSG, AMNO, AMNF

The nine msgno macros are used to initiate, append, and finally dispatch messages and formatted output to a user defined handler (default stderr).

MSG, MNO, MNF, PMSG, PMNO, PMNF, AMSG, AMNO, AMNF
Synopsis


#include <mba/msgno.h> MSG(fmt, args...) MNO(msgno) MNF(msgno, fmt, args...) /* Primary */ PMSG(fmt, args...) PMNO(msgno) PMNF(msgno, fmt, args...) /* Additional */ AMSG(fmt, args...) AMNO(msgno) AMNF(msgno, fmt, args...) extern int (*msgno_hdlr)(const char *fmt, ...); struct msgno_entry { unsigned int msgno; const char *msg; };
Description
The Primary and Additional macros (begin with P and A) do not dispatch messages to the msgno_hdlr but instead write to a global buffer. The MSG, MNO, or MNF macros will dispatch messages to the msgno_hdlr in addition to any Primary and Additional messages in the global buffer. The general flow should be to use a Primary macro to initiate a new buffered message, then use the Additional macros to append messages, and finally trigger the entire stack-trace-like message to be dispatched to the msgno_hdlr with a MSG, MNO, or MNF macro.
Table 1.
Formatted StringMessage NumberMessage Number and Formatted String
Primary message at the beginning of the message buffer PMSG - The primary message macro writes formated printf like string to the beginning of the message buffer PMNO - The primary message number macro accepts just a message number and writes the associated message the to the beginning of the message buffer PMNF - The primary message number format macro accepts a message number and a formatted printf like string and writes both the message associated with the message number and the formatted output to the beginning of the message buffer
Additional message appended to the message buffer AMSG - The additional message macro appends a formated printf like string to the message buffer AMNO - The additional message number macro accepts just a message number and appends the associated message the to the message buffer AMNF - The additional message number format macro accepts a message number and a formatted printf like string and appends both the message associated with the message number and the formatted output to the message buffer
Dispatched immediatedly to msgno_hdlr MSG - The message macro writes a formatted string to the registered msgno_hdlr MNO - THe message number macro writes the message associated with the provided number to the msgno_hdlr MNF - The message number format macro writes both the message associated with the message number and a formatted printf like string to the msgno_hdlr.

The msgno macros are designed to be the least intrusive way to place debugging information within C source code. The following is an example of how and where these macros might be used. If main calls zsvr_init calls bind and bind fails the above stack-trace-like output would be generated.

  if ((n = dec_mbsncpy(&s, sn, NULL, -1, -1, "UTF-8")) == (size_t)-1) {
  	DOM_Exception = DOM_CHARACTER_ENC_ERR;
  	PMNO(DOM_Exception);
  	return -1;
  }
  ...
  if (utf8tods(atts[i], -1, ud) == (size_t)-1) {
  	AMSG("");
  	return;
  }
  ...
  if (DOM_DocumentLS_load(doc, argv[1]) == -1 ||
  		DOM_DocumentLS_fwrite(doc, stdout) == -1) {
  	MSG("Failed to process %s", argv[1]);
  	return EXIT_FAILURE;
  }
  

10.2. Message management functions

The msgno_add_codes function
Synopsis

#include <mba/msgno.h> int msgno_add_codes(struct msgno_entry *list);
Description
The mnsgo_add_codes function registers an array of msgno_entry structures. The array must contain at least one element and the msg member of the last element must be NULL. Each msgno value must be greater than the previous value. Values will be created at runtime if not provided (e.g. all 0s becomes 0,1,2,3,..). Create macros for each message value by referencing the msgno member like the following:

  #define DOM_INDEX_SIZE_ERR              dom_codes[0].msgno
  #define DOM_DOMSTRING_SIZE_ERR          dom_codes[1].msgno
  
  struct msgno_entry dom_codes[] = {      
      { 1, "The index specified was out of range" },
      { 0, "The text size is out of range" },
      ...
      { 0, NULL }
  };
  
Returns
The msgno_add_codes function returns 0 if the operation was successful. Otherwise -1 is returned and errno is set appropriately.

The msgno_msg function
Synopsis

#include <mba/msgno.h> const char *msgno_msg(unsigned int msgno);
Description
The msgno_msg function returns the message associated with the msgno parameters that have previously been registered with msgno_add_codes. If no such message has been registered, then the message "No such msgno list" or "No such message in msgno list" is returned.

The msgno_hdlr_stderr function
Synopsis

#include <mba/msgno.h> int msgno_hdlr_stderr(const char *fmt, ...);
Description
The msgno_hdlr_stderr function writes msgno messages to stderr. It is the default msgno message handler. The msgno message handler may be changed by reassigning a new function that matches the signature to the msgno_hdlr function pointer.

Tip: If you are working on a Microsoft Windows MFC application, create a msgno_hdlr function like the one below that calls AfxMessageBox, set it to msgno_hdlr in InitInstance, and compile your application with MSGNO defined. This will permit your MFC application to report errors generated from within libmba.

  static int
  MessageBoxHdlr(const char *fmt, ...)
  {
  	char mbs[4096];
  	wchar_t wcs[4096];
  	va_list ap;
  	va_start(ap, fmt);
  
  	_vsnprintf(mbs, 4096, fmt, ap);
  	if (mbstowcs(wcs, mbs, 4096) != (size_t)-1) {
  		AfxMessageBox(wcs);
  	}
  
  	va_end(ap);
  	return 0;
  }
  BOOL CWutApp::InitInstance()
  {
  	...
  	msgno_hdlr = MessageBoxHdlr;
  
Returns
The msgno_hdlr_stderr function (i.e. the msgno_hdlr function) returns the number of characters printed to stderr.


Copyright 2002 Michael B. Allen <mba2000 ioplex.com>