ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
Loading...
Searching...
No Matches
asciichat_errno.h
Go to the documentation of this file.
1#pragma once
2
3#include <stdbool.h>
4#include <stddef.h>
5#include <stdint.h>
6// Platform-specific includes
7#ifndef _WIN32
8#include <unistd.h>
9#endif
10
11// Platform-specific includes - must be first
12#include "common.h"
13// Include logging.h for log_error macro used in SET_ERRNO
14#include "log/logging.h"
15
87/* ============================================================================
88 * ascii-chat Error Number System
89 * ============================================================================
90 * Thread-local error context that captures:
91 * - Error code (asciichat_error_t)
92 * - File, line, function where error occurred
93 * - Stack trace at error site (debug builds)
94 * - Optional custom context message
95 * - Timestamp of error
96 * - System error context (errno)
97 */
98
127
128/* ============================================================================
129 * Thread-Local Error Storage
130 * ============================================================================
131 */
132
145#ifdef __cplusplus
146extern "C" {
147#endif
148
150
162extern __thread asciichat_error_t asciichat_errno;
163
164/* ============================================================================
165 * Library Error Setting Macros
166 * ============================================================================
167 * Use these in lib/ code to set error context
168 */
169
181#ifdef NDEBUG
182#define SET_ERRNO(code, context_msg, ...) \
183 ({ \
184 asciichat_set_errno_with_message(code, NULL, 0, NULL, context_msg, ##__VA_ARGS__); \
185 log_error("SET_ERRNO: " context_msg " (code: %d, meaning: %s)", ##__VA_ARGS__, code, \
186 asciichat_error_string(code)); \
187 (code); \
188 })
189#else
190#define SET_ERRNO(code, context_msg, ...) \
191 ({ \
192 asciichat_set_errno_with_message(code, __FILE__, __LINE__, __func__, context_msg, ##__VA_ARGS__); \
193 log_error("SET_ERRNO: " context_msg " (code: %d, meaning: %s)", ##__VA_ARGS__, code, \
194 asciichat_error_string(code)); \
195 (code); \
196 })
197#endif
198
210#ifdef NDEBUG
211#define SET_ERRNO_SYS(code, context_msg, ...) \
212 ({ \
213 int captured_errno = platform_get_last_error(); \
214 asciichat_set_errno_with_system_error_and_message(code, NULL, 0, NULL, captured_errno, context_msg, \
215 ##__VA_ARGS__); \
216 log_error("SETERRNO_SYS: " context_msg " (code: %d - %s, system error: %d - %s)", ##__VA_ARGS__, code, \
217 asciichat_error_string(code), captured_errno, platform_strerror(captured_errno)); \
218 (code); \
219 })
220#else
221#define SET_ERRNO_SYS(code, context_msg, ...) \
222 ({ \
223 int captured_errno = platform_get_last_error(); \
224 asciichat_set_errno_with_system_error_and_message(code, __FILE__, __LINE__, __func__, captured_errno, context_msg, \
225 ##__VA_ARGS__); \
226 log_error("SETERRNO_SYS: " context_msg " (code: %d - %s, system error: %d - %s)", ##__VA_ARGS__, code, \
227 asciichat_error_string(code), captured_errno, platform_strerror(captured_errno)); \
228 (code); \
229 })
230#endif
231
232/* ============================================================================
233 * Error Logging Integration Macros
234 * ============================================================================
235 * These macros combine error setting with automatic logging
236 */
237
238/* ============================================================================
239 * Application Error Checking Macros
240 * ============================================================================
241 * Use these in src/ code to check and handle errors
242 */
243
254#define HAS_ERRNO(var) asciichat_has_errno(var)
255
259#define CLEAR_ERRNO() asciichat_clear_errno()
260
264#define GET_ERRNO() asciichat_get_errno()
265
266/* ============================================================================
267 * Implementation Functions
268 * @{
269 */
270
290void asciichat_set_errno(asciichat_error_t code, const char *file, int line, const char *function,
291 const char *context_message);
292
313void asciichat_set_errno_with_message(asciichat_error_t code, const char *file, int line, const char *function,
314 const char *format, ...);
315
335void asciichat_set_errno_with_system_error(asciichat_error_t code, const char *file, int line, const char *function,
336 int sys_errno);
337
359void asciichat_set_errno_with_system_error_and_message(asciichat_error_t code, const char *file, int line,
360 const char *function, int sys_errno, const char *format, ...);
361
379void asciichat_set_errno_with_wsa_error(asciichat_error_t code, const char *file, int line, const char *function,
380 int wsa_error);
381
399
413bool asciichat_has_wsa_error(void);
414
430void asciichat_clear_errno(void);
431
447
470void asciichat_fatal_with_context(asciichat_error_t code, const char *file, int line, const char *function,
471 const char *format, ...);
472
488
491/* ============================================================================
492 * Debug/Development Utilities
493 * ============================================================================
494 */
495
496#if defined(DEBUG) || defined(ENABLE_ERRNO_BACKTRACES)
501#define PRINT_ERRNO_CONTEXT(context) asciichat_print_error_context(context)
502
506#define ASSERT_NO_ERRNO() \
507 do { \
508 asciichat_error_t err = asciichat_get_errno(); \
509 if (err != ASCIICHAT_OK) { \
510 asciichat_error_context_t ctx; \
511 asciichat_has_errno(&ctx); \
512 asciichat_print_error_context(&ctx); \
513 abort(); \
514 } \
515 } while (0)
516
520#define PRINT_ERRNO_IF_ERROR() \
521 do { \
522 asciichat_error_context_t ctx; \
523 if (HAS_ERRNO(&ctx)) { \
524 asciichat_print_error_context(&ctx); \
525 } \
526 } while (0)
527
528#else
529// No-op versions for Release builds
530#define PRINT_ERRNO_CONTEXT(context) ((void)(context))
531#define ASSERT_NO_ERRNO() ((void)0)
532#define PRINT_ERRNO_IF_ERROR() ((void)0)
533#endif
534
535/* ============================================================================
536 * Error Statistics and Monitoring
537 * @{
538 */
539
562
575
589
603
616
631
634/* ============================================================================
635 * Thread-Safe Error Propagation
636 * @{
637 *
638 * For multi-threaded scenarios where errors need to be propagated across
639 * thread boundaries. These functions allow storing error codes per-thread
640 * for later retrieval.
641 */
642
657
672
684
687/* ============================================================================
688 * Error System Control Functions
689 * @{
690 */
691
705void asciichat_errno_suppress(bool suppress);
706
719void asciichat_errno_cleanup(void);
720
723/* ============================================================================
724 * Application Error Checking Macros
725 * ============================================================================
726 * Use these in src/ code to check and log errors
727 */
728
738#define LOG_ERRNO_IF_SET(message) \
739 do { \
740 if (asciichat_errno != ASCIICHAT_OK) { \
741 asciichat_print_error_context(&asciichat_errno_context); \
742 } \
743 } while (0)
744
755#define LOG_ERRNO_IF_CODE(code, message) \
756 do { \
757 if (asciichat_errno == (code)) { \
758 asciichat_print_error_context(&asciichat_errno_context); \
759 } \
760 } while (0)
761
762#ifdef __cplusplus
763}
764#endif
765
/* errno */
int thread_id
unsigned long long uint64_t
Definition common.h:59
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.
bool asciichat_has_wsa_error(void)
Check if current error has WSA error code.
void asciichat_clear_thread_error(int thread_id)
Clear error code for a specific thread.
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_errno_suppress(bool suppress)
Suppress error logging and reporting.
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)
__thread asciichat_error_t asciichat_errno
Thread-local current error code.
void asciichat_error_stats_print(void)
Print error statistics to stderr.
bool asciichat_has_errno(asciichat_error_context_t *context)
Check if error occurred and get full context.
void asciichat_clear_errno(void)
Clear the current error state.
asciichat_error_t asciichat_get_thread_error(int thread_id)
Get error code for a specific thread.
asciichat_error_t asciichat_get_errno(void)
Get current error code.
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)
asciichat_error_stats_t asciichat_error_stats_get(void)
Get current error statistics.
void asciichat_error_stats_init(void)
Initialize error statistics system.
void asciichat_errno_cleanup(void)
Cleanup error system resources.
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_error_stats_reset(void)
Reset all error statistics to zero.
__thread asciichat_error_context_t asciichat_errno_context
Thread-local error context storage.
void asciichat_set_thread_error(int thread_id, asciichat_error_t code)
Set error code for a specific thread.
void asciichat_print_error_context(const asciichat_error_context_t *context)
Print full error context to stderr.
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_error_stats_record(asciichat_error_t code)
Record an error in statistics.
asciichat_error_t
Error and exit codes - unified status values (0-255)
Definition error_codes.h:46
📝 Logging API with multiple log levels and terminal output control
Error context structure.
int wsa_error
Windows socket error code (if applicable, 0 otherwise)
int system_errno
System errno value (if applicable, 0 otherwise)
const char * function
Function name where error occurred (NULL in release builds)
char * context_message
Optional custom message (dynamically allocated, owned by system)
uint64_t timestamp
Timestamp when error occurred (microseconds since epoch)
char ** backtrace_symbols
Stack trace symbol strings (debug builds only)
bool has_system_error
True if system_errno is valid.
const char * file
Source file where error occurred (NULL in release builds)
int stack_depth
Number of stack frames captured (0 if not captured)
int line
Line number where error occurred (0 in release builds)
bool has_wsa_error
True if wsa_error is valid.
asciichat_error_t code
Error code (asciichat_error_t enum value)
Error statistics structure.
uint64_t last_error_time
Timestamp of last error (microseconds since epoch)
asciichat_error_t last_error_code
Error code of last error recorded.
uint64_t total_errors
Total number of errors recorded (sum of all error_counts)
Common SIMD utilities and structures.