### Configuration Macros Source: https://github.com/z-libs/zerror.h/blob/main/README.md Macros that can be defined before including zerror.h to modify the library's behavior. ```APIDOC ## Configuration Define these macros **before** including `zerror.h` to modify behavior: - `ZERROR_IMPLEMENTATION`: Define in **exactly one** `.c` or `.cpp` file to generate logic. - `ZERROR_SHORT_NAMES`: Enables shorter aliases (for example, `try`, `check`, `ensure`). - `ZERROR_DEBUG`: Enables hardware traps/breakpoints at the exact moment an error is created. - `ZERROR_ENABLE_TRACE`: Enables the collection of propagation traces. - `ZERROR_NO_COLOR`: Disables ANSI color codes in `zerr_print`. - `ZERROR_PANIC_ACTION`: Define to override the default `abort()` behavior. ``` -------------------------------- ### C Error Handling with Result Types and Propagation Source: https://github.com/z-libs/zerror.h/blob/main/README.md Demonstrates defining custom Result types and using the 'try' macro for automatic error propagation in C. Ensure ZERROR_IMPLEMENTATION is defined before including zerror.h. ```c #define ZERROR_IMPLEMENTATION #include "zerror.h" // Define a Result type that can return an int or a zerr. DEFINE_RESULT(int, ResInt) ResInt calculate(int val) { if (val < 0) { // Creates error with file/line/func context. return ResInt_err(zerr_create(Z_EINVAL, "Value cannot be negative: %d", val)); } return ResInt_ok(val * 2); } zres process() { // 'try' automatically unwraps value or returns error to caller. int x = try(calculate(-5)); printf("Result: %d\n", x); return zres_ok(); } int main() { // 'run' executes the logic and prints a formatted trace on failure. return run(process()); } ``` -------------------------------- ### z_log Namespace Functions Source: https://github.com/z-libs/zerror.h/blob/main/README.md Provides functions for logging messages at different severity levels using C-style formatting or std::string. ```APIDOC ## Namespace z_log ### Description Functions for logging messages. ### Functions - `z_log::info(fmt, ...)`: Logs an info message using C-style formatting. - `z_log::warn(fmt, ...)`: Logs a warning message using C-style formatting. - `z_log::error(fmt, ...)`: Logs an error message using C-style formatting. - `z_log::info(str)`: Logs a `std::string` as info. - `z_log::error(str)`: Logs a `std::string` as error. ``` -------------------------------- ### C++ Result Type for Error Handling Source: https://github.com/z-libs/zerror.h/blob/main/README.md Illustrates using the C++ wrapper for zerror.h, providing a template-based `result` similar to `std::expected`. Include zerror.h after defining ZERROR_IMPLEMENTATION. ```cpp #include #define ZERROR_IMPLEMENTATION #include "zerror.h" z_error::result compute(int n) { if (n == 0) { return zerr_create(Z_ERR, "Division by zero"); } return 100 / n; } int main() { auto res = compute(0); if (!res) { std::cerr << "Caught error: " << res.err << std::endl; return 1; } std::cout << "Value: " << res.unwrap_val() << std::endl; return 0; } ``` -------------------------------- ### ztry Macro Source: https://github.com/z-libs/zerror.h/blob/main/README.md A macro that simplifies error handling by unwrapping a result or returning the error immediately. ```APIDOC ## Macro ztry ### Description Statement expression that unwraps a `result` or returns the error immediately. ### Usage `ztry(expression)` ``` -------------------------------- ### z_error::result Class Source: https://github.com/z-libs/zerror.h/blob/main/README.md Represents a type that is either a success value of type T or an error. Provides methods to check status and access the value or error. ```APIDOC ## Class z_error::result ### Description A type that holds either a success value or an error. ### Constructors - `result(val)`: Creates a success result (move or copy). - `result(err)`: Creates a failure result from `zerr` or `zres`. ### Member Functions - `ok()`: Returns `true` if the result contains a value (success). - `unwrap_val()`: Returns the contained value or panics if it is an error. - `operator bool()`: Implicit conversion to boolean (true = success). ### Members - `err`: Public member accessing the underlying `zerr` struct (valid only if !ok()). ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.