1
/* $Id: errno.h 4359 2013-02-21 11:18:36Z bennylp $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#ifndef __PJ_ERRNO_H__
21
#define __PJ_ERRNO_H__
25
* @brief PJLIB Error Subsystem
28
#include <pj/compat/errno.h>
34
* @defgroup pj_errno Error Subsystem
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
44
* @section pj_errno_retval Return Values
46
* All functions that returns @a pj_status_t returns @a PJ_SUCCESS if the
47
* operation was completed successfully, or non-zero value to indicate
48
* error. If the error came from operating system, then the native error
49
* code is translated/folded into PJLIB's error namespace by using
50
* #PJ_STATUS_FROM_OS() macro. The function will do this automatically
51
* before returning the error to caller.
53
* @section err_services Retrieving and Displaying Error Messages
55
* The framework provides the following APIs to retrieve and/or display
58
* - #pj_strerror(): this is the base API to retrieve error string
59
* description for the specified pj_status_t error code.
61
* - #PJ_PERROR() macro: use this macro similar to PJ_LOG to format
62
* an error message and display them to the log
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.
69
* Application MUST NOT pass native error codes (such as error code from
70
* functions like GetLastError() or errno) to PJLIB functions expecting
73
* @section err_extending Extending the Error Space
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
83
* Guidelines on error message length.
85
#define PJ_ERR_MSG_SIZE 80
88
* Buffer for title string of #PJ_PERROR().
90
#ifndef PJ_PERROR_TITLE_BUF_SIZE
91
# define PJ_PERROR_TITLE_BUF_SIZE 120
96
* Get the last platform error/status, folded into pj_status_t.
97
* @return OS dependent error code, folded into pj_status_t.
98
* @remark This function gets errno, or calls GetLastError() function and
99
* convert the code into pj_status_t with PJ_STATUS_FROM_OS. Do
100
* not call this for socket functions!
101
* @see pj_get_netos_error()
103
PJ_DECL(pj_status_t) pj_get_os_error(void);
107
* @param code pj_status_t
109
PJ_DECL(void) pj_set_os_error(pj_status_t code);
112
* Get the last error from socket operations.
113
* @return Last socket error, folded into pj_status_t.
115
PJ_DECL(pj_status_t) pj_get_netos_error(void);
119
* @param code pj_status_t.
121
PJ_DECL(void) pj_set_netos_error(pj_status_t code);
125
* Get the error message for the specified error code. The message
126
* string will be NULL terminated.
128
* @param statcode The error code.
129
* @param buf Buffer to hold the error message string.
130
* @param bufsize Size of the buffer.
132
* @return The error message as NULL terminated string,
133
* wrapped with pj_str_t.
135
PJ_DECL(pj_str_t) pj_strerror( pj_status_t statcode,
136
char *buf, pj_size_t bufsize);
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.
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.
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
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
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
172
PJ_PERROR(2, (__FILE__, PJ_EBUSY, "Error making %s", "coffee"));
176
#define PJ_PERROR(level,arg) do { \
177
pj_perror_wrapper_##level(arg); \
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.
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
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
202
PJ_DECL(void) pj_perror(int log_level, const char *sender, pj_status_t status,
203
const char *title_fmt, ...);
207
* Type of callback to be specified in #pj_register_strerror()
209
* @param e The error code to lookup.
210
* @param msg Buffer to store the error message.
211
* @param max Length of the buffer.
213
* @return The error string.
215
typedef pj_str_t (*pj_error_callback)(pj_status_t e, char *msg, pj_size_t max);
219
* Register strerror message handler for the specified error space.
220
* Application can register its own handler to supply the error message
221
* for the specified error code range. This handler will be called
224
* @param start_code The starting error code where the handler should
225
* be called to retrieve the error message.
226
* @param err_space The size of error space. The error code range then
227
* will fall in start_code to start_code+err_space-1
229
* @param f The handler to be called when #pj_strerror() is
230
* supplied with error code that falls into this range.
232
* @return PJ_SUCCESS or the specified error code. The
233
* registration may fail when the error space has been
234
* occupied by other handler, or when there are too many
235
* handlers registered to PJLIB.
237
PJ_DECL(pj_status_t) pj_register_strerror(pj_status_t start_code,
238
pj_status_t err_space,
239
pj_error_callback f);
243
* Return platform os error code folded into pj_status_t code. This is
244
* the macro that is used throughout the library for all PJLIB's functions
245
* that returns error from operating system. Application may override
246
* this macro to reduce size (e.g. by defining it to always return
250
* This macro MUST return non-zero value regardless whether zero is
251
* passed as the argument. The reason is to protect logic error when
252
* the operating system doesn't report error codes properly.
254
* @param os_code Platform OS error code. This value may be evaluated
256
* @return The platform os error code folded into pj_status_t.
258
#ifndef PJ_RETURN_OS_ERROR
259
# define PJ_RETURN_OS_ERROR(os_code) (os_code ? \
260
PJ_STATUS_FROM_OS(os_code) : -1)
266
* Fold a platform specific error into an pj_status_t code.
268
* @param e The platform os error code.
269
* @return pj_status_t
270
* @warning Macro implementation; the syserr argument may be evaluated
273
#if PJ_NATIVE_ERR_POSITIVE
274
# define PJ_STATUS_FROM_OS(e) (e == 0 ? PJ_SUCCESS : e + PJ_ERRNO_START_SYS)
276
# define PJ_STATUS_FROM_OS(e) (e == 0 ? PJ_SUCCESS : PJ_ERRNO_START_SYS - e)
281
* Fold an pj_status_t code back to the native platform defined error.
283
* @param e The pj_status_t folded platform os error code.
284
* @return pj_os_err_type
285
* @warning macro implementation; the statcode argument may be evaluated
286
* multiple times. If the statcode was not created by
287
* pj_get_os_error or PJ_STATUS_FROM_OS, the results are undefined.
289
#if PJ_NATIVE_ERR_POSITIVE
290
# define PJ_STATUS_TO_OS(e) (e == 0 ? PJ_SUCCESS : e - PJ_ERRNO_START_SYS)
292
# define PJ_STATUS_TO_OS(e) (e == 0 ? PJ_SUCCESS : PJ_ERRNO_START_SYS - e)
297
* @defgroup pj_errnum PJLIB's Own Error Codes
303
* Use this macro to generate error message text for your error code,
304
* so that they look uniformly as the rest of the libraries.
306
* @param code The error code
307
* @param msg The error test.
310
# define PJ_BUILD_ERR(code,msg) { code, msg " (" #code ")" }
316
* Unknown error has been reported.
318
#define PJ_EUNKNOWN (PJ_ERRNO_START_STATUS + 1) /* 70001 */
321
* The operation is pending and will be completed later.
323
#define PJ_EPENDING (PJ_ERRNO_START_STATUS + 2) /* 70002 */
326
* Too many connecting sockets.
328
#define PJ_ETOOMANYCONN (PJ_ERRNO_START_STATUS + 3) /* 70003 */
333
#define PJ_EINVAL (PJ_ERRNO_START_STATUS + 4) /* 70004 */
336
* Name too long (eg. hostname too long).
338
#define PJ_ENAMETOOLONG (PJ_ERRNO_START_STATUS + 5) /* 70005 */
343
#define PJ_ENOTFOUND (PJ_ERRNO_START_STATUS + 6) /* 70006 */
348
#define PJ_ENOMEM (PJ_ERRNO_START_STATUS + 7) /* 70007 */
353
#define PJ_EBUG (PJ_ERRNO_START_STATUS + 8) /* 70008 */
356
* Operation timed out.
358
#define PJ_ETIMEDOUT (PJ_ERRNO_START_STATUS + 9) /* 70009 */
363
#define PJ_ETOOMANY (PJ_ERRNO_START_STATUS + 10)/* 70010 */
368
#define PJ_EBUSY (PJ_ERRNO_START_STATUS + 11)/* 70011 */
371
* The specified option is not supported.
373
#define PJ_ENOTSUP (PJ_ERRNO_START_STATUS + 12)/* 70012 */
378
#define PJ_EINVALIDOP (PJ_ERRNO_START_STATUS + 13)/* 70013 */
381
* Operation is cancelled.
383
#define PJ_ECANCELLED (PJ_ERRNO_START_STATUS + 14)/* 70014 */
386
* Object already exists.
388
#define PJ_EEXISTS (PJ_ERRNO_START_STATUS + 15)/* 70015 */
393
#define PJ_EEOF (PJ_ERRNO_START_STATUS + 16)/* 70016 */
398
#define PJ_ETOOBIG (PJ_ERRNO_START_STATUS + 17)/* 70017 */
401
* Error in gethostbyname(). This is a generic error returned when
402
* gethostbyname() has returned an error.
404
#define PJ_ERESOLVE (PJ_ERRNO_START_STATUS + 18)/* 70018 */
409
#define PJ_ETOOSMALL (PJ_ERRNO_START_STATUS + 19)/* 70019 */
414
#define PJ_EIGNORED (PJ_ERRNO_START_STATUS + 20)/* 70020 */
417
* IPv6 is not supported
419
#define PJ_EIPV6NOTSUP (PJ_ERRNO_START_STATUS + 21)/* 70021 */
422
* Unsupported address family
424
#define PJ_EAFNOTSUP (PJ_ERRNO_START_STATUS + 22)/* 70022 */
427
* Object no longer exists
429
#define PJ_EGONE (PJ_ERRNO_START_STATUS + 23)/* 70023 */
431
/** @} */ /* pj_errnum */
433
/** @} */ /* pj_errno */
437
* PJ_ERRNO_START is where PJLIB specific error values start.
439
#define PJ_ERRNO_START 20000
442
* PJ_ERRNO_SPACE_SIZE is the maximum number of errors in one of
443
* the error/status range below.
445
#define PJ_ERRNO_SPACE_SIZE 50000
448
* PJ_ERRNO_START_STATUS is where PJLIB specific status codes start.
449
* Effectively the error in this class would be 70000 - 119000.
451
#define PJ_ERRNO_START_STATUS (PJ_ERRNO_START + PJ_ERRNO_SPACE_SIZE)
454
* PJ_ERRNO_START_SYS converts platform specific error codes into
455
* pj_status_t values.
456
* Effectively the error in this class would be 120000 - 169000.
458
#define PJ_ERRNO_START_SYS (PJ_ERRNO_START_STATUS + PJ_ERRNO_SPACE_SIZE)
461
* PJ_ERRNO_START_USER are reserved for applications that use error
462
* codes along with PJLIB codes.
463
* Effectively the error in this class would be 170000 - 219000.
465
#define PJ_ERRNO_START_USER (PJ_ERRNO_START_SYS + PJ_ERRNO_SPACE_SIZE)
469
* Below are list of error spaces that have been taken so far:
470
* - PJSIP_ERRNO_START (PJ_ERRNO_START_USER)
471
* - PJMEDIA_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE)
472
* - PJSIP_SIMPLE_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE*2)
473
* - PJLIB_UTIL_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE*3)
474
* - PJNATH_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE*4)
475
* - PJMEDIA_AUDIODEV_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE*5)
476
* - PJ_SSL_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE*6)
477
* - PJMEDIA_VIDEODEV_ERRNO_START (PJ_ERRNO_START_USER + PJ_ERRNO_SPACE_SIZE*7)
481
void pj_errno_clear_handlers(void);
484
/****** Internal for PJ_PERROR *******/
487
* @def pj_perror_wrapper_1(arg)
488
* Internal function to write log with verbosity 1. Will evaluate to
489
* empty expression if PJ_LOG_MAX_LEVEL is below 1.
490
* @param arg Log expression.
492
#if PJ_LOG_MAX_LEVEL >= 1
493
#define pj_perror_wrapper_1(arg) pj_perror_1 arg
494
/** Internal function. */
495
PJ_DECL(void) pj_perror_1(const char *sender, pj_status_t status,
496
const char *title_fmt, ...);
498
#define pj_perror_wrapper_1(arg)
502
* @def pj_perror_wrapper_2(arg)
503
* Internal function to write log with verbosity 2. Will evaluate to
504
* empty expression if PJ_LOG_MAX_LEVEL is below 2.
505
* @param arg Log expression.
507
#if PJ_LOG_MAX_LEVEL >= 2
508
#define pj_perror_wrapper_2(arg) pj_perror_2 arg
509
/** Internal function. */
510
PJ_DECL(void) pj_perror_2(const char *sender, pj_status_t status,
511
const char *title_fmt, ...);
513
#define pj_perror_wrapper_2(arg)
517
* @def pj_perror_wrapper_3(arg)
518
* Internal function to write log with verbosity 3. Will evaluate to
519
* empty expression if PJ_LOG_MAX_LEVEL is below 3.
520
* @param arg Log expression.
522
#if PJ_LOG_MAX_LEVEL >= 3
523
#define pj_perror_wrapper_3(arg) pj_perror_3 arg
524
/** Internal function. */
525
PJ_DECL(void) pj_perror_3(const char *sender, pj_status_t status,
526
const char *title_fmt, ...);
528
#define pj_perror_wrapper_3(arg)
532
* @def pj_perror_wrapper_4(arg)
533
* Internal function to write log with verbosity 4. Will evaluate to
534
* empty expression if PJ_LOG_MAX_LEVEL is below 4.
535
* @param arg Log expression.
537
#if PJ_LOG_MAX_LEVEL >= 4
538
#define pj_perror_wrapper_4(arg) pj_perror_4 arg
539
/** Internal function. */
540
PJ_DECL(void) pj_perror_4(const char *sender, pj_status_t status,
541
const char *title_fmt, ...);
543
#define pj_perror_wrapper_4(arg)
547
* @def pj_perror_wrapper_5(arg)
548
* Internal function to write log with verbosity 5. Will evaluate to
549
* empty expression if PJ_LOG_MAX_LEVEL is below 5.
550
* @param arg Log expression.
552
#if PJ_LOG_MAX_LEVEL >= 5
553
#define pj_perror_wrapper_5(arg) pj_perror_5 arg
554
/** Internal function. */
555
PJ_DECL(void) pj_perror_5(const char *sender, pj_status_t status,
556
const char *title_fmt, ...);
558
#define pj_perror_wrapper_5(arg)
562
* @def pj_perror_wrapper_6(arg)
563
* Internal function to write log with verbosity 6. Will evaluate to
564
* empty expression if PJ_LOG_MAX_LEVEL is below 6.
565
* @param arg Log expression.
567
#if PJ_LOG_MAX_LEVEL >= 6
568
#define pj_perror_wrapper_6(arg) pj_perror_6 arg
569
/** Internal function. */
570
PJ_DECL(void) pj_perror_6(const char *sender, pj_status_t status,
571
const char *title_fmt, ...);
573
#define pj_perror_wrapper_6(arg)
581
#endif /* __PJ_ERRNO_H__ */