msgno(3m)

NAME
msgno - manage error codes and associated messages across separate libraries

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;
};

int msgno_add_codes(struct msgno_entry *list);
const char *msgno_msg(unsigned int msgno);
int msgno_hdlr_stderr(const char *fmt, ...);

DESCRIPTION
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.

add_codes
    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 }
    };

msg 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.

hdlr_stderr
    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

add_codes
    The msgno_add_codes function returns 0 if the operation was successful. Otherwise -1 is returned and errno is set appropriately.

hdlr_stderr
    The msgno_hdlr_stderr function (i.e. the msgno_hdlr function) returns the number of characters printed to stderr.