Changeset 128


Ignore:
Timestamp:
Feb 2, 2006 7:15:03 PM (18 years ago)
Author:
bennylp
Message:

Added framework for attaching custom error message handler

Location:
pjproject/trunk/pjlib
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • pjproject/trunk/pjlib/include/pj/errno.h

    r66 r128  
    107107                               char *buf, pj_size_t bufsize); 
    108108 
     109/** 
     110 * Register strerror message handler for the specified error space. 
     111 * Application can register its own handler to supply the error message 
     112 * for the specified error code range. This handler will be called 
     113 * by #pj_strerror(). 
     114 * 
     115 * @param start_code    The starting error code where the handler should 
     116 *                      be called to retrieve the error message. 
     117 * @param err_space     The size of error space. The error code range then 
     118 *                      will fall in start_code to start_code+err_space-1 
     119 *                      range. 
     120 * @param f             The handler to be called when #pj_strerror() is 
     121 *                      supplied with error code that falls into this range. 
     122 * 
     123 * @return              PJ_SUCCESS or the specified error code. The  
     124 *                      registration may fail when the error space has been 
     125 *                      occupied by other handler, or when there are too many 
     126 *                      handlers registered to PJLIB. 
     127 */ 
     128PJ_DECL(pj_status_t) pj_register_strerror(pj_status_t start_code, 
     129                                          pj_status_t err_space, 
     130                                          pj_str_t (*f)(pj_status_t,char*, 
     131                                                        pj_size_t)); 
    109132 
    110133/** 
  • pjproject/trunk/pjlib/src/pj/errno.c

    r66 r128  
    1919#include <pj/errno.h> 
    2020#include <pj/string.h> 
     21#include <pj/assert.h> 
    2122#include <pj/compat/sprintf.h> 
    2223 
     
    2627extern int platform_strerror( pj_os_err_type code,  
    2728                              char *buf, pj_size_t bufsize ); 
     29 
     30#define PJLIB_MAX_ERR_MSG_HANDLER   8 
     31 
     32/* Error message handler. */ 
     33static unsigned err_msg_hnd_cnt; 
     34static struct err_msg_hnd 
     35{ 
     36    pj_status_t     begin; 
     37    pj_status_t     end; 
     38    pj_str_t      (*strerror)(pj_status_t, char*, pj_size_t); 
     39 
     40} err_msg_hnd[PJLIB_MAX_ERR_MSG_HANDLER]; 
    2841 
    2942/* PJLIB's own error codes/messages */ 
     
    7790} 
    7891 
     92#define IN_RANGE(val,start,end)     ((val)>=(start) && (val)<(end)) 
     93 
     94/* Register strerror handle. */ 
     95PJ_DECL(pj_status_t) pj_register_strerror(pj_status_t start, 
     96                                          pj_status_t space, 
     97                                          pj_str_t (*f)(pj_status_t,char*, 
     98                                                        pj_size_t)) 
     99{ 
     100    unsigned i; 
     101 
     102    /* Check arguments. */ 
     103    PJ_ASSERT_RETURN(start && space && f, PJ_EINVAL); 
     104 
     105    /* Check if there aren't too many handlers registered. */ 
     106    PJ_ASSERT_RETURN(err_msg_hnd_cnt < PJ_ARRAY_SIZE(err_msg_hnd), 
     107                     PJ_ETOOMANY); 
     108 
     109    /* Start error must be greater than PJ_ERRNO_START_USER */ 
     110    PJ_ASSERT_RETURN(start >= PJ_ERRNO_START_USER, PJ_EEXISTS); 
     111 
     112    /* Check that no existing handler has covered the specified range. */ 
     113    for (i=0; i<err_msg_hnd_cnt; ++i) { 
     114        if (IN_RANGE(start, err_msg_hnd[i].begin, err_msg_hnd[i].end) || 
     115            IN_RANGE(start+space-1, err_msg_hnd[i].begin, err_msg_hnd[i].end)) 
     116        { 
     117            return PJ_EEXISTS; 
     118        } 
     119    } 
     120 
     121    /* Register the handler. */ 
     122    err_msg_hnd[err_msg_hnd_cnt].begin = start; 
     123    err_msg_hnd[err_msg_hnd_cnt].end = start + space; 
     124    err_msg_hnd[err_msg_hnd_cnt].strerror = f; 
     125 
     126    ++err_msg_hnd_cnt; 
     127 
     128    return PJ_SUCCESS; 
     129} 
     130 
    79131/* 
    80132 * pj_strerror() 
     
    85137    int len = -1; 
    86138    pj_str_t errstr; 
     139 
     140    pj_assert(buf && bufsize); 
    87141 
    88142    if (statcode < PJ_ERRNO_START + PJ_ERRNO_SPACE_SIZE) { 
     
    95149        len = platform_strerror(PJ_STATUS_TO_OS(statcode), buf, bufsize); 
    96150 
    97     } else if (statcode < PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE) { 
    98         len = pj_snprintf( buf, bufsize, "User error %d", statcode); 
     151    } else { 
     152        unsigned i; 
    99153 
    100     } else { 
    101         len = pj_snprintf( buf, bufsize, "Invalid error %d", statcode); 
    102  
     154        /* Find user handler to get the error message. */ 
     155        for (i=0; i<err_msg_hnd_cnt; ++i) { 
     156            if (IN_RANGE(statcode, err_msg_hnd[i].begin, err_msg_hnd[i].end)) { 
     157                return (*err_msg_hnd[i].strerror)(statcode, buf, bufsize); 
     158            } 
     159        } 
    103160    } 
    104161 
Note: See TracChangeset for help on using the changeset viewer.