NAME
sg_get_error, sg_get_error_arg, sg_get_error_errno, sg_str_error, sg_get_error_details, sg_strperror - get last error status
SYNOPSIS
FC#include <statgrab.h>F[0]
FCsg_error sg_get_errorF[0] FC(void);F[0]
FCconst char *sg_get_error_argF[0] FC(void);F[0]
FCint sg_get_error_errnoF[0] FC(void);F[0]
FCsg_error sg_get_error_detailsF[0] FC(sg_error_details *err_details);F[0]
FCconst char *sg_str_errorF[0] FC(sg_error code);F[0]
FCchar *sg_strperrorF[0] FC(char **buf, const sg_error_details * const err_details);F[0]
DESCRIPTION
There are four functions to get information about the last occurred error: FCsg_get_errorF[0], FCsg_get_error_argF[0], FCsg_get_error_errnoF[0] and FCsg_get_error_detailsF[0]. The remaining functions are intended to improve the machine-human-interface (e.g. the error log or a message box): FCsg_str_errorF[0] delivers a human readable error name and FCsg_strperrorF[0] prepares a full blown error message for printing.
The error argument (FCsg_get_error_argF[0]) is stored thread local and is reused every time an error occures. If a later usage is intended, duplicating it is a suitable strategy. Same for the FCerror_argF[0] of FCsg_error_detailsF[0] delivered by FCsg_get_error_detailsF[0].
When someone calls the function FCsg_get_error_detailsF[0] with a NULL pointer, the last error is overridden with a new one describing that FCsg_get_error_detailsF[0] is called with invalid arguments. Please be careful.
The function FCsg_strperrorF[0] is allowed to be called with or without (FCerr_detailsF[0] is NULL) collected error details. In the latter case, the last occurred error of this thread is used to prepare the error message. Be aware, the the buffer pointer must be non-NULL (points to an existing FCchar *F[0] lvalue), but the FCchar *F[0] lvalue it points to, must be NULL. When invoked correctly, there are only two possible error conditions let FCsg_strperrorF[0] fail: ENOMEM (out of memory) and EINVAL (invalid error code).
Example
FC
if( NULL == sg_get_cpu_stats() ) {
char *buf = NULL;
if( NULL != sg_strperror( &buf, NULL ) ) {
fprintf( stderr, "error getting CPU stats: %s\n", buf );
free(buf);
exit(255);
}
else {
fprintf( stderr, "error getting CPU stats and error information\n" );
exit(255);
}
}
F[0]
RETURN VALUES
The error details contains following information:
FC
typedef struct sg_error_details {
sg_error error;
int errno_value;
const char *error_arg;
} sg_error_details;
F[0]
- FCerrorF[0]
-
The statgrab library error code.
- FCerrno_valueF[0]
-
The operating system error code.
- FCerror_argF[0]
-
Additional information set when the error was reported.
SEE ALSO
WEBSITE
⟨https://libstatgrab.org/⟩