libuproc  1.2.0
Macros | Typedefs | Enumerations | Functions
Error handling

Macros

#define uproc_error_msg(num, ...)
 Set uproc_errno with a custom message. More...
 
#define uproc_error(num)   uproc_error_msg((num), NULL)
 Set uproc_errno with a standard message. More...
 
#define uproc_errno   (*(uproc_error_errno_()))
 errno -like error indicator More...
 
#define uproc_errmsg   (uproc_error_errmsg_())
 Error message. More...
 
#define uproc_errloc   (uproc_error_errloc_())
 Error location. More...
 

Typedefs

typedef void uproc_error_handler(enum uproc_error_code num, const char *msg, const char *loc, void *context)
 Error handler type. More...
 

Enumerations

enum  uproc_error_code {
  UPROC_SUCCESS = 0,
  UPROC_FAILURE,
  UPROC_ERRNO,
  UPROC_ENOMEM,
  UPROC_EINVAL,
  UPROC_ENOENT,
  UPROC_EEXIST,
  UPROC_EIO,
  UPROC_ENOTSUP
}
 Available error codes. More...
 

Functions

void uproc_perror (const char *fmt,...)
 Print error message to stderr. More...
 
void uproc_error_set_handler (uproc_error_handler *hdl, void *context)
 Set error handler. More...
 

Detailed Description

This module provides functions and macros to report errors and retrieve information about errors that have occured.

Macro Definition Documentation

#define uproc_error_msg (   num,
  ... 
)

Set uproc_errno with a custom message.

Example:

1 void *foo = malloc(sz);
2 if (!foo) {
3  return uproc_error_msg(UPROC_ENOMEM,
4  "can't allocate foo with size %lu",
5  (unsigned long)sz);
6  }
Parameters
numerror code
...printf-style format string and corresponding arguments
Return values
0if num is UPROC_SUCCESS
-1else
#define uproc_error (   num)    uproc_error_msg((num), NULL)

Set uproc_errno with a standard message.

#define uproc_errno   (*(uproc_error_errno_()))

errno -like error indicator

Like the original errno, evaluates to an (assignable) lvalue of type int, used as an error indicator. This should usually be one of the values of uproc_error_code.

#define uproc_errmsg   (uproc_error_errmsg_())

Error message.

Evaluates to a const char* containing a description of the last occured error.

#define uproc_errloc   (uproc_error_errloc_())

Error location.

Evaluates to a const char* containing the location (source file, function and line number) from where uproc_error() or uproc_error_msg() was called the last time.

Typedef Documentation

typedef void uproc_error_handler(enum uproc_error_code num, const char *msg, const char *loc, void *context)

Error handler type.

Parameters
numerror code
msgerror message
locsource file and line
contextuser defined pointer as set via uproc_error_set_handler

Enumeration Type Documentation

Available error codes.

Enumerator
UPROC_SUCCESS 

Success.

UPROC_FAILURE 

General failure.

UPROC_ERRNO 

A system call (that sets errno) returned an error.

UPROC_ENOMEM 

Memory allocation failed.

UPROC_EINVAL 

Invalid argument.

UPROC_ENOENT 

Object doesn't exist.

UPROC_EEXIST 

Object already exists.

UPROC_EIO 

Input/output error.

UPROC_ENOTSUP 

Operation not supported.

Function Documentation

void uproc_perror ( const char *  fmt,
  ... 
)

Print error message to stderr.

If fmt is a nonempty string, format it using the other arguments and prepend the result, followed by a colon and space, to the error message.

Parameters
fmtprintf-style format string
...format string arguments
void uproc_error_set_handler ( uproc_error_handler hdl,
void *  context 
)

Set error handler.

Set an error handler to be called every time libuproc encounters an error.

A simple error handler that just prints the message and exits the program could look like this:

1 void handler(enum uproc_error_code num, const char *msg, const char *loc,
2  void *context)
3 {
4  uproc_perror("");
5  exit(EXIT_FAILURE);
6 }
Parameters
hdlthe error handler function
contextopaque pointer that is passed as the last argument to the error handler function (may be NULL)