archeus/doc/pages/standard/errno.md

2.2 KiB

\page standard-errno ARC_Errno

Note: this file is being actively worked on and might change during minor updates

Basic Overview

arc/std/errno.h is used for handling errors as well as printing to debug. It defines a global uint32_t ::arc_errno, as well as a global FILE ::arc_errno_log_file. For now it just has printing errors, but in the future it is planned to have warnings and general logs added. For error logs to be printed, ARC_DEBUG needs to be defined when compiled.

Basic Error Handling

When throwing errors, ::arc_errno should usually be set to an ARC_ERRNO_ define describing the error. After setting ::arc_errno it is strongly recommended to log that the error happened (this really helps when debugging).

Basic Example

#include <arc/std/errno.h>

void ARC_Example_ThrowNullError(){
    uint32_t *exampleNumber = NULL;

    if(exampleNumber == NULL){
        //set arc_errno to the most applicable error
        arc_errno = ARC_ERRNO_NULL;

        //log where the error happened
        ARC_DEBUG_LOG_ERROR("ARC_Example_ThrowNullError(), exampleNumber was NULL");
    }
}

Error Handling With More Information

Sometimes the error will have data that could be helpful when debugging, so there is also a log macro that can log the error with variables added. The formatting is the same as printf.

Example With Variables

#include <arc/std/errno.h>
#include <arc/std/string.h>

void ARC_Example_ThrowPathError(ARC_String *path){
    ARC_String *data;
    /* .. try loading data from path here ..  */

    if(data == NULL){
        //set arc_errno to the most applicable error
        arc_errno = ARC_ERRNO_DATA;

        //log where the error happened
        ARC_DEBUG_LOG_ERROR_WITH_VARIABLES("ARC_Example_ThrowPathError(path), path was %s", path->data);
    }
}

Overriding Output File

By default the logs will output to stdout, however this can has an override by defining ARC_DEBUG_LOG_STREAM_OVERRIDE

Example of Overriding Log Stream:

//when compiling, ARC_DEBUG_LOG_STREAM_OVERRIDE needs to be set, then just add

//this sets the output to a example_out.txt file that has write permissions
arc_errno_log_file = fopen("example_out.txt", "w");