Ignore:
Timestamp:
Nov 9, 2009 4:09:13 AM (14 years ago)
Author:
bennylp
Message:

Updated ticket #981: pj_perror() and PJ_PERROR() API:

  • added PJ_PERROR() macro to allow compile time omitting
  • changed pj_perror() API to allow formatting of the title using printf like format
  • added a simple test in pjlib-test
  • updated Doxygen documentation
File:
1 edited

Legend:

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

    r2965 r2992  
    2323/** 
    2424 * @file errno.h 
    25  * @brief PJLIB Error Codes 
     25 * @brief PJLIB Error Subsystem 
    2626 */ 
    2727#include <pj/types.h> 
    2828#include <pj/compat/errno.h> 
     29#include <stdarg.h> 
    2930 
    3031PJ_BEGIN_DECL 
    3132 
    3233/** 
    33  * @defgroup pj_errno Error Codes 
     34 * @defgroup pj_errno Error Subsystem 
    3435 * @{ 
    3536 * 
    36  * In PJLIB, error/status codes from operating system are translated 
    37  * into PJLIB error namespace, and stored in @a pj_status_t. All functions 
    38  * that work with @a pj_status_t expect to get PJLIB error code instead 
    39  * of native codes. 
     37 * The PJLIB Error Subsystem is a framework to unify all error codes 
     38 * produced by all components into a single error space, and provide 
     39 * uniform set of APIs to access them. With this framework, any error 
     40 * codes are encoded as pj_status_t value. The framework is extensible, 
     41 * application may register new error spaces to be recognized by 
     42 * the framework. 
    4043 * 
    4144 * @section pj_errno_retval Return Values 
     
    4851 * before returning the error to caller. 
    4952 * 
    50  * @section pj_errno_errmsg Error Message 
    51  * 
    52  * To get the error message corresponding to a particular code, use function 
    53  * #pj_strerror(). This function expects error code in PJLIB error namespace, 
    54  * not the native error code. Application can pass the value from the  
    55  * following sources to this function: 
    56  *  - #pj_get_os_error() 
    57  *  - #pj_get_netos_error() 
    58  *  - any return value from function returning @a pj_status_t. 
    59  * 
    60  * Application MUST NOT pass native error code (such as error code from 
     53 * @section err_services Retrieving and Displaying Error Messages 
     54 * 
     55 * The framework provides the following APIs to retrieve and/or display 
     56 * error messages: 
     57 * 
     58 *   - #pj_strerror(): this is the base API to retrieve error string 
     59 *      description for the specified pj_status_t error code. 
     60 * 
     61 *   - #PJ_PERROR() macro: use this macro similar to PJ_LOG to format 
     62 *      an error message and display them to the log 
     63 * 
     64 *   - #pj_perror(): this function is similar to PJ_PERROR() but unlike 
     65 *      #PJ_PERROR(), this function will always be included in the 
     66 *      link process. Due to this reason, prefer to use #PJ_PERROR() 
     67 *      if the application is concerned about the executable size. 
     68 * 
     69 * Application MUST NOT pass native error codes (such as error code from 
    6170 * functions like GetLastError() or errno) to PJLIB functions expecting 
    6271 * @a pj_status_t. 
    6372 * 
     73 * @section err_extending Extending the Error Space 
     74 * 
     75 * Application may register new error space to be recognized by the 
     76 * framework by using #pj_register_strerror(). Use the range started 
     77 * from PJ_ERRNO_START_USER to avoid conflict with existing error 
     78 * spaces. 
     79 * 
    6480 */ 
    6581 
     
    6884 */ 
    6985#define PJ_ERR_MSG_SIZE  80 
     86 
     87/** 
     88 * Buffer for title string of #PJ_PERROR(). 
     89 */ 
     90#ifndef PJ_PERROR_TITLE_BUF_SIZE 
     91#   define PJ_PERROR_TITLE_BUF_SIZE     120 
     92#endif 
     93 
    7094 
    7195/** 
     
    113137 
    114138/** 
    115  * Print the error message pertaining to the specified error code to 
    116  * the log. 
    117  * 
    118  * @param log_level The log will be printed at this log level. 
    119  * @param sender    The log sender string. 
    120  * @param status    The error code which error message will be printed. 
    121  * @param title     String to be printed before the error message. Note 
    122  *                  that a colon will be added automatically between 
    123  *                  this string and the error message. 
    124  * @param options   Options, currently must be zero. 
    125  */ 
    126 #if PJ_LOG_MAX_LEVEL >= 1 
    127 PJ_DECL(void) pj_perror(int log_level, const char *sender,  
    128                         pj_status_t status, const char *title, 
    129                         int options); 
    130 #else 
    131 #   define pj_perror(level, sender, status, title, options) 
    132 #endif  /* #if PJ_LOG_MAX_LEVEL >= 1 */ 
     139 * A utility macro to print error message pertaining to the specified error  
     140 * code to the log. This macro will construct the error message title  
     141 * according to the 'title_fmt' argument, and add the error string pertaining 
     142 * to the error code after the title string. A colon (':') will be added  
     143 * automatically between the title and the error string. 
     144 * 
     145 * This function is similar to pj_perror() function, but has the advantage 
     146 * that the function call can be omitted from the link process if the 
     147 * log level argument is below PJ_LOG_MAX_LEVEL threshold. 
     148 * 
     149 * Note that the title string constructed from the title_fmt will be built on 
     150 * a string buffer which size is PJ_PERROR_TITLE_BUF_SIZE, which normally is 
     151 * allocated from the stack. By default this buffer size is small (around 
     152 * 120 characters). Application MUST ensure that the constructed title string 
     153 * will not exceed this limit, since not all platforms support truncating 
     154 * the string. 
     155 * 
     156 * @see pj_perror() 
     157 * 
     158 * @param level     The logging verbosity level, valid values are 0-6. Lower 
     159 *                  number indicates higher importance, with level zero  
     160 *                  indicates fatal error. Only numeral argument is  
     161 *                  permitted (e.g. not variable). 
     162 * @param arg       Enclosed 'printf' like arguments, with the following 
     163 *                  arguments: 
     164 *                   - the sender (NULL terminated string), 
     165 *                   - the error code (pj_status_t) 
     166 *                   - the format string (title_fmt), and  
     167 *                   - optional variable number of arguments suitable for the  
     168 *                     format string. 
     169 * 
     170 * Sample: 
     171 * \verbatim 
     172   PJ_PERROR(2, (__FILE__, PJ_EBUSY, "Error making %s", "coffee")); 
     173   \endverbatim 
     174 * @hideinitializer 
     175 */ 
     176#define PJ_PERROR(level,arg)    do { \ 
     177                                    pj_perror_wrapper_##level(arg); \ 
     178                                } while (0) 
     179 
     180/** 
     181 * A utility function to print error message pertaining to the specified error  
     182 * code to the log. This function will construct the error message title  
     183 * according to the 'title_fmt' argument, and add the error string pertaining 
     184 * to the error code after the title string. A colon (':') will be added  
     185 * automatically between the title and the error string. 
     186 * 
     187 * Unlike the PJ_PERROR() macro, this function takes the \a log_level argument 
     188 * as a normal argument, unlike in PJ_PERROR() where a numeral value must be 
     189 * given. However this function will always be linked to the executable, 
     190 * unlike PJ_PERROR() which can be omitted when the level is below the  
     191 * PJ_LOG_MAX_LEVEL. 
     192 * 
     193 * Note that the title string constructed from the title_fmt will be built on 
     194 * a string buffer which size is PJ_PERROR_TITLE_BUF_SIZE, which normally is 
     195 * allocated from the stack. By default this buffer size is small (around 
     196 * 120 characters). Application MUST ensure that the constructed title string 
     197 * will not exceed this limit, since not all platforms support truncating 
     198 * the string. 
     199 * 
     200 * @see PJ_PERROR() 
     201 */ 
     202PJ_DECL(void) pj_perror(int log_level, const char *sender, pj_status_t status, 
     203                        const char *title_fmt, ...); 
    133204 
    134205 
     
    403474void pj_errno_clear_handlers(void); 
    404475 
     476 
     477/****** Internal for PJ_PERROR *******/ 
     478 
     479/** 
     480 * @def pj_perror_wrapper_1(arg) 
     481 * Internal function to write log with verbosity 1. Will evaluate to 
     482 * empty expression if PJ_LOG_MAX_LEVEL is below 1. 
     483 * @param arg       Log expression. 
     484 */ 
     485#if PJ_LOG_MAX_LEVEL >= 1 
     486    #define pj_perror_wrapper_1(arg)    pj_perror_1 arg 
     487    /** Internal function. */ 
     488    PJ_DECL(void) pj_perror_1(const char *sender, pj_status_t status,  
     489                              const char *title_fmt, ...); 
     490#else 
     491    #define pj_perror_wrapper_1(arg) 
     492#endif 
     493 
     494/** 
     495 * @def pj_perror_wrapper_2(arg) 
     496 * Internal function to write log with verbosity 2. Will evaluate to 
     497 * empty expression if PJ_LOG_MAX_LEVEL is below 2. 
     498 * @param arg       Log expression. 
     499 */ 
     500#if PJ_LOG_MAX_LEVEL >= 2 
     501    #define pj_perror_wrapper_2(arg)    pj_perror_2 arg 
     502    /** Internal function. */ 
     503    PJ_DECL(void) pj_perror_2(const char *sender, pj_status_t status,  
     504                              const char *title_fmt, ...); 
     505#else 
     506    #define pj_perror_wrapper_2(arg) 
     507#endif 
     508 
     509/** 
     510 * @def pj_perror_wrapper_3(arg) 
     511 * Internal function to write log with verbosity 3. Will evaluate to 
     512 * empty expression if PJ_LOG_MAX_LEVEL is below 3. 
     513 * @param arg       Log expression. 
     514 */ 
     515#if PJ_LOG_MAX_LEVEL >= 3 
     516    #define pj_perror_wrapper_3(arg)    pj_perror_3 arg 
     517    /** Internal function. */ 
     518    PJ_DECL(void) pj_perror_3(const char *sender, pj_status_t status,  
     519                              const char *title_fmt, ...); 
     520#else 
     521    #define pj_perror_wrapper_3(arg) 
     522#endif 
     523 
     524/** 
     525 * @def pj_perror_wrapper_4(arg) 
     526 * Internal function to write log with verbosity 4. Will evaluate to 
     527 * empty expression if PJ_LOG_MAX_LEVEL is below 4. 
     528 * @param arg       Log expression. 
     529 */ 
     530#if PJ_LOG_MAX_LEVEL >= 4 
     531    #define pj_perror_wrapper_4(arg)    pj_perror_4 arg 
     532    /** Internal function. */ 
     533    PJ_DECL(void) pj_perror_4(const char *sender, pj_status_t status,  
     534                              const char *title_fmt, ...); 
     535#else 
     536    #define pj_perror_wrapper_4(arg) 
     537#endif 
     538 
     539/** 
     540 * @def pj_perror_wrapper_5(arg) 
     541 * Internal function to write log with verbosity 5. Will evaluate to 
     542 * empty expression if PJ_LOG_MAX_LEVEL is below 5. 
     543 * @param arg       Log expression. 
     544 */ 
     545#if PJ_LOG_MAX_LEVEL >= 5 
     546    #define pj_perror_wrapper_5(arg)    pj_perror_5 arg 
     547    /** Internal function. */ 
     548    PJ_DECL(void) pj_perror_5(const char *sender, pj_status_t status,  
     549                              const char *title_fmt, ...); 
     550#else 
     551    #define pj_perror_wrapper_5(arg) 
     552#endif 
     553 
     554/** 
     555 * @def pj_perror_wrapper_6(arg) 
     556 * Internal function to write log with verbosity 6. Will evaluate to 
     557 * empty expression if PJ_LOG_MAX_LEVEL is below 6. 
     558 * @param arg       Log expression. 
     559 */ 
     560#if PJ_LOG_MAX_LEVEL >= 6 
     561    #define pj_perror_wrapper_6(arg)    pj_perror_6 arg 
     562    /** Internal function. */ 
     563    PJ_DECL(void) pj_perror_6(const char *sender, pj_status_t status,  
     564                              const char *title_fmt, ...); 
     565#else 
     566    #define pj_perror_wrapper_6(arg) 
     567#endif 
     568 
     569 
     570 
     571 
    405572PJ_END_DECL 
    406573 
Note: See TracChangeset for help on using the changeset viewer.