JWS C Library
C language utility library
error.c File Reference

Definitions of an Error and associated functions. More...

#include <jwsc/config.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <inttypes.h>
#include "jwsc/base/error.h"
Include dependency graph for error.c:

Go to the source code of this file.

Functions

Errorcreate_error (Error_type type, const char *file, unsigned int line, const char *msg)
 Creates a new Error.
void free_error (Error *e)
 Frees an Error.
void print_error (const char *prefix, const Error *e)
 Prints an Error on stderr.
void print_error_exit (const char *prefix, const Error *e)
 Prints an Error on stderr and exits.
void print_error_msg (const char *prefix, const char *msg)
 Prints an error message on stderr.
void print_error_msg_exit (const char *prefix, const char *msg)
 Prints an error message on stderr and exits.
void print_errno (const char *prefix)
 Prints the error message associated with the current value of errno on stderr.
void print_errno_exit (const char *prefix)
 Prints the error message associated with the current value of errno on stderr and exits.
uint32_t get_num_unhandled_errors ()
 Returns the number of errors created and never handled (freed).

Variables

static uint32_t num_unhandled_errors = 0
 Number of errors created and not handled (freed).
static Error no_memory
 Used for special memory Exception.
Errorerror_no_memory = &no_memory
 Special Error for memory allocation errors.

Detailed Description

Definitions of an Error and associated functions.

Author:
Joseph Schlecht
License:
Creative Commons BY-NC-SA 3.0

Definition in file error.c.


Function Documentation

Error* create_error ( Error_type  type,
const char *  file,
unsigned int  line,
const char *  msg 
)

Creates a new Error.

Any and all of the const char* parameters can be NULL. If non-NULL, a copy of the the const char* parameters is allocated. The Error::code is set to 0.

Parameters:
typeError_type.
fileFile where the Error occurred.
lineLine near the offending code.
msgMessage for the Error.
Returns:
An Error is always returned. If the Error could not be created because of memory exhaustion, error_no_memory is returned.

Definition at line 97 of file error.c.

void free_error ( Error e)

Frees an Error.

Parameters:
eError to free.

Definition at line 151 of file error.c.

void print_error ( const char *  prefix,
const Error e 
)

Prints an Error on stderr.

Prints all the non-NULL fields of the Error in the format

 'prefix: file: line: msg\n' 

If prefix == NULL, just the error message is printed. The Error e must not be NULL.

Parameters:
prefixPrefix to print before the error message.
eError to print.

Definition at line 181 of file error.c.

void print_error_exit ( const char *  prefix,
const Error e 
)

Prints an Error on stderr and exits.

Definition at line 229 of file error.c.

void print_error_msg ( const char *  prefix,
const char *  msg 
)

Prints an error message on stderr.

Prints the Error in the format

 'prefix: msg\n' 

If prefix == NULL, just the error message is printed. If msg is NULL, no new-line is printed. This is useful for printing multi-prefixed error messages.

Parameters:
prefixPrefix to print before the error message.
msgError message to print.

Definition at line 248 of file error.c.

void print_error_msg_exit ( const char *  prefix,
const char *  msg 
)

Prints an error message on stderr and exits.

Prints the Error in the format

 'prefix: msg\n' 

If prefix == NULL, just the error message is printed. If msg is NULL, no new-line is printed. This is useful for printing multi-prefixed error messages.

The exit status is EXIT_FAILURE.

Parameters:
prefixPrefix to print before the error message.
msgError message to print.

Definition at line 276 of file error.c.

void print_errno ( const char *  prefix)

Prints the error message associated with the current value of errno on stderr.

Prints the Error in the format

 'prefix: errno_msg\n' 

The standard function strerror is used to get the message for errno.

If prefix == NULL, just the error message is printed.

Parameters:
prefixPrefix to print before the error message.

Definition at line 294 of file error.c.

void print_errno_exit ( const char *  prefix)

Prints the error message associated with the current value of errno on stderr and exits.

Prints the Error in the format

 'prefix: errno_msg\n' 

The standard function strerror is used to get the message for errno.

If prefix == NULL, just the error message is printed.

The exit status is EXIT_FAILURE.

Parameters:
prefixPrefix to print before the error message.

Definition at line 318 of file error.c.

uint32_t get_num_unhandled_errors ( )

Returns the number of errors created and never handled (freed).

Returns:
The number of errors created and never handled (freed).

Definition at line 326 of file error.c.


Variable Documentation

uint32_t num_unhandled_errors = 0 [static]

Number of errors created and not handled (freed).

Definition at line 66 of file error.c.

Error no_memory [static]
Initial value:
 
{ 
    ERROR_NO_MEMORY, 
    "No memory available.",
    NULL,
    0,
    0
}

Used for special memory Exception.

Definition at line 70 of file error.c.

Special Error for memory allocation errors.

Global pointer to the static Error no_memory.

Definition at line 81 of file error.c.