ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
asciichat_errno.c File Reference

Bounded, synchronized error stacks with allocation-free recording. More...

Go to the source code of this file.

Data Structures

struct  error_frame_t
struct  error_thread
 For error thread, this is the live record that ties id, generation, top, depth together. The owning code creates it, updates it while work is active, and releases its handles and pointed-to storage before freeing the record. More...

Macros

#define ASCIICHAT_ERRNO_SUFFIX_BUFFER_SIZE   256
#define ERROR_RENDER_SLOT_COUNT   8
#define ERRNO_BUCKETS   257
#define NO_FRAME   (-1)
#define RESERVED_FRAMES   (2 * ASCIICHAT_ERRNO_MAX_THREADS)

Typedefs

typedef struct error_thread error_thread_t
 For error thread, this is the live record that ties id, generation, top, depth together. The owning code creates it, updates it while work is active, and releases its handles and pointed-to storage before freeing the record.

Functions

void errno_notifications_enable (bool enabled)
void errno_notifications_pause (void)
bool errno_notifications_pending (void)
bool errno_notifications_suppress (bool suppress)
size_t errno_notifications_snapshot (asciichat_error_context_t *out, size_t capacity, uint64_t *dropped)
void errno_notifications_dismiss (uint64_t through_id)
const char * asciichat_error_format (const char *message, asciichat_error_t code)
size_t errno_title_snapshot (asciichat_error_context_t *latest)
asciichat_error_t errno_set_with_message (asciichat_error_t code, const char *file, int line, const char *function, const char *format,...)
asciichat_error_t errno_set (asciichat_error_t code, const char *file, int line, const char *function, const char *message)
asciichat_error_t errno_set_with_system_error_and_message (asciichat_error_t code, const char *file, int line, const char *function, int sys_errno, const char *format,...)
asciichat_error_t errno_set_with_system_error (asciichat_error_t code, const char *file, int line, const char *function, int sys_errno)
asciichat_error_t errno_set_with_wsa_error (asciichat_error_t code, const char *file, int line, const char *function, int wsa_error)
bool errno_has_thread_error (uint64_t id, asciichat_error_context_t *context)
bool errno_has (asciichat_error_context_t *context)
asciichat_error_t errno_get_thread_error (uint64_t id)
asciichat_error_t errno_get (void)
bool errno_has_wsa_error (void)
bool errno_has_code_tid (uint64_t id, asciichat_error_t code)
bool errno_has_code (asciichat_error_t code)
bool errno_has_code_since (asciichat_errno_scope_t scope, asciichat_error_t code)
bool errno_peek_since (asciichat_errno_scope_t scope, asciichat_error_context_t *out)
asciichat_error_t errno_import (const asciichat_error_context_t *cause)
void errno_clear_thread_error (uint64_t id)
void errno_clear_thread_errors (uint64_t id)
void errno_clear (void)
void errno_clear_all (void)
bool errno_clear_if_top (uint64_t id, uint64_t generation, uint64_t error_id)
bool errno_clear_if_current (uint64_t id, uint64_t generation, uint64_t error_id)
asciichat_errno_scope_t errno_checkpoint (void)
asciichat_errno_scope_t errno_scope_begin (void)
void errno_scope_end (asciichat_errno_scope_t scope, asciichat_errno_outcome_t outcome)
asciichat_errno_snapshot_t errno_snapshot (uint64_t id, asciichat_error_context_t *out, size_t capacity)
size_t errno_threads_snapshot (uint64_t *out, size_t capacity)
size_t errno_history_snapshot (asciichat_errno_history_t *out, size_t capacity)
void errno_suppress (bool suppress)
void errno_destroy (void)
void errno_shutdown (void)
void asciichat_error_stats_init (void)
void asciichat_error_stats_record (asciichat_error_t code)
asciichat_error_stats_t asciichat_error_stats_get (void)
void asciichat_error_stats_reset (void)
void asciichat_error_stats_print (void)
void asciichat_print_error_context (const asciichat_error_context_t *context)
void errno_print_stacks (void)
void errno_print_history (void)
void errno_print_hash_stats (void)
void asciichat_fatal_with_context (asciichat_error_t code, const char *file, int line, const char *function, const char *format,...)
bool errno_scope_is_clean (asciichat_errno_scope_t scope)
void errno_print_chain (uint64_t id, uint64_t error_id)
void errno_request_exit (asciichat_error_t code)
asciichat_error_t errno_exit_code (void)

Detailed Description

Bounded, synchronized error stacks with allocation-free recording.

Definition in file asciichat_errno.c.

Macro Definition Documentation

◆ ASCIICHAT_ERRNO_SUFFIX_BUFFER_SIZE

#define ASCIICHAT_ERRNO_SUFFIX_BUFFER_SIZE   256

Definition at line 7 of file asciichat_errno.c.

Referenced by asciichat_error_format().

◆ ERRNO_BUCKETS

#define ERRNO_BUCKETS   257

Definition at line 29 of file asciichat_errno.c.

Referenced by errno_print_hash_stats().

◆ ERROR_RENDER_SLOT_COUNT

#define ERROR_RENDER_SLOT_COUNT   8

Definition at line 10 of file asciichat_errno.c.

Referenced by asciichat_error_format().

◆ NO_FRAME

#define NO_FRAME   (-1)

◆ RESERVED_FRAMES

#define RESERVED_FRAMES   (2 * ASCIICHAT_ERRNO_MAX_THREADS)

Definition at line 31 of file asciichat_errno.c.

Typedef Documentation

◆ error_thread_t

typedef struct error_thread error_thread_t

For error thread, this is the live record that ties id, generation, top, depth together. The owning code creates it, updates it while work is active, and releases its handles and pointed-to storage before freeing the record.

Function Documentation

◆ asciichat_error_format()

const char * asciichat_error_format ( const char * message,
asciichat_error_t code )

Format "message: description (code=N)"; NULL/empty message returns only "description (code=N)". Allocation-free and thread-local; the returned string survives the next seven calls on this thread. Copy it for longer storage. Message text is limited to ASCIICHAT_ERRNO_MESSAGE_MAX-1 bytes; the code and meaning are retained. An existing matching suffix is not duplicated. This is plain text: use SET_ERRNO(code, "%s", formatted), never pass formatted text as the printf format. SET_ERRNO already logs code/meaning automatically, so callers normally supply only their contextual message.

Definition at line 163 of file asciichat_errno.c.

163 {
165 static PLATFORM_THREAD_LOCAL unsigned next;
166 char *output = buffers[next++ % 8];
168 snprintf(suffix, sizeof(suffix), "%s (code=%d)", asciichat_error_string(code), code);
169 if (!message || !*message || !strcmp(message, asciichat_error_string(code))) {
170 snprintf(output, sizeof(buffers[0]), "%s", suffix);
171 } else {
172 size_t length = strlen(message);
173 size_t suffix_length = strlen(suffix);
174 if (length >= suffix_length && !strcmp(message + length - suffix_length, suffix))
175 snprintf(output, sizeof(buffers[0]), "%s", message);
176 else {
177 size_t limit = length < ASCIICHAT_ERRNO_MESSAGE_MAX ? length : ASCIICHAT_ERRNO_MESSAGE_MAX - 1;
178 // Do not cut a UTF-8 character in half when shortening the message.
179 while (limit && ((unsigned char)message[limit] & 0xc0) == 0x80)
180 --limit;
181 snprintf(output, sizeof(buffers[0]), "%.*s: %s", (int)limit, message, suffix);
182 }
183 }
184 return output;
185}
#define ERROR_RENDER_SLOT_COUNT
#define ASCIICHAT_ERRNO_SUFFIX_BUFFER_SIZE
#define ASCIICHAT_ERRNO_MESSAGE_MAX

References ASCIICHAT_ERRNO_MESSAGE_MAX, ASCIICHAT_ERRNO_SUFFIX_BUFFER_SIZE, ERROR_RENDER_SLOT_COUNT, and error_thread::next.

Referenced by acds_on_host_announcement_ex(), acds_send_current_host(), acip_handle_server_packet(), acip_send_image_frame_h265(), acip_server_receive_and_dispatch(), asciichat_fatal_with_context(), capture_init(), discovery_main(), discovery_session_start(), h265_client_init_media_source(), handle_audio_batch_packet(), handle_audio_opus_batch_packet(), handle_audio_opus_packet(), handle_audio_packet(), handle_image_frame_h265_packet(), log_named_format_or_original(), main(), media_source_create_interruptible(), media_source_seek(), mirror_main(), mixer_add_source(), render_file_create(), session_display_create(), session_display_encode_frame(), session_display_init_render_file(), session_display_render_frame(), session_display_write_ascii_frame(), session_render_loop(), splash_wait_for_animation(), and video_frame_buffer_create().

◆ asciichat_error_stats_get()

asciichat_error_stats_t asciichat_error_stats_get ( void )

Definition at line 760 of file asciichat_errno.c.

760 {
761 registry_lock();
762 asciichat_error_stats_t stats = g_stats;
763 registry_unlock();
764 return stats;
765}

Referenced by asciichat_error_stats_print().

◆ asciichat_error_stats_init()

void asciichat_error_stats_init ( void )

Definition at line 749 of file asciichat_errno.c.

749 {
750 registry_lock();
751 initialize_locked();
752 registry_unlock();
753}

◆ asciichat_error_stats_print()

void asciichat_error_stats_print ( void )

Definition at line 774 of file asciichat_errno.c.

774 {
776 log_plain("Errors: total=%" PRIu64 " pending=%zu threads=%zu dropped=%" PRIu64 " history_overwritten=%" PRIu64, stats.total_errors, stats.pending_errors, stats.registered_threads,
778 for (size_t i = 1; i < ARRAY_SIZE(stats.error_counts); ++i)
779 if (stats.error_counts[i])
780 log_plain(" %s: %" PRIu64, asciichat_error_string((asciichat_error_t)i), stats.error_counts[i]);
781}
asciichat_error_stats_t asciichat_error_stats_get(void)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
#define log_plain(...)
Plain logging - writes to both log file and stderr without timestamps or log levels.
Definition log/log.h:659

References asciichat_error_stats_get(), asciichat_error_stats_t::dropped_errors, asciichat_error_stats_t::error_counts, log_plain, asciichat_error_stats_t::overwritten_history, asciichat_error_stats_t::pending_errors, asciichat_error_stats_t::registered_threads, and asciichat_error_stats_t::total_errors.

Referenced by debug_stats_print(), errno_print_stacks(), and stats_logger_thread().

◆ asciichat_error_stats_record()

void asciichat_error_stats_record ( asciichat_error_t code)

Definition at line 754 of file asciichat_errno.c.

754 {
755 uint64_t timestamp = time_ns_to_us(time_get_realtime_ns());
756 registry_lock();
757 record_stats_locked(code, timestamp);
758 registry_unlock();
759}
unsigned long long uint64_t

◆ asciichat_error_stats_reset()

void asciichat_error_stats_reset ( void )

Definition at line 766 of file asciichat_errno.c.

766 {
767 registry_lock();
768 size_t pending = g_stats.pending_errors, threads = g_stats.registered_threads;
769 memset(&g_stats, 0, sizeof(g_stats));
770 g_stats.pending_errors = pending;
771 g_stats.registered_threads = threads;
772 registry_unlock();
773}

◆ asciichat_print_error_context()

void asciichat_print_error_context ( const asciichat_error_context_t * context)

Definition at line 811 of file asciichat_errno.c.

811 {
812 print_error_context(context, true);
813}

Referenced by errno_print_stacks().

◆ errno_checkpoint()

◆ errno_clear()

void errno_clear ( void )

Definition at line 602 of file asciichat_errno.c.

602 {
603 clear_thread(thread_current_id(), false);
605}
void platform_clear_error_state(void)
Definition util.c:76

References platform_clear_error_state().

◆ errno_clear_all()

void errno_clear_all ( void )

Definition at line 606 of file asciichat_errno.c.

606 {
607 clear_thread(thread_current_id(), true);
609}

References platform_clear_error_state().

Referenced by browser_error_dismiss().

◆ errno_clear_if_current()

bool errno_clear_if_current ( uint64_t thread_id,
uint64_t generation,
uint64_t error_id )

Clear one displayed pending frame when its thread generation and ID still match.

Definition at line 620 of file asciichat_errno.c.

620 {
621 registry_lock();
622 uint64_t now = time_get_ns();
623 error_thread_t *thread = find_thread(id);
624 int previous = NO_FRAME;
625 int index = thread ? thread->top : NO_FRAME;
626 while (index != NO_FRAME && g_frames[index].context.error_id != error_id) {
627 previous = index;
628 index = g_frames[index].next;
629 }
630 bool matches = thread && thread->generation == generation && index != NO_FRAME;
631 if (matches) {
632 if (previous == NO_FRAME)
633 thread->top = g_frames[index].next;
634 else
635 g_frames[previous].next = g_frames[index].next;
636 --thread->depth;
637 release_frame_locked(index, ASCIICHAT_ERRNO_HANDLED, now);
638 }
639 registry_unlock();
640 return matches;
641}
#define NO_FRAME
struct error_thread error_thread_t
For error thread, this is the live record that ties id, generation, top, depth together....
@ ASCIICHAT_ERRNO_HANDLED
static bool size_t index
Definition queries.h:8
uint64_t generation

References ASCIICHAT_ERRNO_HANDLED, error_thread::depth, asciichat_error_context_t::error_id, error_thread::generation, index, NO_FRAME, and error_thread::top.

Referenced by ui_errno_stacks_handle_key().

◆ errno_clear_if_top()

bool errno_clear_if_top ( uint64_t id,
uint64_t generation,
uint64_t error_id )

Definition at line 610 of file asciichat_errno.c.

610 {
611 registry_lock();
612 uint64_t now = time_get_ns();
613 error_thread_t *thread = find_thread(id);
614 bool matches = thread && thread->generation == generation && thread->depth && g_frames[thread->top].context.error_id == error_id;
615 if (matches)
616 pop_locked(thread, ASCIICHAT_ERRNO_HANDLED, now);
617 registry_unlock();
618 return matches;
619}

References ASCIICHAT_ERRNO_HANDLED, error_thread::depth, error_thread::generation, and error_thread::top.

◆ errno_clear_thread_error()

void errno_clear_thread_error ( uint64_t id)

Definition at line 596 of file asciichat_errno.c.

596 {
597 clear_thread(id, false);
598}

◆ errno_clear_thread_errors()

void errno_clear_thread_errors ( uint64_t id)

Definition at line 599 of file asciichat_errno.c.

599 {
600 clear_thread(id, true);
601}

◆ errno_destroy()

void errno_destroy ( void )

Definition at line 713 of file asciichat_errno.c.

713 {
714 uint64_t now = time_get_ns();
715 uint64_t id = thread_current_id();
716 registry_lock();
717#ifndef EMSCRIPTEN_BUILD
718 if (g_tls_ready)
719 platform_tls_set(g_tls_key, NULL);
720#endif
721 error_thread_t *thread = find_thread(id);
722 if (thread)
723 unregister_locked(thread, now);
724 registry_unlock();
725}

Referenced by asciichat_shared_destroy(), browser_client_cleanup(), client_cleanup(), errno_shutdown(), and mirror_cleanup().

◆ errno_exit_code()

asciichat_error_t errno_exit_code ( void )

Definition at line 915 of file asciichat_errno.c.

915 {
916 registry_lock();
917 asciichat_error_t code = g_exit_code;
918 registry_unlock();
919 return code;
920}

Referenced by main().

◆ errno_get()

asciichat_error_t errno_get ( void )

Definition at line 521 of file asciichat_errno.c.

521 {
522 return errno_get_thread_error(thread_current_id());
523}
asciichat_error_t errno_get_thread_error(uint64_t id)

References errno_get_thread_error().

◆ errno_get_thread_error()

asciichat_error_t errno_get_thread_error ( uint64_t id)

Definition at line 514 of file asciichat_errno.c.

514 {
515 registry_lock();
516 error_thread_t *thread = find_thread(id);
517 asciichat_error_t code = thread && thread->depth ? g_frames[thread->top].context.code : ASCIICHAT_OK;
518 registry_unlock();
519 return code;
520}
@ ASCIICHAT_OK
Definition error_codes.h:50

References ASCIICHAT_OK, error_thread::depth, and error_thread::top.

Referenced by errno_get().

◆ errno_has()

bool errno_has ( asciichat_error_context_t * context)

Definition at line 511 of file asciichat_errno.c.

511 {
512 return errno_has_thread_error(thread_current_id(), context);
513}
bool errno_has_thread_error(uint64_t id, asciichat_error_context_t *context)

References errno_has_thread_error().

Referenced by BrowserSessionBinding::process(), MirrorFrameBinding::processFrame(), and DiscoveryBinding::result().

◆ errno_has_code()

bool errno_has_code ( asciichat_error_t code)

Definition at line 547 of file asciichat_errno.c.

547 {
548 return errno_has_code_tid(thread_current_id(), code);
549}
bool errno_has_code_tid(uint64_t id, asciichat_error_t code)

References errno_has_code_tid().

◆ errno_has_code_since()

bool errno_has_code_since ( asciichat_errno_scope_t scope,
asciichat_error_t code )

Definition at line 550 of file asciichat_errno.c.

550 {
551 registry_lock();
552 error_thread_t *thread = find_thread(scope.thread_id);
553 bool found = thread && thread->generation == scope.generation && has_code_locked(thread, scope.after_id, code);
554 registry_unlock();
555 return found;
556}

References asciichat_errno_scope_t::after_id, asciichat_errno_scope_t::generation, error_thread::generation, and asciichat_errno_scope_t::thread_id.

◆ errno_has_code_tid()

bool errno_has_code_tid ( uint64_t id,
asciichat_error_t code )

Definition at line 541 of file asciichat_errno.c.

541 {
542 registry_lock();
543 bool found = has_code_locked(find_thread(id), 0, code);
544 registry_unlock();
545 return found;
546}

Referenced by errno_has_code().

◆ errno_has_thread_error()

bool errno_has_thread_error ( uint64_t id,
asciichat_error_context_t * context )

Definition at line 498 of file asciichat_errno.c.

498 {
499 registry_lock();
500 error_thread_t *thread = find_thread(id);
501 bool found = thread && thread->depth;
502 if (context) {
503 if (found)
504 *context = g_frames[thread->top].context;
505 else
506 memset(context, 0, sizeof(*context));
507 }
508 registry_unlock();
509 return found;
510}

References error_thread::depth, and error_thread::top.

Referenced by errno_has().

◆ errno_has_wsa_error()

bool errno_has_wsa_error ( void )

Definition at line 524 of file asciichat_errno.c.

524 {
526 return HAS_ERRNO(&context) && context.has_wsa_error;
527}
#define HAS_ERRNO(context)

References HAS_ERRNO, and asciichat_error_context_t::has_wsa_error.

◆ errno_history_snapshot()

size_t errno_history_snapshot ( asciichat_errno_history_t * out,
size_t capacity )

Definition at line 700 of file asciichat_errno.c.

700 {
701 registry_lock();
702 size_t count = g_history_count;
703 if (out)
704 for (size_t i = 0; i < count && i < capacity; ++i)
705 out[i] = g_history[(g_history_next + ASCIICHAT_ERRNO_HISTORY_SIZE - 1 - i) % ASCIICHAT_ERRNO_HISTORY_SIZE];
706 registry_unlock();
707 return count;
708}
#define ASCIICHAT_ERRNO_HISTORY_SIZE
static bool size_t gotel_query_t * out
Definition queries.h:8

References ASCIICHAT_ERRNO_HISTORY_SIZE, and out.

Referenced by errno_print_history().

◆ errno_import()

asciichat_error_t errno_import ( const asciichat_error_context_t * cause)

Import an owned value snapshot into the caller's stack, linking to its original ID. Synchronization of snapshot transfer belongs to the operation owner.

Definition at line 567 of file asciichat_errno.c.

567 {
568 if (!cause || cause->code == ASCIICHAT_OK)
569 return SET_ERRNO(ERROR_INVALID_PARAM, "Missing failure snapshot");
570 if (g_recording)
571 return cause->code;
572 g_recording = true;
573 asciichat_error_context_t copy = *cause;
574 copy.cause_id = cause->error_id;
575 copy.thread_id = thread_current_id();
576 copy.timestamp = time_ns_to_us(time_get_realtime_ns());
577 copy.created_ns = time_get_ns();
578 copy.backtrace.symbols = NULL;
579 copy.backtrace.tried_symbolize = false;
580 push_context(&copy);
581 g_recording = false;
582 return copy.code;
583}
#define SET_ERRNO(code,...)
@ ERROR_INVALID_PARAM
uint64_t created_ns
Monotonic time, for age.
uint64_t timestamp
Microseconds since epoch, for logs.

References ASCIICHAT_OK, asciichat_error_context_t::backtrace, asciichat_error_context_t::cause_id, asciichat_error_context_t::code, asciichat_error_context_t::created_ns, asciichat_error_context_t::error_id, ERROR_INVALID_PARAM, SET_ERRNO, asciichat_error_context_t::thread_id, and asciichat_error_context_t::timestamp.

Referenced by ffmpeg_decoder_read_video_frame(), http_renderer_render_file(), and session_pipeline_destroy().

◆ errno_notifications_dismiss()

void errno_notifications_dismiss ( uint64_t through_id)

Definition at line 137 of file asciichat_errno.c.

137 {
138 registry_lock();
139 if (through_id > g_title_dismissed_through)
140 g_title_dismissed_through = through_id;
141 while (g_notification_count && g_notifications[g_notification_first].error_id <= through_id) {
142 memset(&g_notifications[g_notification_first], 0, sizeof(g_notifications[0]));
143 g_notification_first = (g_notification_first + 1) % ASCIICHAT_ERRNO_HISTORY_SIZE;
144 --g_notification_count;
145 }
146 g_notification_dropped = 0;
147 atomic_store_bool_impl(&g_notifications_pending, g_notification_count != 0);
148 registry_unlock();
149}
void atomic_store_bool_impl(atomic_t *a, uint64_t value)

References ASCIICHAT_ERRNO_HISTORY_SIZE, and atomic_store_bool_impl().

Referenced by ui_error_popup_key().

◆ errno_notifications_enable()

void errno_notifications_enable ( bool enabled)

Independent, bounded UI notifications. Acknowledgment never changes pending stacks. Enabling starts a fresh terminal session; repeated enable calls preserve the batch. Snapshots are oldest-first. Dismiss only through the last displayed ID so concurrent arrivals remain visible. No callbacks, allocation, or symbolization run on producers.

Definition at line 95 of file asciichat_errno.c.

95 {
96 registry_lock();
97 if (!enabled || (!g_notifications_enabled && !g_notifications_paused)) {
98 g_notification_first = g_notification_count = 0;
99 g_notification_dropped = 0;
100 atomic_store_bool_impl(&g_notifications_pending, false);
101 }
102 g_notifications_enabled = enabled;
103 g_notifications_paused = false;
104 registry_unlock();
105}
bool enabled
Is filtering active?
Definition grep.c:62

References atomic_store_bool_impl(), and enabled.

Referenced by errno_shutdown(), ui_controller_finish(), and ui_controller_shutdown().

◆ errno_notifications_pause()

void errno_notifications_pause ( void )

Stop collecting new notifications while retaining those already queued.

Definition at line 107 of file asciichat_errno.c.

107 {
108 registry_lock();
109 if (g_notifications_enabled) {
110 g_notifications_enabled = false;
111 g_notifications_paused = true;
112 }
113 registry_unlock();
114}

◆ errno_notifications_pending()

bool errno_notifications_pending ( void )

Definition at line 116 of file asciichat_errno.c.

116 {
117 return atomic_load_bool_impl(&g_notifications_pending);
118}
bool atomic_load_bool_impl(const atomic_t *a)

References atomic_load_bool_impl().

Referenced by ui_error_popup_preserve(), and ui_error_popup_visible().

◆ errno_notifications_snapshot()

size_t errno_notifications_snapshot ( asciichat_error_context_t * out,
size_t capacity,
uint64_t * dropped )

Definition at line 126 of file asciichat_errno.c.

126 {
127 registry_lock();
128 size_t count = MIN(capacity, g_notification_count);
129 for (size_t i = 0; out && i < count; ++i)
130 out[i] = g_notifications[(g_notification_first + i) % ASCIICHAT_ERRNO_HISTORY_SIZE];
131 if (dropped)
132 *dropped = g_notification_dropped;
133 registry_unlock();
134 return count;
135}
#define MIN(a, b)
Definition param.h:6

References ASCIICHAT_ERRNO_HISTORY_SIZE, MIN, and out.

Referenced by ui_error_popup_preserve().

◆ errno_notifications_suppress()

bool errno_notifications_suppress ( bool suppress)

Suppress UI notifications on this thread, without suppressing errno storage/logging.

Definition at line 120 of file asciichat_errno.c.

120 {
121 bool previous = g_suppress_notifications;
122 g_suppress_notifications = suppress;
123 return previous;
124}

Referenced by discovery_mdns_query(), discovery_session_create(), nat_detect_quality(), session_server_like_run(), and ui_error_popup_preserve().

◆ errno_peek_since()

bool errno_peek_since ( asciichat_errno_scope_t scope,
asciichat_error_context_t * out )

Copy the newest error belonging to this operation, without consuming it.

Definition at line 558 of file asciichat_errno.c.

558 {
559 registry_lock();
560 error_thread_t *thread = find_thread(scope.thread_id);
561 bool found = thread && thread->generation == scope.generation && thread->top != NO_FRAME && g_frames[thread->top].context.error_id > scope.after_id;
562 if (found && out)
563 *out = g_frames[thread->top].context;
564 registry_unlock();
565 return found;
566}

References asciichat_errno_scope_t::after_id, asciichat_errno_scope_t::generation, error_thread::generation, NO_FRAME, out, asciichat_errno_scope_t::thread_id, and error_thread::top.

Referenced by action_completions(), capture_init(), ffmpeg_decoder_poll_video(), log_net_message(), KeyParserBinding::parse(), session_capture_create(), and session_render_loop().

◆ errno_print_chain()

void errno_print_chain ( uint64_t id,
uint64_t error_id )

Definition at line 892 of file asciichat_errno.c.

892 {
894 size_t count = errno_snapshot(id, frames, ASCIICHAT_ERRNO_MAX_DEPTH).copied;
895 log_plain("Failure chain (outer context -> root cause):");
896 for (size_t i = 0; i < count && error_id; ++i) {
897 if (frames[i].error_id == error_id) {
898 // Automatic error logs keep their full cause metadata at normal verbosity.
899 // Symbolization can launch external tools and is reserved for Debug logs;
900 // explicit stack reports above still include backtraces at every level.
901 print_error_context(&frames[i], log_get_level() <= LOG_DEBUG);
902 error_id = frames[i].cause_id;
903 }
904 }
905 if (error_id)
906 log_plain(" Cause #%" PRIu64 " was resolved or evicted; consult handled-error history", error_id);
907}
asciichat_errno_snapshot_t errno_snapshot(uint64_t id, asciichat_error_context_t *out, size_t capacity)
#define ASCIICHAT_ERRNO_MAX_DEPTH
log_level_t log_get_level(void)
Get the current minimum log level.
#define LOG_DEBUG
Definition types.h:43

References ASCIICHAT_ERRNO_MAX_DEPTH, asciichat_error_context_t::cause_id, asciichat_errno_snapshot_t::copied, errno_snapshot(), LOG_DEBUG, log_get_level(), and log_plain.

◆ errno_print_hash_stats()

void errno_print_hash_stats ( void )

Definition at line 848 of file asciichat_errno.c.

848 {
849 size_t used = 0, max_chain = 0;
850 registry_lock();
851 for (size_t i = 0; i < ERRNO_BUCKETS; ++i) {
852 size_t chain = 0;
853 for (error_thread_t *thread = g_buckets[i]; thread; thread = thread->next)
854 ++chain;
855 used += chain != 0;
856 if (chain > max_chain)
857 max_chain = chain;
858 }
859 size_t entries = g_stats.registered_threads;
860 registry_unlock();
861 log_plain("Error registry: entries=%zu buckets=%d used=%zu max_chain=%zu reserved_bytes=%zu", entries, ERRNO_BUCKETS, used, max_chain,
862 sizeof(g_threads) + sizeof(g_buckets) + sizeof(g_frames) + sizeof(g_history));
863}
#define ERRNO_BUCKETS

References ERRNO_BUCKETS, log_plain, and error_thread::next.

◆ errno_print_history()

void errno_print_history ( void )

Definition at line 838 of file asciichat_errno.c.

838 {
841 static const char *outcomes[] = {"handled", "dismissed", "thread-exit", "overflow"};
842 for (size_t i = 0; i < count; ++i) {
843 const asciichat_errno_history_t *event = &events[i];
844 log_plain("Error #%" PRIu64 " thread=%" PRIu64 " code=%d %s after %.3fms: %s", event->context.error_id, event->context.thread_id, event->context.code, outcomes[event->outcome],
845 (double)(event->resolved_ns - event->context.created_ns) / 1e6, event->context.context_message);
846 }
847}
size_t errno_history_snapshot(asciichat_errno_history_t *out, size_t capacity)

References ASCIICHAT_ERRNO_HISTORY_SIZE, errno_history_snapshot(), and log_plain.

◆ errno_print_stacks()

void errno_print_stacks ( void )

Definition at line 815 of file asciichat_errno.c.

815 {
816 // Copy one bounded thread stack at a time. A generation check prevents a
817 // reused thread slot from being attributed to the thread sampled earlier.
819 size_t count = 0;
820 registry_lock();
821 for (size_t i = 0; i < ASCIICHAT_ERRNO_MAX_THREADS; ++i)
822 if (g_threads[i].depth) {
823 ids[count] = g_threads[i].id;
824 generations[count++] = g_threads[i].generation;
825 }
826 registry_unlock();
827 log_plain("Pending error stacks (%zu threads):", count);
828 for (size_t i = 0; i < count; ++i) {
830 size_t depth = errno_snapshot(ids[i], contexts, ASCIICHAT_ERRNO_MAX_DEPTH).copied;
831 if (!depth || contexts[0].generation != generations[i])
832 continue;
833 for (size_t j = 0; j < depth; ++j)
834 asciichat_print_error_context(&contexts[j]);
835 }
837}
void asciichat_error_stats_print(void)
void asciichat_print_error_context(const asciichat_error_context_t *context)
#define ASCIICHAT_ERRNO_MAX_THREADS

References ASCIICHAT_ERRNO_MAX_DEPTH, ASCIICHAT_ERRNO_MAX_THREADS, asciichat_error_stats_print(), asciichat_print_error_context(), asciichat_errno_snapshot_t::copied, error_thread::depth, errno_snapshot(), error_thread::generation, and log_plain.

Referenced by asciichat_fatal_with_context().

◆ errno_request_exit()

void errno_request_exit ( asciichat_error_t code)

Definition at line 909 of file asciichat_errno.c.

909 {
910 registry_lock();
911 if (g_exit_code == ASCIICHAT_OK)
912 g_exit_code = code;
913 registry_unlock();
914}

References ASCIICHAT_OK.

◆ errno_scope_begin()

◆ errno_scope_end()

void errno_scope_end ( asciichat_errno_scope_t scope,
asciichat_errno_outcome_t outcome )

Definition at line 662 of file asciichat_errno.c.

662 {
663 uint64_t now = time_get_ns();
664 // Recovery runs on the offending thread. Cross-thread clearing has separate APIs.
665 if (scope.thread_id != thread_current_id())
666 return;
667 registry_lock();
668 error_thread_t *thread = find_thread(scope.thread_id);
669 if (thread && thread->generation == scope.generation) {
670 while (thread->depth && g_frames[thread->top].context.error_id > scope.after_id)
671 pop_locked(thread, outcome, now);
672 thread->cause_after_id = scope.previous_after_id;
673 }
674 registry_unlock();
675}
uint64_t cause_after_id

References asciichat_errno_scope_t::after_id, error_thread::cause_after_id, error_thread::depth, asciichat_errno_scope_t::generation, error_thread::generation, asciichat_errno_scope_t::previous_after_id, asciichat_errno_scope_t::thread_id, and error_thread::top.

Referenced by acip_settings_request(), asciichat_mdns_advertise(), audio_start_duplex(), browser_transport_security_packet(), config_load_and_apply(), config_load_system_and_user(), discovery_mdns_query(), discovery_session_create(), ffmpeg_decoder_create_with_request_options(), main(), media_source_create_interruptible(), nat_detect_quality(), nat_upnp_open_protocol(), process_decrypted_packet(), session_connection_establish(), session_participant_connect(), session_server_like_run(), ui_controller_size(), ui_input_media_take(), and ui_notice_first_use_complete().

◆ errno_scope_is_clean()

bool errno_scope_is_clean ( asciichat_errno_scope_t scope)

Definition at line 885 of file asciichat_errno.c.

885 {
886 registry_lock();
887 error_thread_t *thread = find_thread(scope.thread_id);
888 bool clean = !thread || thread->generation != scope.generation || !thread->depth || g_frames[thread->top].context.error_id <= scope.after_id;
889 registry_unlock();
890 return clean;
891}

References asciichat_errno_scope_t::after_id, error_thread::depth, asciichat_errno_scope_t::generation, error_thread::generation, asciichat_errno_scope_t::thread_id, and error_thread::top.

◆ errno_set()

asciichat_error_t errno_set ( asciichat_error_t code,
const char * file,
int line,
const char * function,
const char * message )

Definition at line 470 of file asciichat_errno.c.

470 {
471 return errno_set_with_message(code, file, line, function, "%s", message ? message : "");
472}
asciichat_error_t errno_set_with_message(asciichat_error_t code, const char *file, int line, const char *function, const char *format,...)

References errno_set_with_message().

◆ errno_set_with_message()

asciichat_error_t errno_set_with_message ( asciichat_error_t code,
const char * file,
int line,
const char * function,
const char * format,
... )

Definition at line 462 of file asciichat_errno.c.

462 {
463 va_list args;
464 va_start(args, format);
465 asciichat_error_t result = set_formatted(code, file, line, function, 0, false, 0, false, format, args);
466 va_end(args);
467 return result;
468}
action_args_t args

References args.

Referenced by errno_set().

◆ errno_set_with_system_error()

asciichat_error_t errno_set_with_system_error ( asciichat_error_t code,
const char * file,
int line,
const char * function,
int sys_errno )

Definition at line 482 of file asciichat_errno.c.

482 {
483 return errno_set_with_system_error_and_message(code, file, line, function, sys_errno, "System operation failed (%d)", sys_errno);
484}
asciichat_error_t errno_set_with_system_error_and_message(asciichat_error_t code, const char *file, int line, const char *function, int sys_errno, const char *format,...)

References errno_set_with_system_error_and_message().

Referenced by accept_with_timeout(), and recv_with_timeout().

◆ errno_set_with_system_error_and_message()

asciichat_error_t errno_set_with_system_error_and_message ( asciichat_error_t code,
const char * file,
int line,
const char * function,
int sys_errno,
const char * format,
... )

Definition at line 474 of file asciichat_errno.c.

474 {
475 va_list args;
476 va_start(args, format);
477 asciichat_error_t result = set_formatted(code, file, line, function, sys_errno, true, 0, false, format, args);
478 va_end(args);
479 return result;
480}

References args.

Referenced by errno_set_with_system_error().

◆ errno_set_with_wsa_error()

asciichat_error_t errno_set_with_wsa_error ( asciichat_error_t code,
const char * file,
int line,
const char * function,
int wsa_error )

Definition at line 494 of file asciichat_errno.c.

494 {
495 return set_wsa(code, file, line, function, wsa_error, "Socket operation failed (%d)", wsa_error);
496}

◆ errno_shutdown()

void errno_shutdown ( void )

Definition at line 726 of file asciichat_errno.c.

726 {
729 registry_lock();
730 g_shutdown = true;
731 memset(g_threads, 0, sizeof(g_threads));
732 memset(g_buckets, 0, sizeof(g_buckets));
733 memset(g_frames, 0, sizeof(g_frames));
734 memset(g_history, 0, sizeof(g_history));
735 g_history_count = g_history_next = 0;
736 g_stats.pending_errors = g_stats.registered_threads = 0;
737#ifndef EMSCRIPTEN_BUILD
738 bool delete_key = g_tls_ready;
739 tls_key_t key = g_tls_key;
740 g_tls_ready = false;
741#endif
742 registry_unlock();
743#ifndef EMSCRIPTEN_BUILD
744 if (delete_key)
745 platform_tls_key_delete(key);
746#endif
747}
void errno_destroy(void)
void errno_notifications_enable(bool enabled)

References errno_destroy(), and errno_notifications_enable().

Referenced by asciichat_shared_destroy().

◆ errno_snapshot()

asciichat_errno_snapshot_t errno_snapshot ( uint64_t thread_id,
asciichat_error_context_t * out,
size_t capacity )

Copy newest-first up to capacity. The result distinguishes total depth from copied frames.

Definition at line 676 of file asciichat_errno.c.

676 {
677 registry_lock();
678 error_thread_t *thread = find_thread(id);
679 size_t depth = thread ? thread->depth : 0;
680 asciichat_errno_snapshot_t snapshot = {.total = depth};
681 if (thread && out)
682 for (int index = thread->top; index != NO_FRAME && snapshot.copied < capacity; index = g_frames[index].next)
683 out[snapshot.copied++] = g_frames[index].context;
684 registry_unlock();
685 return snapshot;
686}

References asciichat_errno_snapshot_t::copied, error_thread::depth, index, NO_FRAME, out, and error_thread::top.

Referenced by browser_error_snapshot(), errno_print_chain(), errno_print_stacks(), packet_format_error_stack(), and KeyParserBinding::parse().

◆ errno_suppress()

void errno_suppress ( bool suppress)

Definition at line 710 of file asciichat_errno.c.

710 {
711 g_suppress_logging = suppress;
712}

◆ errno_threads_snapshot()

size_t errno_threads_snapshot ( uint64_t * out,
size_t capacity )

Copy registered thread IDs. Returns the total count so truncation is visible.

Definition at line 687 of file asciichat_errno.c.

687 {
688 registry_lock();
689 size_t count = 0;
690 for (size_t i = 0; i < ASCIICHAT_ERRNO_MAX_THREADS; ++i) {
691 if (!g_threads[i].generation)
692 continue;
693 if (out && count < capacity)
694 out[count] = g_threads[i].id;
695 ++count;
696 }
697 registry_unlock();
698 return count;
699}

References ASCIICHAT_ERRNO_MAX_THREADS, error_thread::generation, and out.

◆ errno_title_snapshot()

size_t errno_title_snapshot ( asciichat_error_context_t * latest)

Count distinct unacknowledged pending frames and visible notifications, copying the newest. The count and value come from the same synchronized registry snapshot.

Definition at line 207 of file asciichat_errno.c.

207 {
208 asciichat_error_context_t newest = {0};
209 registry_lock();
210 size_t count = 0;
211 for (size_t i = 0; i < ASCIICHAT_ERRNO_MAX_THREADS; ++i) {
212 error_thread_t *thread = &g_threads[i];
213 for (int frame = thread->depth ? thread->top : NO_FRAME; frame != NO_FRAME; frame = g_frames[frame].next) {
214 const asciichat_error_context_t *entry = &g_frames[frame].context;
215 if (entry->error_id <= g_title_dismissed_through)
216 continue;
217 ++count;
218 if (entry->error_id > newest.error_id)
219 newest = *entry;
220 }
221 }
222 for (size_t i = 0; i < g_notification_count; ++i) {
223 const asciichat_error_context_t *entry = &g_notifications[(g_notification_first + i) % ASCIICHAT_ERRNO_HISTORY_SIZE];
224 error_thread_t *thread = find_thread(entry->thread_id);
225 bool pending = false;
226 if (thread && thread->generation == entry->generation) {
227 for (int frame = thread->top; frame != NO_FRAME; frame = g_frames[frame].next) {
228 if (g_frames[frame].context.error_id == entry->error_id) {
229 pending = true;
230 break;
231 }
232 }
233 }
234 if (!pending)
235 ++count;
236 if (entry->error_id > newest.error_id)
237 newest = *entry;
238 }
239 if (latest)
240 *latest = newest;
241 registry_unlock();
242 return count;
243}

References ASCIICHAT_ERRNO_HISTORY_SIZE, ASCIICHAT_ERRNO_MAX_THREADS, error_thread::depth, asciichat_error_context_t::error_id, asciichat_error_context_t::generation, NO_FRAME, asciichat_error_context_t::thread_id, and error_thread::top.