CLibs
Loading...
Searching...
No Matches
errors.h File Reference
#include "attributes.h"
#include "filenames.h"
#include "terminal_colors.h"
#include <errno.h>
#include <stddef.h>
#include <string.h>
#include <err.h>

Go to the source code of this file.

Macros

#define ERRNO_OK   0 /* No error */
#define RV_EXCEPTION   ( -2 ) /* Non-fatal, recoverable error e.g. OOB index */
#define RV_ERROR   ( -1 ) /* Fatal, un-recoverable error */
#define RV_SUCCESS   0 /* Success */
#define on_fail(func_call)
#define on_error(func_call)
#define return_on_fail(func_call)
#define return_on_error(func_call)
#define goto_on_fail(GOTO_LABEL, func_call)
#define goto_on_error(GOTO_LABEL, func_call)
#define COLOR_WARNING   FOREGROUND_RED
#define f_stack_trace(RETVAL)
#define ffl_stack_trace(RETVAL)
#define warn_ret(RETVAL, ...)
#define warnx_ret(RETVAL, ...)
#define fwarn(...)
#define fwarnx(...)
#define fwarn_ret(RETVAL, ...)
#define fwarnx_ret(RETVAL, ...)
#define fflwarn(...)
#define fflwarnx(...)
#define fflwarn_ret(RETVAL, ...)
#define fflwarnx_ret(RETVAL, ...)

Functions

LibraryDefined const char * rv_to_string (const int rv)
LibraryDefined PrintfLike Cold ptrdiff_t WarnUniversal (bool PrintProgName, const char *__restrict FileName, const char *__restrict FunctionName, int LineNumber, int err_no, ptrdiff_t return_value, const char *__restrict format,...)

Macro Definition Documentation

◆ COLOR_WARNING

#define COLOR_WARNING   FOREGROUND_RED

◆ ERRNO_OK

#define ERRNO_OK   0 /* No error */

◆ f_stack_trace

#define f_stack_trace ( RETVAL)
Value:
WarnUniversal( false, NULL, NULL, -1, -1, ( ptrdiff_t ) RETVAL, "\tin %s", __func__ )
LibraryDefined PrintfLike Cold ptrdiff_t WarnUniversal(bool PrintProgName, const char *__restrict FileName, const char *__restrict FunctionName, int LineNumber, int err_no, ptrdiff_t return_value, const char *__restrict format,...)
Definition errors.h:160

prints

in ‹__func__›

Example:

#include "errors.h"
int foo()
{
fwarnx( "some error message" );
return RV_EXCEPTION;
}
int main()
{
if ( foo() != RV_SUCCESS )
{
return 1;
}
}
#define fwarnx(...)
Definition errors.h:270
#define f_stack_trace(RETVAL)
Definition errors.h:241
#define RV_SUCCESS
Definition errors.h:34
#define RV_EXCEPTION
Definition errors.h:32


prints (to stderr):

‹program-name›: foo: some error message
in main

◆ ffl_stack_trace

#define ffl_stack_trace ( RETVAL)
Value:
WarnUniversal( false, \
NULL, \
NULL, \
-1, \
-1, \
( ptrdiff_t ) RETVAL, \
"\tin %s: %s @ %d", \
__FILE_NAME__, \
__func__, \
__LINE__ )

Like f_stack_trace, just with __FILE_NAME__ and __LINE__

See also
f_stack_trace()

◆ fflwarn

#define fflwarn ( ...)
Value:
( void ) WarnUniversal( \
true, __FILE_NAME__, __func__, __LINE__, errno, -1, __VA_ARGS__ )

◆ fflwarn_ret

#define fflwarn_ret ( RETVAL,
... )
Value:
WarnUniversal( true, \
__FILE_NAME__, \
__func__, \
__LINE__, \
errno, \
( ptrdiff_t ) RETVAL, \
__VA_ARGS__ )

◆ fflwarnx

#define fflwarnx ( ...)
Value:
( void ) WarnUniversal( true, __FILE_NAME__, __func__, __LINE__, -1, -1, __VA_ARGS__ )

◆ fflwarnx_ret

#define fflwarnx_ret ( RETVAL,
... )
Value:
WarnUniversal( true, \
__FILE_NAME__, \
__func__, \
__LINE__, \
-1, \
( ptrdiff_t ) RETVAL, \
__VA_ARGS__ )

◆ fwarn

#define fwarn ( ...)
Value:
( void ) WarnUniversal( true, NULL, __func__, -1, errno, -1, __VA_ARGS__ )

warn() with function name at the start

◆ fwarn_ret

#define fwarn_ret ( RETVAL,
... )
Value:
WarnUniversal( true, NULL, __func__, -1, errno, ( ptrdiff_t ) RETVAL, __VA_ARGS__ )

◆ fwarnx

#define fwarnx ( ...)
Value:
( void ) WarnUniversal( true, NULL, __func__, -1, -1, -1, __VA_ARGS__ )

◆ fwarnx_ret

#define fwarnx_ret ( RETVAL,
... )
Value:
WarnUniversal( true, NULL, __func__, -1, -1, ( ptrdiff_t ) RETVAL, __VA_ARGS__ )

◆ goto_on_error

#define goto_on_error ( GOTO_LABEL,
func_call )
Value:
do \
{ \
on_error ( func_call ) \
goto GOTO_LABEL; \
} \
while ( 0 )

◆ goto_on_fail

#define goto_on_fail ( GOTO_LABEL,
func_call )
Value:
do \
{ \
on_fail ( func_call ) \
goto GOTO_LABEL; \
} \
while ( 0 )

◆ on_error

#define on_error ( func_call)
Value:
if ( ( func_call ) == RV_ERROR )
#define RV_ERROR
Definition errors.h:33

◆ on_fail

#define on_fail ( func_call)
Value:
if ( ( func_call ) != RV_SUCCESS )

◆ return_on_error

#define return_on_error ( func_call)
Value:
do \
{ \
int func_call_retval = ( func_call ); \
on_error ( func_call_retval ) \
{ \
( void ) f_stack_trace( 0 ); \
return func_call_retval; \
} \
} \
while ( 0 )

Returns the value (of type int) of the function call or variable if it equals RV_ERROR

See also
return_on_fail()

◆ return_on_fail

#define return_on_fail ( func_call)
Value:
do \
{ \
int func_call_retval = ( func_call ); \
on_fail ( func_call_retval ) \
{ \
( void ) f_stack_trace( NULL ); \
return func_call_retval; \
} \
} \
while ( 0 )

Returns the value (of type int) of the function call or variable if it doesn't equal RV_SUCCESS

See also
return_on_error()

◆ RV_ERROR

#define RV_ERROR   ( -1 ) /* Fatal, un-recoverable error */

◆ RV_EXCEPTION

#define RV_EXCEPTION   ( -2 ) /* Non-fatal, recoverable error e.g. OOB index */

◆ RV_SUCCESS

#define RV_SUCCESS   0 /* Success */

◆ warn_ret

#define warn_ret ( RETVAL,
... )
Value:
WarnUniversal( true, NULL, NULL, -1, errno, ( ptrdiff_t ) RETVAL, __VA_ARGS__ )

Warns like warn() and returns RETVAL

◆ warnx_ret

#define warnx_ret ( RETVAL,
... )
Value:
WarnUniversal( true, NULL, NULL, -1, -1, ( ptrdiff_t ) RETVAL, __VA_ARGS__ )

Function Documentation

◆ rv_to_string()

LibraryDefined const char * rv_to_string ( const int rv)

Turns an RV to a string.

Parameters
rvRV_* from this file
Returns
string literal

◆ WarnUniversal()

ptrdiff_t WarnUniversal ( bool PrintProgName,
const char *__restrict FileName,
const char *__restrict FunctionName,
int LineNumber,
int err_no,
ptrdiff_t return_value,
const char *__restrict format,
... )

Similar to warn() (especially warnc)

Prints

‹executable name›: ‹FileName›: ‹FunctionName› @ LineNumber: ‹formatted string›[: strerror(err_no)]

If ‹FileName› or ‹FunctionName› is NULL or if ‹LineNumber› or ‹err_no› equals -1, then they are not printed.

Example of an actual function-like macro defined in errors.h:

#define fwarn( ... ) ( void ) WarnUniversal( NULL, __func__, -1, errno, -1, __VA_ARGS__ )
Parameters
PrintProgNametrue if the function should print the executable name
FileNamename of the current file (FILE_NAME)
FunctionNamename of the function (func)
LineNumbernumber of the line to warn about (LINE)
err_noerrno to warn about
return_valuereturned value
formatformat string
...printf-like arguments for ‹format›
Returns
return_value
Bug
"%p" for some reason sometimes throws compiler errors for non void * pointers