|
ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
|
⚠️ Thread-Local Error Number System with Context More...
Files | |
| file | asciichat_errno.c |
| 🚨 Custom error code system with formatted messages, thread-local storage, and errno mapping | |
| file | asciichat_errno.h |
| ⚠️‼️ Error and/or exit() when things go bad. | |
Data Structures | |
| struct | asciichat_error_context_t |
| Error context structure. More... | |
| struct | asciichat_error_stats_t |
| Error statistics structure. More... | |
Macros | |
| #define | SET_ERRNO(code, context_msg, ...) |
| Set error code with custom context message and log it. | |
| #define | SET_ERRNO_SYS(code, context_msg, ...) |
| Set error code with custom message and system error context. | |
| #define | HAS_ERRNO(var) asciichat_has_errno(var) |
| Check if an error occurred and get full context. | |
| #define | CLEAR_ERRNO() asciichat_clear_errno() |
| Clear the current error state. | |
| #define | GET_ERRNO() asciichat_get_errno() |
| Get current error code (0 if no error) | |
Functions | |
| void | asciichat_set_errno (asciichat_error_t code, const char *file, int line, const char *function, const char *context_message) |
| Set error code with basic context. | |
| void | asciichat_set_errno_with_message (asciichat_error_t code, const char *file, int line, const char *function, const char *format,...) |
| Set error code with formatted message. | |
| void | asciichat_set_errno_with_system_error (asciichat_error_t code, const char *file, int line, const char *function, int sys_errno) |
| Set error code with system error (errno) | |
| void | asciichat_set_errno_with_system_error_and_message (asciichat_error_t code, const char *file, int line, const char *function, int sys_errno, const char *format,...) |
| Set error code with system error and formatted message. | |
| void | asciichat_set_errno_with_wsa_error (asciichat_error_t code, const char *file, int line, const char *function, int wsa_error) |
| Set error code with Windows socket error (WSA error) | |
| bool | asciichat_has_errno (asciichat_error_context_t *context) |
| Check if error occurred and get full context. | |
| bool | asciichat_has_wsa_error (void) |
| Check if current error has WSA error code. | |
| void | asciichat_clear_errno (void) |
| Clear the current error state. | |
| asciichat_error_t | asciichat_get_errno (void) |
| Get current error code. | |
| void | asciichat_fatal_with_context (asciichat_error_t code, const char *file, int line, const char *function, const char *format,...) |
| Exit with error code and context (used by FATAL macro) | |
| void | asciichat_print_error_context (const asciichat_error_context_t *context) |
| Print full error context to stderr. | |
| void | asciichat_error_stats_init (void) |
| Initialize error statistics system. | |
| void | asciichat_error_stats_record (asciichat_error_t code) |
| Record an error in statistics. | |
| void | asciichat_error_stats_print (void) |
| Print error statistics to stderr. | |
| void | asciichat_error_stats_reset (void) |
| Reset all error statistics to zero. | |
| asciichat_error_stats_t | asciichat_error_stats_get (void) |
| Get current error statistics. | |
| asciichat_error_t | asciichat_get_thread_error (int thread_id) |
| Get error code for a specific thread. | |
| void | asciichat_set_thread_error (int thread_id, asciichat_error_t code) |
| Set error code for a specific thread. | |
| void | asciichat_clear_thread_error (int thread_id) |
| Clear error code for a specific thread. | |
| void | asciichat_errno_suppress (bool suppress) |
| Suppress error logging and reporting. | |
| void | asciichat_errno_cleanup (void) |
| Cleanup error system resources. | |
Variables | |
| __thread asciichat_error_context_t | asciichat_errno_context |
| Thread-local error context storage. | |
| __thread asciichat_error_t | asciichat_errno |
| Thread-local current error code. | |
⚠️ Thread-Local Error Number System with Context
This header provides a comprehensive thread-local error number system that captures full error context including location, stack traces, and system errors. The system integrates with ascii-chat's error handling to provide detailed debugging information.
The system captures:
Use SET_ERRNO and SET_ERRNO_SYS in lib/ code:
Use HAS_ERRNO, GET_ERRNO, CLEAR_ERRNO in src/ code:
In debug builds, the system:
The Error Handling System provides comprehensive error tracking and reporting for ascii-chat. It uses typed error codes, error context capture, and thread-local storage for safe multi-threaded error handling.
Implementation: lib/asciichat_errno.h
Key Features:
Error Code System:
asciichat_error_t instead of intThread-Local Storage:
Error Context Capture:
SET_ERRNO Macro (preferred):
SET_ERRNO_SYS Macro (captures system errno):
Check for Error:
Get Current Error Code:
Clear Error State:
Print Error Context (debug builds only):
General Errors:
Network Errors:
Crypto Errors:
Platform Errors:
Configuration Errors:
Automatic Logging:
Example Integration:
DO:
DON'T:
Thread-Local Storage:
Example Multi-threaded Usage:
| #define CLEAR_ERRNO | ( | ) | asciichat_clear_errno() |
#include <asciichat_errno.h>
Clear the current error state.
Definition at line 259 of file asciichat_errno.h.
| #define GET_ERRNO | ( | ) | asciichat_get_errno() |
#include <asciichat_errno.h>
Get current error code (0 if no error)
Definition at line 264 of file asciichat_errno.h.
| #define HAS_ERRNO | ( | var | ) | asciichat_has_errno(var) |
#include <asciichat_errno.h>
Check if an error occurred and get full context.
| var | Variable to store error context |
Usage in src/ code: asciichat_error_context_t err_ctx; if (HAS_ERRNO(&err_ctx)) { FATAL(err_ctx.code, "Library error occurred"); }
Definition at line 254 of file asciichat_errno.h.
| #define SET_ERRNO | ( | code, | |
| context_msg, | |||
| ... | |||
| ) |
#include <asciichat_errno.h>
Set error code with custom context message and log it.
| code | Error code to set |
| context_msg | Custom message explaining the error |
Usage in lib/ code: if (bind(sockfd, ...) < 0) { SET_ERRNO(ERROR_NETWORK_BIND, "Cannot bind to port %d", port); return ERROR_NETWORK_BIND; }
Definition at line 190 of file asciichat_errno.h.
| #define SET_ERRNO_SYS | ( | code, | |
| context_msg, | |||
| ... | |||
| ) |
#include <asciichat_errno.h>
Set error code with custom message and system error context.
| code | Error code to set |
| context_msg | Custom message explaining the error |
Usage in lib/ code: if (open(file, O_RDONLY) < 0) { SET_ERRNO_SYS(ERROR_CONFIG, "Failed to open config file: %s", path); return ERROR_CONFIG; }
Definition at line 221 of file asciichat_errno.h.
| void asciichat_clear_errno | ( | void | ) |
#include <asciichat_errno.h>
Clear the current error state.
Clears the thread-local error state, resetting error code to ASCIICHAT_OK and freeing any associated context message. This is the low-level function used by CLEAR_ERRNO() macro.
Definition at line 241 of file asciichat_errno.c.
References asciichat_errno_context, ASCIICHAT_OK, asciichat_error_context_t::backtrace_symbols, asciichat_error_context_t::code, asciichat_error_context_t::context_message, errno, asciichat_error_context_t::has_system_error, asciichat_error_context_t::has_wsa_error, platform_backtrace_symbols_free(), SAFE_FREE, asciichat_error_context_t::system_errno, and asciichat_error_context_t::wsa_error.
| void asciichat_clear_thread_error | ( | int | thread_id | ) |
#include <asciichat_errno.h>
Clear error code for a specific thread.
| thread_id | Thread ID to clear error for |
Clears the error code for a specific thread, resetting it to ASCIICHAT_OK.
Definition at line 468 of file asciichat_errno.c.
References MAX_THREAD_ERRORS, thread_id, and valid.
| void asciichat_errno_cleanup | ( | void | ) |
#include <asciichat_errno.h>
Cleanup error system resources.
Cleans up error system resources including statistics and thread error storage. Should be called at application shutdown after all error handling is complete.
Definition at line 486 of file asciichat_errno.c.
References asciichat_errno_context, ASCIICHAT_OK, asciichat_error_context_t::backtrace_symbols, asciichat_error_context_t::code, asciichat_error_context_t::context_message, platform_backtrace_symbols_free(), and SAFE_FREE.
Referenced by __attribute__(), asciichat_shared_init(), client_audio_render_thread(), client_receive_thread(), client_send_thread_func(), client_video_render_thread(), server_main(), and stats_logger_thread().
| void asciichat_errno_suppress | ( | bool | suppress | ) |
#include <asciichat_errno.h>
Suppress error logging and reporting.
| suppress | If true, suppress error logging; if false, enable logging |
Controls whether errors are automatically logged when set. When suppressed, errors are still set in thread-local storage but logging is disabled. Useful for testing or when error logging would be too verbose.
Definition at line 482 of file asciichat_errno.c.
| asciichat_error_stats_t asciichat_error_stats_get | ( | void | ) |
#include <asciichat_errno.h>
Get current error statistics.
Retrieves a copy of the current error statistics. Returns all statistics including per-error-code counts, total errors, and last error information.
Definition at line 427 of file asciichat_errno.c.
References asciichat_error_stats_init().
| void asciichat_error_stats_init | ( | void | ) |
#include <asciichat_errno.h>
Initialize error statistics system.
Initializes the error statistics tracking system. Must be called before recording any error statistics. Statistics are initialized to zero.
Definition at line 374 of file asciichat_errno.c.
Referenced by asciichat_error_stats_get(), and asciichat_error_stats_record().
| void asciichat_error_stats_print | ( | void | ) |
#include <asciichat_errno.h>
Print error statistics to stderr.
Prints comprehensive error statistics including per-error-code counts, total errors, and last error information. Useful for periodic monitoring and debugging.
Definition at line 394 of file asciichat_errno.c.
References ASCIICHAT_OK, asciichat_error_stats_t::error_counts, asciichat_error_stats_t::last_error_code, asciichat_error_stats_t::last_error_time, log_plain, platform_localtime(), and asciichat_error_stats_t::total_errors.
Referenced by server_main(), and stats_logger_thread().
| void asciichat_error_stats_record | ( | asciichat_error_t | code | ) |
#include <asciichat_errno.h>
Record an error in statistics.
| code | Error code to record (asciichat_error_t) |
Records an error occurrence in the statistics system. Updates per-error-code count, total error count, and last error information.
Definition at line 381 of file asciichat_errno.c.
References asciichat_error_stats_init(), asciichat_error_stats_t::error_counts, asciichat_error_stats_t::last_error_code, asciichat_error_stats_t::last_error_time, and asciichat_error_stats_t::total_errors.
Referenced by asciichat_set_errno().
| void asciichat_error_stats_reset | ( | void | ) |
#include <asciichat_errno.h>
Reset all error statistics to zero.
Resets all error statistics counters to zero. Useful for resetting statistics between test runs or monitoring periods.
Definition at line 423 of file asciichat_errno.c.
| void asciichat_fatal_with_context | ( | asciichat_error_t | code, |
| const char * | file, | ||
| int | line, | ||
| const char * | function, | ||
| const char * | format, | ||
| ... | |||
| ) |
#include <asciichat_errno.h>
Exit with error code and context (used by FATAL macro)
| code | Error code (asciichat_error_t) |
| file | Source file name (can be NULL) |
| line | Line number (0 if not provided) |
| function | Function name (can be NULL) |
| format | Format string (printf-style) |
| ... | Format arguments |
Low-level function used by FATAL() macro. Sets error context and exits the program with the specified error code. In debug builds, prints stack trace before exiting.
Definition at line 275 of file asciichat_errno.c.
| asciichat_error_t asciichat_get_errno | ( | void | ) |
#include <asciichat_errno.h>
Get current error code.
Returns the current thread-local error code. Returns ASCIICHAT_OK if no error has occurred. This is the low-level function used by GET_ERRNO().
Definition at line 266 of file asciichat_errno.c.
References asciichat_errno_context, and asciichat_error_context_t::code.
| asciichat_error_t asciichat_get_thread_error | ( | int | thread_id | ) |
#include <asciichat_errno.h>
Get error code for a specific thread.
| thread_id | Thread ID to query |
Retrieves the error code for a specific thread. Useful for checking errors from other threads or propagating errors across thread boundaries.
Definition at line 439 of file asciichat_errno.c.
References ASCIICHAT_OK, MAX_THREAD_ERRORS, thread_id, and valid.
| bool asciichat_has_errno | ( | asciichat_error_context_t * | context | ) |
#include <asciichat_errno.h>
Check if error occurred and get full context.
| context | Output structure for error context (must not be NULL) |
Checks if an error has occurred and copies the full error context into the provided structure. This is the low-level function used by HAS_ERRNO().
Definition at line 229 of file asciichat_errno.c.
References asciichat_errno_context, ASCIICHAT_OK, and asciichat_error_context_t::code.
| bool asciichat_has_wsa_error | ( | void | ) |
#include <asciichat_errno.h>
Check if current error has WSA error code.
Checks whether the current thread-local error context includes a Windows socket error (WSA error) code. Useful for platform-specific error handling.
Definition at line 220 of file asciichat_errno.c.
References asciichat_errno_context, and asciichat_error_context_t::has_wsa_error.
| void asciichat_print_error_context | ( | const asciichat_error_context_t * | context | ) |
#include <asciichat_errno.h>
Print full error context to stderr.
| context | Error context to print (must not be NULL) |
Prints comprehensive error information including error code, location, context message, system errors, and stack trace (if available) to stderr. Useful for debugging and error reporting.
Definition at line 327 of file asciichat_errno.c.
References ASCIICHAT_OK, asciichat_error_context_t::backtrace_symbols, asciichat_error_context_t::code, asciichat_error_context_t::context_message, extract_project_relative_path(), asciichat_error_context_t::file, asciichat_error_context_t::function, asciichat_error_context_t::has_system_error, asciichat_error_context_t::line, LOG_COLOR_RESET, LOG_COLOR_WARN, log_file, log_level_color(), log_plain, platform_localtime(), platform_print_backtrace_symbols(), safe_fprintf(), SAFE_STRERROR, asciichat_error_context_t::stack_depth, asciichat_error_context_t::system_errno, and asciichat_error_context_t::timestamp.
Referenced by asciichat_fatal_with_context().
| void asciichat_set_errno | ( | asciichat_error_t | code, |
| const char * | file, | ||
| int | line, | ||
| const char * | function, | ||
| const char * | context_message | ||
| ) |
#include <asciichat_errno.h>
Set error code with basic context.
| code | Error code to set (asciichat_error_t) |
| file | Source file name (can be NULL) |
| line | Line number (0 if not provided) |
| function | Function name (can be NULL) |
| context_message | Custom context message (can be NULL, will be copied) |
Sets the thread-local error code with basic context information. This is the low-level function used by SET_ERRNO() macros.
Definition at line 117 of file asciichat_errno.c.
References asciichat_errno, asciichat_errno_context, asciichat_error_stats_record(), asciichat_error_context_t::backtrace, asciichat_error_context_t::backtrace_symbols, asciichat_error_context_t::code, asciichat_error_context_t::context_message, asciichat_error_context_t::file, asciichat_error_context_t::function, asciichat_error_context_t::has_system_error, asciichat_error_context_t::line, log_error, platform_backtrace_symbols_free(), SAFE_FREE, SAFE_MALLOC, SAFE_STRNCPY, asciichat_error_context_t::stack_depth, and asciichat_error_context_t::timestamp.
Referenced by asciichat_set_errno_with_message(), asciichat_set_errno_with_system_error(), asciichat_set_errno_with_system_error_and_message(), and asciichat_set_errno_with_wsa_error().
| void asciichat_set_errno_with_message | ( | asciichat_error_t | code, |
| const char * | file, | ||
| int | line, | ||
| const char * | function, | ||
| const char * | format, | ||
| ... | |||
| ) |
#include <asciichat_errno.h>
Set error code with formatted message.
| code | Error code to set (asciichat_error_t) |
| file | Source file name (can be NULL) |
| line | Line number (0 if not provided) |
| function | Function name (can be NULL) |
| format | Format string (printf-style) |
| ... | Format arguments |
Sets the thread-local error code with a formatted context message. This is the low-level function used by SET_ERRNO() macros.
Definition at line 174 of file asciichat_errno.c.
References asciichat_set_errno(), format_message(), and SAFE_FREE.
| void asciichat_set_errno_with_system_error | ( | asciichat_error_t | code, |
| const char * | file, | ||
| int | line, | ||
| const char * | function, | ||
| int | sys_errno | ||
| ) |
#include <asciichat_errno.h>
Set error code with system error (errno)
| code | Error code to set (asciichat_error_t) |
| file | Source file name (can be NULL) |
| line | Line number (0 if not provided) |
| function | Function name (can be NULL) |
| sys_errno | System errno value |
Sets the thread-local error code with system error context. Captures the system errno value for detailed error reporting.
Definition at line 189 of file asciichat_errno.c.
References asciichat_errno_context, asciichat_set_errno(), asciichat_error_context_t::has_system_error, and asciichat_error_context_t::system_errno.
| void asciichat_set_errno_with_system_error_and_message | ( | asciichat_error_t | code, |
| const char * | file, | ||
| int | line, | ||
| const char * | function, | ||
| int | sys_errno, | ||
| const char * | format, | ||
| ... | |||
| ) |
#include <asciichat_errno.h>
Set error code with system error and formatted message.
| code | Error code to set (asciichat_error_t) |
| file | Source file name (can be NULL) |
| line | Line number (0 if not provided) |
| function | Function name (can be NULL) |
| sys_errno | System errno value |
| format | Format string (printf-style) |
| ... | Format arguments |
Sets the thread-local error code with both system error context and a formatted message. This is the low-level function used by SET_ERRNO_SYS().
Definition at line 196 of file asciichat_errno.c.
References asciichat_errno_context, asciichat_set_errno(), format_message(), asciichat_error_context_t::has_system_error, SAFE_FREE, and asciichat_error_context_t::system_errno.
| void asciichat_set_errno_with_wsa_error | ( | asciichat_error_t | code, |
| const char * | file, | ||
| int | line, | ||
| const char * | function, | ||
| int | wsa_error | ||
| ) |
#include <asciichat_errno.h>
Set error code with Windows socket error (WSA error)
| code | Error code to set (asciichat_error_t) |
| file | Source file name (can be NULL) |
| line | Line number (0 if not provided) |
| function | Function name (can be NULL) |
| wsa_error | Windows socket error code |
Sets the thread-local error code with Windows-specific socket error context. Used for Windows socket operations that fail with WSA error codes.
Definition at line 213 of file asciichat_errno.c.
References asciichat_errno_context, asciichat_set_errno(), asciichat_error_context_t::has_wsa_error, and asciichat_error_context_t::wsa_error.
Referenced by accept_with_timeout().
| void asciichat_set_thread_error | ( | int | thread_id, |
| asciichat_error_t | code | ||
| ) |
#include <asciichat_errno.h>
Set error code for a specific thread.
| thread_id | Thread ID to set error for |
| code | Error code to set (asciichat_error_t) |
Sets the error code for a specific thread. Useful for propagating errors from one thread to another or storing thread-specific error states.
Definition at line 448 of file asciichat_errno.c.
References MAX_THREAD_ERRORS, thread_id, and valid.
|
extern |
#include <asciichat_errno.h>
Thread-local current error code.
Current error code for the calling thread. Set to ASCIICHAT_OK when no error. Updated automatically when errors are set via SET_ERRNO() macros.
Definition at line 39 of file asciichat_errno.c.
Referenced by accept_with_timeout(), and asciichat_set_errno().
|
extern |
#include <asciichat_errno.h>
Thread-local error context storage.
Each thread has its own independent error context. This ensures thread-safe error handling without synchronization overhead.
Definition at line 27 of file asciichat_errno.c.
Referenced by accept_with_timeout(), asciichat_clear_errno(), asciichat_errno_cleanup(), asciichat_fatal_with_context(), asciichat_get_errno(), asciichat_has_errno(), asciichat_has_wsa_error(), asciichat_set_errno(), asciichat_set_errno_with_system_error(), asciichat_set_errno_with_system_error_and_message(), and asciichat_set_errno_with_wsa_error().