ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
Video chat in your terminal
Loading...
Searching...
No Matches
log/named.c
Go to the documentation of this file.
1
17
18/* Storage capacities in elements; text buffers include the terminating NUL. */
19#define LOG_NAMED_ID_BUFFER_SIZE 64
20#define LOG_NAMED_TEMP_OUTPUT_BUFFER_SIZE 512
21#define LOG_NAMED_ID_EXTENDED_BUFFER_SIZE 128
22#define LOG_NAMED_TYPE_BUFFER_SIZE 64
23#define LOG_NAMED_KW_BUFFER_SIZE 32
24#define LOG_NAMED_TEMP_BUFFER_SIZE 512
25#define LOG_NAMED_TYPE_EXTENDED_BUFFER_SIZE 256
26#define LOG_NAMED_NAME_BUFFER_SIZE 256
27#define LOG_NAMED_ADDR_BUFFER_SIZE 128
28
30#include "ascii-chat/log/log.h"
32#include <ctype.h>
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36
37static int named_format_int(char *buffer, size_t capacity, const char *format,
38 int value) {
39 int (*writer)(char *, size_t, const char *, ...) = snprintf;
40 return writer(buffer, capacity, format, value);
41}
42
43static int named_format_ptrdiff(char *buffer, size_t capacity,
44 const char *format, ptrdiff_t value) {
45 int (*writer)(char *, size_t, const char *, ...) = snprintf;
46 return writer(buffer, capacity, format, value);
47}
48
49/* Use a thread-local buffer to avoid allocations in the hot path */
50#define NAMED_FORMAT_BUFFER_SIZE (4096)
51
62static bool is_type_in_registry(const char *type_str, size_t type_len) {
63 return named_registry_has_type(type_str, type_len);
64}
65
78
91static const char *find_hex_type_prefix_start(const char *start, const char *p,
92 const char **out_type,
93 size_t *out_type_len) {
94 *out_type = NULL;
95 *out_type_len = 0;
96
97 if (p <= start)
98 return p;
99
100 /* Check for '=' immediately before hex address */
101 const char *check = p - 1;
102 if (check < start || *check != '=')
103 return p;
104
105 /* Walk back over the keyword before '=' */
106 const char *eq_pos = check;
107 check = eq_pos - 1;
108 if (check < start)
109 return p;
110
111 /* Find start of keyword */
112 while (check > start && (isalnum(*check) || *check == '_')) {
113 check--;
114 }
115 if (!(isalnum(*check) || *check == '_')) {
116 check++;
117 }
118
119 size_t word_len = eq_pos - check;
120 if (word_len == 0)
121 return p;
122
123 /* Known pointer-type keywords that appear with hex values */
124 static const struct {
125 const char *type;
126 size_t len;
127 } known_types[] = {
128 {"transport", 9}, {"client", 6}, {"context", 7}, {"crypto", 6},
129 {"websocket", 9}, {"mutex", 5}, {"rwlock", 6}, {"cond", 4},
130 {"thread", 6}, {"socket", 6}, {"connection", 10},
131 };
132
133 for (size_t i = 0; i < sizeof(known_types) / sizeof(known_types[0]); i++) {
134 if (word_len == known_types[i].len &&
135 strncmp(check, known_types[i].type, word_len) == 0) {
136 *out_type = check;
137 *out_type_len = word_len;
138 return check;
139 }
140 }
141
142 return p;
143}
144
155static const char *find_fd_prefix_start(const char *start, const char *p) {
156 if (p == start)
157 return p;
158
159 const char *initial_pos = p - 1;
160 const char *word_start = initial_pos;
161 /* Skip back over whitespace, '=', ':' */
162 while (word_start > start && (*word_start == ' ' || *word_start == '\t' ||
163 *word_start == '=' || *word_start == ':')) {
164 word_start--;
165 }
166
167 /* Require at least one separator between keyword and digit.
168 * Without this, "fd7a" in IPv6 addresses like "fd7a:115c:..." would
169 * be misinterpreted as fd prefix + digit 7. */
170 if (word_start == initial_pos)
171 return p;
172
173 /* Check if we have a digit (we're in a decimal number) */
174 if (!isdigit(*word_start)) {
175 /* Not a digit, so look for fd/file/descriptor/socket/sockfd keywords before
176 * us */
177 const char *check = word_start;
178 while (check > start && (isalnum(*check) || *check == '_')) {
179 check--;
180 }
181 check++; /* Move back to start of word */
182
183 size_t word_len = word_start - check + 1;
184 if (word_len >= 2) {
185 if ((word_len == 2 && strncasecmp(check, "fd", 2) == 0) ||
186 (word_len == 4 && strncasecmp(check, "file", 4) == 0) ||
187 (word_len == 6 && strncasecmp(check, "socket", 6) == 0) ||
188 (word_len == 6 && strncasecmp(check, "sockfd", 6) == 0) ||
189 (word_len == 10 && strncasecmp(check, "descriptor", 10) == 0)) {
190 return check; /* Return start of keyword */
191 }
192 }
193 }
194
195 return p; /* No prefix found, return original position */
196}
197
208static const char *find_packet_type_prefix_start(const char *start,
209 const char *p) {
210 if (p == start)
211 return p;
212
213 const char *initial_pos = p - 1;
214 const char *word_start = initial_pos;
215 /* Skip back over whitespace, '=', ':', '(' */
216 while (word_start > start &&
217 (*word_start == ' ' || *word_start == '\t' || *word_start == '=' ||
218 *word_start == ':' || *word_start == '(')) {
219 word_start--;
220 }
221
222 /* Require at least one separator between keyword and digit */
223 if (word_start == initial_pos)
224 return p;
225
226 /* Check if we have a digit (we're in a decimal number) */
227 if (!isdigit(*word_start)) {
228 /* Not a digit, so look for packet type keywords before us */
229 const char *check = word_start;
230 while (check > start && (isalnum(*check) || *check == '_')) {
231 check--;
232 }
233 check++; /* Move back to start of word */
234
235 size_t word_len = word_start - check + 1;
236 /* Check for "type" keyword (part of "packet type") */
237 if (word_len >= 4 && strncasecmp(check, "type", 4) == 0) {
238 /* Look for "packet" before "type" */
239 const char *packet_check = check - 1;
240 while (packet_check > start &&
241 (*packet_check == ' ' || *packet_check == '\t')) {
242 packet_check--;
243 }
244 if (packet_check > start) {
245 const char *packet_start = packet_check;
246 while (packet_start > start &&
247 (isalnum(*packet_start) || *packet_start == '_')) {
248 packet_start--;
249 }
250 packet_start++;
251 size_t packet_len = packet_check - packet_start + 1;
252 if (packet_len == 6 && strncasecmp(packet_start, "packet", 6) == 0) {
253 return packet_start; /* Return start of "packet" in "packet type" */
254 }
255 }
256 }
257 /* Check for "packet" keyword alone */
258 if (word_len >= 6 && strncasecmp(check, "packet", 6) == 0) {
259 return check; /* Return start of "packet" */
260 }
261 }
262
263 return p; /* No prefix found, return original position */
264}
265
271static bool has_packet_type_prefix(const char *start, const char *p) {
272 return find_packet_type_prefix_start(start, p) != p;
273}
274
280static bool has_fd_prefix(const char *start, const char *p) {
281 return find_fd_prefix_start(start, p) != p;
282}
283
284/* ============================================================================
285 * Granular Helper Functions for Message Transformation
286 * ============================================================================
287 */
288
289/* --- Decimal parsing --- */
290static int parse_decimal_digits(const char *start, const char **end) {
291 int value = 0;
292 int count = 0;
293 const char *p = start;
294 while (*p && isdigit(*p) && count < 6) {
295 value = value * 10 + (*p - '0');
296 p++;
297 count++;
298 }
299 *end = p;
300 return value;
301}
302
303static bool write_formatted_packet_type(int pkt_value, const char *name,
304 char *output, size_t output_size,
305 size_t *out_pos) {
306 named_metadata_t metadata;
307 const char *fmt_spec =
308 named_packet_metadata_copy(pkt_value, &metadata) ? metadata.format : "%d";
309
310 char id_buffer[LOG_NAMED_ID_BUFFER_SIZE];
311 int id_written =
312 named_format_int(id_buffer, sizeof(id_buffer), fmt_spec, pkt_value);
313 if (id_written <= 0 || id_written >= (int)sizeof(id_buffer)) {
314 return false;
315 }
316
317 char temp_output[LOG_NAMED_TEMP_OUTPUT_BUFFER_SIZE];
318 int temp_written = snprintf(temp_output, sizeof(temp_output),
319 "packet_type/%s (pkt_type=%s)", name, id_buffer);
320 if (temp_written <= 0 || (size_t)temp_written >= sizeof(temp_output)) {
321 return false;
322 }
323
324 if (*out_pos + temp_written >= output_size - 1) {
325 return false;
326 }
327
328 memcpy(output + *out_pos, temp_output, temp_written);
329 *out_pos += temp_written;
330 return true;
331}
332
333static bool write_formatted_fd(int fd_value, const char *name, char *output,
334 size_t output_size, size_t *out_pos) {
335 named_metadata_t metadata;
336 const char *fmt_spec =
337 named_fd_metadata_copy(fd_value, &metadata) ? metadata.format : "%d";
338
339 char id_buffer[LOG_NAMED_ID_BUFFER_SIZE];
340 int id_written =
341 named_format_int(id_buffer, sizeof(id_buffer), fmt_spec, fd_value);
342 if (id_written <= 0 || id_written >= (int)sizeof(id_buffer)) {
343 return false;
344 }
345
346 char temp_output[LOG_NAMED_TEMP_OUTPUT_BUFFER_SIZE];
347 int temp_written = snprintf(temp_output, sizeof(temp_output), "fd/%s (fd=%s)",
348 name, id_buffer);
349 if (temp_written <= 0 || (size_t)temp_written >= sizeof(temp_output)) {
350 return false;
351 }
352
353 if (*out_pos + temp_written >= output_size - 1) {
354 return false;
355 }
356
357 memcpy(output + *out_pos, temp_output, temp_written);
358 *out_pos += temp_written;
359 return true;
360}
361
362static bool copy_unformatted_decimal(const char *int_start, const char *int_end,
363 char *output, size_t output_size,
364 size_t *out_pos) {
365 size_t int_len = int_end - int_start;
366 if (int_len >= output_size - *out_pos) {
367 return false;
368 }
369 memcpy(output + *out_pos, int_start, int_len);
370 *out_pos += int_len;
371 return true;
372}
373
374/* --- Type= pattern detection and parsing --- */
375static bool is_type_equals_pattern(const char *p) {
376 return p[0] == 't' && p[1] == 'y' && p[2] == 'p' && p[3] == 'e' &&
377 p[4] == '=';
378}
379
380static bool type_equals_inside_parens(const char *p, const char *message) {
381 if (p <= message) {
382 return false;
383 }
384
385 const char *back = p - 1;
386 while (back > message && isspace(*back)) {
387 back--;
388 }
389 return back >= message && *back == '(';
390}
391
392static bool write_formatted_type_equals(int pkt_value, const char *name,
393 char *output, size_t output_size,
394 size_t *out_pos) {
395 named_metadata_t metadata;
396 const char *fmt_spec =
397 named_packet_metadata_copy(pkt_value, &metadata) ? metadata.format : "%d";
398
399 char id_buffer[LOG_NAMED_ID_BUFFER_SIZE];
400 int id_written =
401 named_format_int(id_buffer, sizeof(id_buffer), fmt_spec, pkt_value);
402 if (id_written <= 0 || id_written >= (int)sizeof(id_buffer)) {
403 return false;
404 }
405
406 char temp_output[LOG_NAMED_TEMP_OUTPUT_BUFFER_SIZE];
407 int temp_written = snprintf(temp_output, sizeof(temp_output),
408 "packet_type/%s (type=%s)", name, id_buffer);
409 if (temp_written <= 0 || (size_t)temp_written >= sizeof(temp_output)) {
410 return false;
411 }
412
413 if (*out_pos + temp_written >= output_size - 1) {
414 return false;
415 }
416
417 memcpy(output + *out_pos, temp_output, temp_written);
418 *out_pos += temp_written;
419 return true;
420}
421
422/* --- Buffer operations --- */
423
424static void copy_char_to_output(char *output, size_t *out_pos, char c) {
425 output[(*out_pos)++] = c;
426}
427
428/* ============================================================================
429 * Message Transformation Helpers
430 * ============================================================================
431 */
432
443static bool try_format_hex_address(const char *message, const char *hex_start,
444 char *output, size_t output_size,
445 size_t *out_pos, const char **p) {
446 const char *hex_ptr = hex_start + 2; /* Skip "0x" */
447
448 /* Parse hex digits */
449 uintptr_t address = 0;
450 int hex_count = 0;
451 while (*hex_ptr && isxdigit(*hex_ptr) && hex_count < 16) {
452 address = (address << 4) |
453 (unsigned int)(*hex_ptr > '9' ? (*hex_ptr | 0x20) - 'a' + 10
454 : *hex_ptr - '0');
455 hex_ptr++;
456 hex_count++;
457 }
458
459 if (hex_count == 0) {
460 return false; /* No valid hex digits */
461 }
462
463 /* Check if already inside parentheses (part of previous formatting) */
464 bool is_already_in_parens = (hex_start > message && *(hex_start - 1) == '(');
465 if (is_already_in_parens) {
466 return false;
467 }
468
469 /* Detect type prefix before hex address (e.g., "transport=" in
470 * "transport=0x1234") */
471 const char *prefix_type = NULL;
472 size_t prefix_type_len = 0;
473 const char *prefix_start = find_hex_type_prefix_start(
474 message, hex_start, &prefix_type, &prefix_type_len);
475 bool has_type_prefix = (prefix_type != NULL && prefix_start != hex_start);
476
477 /* Check if address is registered */
478 named_metadata_t metadata;
479 if (named_metadata_copy(address, &metadata)) {
480 const char *name = metadata.name;
481 const char *type = metadata.type;
482 const char *fmt_spec = metadata.format;
483
484 char id_buffer[LOG_NAMED_ID_EXTENDED_BUFFER_SIZE];
485 int id_written = named_format_ptrdiff(id_buffer, sizeof(id_buffer),
486 fmt_spec, (ptrdiff_t)address);
487 if (id_written <= 0 || id_written >= (int)sizeof(id_buffer)) {
488 return false;
489 }
490
491 size_t saved_pos = *out_pos;
492
493 /* Backtrack type prefix if it matches the registered type to avoid
494 * redundancy (e.g., "transport=transport/name" becomes "transport/name") */
495 if (has_type_prefix && prefix_type_len == strlen(type) &&
496 strncmp(prefix_type, type, prefix_type_len) == 0) {
497 size_t prefix_len = hex_start - prefix_start;
498 if (*out_pos >= prefix_len) {
499 *out_pos -= prefix_len;
500 }
501 }
502
503 char temp_output[LOG_NAMED_TEMP_OUTPUT_BUFFER_SIZE];
504 int temp_written = snprintf(temp_output, sizeof(temp_output), "%s/%s (%s)",
505 type, name, id_buffer);
506 if (temp_written <= 0 || (size_t)temp_written >= sizeof(temp_output)) {
507 *out_pos = saved_pos;
508 return false;
509 }
510
511 if (*out_pos + temp_written >= output_size - 1) {
512 *out_pos = saved_pos;
513 return false;
514 }
515
516 memcpy(output + *out_pos, temp_output, temp_written);
517 *out_pos += temp_written;
518 *p = hex_ptr;
519 return true;
520 }
521
522 /* Handle NULL pointer (address 0) with a known type prefix */
523 if (address == 0 && has_type_prefix) {
524 size_t saved_pos = *out_pos;
525 size_t prefix_len = hex_start - prefix_start;
526 if (*out_pos >= prefix_len) {
527 *out_pos -= prefix_len;
528 }
529
530 char type_str[LOG_NAMED_TYPE_BUFFER_SIZE];
531 if (prefix_type_len >= sizeof(type_str)) {
532 *out_pos = saved_pos;
533 return false;
534 }
535 memcpy(type_str, prefix_type, prefix_type_len);
536 type_str[prefix_type_len] = '\0';
537
538 char temp_output[LOG_NAMED_TEMP_OUTPUT_BUFFER_SIZE];
539 int temp_written =
540 snprintf(temp_output, sizeof(temp_output), "%s/(null)", type_str);
541 if (temp_written <= 0 || (size_t)temp_written >= sizeof(temp_output)) {
542 *out_pos = saved_pos;
543 return false;
544 }
545
546 if (*out_pos + temp_written >= output_size - 1) {
547 *out_pos = saved_pos;
548 return false;
549 }
550
551 memcpy(output + *out_pos, temp_output, temp_written);
552 *out_pos += temp_written;
553 *p = hex_ptr;
554 return true;
555 }
556
557 return false;
558}
559
560int log_named_format_message(const char *message, char *output,
561 size_t output_size) {
562 if (!message || !output || output_size == 0) {
563 return -1;
564 }
565
566 size_t out_pos = 0;
567 const char *p = message;
568 bool any_transformed = false;
569
570 while (*p && out_pos < output_size - 1) {
571 /* Look for "0x" prefix indicating a hex address */
572 if (*p == '0' && *(p + 1) == 'x' && isxdigit(*(p + 2))) {
573 const char *hex_start = p;
574 if (try_format_hex_address(message, hex_start, output, output_size,
575 &out_pos, &p)) {
576 any_transformed = true;
577 continue;
578 }
579
580 /* Not registered or formatting failed - copy original hex address */
581 const char *hex_end = hex_start + 2; /* Start after "0x" */
582 while (*hex_end && isxdigit(*hex_end)) {
583 hex_end++;
584 }
585 size_t hex_len = hex_end - hex_start;
586 if (hex_len < output_size - out_pos) {
587 memcpy(output + out_pos, hex_start, hex_len);
588 out_pos += hex_len;
589 p = hex_end;
590 } else {
591 /* Buffer overflow - truncate and stop */
592 break;
593 }
594 } else if (isdigit(*p)) {
595 /* Look for decimal integers that might be FDs or packet types */
596 const char *int_start = p;
597 int fd_value = 0;
598 int digit_count = 0;
599
600 /* Parse decimal digits */
601 while (*p && isdigit(*p) &&
602 digit_count < 6) { /* FDs rarely exceed 999999 */
603 fd_value = fd_value * 10 + (*p - '0');
604 p++;
605 digit_count++;
606 }
607
608 /* Skip if this integer is part of already-formatted output like "(fd=20)"
609 * or "(type/name (...))" */
610 bool is_already_formatted = false;
611 if (int_start - message >= 5) {
612 /* Check for "(fd=", "(pkt_type=", "(socket=", "(sockfd=", or "(type/"
613 * patterns to prevent re-formatting. Named objects are formatted as:
614 * type/name (0xaddress) or type/name (key=value) We need to detect if
615 * we're already inside a formatted output.
616 */
617 const char *check = int_start - 1;
618 while (check > message && isspace(*check))
619 check--;
620
621 // Only access *check if pointer is within bounds
622 if (check >= message && *check == '=') {
623 /* Found "=", now check what prefix it has for patterns like "(fd=20)"
624 * or "(socket=20)" or "(pkt_type=123)" */
625 const char *eq_pos = check;
626 check--;
627 while (check > message && (isalnum(*check) || *check == '_')) {
628 check--;
629 }
630 check++;
631
632 size_t prefix_len = eq_pos - check;
633 if ((prefix_len == 2 && strncmp(check, "fd", 2) == 0) ||
634 (prefix_len == 6 && strncmp(check, "socket", 6) == 0) ||
635 (prefix_len == 6 && strncmp(check, "sockfd", 6) == 0) ||
636 (prefix_len == 8 && strncmp(check, "pkt_type", 8) == 0)) {
637 /* Only mark as already-formatted if inside formatted output:
638 * - "(" before keyword: parenthesized value like "(sockfd=15)"
639 * - "/" before keyword: name part of type/name like "fd/fd=7"
640 * Raw log patterns like "sockfd=15" should still be eligible for
641 * replacement. */
642 const char *before_kw = check - 1;
643 while (before_kw > message && isspace(*before_kw))
644 before_kw--;
645 if (before_kw >= message &&
646 (*before_kw == '(' || *before_kw == '/')) {
647 is_already_formatted = true;
648 }
649 }
650 } else if (check >= message && *check == '/') {
651 /* Found "/", now check if this is a type/name pattern.
652 * Format is "(type/name (...)" where type is word characters (thread,
653 * mutex, socket, etc.) Must verify "/" is preceded by word characters
654 * AND "(" to ensure it's a formatted type.
655 */
656 const char *slash_pos = check;
657 check--;
658 while (check > message && (isalnum(*check) || *check == '_')) {
659 check--;
660 }
661 check++;
662
663 /* Check that the "/" was preceded by word characters (indicating a
664 * type name) */
665 size_t type_len = slash_pos - check;
666 if (type_len > 0 && (isalpha(*check) || *check == '_')) {
667 /* Verify this is a registered type/name pattern by checking for "("
668 * before the type AND checking if the type actually exists in the
669 * registry */
670 const char *type_start = check;
671 const char *before_type = type_start - 1;
672 while (before_type > message && isspace(*before_type)) {
673 before_type--;
674 }
675
676 /* Only mark as formatted if:
677 * 1. Preceded by "(" - ensures pattern looks like "(type/name ...)"
678 * 2. Type exists in registry - prevents false positives on random
679 * text */
680 if (before_type >= message && *before_type == '(' &&
681 is_type_in_registry(type_start, type_len)) {
682 is_already_formatted = true;
683 }
684 }
685 }
686 }
687
688 /* Check if this is a registered packet type */
689 if (!is_already_formatted && digit_count > 0 &&
690 has_packet_type_prefix(message, int_start)) {
691 named_metadata_t metadata;
692 const char *name = named_packet_metadata_copy(fd_value, &metadata)
693 ? metadata.name
694 : NULL;
695 if (name) {
696 const char *prefix_start =
697 find_packet_type_prefix_start(message, int_start);
698 size_t prefix_len = int_start - prefix_start;
699 size_t saved_pos = out_pos;
700
701 if (out_pos >= prefix_len) {
702 out_pos -= prefix_len;
703 }
704
705 if (write_formatted_packet_type(fd_value, name, output, output_size,
706 &out_pos)) {
707 any_transformed = true;
708 continue;
709 }
710
711 out_pos = saved_pos; /* Restore on failure */
712 }
713 }
714
715 /* Check if this is a registered file descriptor */
716 if (!is_already_formatted && digit_count > 0 &&
717 has_fd_prefix(message, int_start)) {
718 named_metadata_t metadata;
719 const char *name =
720 named_fd_metadata_copy(fd_value, &metadata) ? metadata.name : NULL;
721 const char *reg_type = NULL;
722 if (!name &&
723 named_metadata_copy((uintptr_t)(intptr_t)fd_value, &metadata)) {
724 name = metadata.name;
725 reg_type = metadata.type;
726 }
727
728 if (name) {
729 const char *prefix_start = find_fd_prefix_start(message, int_start);
730 size_t prefix_len = int_start - prefix_start;
731 size_t saved_pos = out_pos;
732
733 if (out_pos >= prefix_len) {
734 out_pos -= prefix_len;
735 }
736
737 bool formatted = false;
738 if (reg_type) {
739 /* From main registry — use registered type and original prefix
740 * keyword */
741 const char *kw_end = prefix_start;
742 while (kw_end < int_start && (isalnum(*kw_end) || *kw_end == '_'))
743 kw_end++;
744 size_t kw_len = kw_end - prefix_start;
745
746 char kw_buf[LOG_NAMED_KW_BUFFER_SIZE];
747 if (kw_len > 0 && kw_len < sizeof(kw_buf)) {
748 memcpy(kw_buf, prefix_start, kw_len);
749 kw_buf[kw_len] = '\0';
750 } else {
751 snprintf(kw_buf, sizeof(kw_buf), "%.*s",
752 (int)(sizeof(kw_buf) - 1), reg_type);
753 }
754
756 int written = snprintf(temp, sizeof(temp), "%s/%s (%s=%d)",
757 reg_type, name, kw_buf, fd_value);
758 if (written > 0 && (size_t)written < sizeof(temp) &&
759 out_pos + (size_t)written < output_size - 1) {
760 memcpy(output + out_pos, temp, (size_t)written);
761 out_pos += (size_t)written;
762 formatted = true;
763 }
764 } else {
765 formatted = write_formatted_fd(fd_value, name, output, output_size,
766 &out_pos);
767 }
768
769 if (formatted) {
770 any_transformed = true;
771 continue;
772 }
773
774 out_pos = saved_pos; /* Restore on failure */
775 }
776 }
777
778 /* Not a registered FD or formatting failed - copy original number */
779 if (!copy_unformatted_decimal(int_start, p, output, output_size,
780 &out_pos)) {
781 break;
782 }
783 } else {
784 /* Check if this is the start of "type=DIGIT" pattern */
785 if (*p == 't' && p + 4 < message + strlen(message) &&
786 is_type_equals_pattern(p)) {
787 if (!type_equals_inside_parens(p, message)) {
788 const char *digit_end = p + 5;
789 int pkt_value = parse_decimal_digits(digit_end, &digit_end);
790
791 if (pkt_value > 0) {
792 named_metadata_t metadata;
793 const char *name = named_packet_metadata_copy(pkt_value, &metadata)
794 ? metadata.name
795 : NULL;
796 if (name && write_formatted_type_equals(pkt_value, name, output,
797 output_size, &out_pos)) {
798 p = digit_end;
799 any_transformed = true;
800 continue;
801 }
802 }
803 }
804 }
805
806 /* Regular character - copy it */
807 copy_char_to_output(output, &out_pos, *p++);
808 }
809 }
810
811 /* Null terminate */
812 if (out_pos < output_size) {
813 output[out_pos] = '\0';
814 } else {
815 output[output_size - 1] = '\0';
816 }
817
818 return any_transformed ? (int)out_pos : -1;
819}
820
831const char *log_named_format_or_original(const char *message) {
832 if (!message) {
833 return message;
834 }
835
836 static _Thread_local char format_buffer[NAMED_FORMAT_BUFFER_SIZE];
837 static _Thread_local char work_buffer[NAMED_FORMAT_BUFFER_SIZE];
838
839 /* Apply formatting iteratively until the message stabilizes (no more changes)
840 */
841 const char *current = message;
842 size_t max_iterations = 3; /* Reduce iterations to prevent recursion (prevent
843 transforming already-transformed output) */
844
845 for (size_t iter = 0; iter < max_iterations; iter++) {
846 int result =
847 log_named_format_message(current, format_buffer, sizeof(format_buffer));
848
849 if (result <= 0) {
850 /* No transformation was made, we've reached a fixpoint */
851 return current == message ? message : format_buffer;
852 }
853
854 /* Check if the message changed */
855 if (strcmp(current, format_buffer) == 0) {
856 /* No actual change, return the current version */
857 return format_buffer;
858 }
859
860 /* Message changed, prepare for next iteration */
861 if (current == message) {
862 /* First iteration, use format_buffer as input for next iteration */
863 current = format_buffer;
864 } else {
865 /* Subsequent iterations, swap buffers */
866 asciichat_error_t strcpy_result =
867 SAFE_STRCPY(work_buffer, sizeof(work_buffer), format_buffer);
868 if (strcpy_result != ASCIICHAT_OK) {
869 log_error("Failed to copy format buffer: %s",
870 asciichat_error_format(NULL, strcpy_result));
871 return format_buffer;
872 }
873 current = work_buffer;
874 }
875 }
876
877 /* Max iterations reached, return last formatted version */
878 return format_buffer;
879}
880
897const char *colorize_named_string(const char *name_str) {
898#define COLORIZE_BUFFERS 4
899#define COLORIZE_BUFFER_SIZE 512
900 static char buffers[COLORIZE_BUFFERS][COLORIZE_BUFFER_SIZE];
901 static int buffer_idx = 0;
902
903 if (!name_str || name_str[0] == '\0') {
904 return name_str;
905 }
906
907 char *current_buf = buffers[buffer_idx];
908 buffer_idx = (buffer_idx + 1) % COLORIZE_BUFFERS;
909
910 /* Find the slash separator (type/name) */
911 const char *slash = strchr(name_str, '/');
912 if (!slash) {
913 /* No slash found - just return the string as-is */
914 return name_str;
915 }
916
917 /* Find opening paren for address */
918 const char *paren = strchr(name_str, '(');
919
920 /* Extract type (before slash) */
921 size_t type_len = slash - name_str;
923 size_t copy_len =
924 type_len < sizeof(type_buf) - 1 ? type_len : sizeof(type_buf) - 1;
925 memcpy(type_buf, name_str, copy_len);
926 type_buf[copy_len] = '\0';
927
928 /* Extract name (between slash and paren, or end of string) */
929 const char *name_start = slash + 1;
930 size_t name_len;
931 if (paren && paren > slash) {
932 /* Skip spaces before paren */
933 const char *name_end = paren - 1;
934 while (name_end > name_start && *name_end == ' ') {
935 name_end--;
936 }
937 name_len = name_end - name_start + 1;
938 } else {
939 name_len = strlen(name_start);
940 }
941
942 char name_buf[LOG_NAMED_NAME_BUFFER_SIZE];
943 size_t name_copy_len =
944 name_len < sizeof(name_buf) - 1 ? name_len : sizeof(name_buf) - 1;
945 memcpy(name_buf, name_start, name_copy_len);
946 name_buf[name_copy_len] = '\0';
947
948 /* Apply colors */
949 const char *type_colored = colored_string(LOG_COLOR_WARN, type_buf);
950 const char *name_colored = colored_string(LOG_COLOR_DEV, name_buf);
951
952 /* Format: type(yellow)/name(blue) and add address part if present */
953 if (paren) {
954 /* Extract address (0x...) from parentheses */
955 const char *addr_start = paren + 1;
956 const char *addr_end = strchr(addr_start, ')');
957 if (addr_end) {
958 size_t addr_len = addr_end - addr_start;
959 char addr_buf[LOG_NAMED_ADDR_BUFFER_SIZE];
960 size_t addr_copy_len =
961 addr_len < sizeof(addr_buf) - 1 ? addr_len : sizeof(addr_buf) - 1;
962 memcpy(addr_buf, addr_start, addr_copy_len);
963 addr_buf[addr_copy_len] = '\0';
964
965 const char *addr_colored = colored_string(LOG_COLOR_GREY, addr_buf);
966 safe_snprintf(current_buf, COLORIZE_BUFFER_SIZE, "%s/%s (%s)",
967 type_colored, name_colored, addr_colored);
968 } else {
969 safe_snprintf(current_buf, COLORIZE_BUFFER_SIZE, "%s/%s %s", type_colored,
970 name_colored, paren);
971 }
972 } else {
973 safe_snprintf(current_buf, COLORIZE_BUFFER_SIZE, "%s/%s", type_colored,
974 name_colored);
975 }
976
977 return current_buf;
978#undef COLORIZE_BUFFERS
979#undef COLORIZE_BUFFER_SIZE
980}
const char * asciichat_error_format(const char *message, asciichat_error_t code)
@ LOG_COLOR_GREY
Definition color.h:13
@ LOG_COLOR_DEV
Definition color.h:7
@ LOG_COLOR_WARN
Definition color.h:10
Named object registry for debugging — log identifiable resource names.
#define SAFE_STRCPY(dest, dest_size, src)
bool named_registry_has_type(const char *type, size_t length)
bool named_packet_metadata_copy(int packet_type, named_metadata_t *output)
bool named_metadata_copy(uintptr_t key, named_metadata_t *output)
bool named_fd_metadata_copy(int fd, named_metadata_t *output)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ASCIICHAT_OK
Definition error_codes.h:50
int log_named_format_message(const char *message, char *output, size_t output_size)
Format hex addresses in a message as named object descriptions.
Definition log/named.c:560
const char * log_named_format_or_original(const char *message)
Format named objects in a message (thread-local buffer).
Definition log/named.c:831
#define log_error(...)
Log an ERROR message.
Definition log/log.h:604
const char * colorize_named_string(const char *name_str)
Colorize a "type/name (0xaddress)" string for display in memory reports.
Definition log/named.c:897
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
📝 Logging API with multiple log levels and terminal output control
#define LOG_NAMED_ID_EXTENDED_BUFFER_SIZE
Definition log/named.c:21
#define LOG_NAMED_KW_BUFFER_SIZE
Definition log/named.c:23
#define LOG_NAMED_ADDR_BUFFER_SIZE
Definition log/named.c:27
#define LOG_NAMED_ID_BUFFER_SIZE
Definition log/named.c:19
#define COLORIZE_BUFFER_SIZE
#define LOG_NAMED_TYPE_BUFFER_SIZE
Definition log/named.c:22
#define LOG_NAMED_TEMP_OUTPUT_BUFFER_SIZE
Definition log/named.c:20
#define LOG_NAMED_TYPE_EXTENDED_BUFFER_SIZE
Definition log/named.c:25
#define LOG_NAMED_TEMP_BUFFER_SIZE
Definition log/named.c:24
#define COLORIZE_BUFFERS
#define NAMED_FORMAT_BUFFER_SIZE
Definition log/named.c:50
#define LOG_NAMED_NAME_BUFFER_SIZE
Definition log/named.c:26
🔤 String Manipulation and Shell Escaping Utilities
The useful part of named metadata is in its fields: together they describe what is pending,...