ascii-chat 0.11.33
Video chat in your terminal
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#include "common.h"
12
208/* ============================================================================
209 * ascii-chat Error Number System
210 * ============================================================================
211 * Thread-local error context that captures:
212 * - Error code (asciichat_error_t)
213 * - File, line, function where error occurred
214 * - Stack trace at error site (debug builds)
215 * - Optional custom context message
216 * - Timestamp of error
217 * - System error context (errno)
218 */
219
220/* Forward declaration of backtrace_t - will be typedef'd next */
221typedef struct {
222 void *ptrs[32];
223 char **symbols;
224 int count;
227
254
255/* ============================================================================
256 * Thread-Local Error Storage
257 * ============================================================================
258 */
259
272/* Include logging early before extern "C" to avoid C++ headers inside extern "C" */
273#include "log/log.h"
274
275#ifdef __cplusplus
276extern "C" {
277#endif
278
280
292extern __thread asciichat_error_t asciichat_errno;
293
294/* ============================================================================
295 * Library Error Setting Macros
296 * ============================================================================
297 * Use these in lib/ code to set error context
298 */
299
310#ifdef NDEBUG
311#define SET_ERRNO(code, context_msg, ...) \
312 (asciichat_set_errno_with_message(code, NULL, 0, NULL, context_msg, ##__VA_ARGS__), \
313 log_error("SET_ERRNO: " context_msg " (code: %d, meaning: %s)", ##__VA_ARGS__, code, asciichat_error_string(code)), \
314 (code))
315#else
316#define SET_ERRNO(code, context_msg, ...) \
317 (asciichat_set_errno_with_message(code, __FILE__, __LINE__, __func__, context_msg, ##__VA_ARGS__), \
318 log_error("SET_ERRNO: " context_msg " (code: %d, meaning: %s)", ##__VA_ARGS__, code, asciichat_error_string(code)), \
319 (code))
320#endif
321
332#ifdef NDEBUG
333#define SET_ERRNO_SYS(code, context_msg, ...) \
334 (asciichat_set_errno_with_system_error_and_message(code, NULL, 0, NULL, platform_get_last_error(), context_msg, \
335 ##__VA_ARGS__), \
336 log_error("SETERRNO_SYS: " context_msg " (code: %d - %s, system error: %d - %s)", ##__VA_ARGS__, code, \
337 asciichat_error_string(code), platform_get_last_error(), platform_strerror(platform_get_last_error())), \
338 (code))
339#else
340#define SET_ERRNO_SYS(code, context_msg, ...) \
341 (asciichat_set_errno_with_system_error_and_message(code, __FILE__, __LINE__, __func__, platform_get_last_error(), \
342 context_msg, ##__VA_ARGS__), \
343 log_error("SETERRNO_SYS: " context_msg " (code: %d - %s, system error: %d - %s)", ##__VA_ARGS__, code, \
344 asciichat_error_string(code), platform_get_last_error(), platform_strerror(platform_get_last_error())), \
345 (code))
346#endif
347
348/* ============================================================================
349 * Error Logging Integration Macros
350 * ============================================================================
351 * These macros combine error setting with automatic logging
352 */
353
354/* ============================================================================
355 * Application Error Checking Macros
356 * ============================================================================
357 * Use these in src/ code to check and handle errors
358 */
359
370#define HAS_ERRNO(var) asciichat_has_errno(var)
371
375#define CLEAR_ERRNO() asciichat_clear_errno()
376
380#define GET_ERRNO() asciichat_get_errno()
381
382/* ============================================================================
383 * Implementation Functions
384 * @{
385 */
386
406void asciichat_set_errno(asciichat_error_t code, const char *file, int line, const char *function,
407 const char *context_message);
408
429void asciichat_set_errno_with_message(asciichat_error_t code, const char *file, int line, const char *function,
430 const char *format, ...);
431
451void asciichat_set_errno_with_system_error(asciichat_error_t code, const char *file, int line, const char *function,
452 int sys_errno);
453
475void asciichat_set_errno_with_system_error_and_message(asciichat_error_t code, const char *file, int line,
476 const char *function, int sys_errno, const char *format, ...);
477
495void asciichat_set_errno_with_wsa_error(asciichat_error_t code, const char *file, int line, const char *function,
496 int wsa_error);
497
515
529bool asciichat_has_wsa_error(void);
530
546void asciichat_clear_errno(void);
547
563
586void asciichat_fatal_with_context(asciichat_error_t code, const char *file, int line, const char *function,
587 const char *format, ...);
588
604
607/* ============================================================================
608 * Debug/Development Utilities
609 * ============================================================================
610 */
611
612#if defined(DEBUG) || defined(ENABLE_ERRNO_BACKTRACES)
617#define PRINT_ERRNO_CONTEXT(context) asciichat_print_error_context(context)
618
622#define ASSERT_NO_ERRNO() \
623 do { \
624 asciichat_error_t err = asciichat_get_errno(); \
625 if (err != ASCIICHAT_OK) { \
626 asciichat_error_context_t ctx; \
627 asciichat_has_errno(&ctx); \
628 asciichat_print_error_context(&ctx); \
629 abort(); \
630 } \
631 } while (0)
632
636#define PRINT_ERRNO_IF_ERROR() \
637 do { \
638 asciichat_error_context_t ctx; \
639 if (HAS_ERRNO(&ctx)) { \
640 asciichat_print_error_context(&ctx); \
641 } \
642 } while (0)
643
644#else
645// No-op versions for Release builds
646#define PRINT_ERRNO_CONTEXT(context) ((void)(context))
647#define ASSERT_NO_ERRNO() ((void)0)
648#define PRINT_ERRNO_IF_ERROR() ((void)0)
649#endif
650
651/* ============================================================================
652 * Error Statistics and Monitoring
653 * @{
654 */
655
678
691
705
719
732
747
750/* ============================================================================
751 * Thread-Safe Error Propagation
752 * @{
753 *
754 * For multi-threaded scenarios where errors need to be propagated across
755 * thread boundaries. These functions allow storing error codes per-thread
756 * for later retrieval.
757 */
758
773
788
800
803/* ============================================================================
804 * Error System Control Functions
805 * @{
806 */
807
821void asciichat_errno_suppress(bool suppress);
822
835void asciichat_errno_destroy(void);
836
839/* ============================================================================
840 * Application Error Checking Macros
841 * ============================================================================
842 * Use these in src/ code to check and log errors
843 */
844
854#define LOG_ERRNO_IF_SET(message) \
855 do { \
856 if (asciichat_errno != ASCIICHAT_OK) { \
857 asciichat_print_error_context(&asciichat_errno_context); \
858 } \
859 } while (0)
860
871#define LOG_ERRNO_IF_CODE(code, message) \
872 do { \
873 if (asciichat_errno == (code)) { \
874 asciichat_print_error_context(&asciichat_errno_context); \
875 } \
876 } while (0)
877
878#ifdef __cplusplus
879}
880#endif
881
/* 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_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_errno_destroy(void)
Cleanup error system resources.
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:49
📝 Logging API with multiple log levels and terminal output control
ClangTool/LibTooling compatibility shim for stdbool.h.
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)
bool has_system_error
True if system_errno is valid.
const char * file
Source file where error occurred (NULL in release builds)
int line
Line number where error occurred (0 in release builds)
bool has_wsa_error
True if wsa_error is valid.
backtrace_t backtrace
Stack trace (debug builds only)
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)
int count
Number of frames captured.
bool tried_symbolize
Track if we've already tried to symbolize (prevents duplicate work)
char ** symbols
Symbolized strings (NULL until backtrace_symbolize called)
ARM NEON-accelerated ASCII rendering utilities (declarations)