sg_get_error(3)

πŸ“¦
version 0.92.1 Β· 2 versions
, in alpine 3.24, arch rolling, fedora 43, fedora 44, nixos 26.05, nixpkgs unstable, opensuse-leap 16.0

🌸 NAME 🌸

okay bestie, welcome to the sg_get_error(3) page from the statgrab manual πŸ’… β€” this one is all about the little squad of functions that tell you what the last error was: sg_get_error, sg_get_error_arg, sg_get_error_errno, sg_str_error, sg_get_error_details, and sg_strperror.

✨ SYNOPSIS ✨

#include <statgrab.h>

sg_error sg_get_error (void);

const char *sg_get_error_arg (void);

int sg_get_error_errno (void);

sg_error sg_get_error_details (sg_error_details *err_details);

const char *sg_str_error (sg_error code);

char *sg_strperror (char **buf, const sg_error_details * const
                   err_details);

πŸ’– DESCRIPTION πŸ’–

so there are four functions that get you info about the last error that happened: sg_get_error, sg_get_error_arg, sg_get_error_errno, and sg_get_error_details. The other two are here to make the machine-human-interface nicer β€” like your error log or a message box, bestie. sg_str_error gives you a human-readable error name, and sg_strperror prepares a full-blown error message ready for printing ✨

the error argument from sg_get_error_arg is stored thread local and gets reused every time an error occurs. if you're gonna use it later, duplicating it is a suitable strategy πŸ’– same goes for the error_arg of sg_error_details delivered by sg_get_error_details.

⚠️ careful, bestie: if someone calls sg_get_error_details with a NULL pointer, the last error gets overridden with a new one describing that sg_get_error_details was called with invalid arguments. please be careful πŸ™ˆ

sg_strperror is allowed to be called with or without collected error details (err_details is NULL). in the latter case, the last occurred error of this thread is used to prepare the error message. be aware that the buffer pointer must be non-NULL β€” it points to an existing char * lvalue β€” but the char * lvalue it points to must be NULL. when invoked correctly, there are only two possible error conditions that let sg_strperror fail: ENOMEM (out of memory) and EINVAL (invalid error code) πŸ’€

πŸŽ€ Example πŸŽ€

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);
    }
}

πŸ¦‹ RETURN VALUES πŸ¦‹

the error details contains following information:

typedef struct sg_error_details {
        sg_error error;
        int errno_value;
        const char *error_arg;
} sg_error_details;
  • error β€” the statgrab library error code πŸ‘‘
  • errno_value β€” the operating system error code πŸ”₯
  • error_arg β€” additional information set when the error was reported 🫢

🌈 SEE ALSO 🌈

  • statgrab(3) β€” go peek at the main character, no cap πŸ’…

πŸŽ€ WEBSITE πŸŽ€

⟨https://libstatgrab.org/⟩ β€” the website, literally right there πŸ¦‹


source/version: libstatgrab Β· date: 2019-03-08 Β· this was the sg_get_error(3) manual page, bye bestie, slay responsibly πŸ’–βœ¨