ascii-chat
Binary v0.16.50-198-g78f51665f
Library lib/v0.3.2-2240-g78f51665f
Video chat in your terminal
Loading...
Searching...
No Matches
log/format.c
Go to the documentation of this file.
1
6
7/* Storage capacities in elements; text buffers include the terminating NUL. */
8#define LOG_FORMAT_STRFTIME_LITERAL_MAX 64
9#define LOG_FORMAT_FORMAT_BUFFER_SIZE (1 + LOG_FORMAT_STRFTIME_LITERAL_MAX + 1)
10#define LOG_FORMAT_LEVEL_NAME_BUFFER_SIZE 32
11#define LOG_FORMAT_CONTENT_BUFFER_SIZE 512
12
20#include <ascii-chat/source_root.h>
21#include <ascii-chat/common.h>
22#include <ascii-chat/log/log.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <ctype.h>
28#include <time.h>
29
30/* ============================================================================
31 * Default Format String (mode-aware)
32 * ============================================================================ */
33
34/* ============================================================================
35 * Format Parser Implementation
36 * ============================================================================ */
37
51static log_template_t *parse_format_string(const char *format_str, bool console_only) {
52 if (!format_str) {
53 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid format string: NULL");
54 return NULL;
55 }
56
57 bool is_valid = utf8_is_valid(format_str);
58
59 if (!is_valid) {
60 SET_ERRNO(ERROR_INVALID_STATE, "Invalid UTF-8 in log format string");
61 return NULL;
62 }
63
64 // Use regular malloc/calloc instead of SAFE_* to avoid debug tracking overhead at startup
65 // This avoids mutex contention and memory tracking overhead during log initialization
66 log_template_t *result = (log_template_t *)calloc(1, sizeof(log_template_t));
67 if (!result) {
68 return NULL;
69 }
70
71 size_t format_len = strlen(format_str) + 1;
72 result->original = (char *)malloc(format_len);
73 if (!result->original) {
74 free(result);
75 return NULL;
76 }
77 asciichat_error_t strcpy_result = SAFE_STRCPY(result->original, format_len, format_str);
78 if (strcpy_result != ASCIICHAT_OK) {
79 log_error("Failed to copy format string: %s", asciichat_error_format(NULL, strcpy_result));
80 free(result->original);
81 free(result);
82 return NULL;
83 }
84 result->console_only = console_only;
85
86 /* Pre-allocate spec array (worst case: every char is a specifier) */
87 result->specs = (log_format_spec_t *)calloc(strlen(format_str) + 1, sizeof(log_format_spec_t));
88 if (!result->specs) {
89 free(result->original);
90 free(result);
91 return NULL;
92 }
93
94 const char *p = format_str;
95 size_t spec_idx = 0;
96
97 while (*p) {
98 /* Check for escape sequences first */
99 if (*p == '\\' && *(p + 1)) {
100 char next = *(p + 1);
101
102 if (next == 'n') {
103 /* \n - platform-aware newline */
104 result->specs[spec_idx].type = LOG_FORMAT_NEWLINE;
105 spec_idx++;
106 p += 2;
107 } else if (next == '\\') {
108 /* \\ - escaped backslash (output single \‍) */
109 result->specs[spec_idx].type = LOG_FORMAT_LITERAL;
110 result->specs[spec_idx].literal = (char *)malloc(2);
111 if (!result->specs[spec_idx].literal) {
112 goto cleanup;
113 }
114 result->specs[spec_idx].literal[0] = '\\';
115 result->specs[spec_idx].literal[1] = '\0';
116 result->specs[spec_idx].literal_len = 1;
117 spec_idx++;
118 p += 2;
119 } else {
120 /* Invalid escape sequence - treat backslash as literal */
121 result->specs[spec_idx].type = LOG_FORMAT_LITERAL;
122 result->specs[spec_idx].literal = (char *)malloc(2);
123 if (!result->specs[spec_idx].literal) {
124 goto cleanup;
125 }
126 result->specs[spec_idx].literal[0] = '\\';
127 result->specs[spec_idx].literal[1] = '\0';
128 result->specs[spec_idx].literal_len = 1;
129 spec_idx++;
130 p++;
131 }
132 } else if (*p == '%' && *(p + 1)) {
133 if (*(p + 1) == '%') {
134 /* %% - escaped percent (output single %) */
135 result->specs[spec_idx].type = LOG_FORMAT_LITERAL;
136 result->specs[spec_idx].literal = (char *)malloc(2);
137 if (!result->specs[spec_idx].literal) {
138 goto cleanup;
139 }
140 result->specs[spec_idx].literal[0] = '%';
141 result->specs[spec_idx].literal[1] = '\0';
142 result->specs[spec_idx].literal_len = 1;
143 spec_idx++;
144 p += 2;
145 } else {
146 /* Start of format specifier */
147 p++;
148
149 if (strncmp(p, "time(", 5) == 0) {
150 /* Parse %time(format) */
151 p += 5;
152 const char *fmt_start = p;
153 const char *fmt_end = strchr(p, ')');
154 if (!fmt_end) {
155 /* Parse error: unterminated time format */
156 log_error("Invalid %%time format: missing closing )");
157 goto cleanup;
158 }
159
160 result->specs[spec_idx].type = LOG_FORMAT_TIME;
161 size_t fmt_len = fmt_end - fmt_start;
162 result->specs[spec_idx].literal = (char *)malloc(fmt_len + 1);
163 if (!result->specs[spec_idx].literal) {
164 goto cleanup;
165 }
166 memcpy(result->specs[spec_idx].literal, fmt_start, fmt_len);
167 result->specs[spec_idx].literal[fmt_len] = '\0';
168 result->specs[spec_idx].literal_len = fmt_len;
169
170 spec_idx++;
171 p = fmt_end + 1;
172 } else if (strncmp(p, "level_aligned", 13) == 0) {
173 result->specs[spec_idx].type = LOG_FORMAT_LEVEL_ALIGNED;
174 spec_idx++;
175 p += 13;
176 } else if (strncmp(p, "level", 5) == 0) {
177 result->specs[spec_idx].type = LOG_FORMAT_LEVEL;
178 spec_idx++;
179 p += 5;
180 } else if (strncmp(p, "file_relative", 13) == 0) {
181 result->specs[spec_idx].type = LOG_FORMAT_FILE_RELATIVE;
182 spec_idx++;
183 p += 13;
184 } else if (strncmp(p, "file", 4) == 0) {
185 result->specs[spec_idx].type = LOG_FORMAT_FILE;
186 spec_idx++;
187 p += 4;
188 } else if (strncmp(p, "line", 4) == 0) {
189 result->specs[spec_idx].type = LOG_FORMAT_LINE;
190 spec_idx++;
191 p += 4;
192 } else if (strncmp(p, "func", 4) == 0) {
193 result->specs[spec_idx].type = LOG_FORMAT_FUNC;
194 spec_idx++;
195 p += 4;
196 } else if (strncmp(p, "tname", 5) == 0) {
197 result->specs[spec_idx].type = LOG_FORMAT_TNAME;
198 spec_idx++;
199 p += 5;
200 } else if (strncmp(p, "tid", 3) == 0) {
201 result->specs[spec_idx].type = LOG_FORMAT_TID;
202 spec_idx++;
203 p += 3;
204 } else if (strncmp(p, "colored_message", 15) == 0) {
205 result->specs[spec_idx].type = LOG_FORMAT_COLORED_MESSAGE;
206 spec_idx++;
207 p += 15;
208 } else if (strncmp(p, "ms", 2) == 0) {
209 result->specs[spec_idx].type = LOG_FORMAT_MICROSECONDS;
210 spec_idx++;
211 p += 2;
212 } else if (strncmp(p, "ns", 2) == 0) {
213 result->specs[spec_idx].type = LOG_FORMAT_NANOSECONDS;
214 spec_idx++;
215 p += 2;
216 } else if (strncmp(p, "message", 7) == 0) {
217 result->specs[spec_idx].type = LOG_FORMAT_MESSAGE;
218 spec_idx++;
219 p += 7;
220 } else if (strncmp(p, "colorlog_level_string_to_color", 30) == 0) {
221 result->specs[spec_idx].type = LOG_FORMAT_COLORLOG_LEVEL;
222 spec_idx++;
223 p += 30;
224 } else if (strncmp(p, "color(", 6) == 0) {
225 /* Parse %color(LEVEL, content) */
226 p += 6; /* Skip "color(" */
227
228 /* Find the matching closing paren */
229 int paren_depth = 1;
230 const char *color_start = p;
231 while (*p && paren_depth > 0) {
232 if (*p == '(')
233 paren_depth++;
234 else if (*p == ')')
235 paren_depth--;
236 if (paren_depth > 0)
237 p++;
238 }
239
240 if (!*p || paren_depth != 0) {
241 /* Parse error: unterminated color format */
242 log_error("Invalid %%color format: missing closing )");
243 goto cleanup;
244 }
245
246 size_t color_arg_len = p - color_start;
247
248 /* Store as "LEVEL|content" for later parsing */
249 if (result->specs) {
250 result->specs[spec_idx].type = LOG_FORMAT_COLOR;
251 result->specs[spec_idx].literal = (char *)malloc(color_arg_len + 1);
252 }
253
254 if (!result->specs[spec_idx].literal) {
255 goto cleanup;
256 }
257
258 if (result->specs) {
259 memcpy(result->specs[spec_idx].literal, color_start, color_arg_len);
260 result->specs[spec_idx].literal[color_arg_len] = '\0';
261 result->specs[spec_idx].literal_len = color_arg_len;
262 }
263
264 spec_idx++;
265 p++; /* Skip closing paren */
266 } else {
267 /* Unknown ascii-chat specifier - treat as strftime format code and pass to strftime
268 * This allows strftime formats like %H, %M, %S, %A, %B, etc. to work
269 * without needing custom parsing for each one. strftime will handle validation. */
270 const char *fmt_start = p;
271 size_t fmt_len = 0;
272
273 /* Collect the format code (usually 1-2 chars, but allow flexibility) */
274 while (*p && *p != '%' && *p != '\\' && fmt_len < 8) {
275 p++;
276 fmt_len++;
277 }
278
279 if (fmt_len == 0) {
280 log_error("Empty format specifier: %%");
281 goto cleanup;
282 }
283
284 result->specs[spec_idx].type = LOG_FORMAT_STRFTIME_CODE;
285 result->specs[spec_idx].literal = (char *)malloc(fmt_len + 1);
286 if (!result->specs[spec_idx].literal) {
287 goto cleanup;
288 }
289 memcpy(result->specs[spec_idx].literal, fmt_start, fmt_len);
290 result->specs[spec_idx].literal[fmt_len] = '\0';
291 result->specs[spec_idx].literal_len = fmt_len;
292
293 spec_idx++;
294 /* p already advanced in the while loop above */
295 }
296 }
297 } else {
298 /* Literal text until next escape or specifier */
299 const char *text_start = p;
300 while (*p && *p != '\\' && *p != '%') {
301 p++;
302 }
303
304 result->specs[spec_idx].type = LOG_FORMAT_LITERAL;
305 size_t text_len = p - text_start;
306 result->specs[spec_idx].literal = (char *)malloc(text_len + 1);
307 if (!result->specs[spec_idx].literal) {
308 goto cleanup;
309 }
310
311 /* Safe memcpy for UTF-8 literal text (already validated) */
312 memcpy(result->specs[spec_idx].literal, text_start, text_len);
313 result->specs[spec_idx].literal[text_len] = '\0';
314 result->specs[spec_idx].literal_len = text_len;
315 spec_idx++;
316 }
317 }
318
319 result->spec_count = spec_idx;
320 return result;
321
322cleanup:
323 log_template_free(result);
324 result = NULL;
325 return NULL;
326}
327
328log_template_t *log_template_parse(const char *format_str, bool console_only) {
329 return parse_format_string(format_str, console_only);
330}
331
333 if (!format) {
334 SET_ERRNO(ERROR_INVALID_PARAM, "null format");
335 return;
336 }
337
338 if (format->specs) {
339 for (size_t i = 0; i < format->spec_count; i++) {
340 if (format->specs[i].literal) {
341 free(format->specs[i].literal);
342 format->specs[i].literal = NULL;
343 }
344 }
345 free(format->specs);
346 format->specs = NULL;
347 }
348
349 if (format->original) {
350 free(format->original);
351 format->original = NULL;
352 }
353
354 free(format);
355 format = NULL;
356}
357
358/* ============================================================================
359 * Format Application Implementation
360 * ============================================================================ */
361
370// get_level_string_padded is declared in logging.h and defined in logging.c
371
378static const char *get_level_string(log_level_t level) {
379 switch (level) {
380 case LOG_DEV:
381 return "DEV";
382 case LOG_DEBUG:
383 return "DEBUG";
384 case LOG_INFO:
385 return "INFO";
386 case LOG_WARN:
387 return "WARN";
388 case LOG_ERROR:
389 return "ERROR";
390 case LOG_FATAL:
391 return "FATAL";
392 default:
393 return "";
394 }
395}
396
409static log_color_t parse_color_level(const char *level_name, log_level_t current_level) {
410 if (!level_name) {
411 return LOG_COLOR_RESET;
412 }
413
414 /* Special case: "*" means use the current log level */
415 if (strcmp(level_name, "*") == 0) {
416 switch (current_level) {
417 case LOG_DEV:
418 return LOG_COLOR_DEV;
419 case LOG_DEBUG:
420 return LOG_COLOR_DEBUG;
421 case LOG_INFO:
422 return LOG_COLOR_INFO;
423 case LOG_WARN:
424 return LOG_COLOR_WARN;
425 case LOG_ERROR:
426 return LOG_COLOR_ERROR;
427 case LOG_FATAL:
428 return LOG_COLOR_FATAL;
429 default:
430 return LOG_COLOR_RESET;
431 }
432 }
433
434 /* Log level names */
435 if (strcmp(level_name, "DEV") == 0) {
436 return LOG_COLOR_DEV;
437 } else if (strcmp(level_name, "DEBUG") == 0) {
438 return LOG_COLOR_DEBUG;
439 } else if (strcmp(level_name, "INFO") == 0) {
440 return LOG_COLOR_INFO;
441 } else if (strcmp(level_name, "WARN") == 0) {
442 return LOG_COLOR_WARN;
443 } else if (strcmp(level_name, "ERROR") == 0) {
444 return LOG_COLOR_ERROR;
445 } else if (strcmp(level_name, "FATAL") == 0) {
446 return LOG_COLOR_FATAL;
447 }
448
449 /* Literal color names */
450 if (strcmp(level_name, "GREY") == 0 || strcmp(level_name, "GRAY") == 0) {
451 return LOG_COLOR_GREY;
452 }
453
454 return LOG_COLOR_RESET;
455}
456
477static int render_format_content(const char *content, char *buf, size_t buf_size, log_level_t level, const char *timestamp, const char *file, int line, const char *func, uint64_t tid,
478 const char *message, bool use_colors, uint64_t time_nanoseconds) {
479 if (!content || !buf || buf_size == 0) {
480 SET_ERRNO(ERROR_INVALID_PARAM, "Invalid arguments: content=%p, buf=%p, buf_size=%zu", content, buf, buf_size);
481 return -1;
482 }
483
484 /* Parse content as a format string and apply it */
485 log_template_t *content_format = log_template_parse(content, false);
486 if (!content_format) {
487 return -1;
488 }
489
490 int written = log_template_apply(content_format, buf, buf_size, level, timestamp, file, line, func, tid, message, use_colors, time_nanoseconds);
491 log_template_free(content_format);
492
493 return written;
494}
495
496int log_template_apply(const log_template_t *format, char *buf, size_t buf_size, log_level_t level, const char *timestamp, const char *file, int line, const char *func, uint64_t tid,
497 const char *message, bool use_colors, uint64_t time_nanoseconds) {
498 /* Check all critical pointers - format could be freed by another thread (TOCTOU race) */
499 if (!format || !buf || buf_size == 0) {
500 return -1;
501 }
502
503 int total_written = 0;
504 char *p = buf;
505 size_t remaining = buf_size - 1; /* Reserve space for null terminator */
506
507 (void)timestamp; /* May be used in future; for now, LOG_FORMAT_TIME uses custom formatting */
508
509 /* Safety check: verify format->specs is valid before dereferencing
510 * This protects against use-after-free if format is freed by another thread (TOCTOU race).
511 * Between our initial NULL check and here, the main thread may have freed this memory. */
512 if (!format || !format->specs || format->spec_count == 0) {
513 return -1;
514 }
515
516 for (size_t i = 0; i < format->spec_count; i++) {
517 /* Re-check specs pointer in loop to catch use-after-free during iteration */
518 if (!format->specs || i >= format->spec_count) {
519 break; /* Format was freed, stop processing */
520 }
521 const log_format_spec_t *spec = &format->specs[i];
522
523 /* Additional safety check: if spec is null or invalid, stop processing */
524 if (!spec) {
525 break;
526 }
527
528 int written = 0;
529
530 switch (spec->type) {
532 written = safe_snprintf(p, remaining + 1, "%s", spec->literal ? spec->literal : "");
533 break;
534
535 case LOG_FORMAT_TIME:
536 /* Format time using custom time formatter */
537 if (spec->literal) {
538 written = time_format_now(spec->literal, p, remaining + 1);
539 if (written <= 0) {
540 log_debug("time_format_now failed for format: %s", spec->literal);
541 written = 0;
542 }
543 }
544 break;
545
546 case LOG_FORMAT_LEVEL:
547 /* Log level as string (DEV, DEBUG, INFO, WARN, ERROR, FATAL) */
548 written = safe_snprintf(p, remaining + 1, "%s", get_level_string(level));
549 break;
550
552 /* Log level padded to exactly 5 characters (prevents truncation to [DEBU]/[ERRO]) */
553 written = safe_snprintf(p, remaining + 1, "%s", get_level_string_padded(level));
554 break;
555
556 case LOG_FORMAT_FILE:
557 if (file) {
558 written = safe_snprintf(p, remaining + 1, "%s", file);
559 }
560 break;
561
563 if (file) {
564 const char *rel_file = extract_project_relative_path(file, ASCIICHAT_OWNED_SOURCE_DIRS);
565 written = safe_snprintf(p, remaining + 1, "%s", rel_file);
566 }
567 break;
568
569 case LOG_FORMAT_LINE:
570 if (line > 0) {
571 written = safe_snprintf(p, remaining + 1, "%d", line);
572 }
573 break;
574
575 case LOG_FORMAT_FUNC:
576 if (func) {
577 written = safe_snprintf(p, remaining + 1, "%s", func);
578 }
579 break;
580
581 case LOG_FORMAT_TID:
582 written = safe_snprintf(p, remaining + 1, "%llu", (unsigned long long)tid);
583 break;
584
585 case LOG_FORMAT_TNAME: {
586 // Registry thread keys use the same numeric identity as log records.
587 uintptr_t key = (uintptr_t)tid;
588 named_metadata_t metadata;
589 if (named_metadata_copy(key, &metadata)) {
590 written = safe_snprintf(p, remaining + 1, "%s", metadata.name);
591 } else {
592 written = safe_snprintf(p, remaining + 1, "%llu", (unsigned long long)tid);
593 }
594 break;
595 }
596
598 /* Extract microseconds from nanoseconds (ns_value % 1_000_000_000 / 1000) */
599 long nanoseconds = (long)(time_nanoseconds % NS_PER_SEC_INT);
600 long microseconds = nanoseconds / 1000;
601 if (microseconds < 0)
602 microseconds = 0;
603 if (microseconds > 999999)
604 microseconds = 999999;
605 written = safe_snprintf(p, remaining + 1, "%06ld", microseconds);
606 break;
607 }
608
610 /* Extract nanoseconds component (ns_value % 1_000_000_000) */
611 long nanoseconds = (long)(time_nanoseconds % NS_PER_SEC_INT);
612 if (nanoseconds < 0)
613 nanoseconds = 0;
614 if (nanoseconds > 999999999)
615 nanoseconds = 999999999;
616 written = safe_snprintf(p, remaining + 1, "%09ld", nanoseconds);
617 break;
618 }
619
621 /* strftime format code (like %H, %M, %S, %A, %B, etc.)
622 * Let strftime handle all the parsing and validation */
624 /* Construct format string with % prefix on stack */
625 char format_str[LOG_FORMAT_FORMAT_BUFFER_SIZE]; /* 1 (%) + 64 (literal) + 1 (null) */
626 format_str[0] = '%';
627 memcpy(format_str + 1, spec->literal, spec->literal_len);
628 format_str[spec->literal_len + 1] = '\0';
629 written = time_format_now(format_str, p, remaining + 1);
630 if (written <= 0) {
631 log_debug("time_format_now failed for format code: %s", format_str);
632 written = 0;
633 }
634 }
635 break;
636 }
637
639 if (message) {
640 /* Format hex addresses as named object descriptions (type/name format) */
641 const char *formatted_msg = log_named_format_or_original(message);
642 written = safe_snprintf(p, remaining + 1, "%s", formatted_msg);
643 }
644 break;
645
647 /* Apply colorize_log_message() for number/unit/hex highlighting, then format named objects */
648 if (message) {
649 const char *formatted_msg = log_named_format_or_original(message);
650 const char *colorized_msg = colorize_log_message_with_color(formatted_msg, use_colors);
651 written = safe_snprintf(p, remaining + 1, "%s", colorized_msg);
652 }
653 break;
654 }
655
657 /* Color code for the log level (placeholder for future color support) */
658 (void)use_colors; /* Suppress unused parameter warning */
659 written = 0;
660 break;
661
662 case LOG_FORMAT_COLOR: {
663 /* Parse and apply %color(LEVEL, content)
664 * literal stores "LEVEL,content" where LEVEL is the color level name (or "*" for current level)
665 * and content is a format string to render and colorize */
666 if (!spec->literal || spec->literal_len == 0) {
667 written = 0;
668 break;
669 }
670
671 /* Find the comma separating LEVEL and content */
672 const char *comma_pos = strchr(spec->literal, ',');
673 if (!comma_pos) {
674 /* Invalid format - no comma found */
675 log_debug("%%color format missing comma in: %s", spec->literal);
676 written = 0;
677 break;
678 }
679
680 /* Extract level name (before comma) */
681 size_t level_len = comma_pos - spec->literal;
682 char level_name[LOG_FORMAT_LEVEL_NAME_BUFFER_SIZE];
683 if (level_len >= sizeof(level_name)) {
684 /* Level name too long */
685 written = 0;
686 break;
687 }
688 memcpy(level_name, spec->literal, level_len);
689 level_name[level_len] = '\0';
690
691 /* Parse level name to log_color_t (pass current level for "*" support) */
692 log_color_t color = parse_color_level(level_name, level);
693
694 /* Extract content (after comma), skip leading whitespace */
695 const char *content_start = comma_pos + 1;
696 while (*content_start == ' ' || *content_start == '\t') {
697 content_start++;
698 }
699
700 /* Render content recursively */
701 char content_buf[LOG_FORMAT_CONTENT_BUFFER_SIZE];
702 int content_len = render_format_content(content_start, content_buf, sizeof(content_buf), level, timestamp, file, line, func, tid, message, use_colors, time_nanoseconds);
703
704 if (content_len < 0 || content_len >= (int)sizeof(content_buf)) {
705 written = 0;
706 break;
707 }
708
709 // The caller already selected colors. Respect that policy for both file
710 // and session logs instead of asking the console again for every field.
711 const char *start = log_level_color_for_output(color, use_colors);
712 const char *reset = log_level_color_for_output(LOG_COLOR_RESET, use_colors);
713 written = safe_snprintf(p, remaining + 1, "%s%s%s", start ? start : "", content_buf, reset ? reset : "");
714 break;
715 }
716
718 /* Platform-aware newline */
719#ifdef _WIN32
720 written = safe_snprintf(p, remaining + 1, "\r\n");
721#else
722 written = safe_snprintf(p, remaining + 1, "\n");
723#endif
724 break;
725
726 default:
727 break;
728 }
729
730 if (written < 0) {
731 /* snprintf error */
732 return -1;
733 }
734
735 if ((size_t)written > remaining) {
736 /* Buffer overflow prevention */
737 log_debug("buffer overflow prevented");
738 return -1;
739 }
740
741 p += written;
742 remaining -= written;
743 total_written += written;
744 }
745
746 *p = '\0';
747 return total_written;
748}
const char * asciichat_error_format(const char *message, asciichat_error_t code)
#define SET_ERRNO(code,...)
const char * colorize_log_message_with_color(const char *message, bool enabled)
Definition colorize.c:536
Log message colorization for terminal output.
Named object registry for debugging — log identifiable resource names.
#define SAFE_STRCPY(dest, dest_size, src)
unsigned long long uint64_t
bool named_metadata_copy(uintptr_t key, named_metadata_t *output)
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
@ ERROR_INVALID_STATE
@ ASCIICHAT_OK
Definition error_codes.h:50
@ ERROR_INVALID_PARAM
const char * log_level_color_for_output(log_color_t color, bool enabled)
int log_template_apply(const log_template_t *format, char *buf, size_t buf_size, log_level_t level, const char *timestamp, const char *file, int line, const char *func, uint64_t tid, const char *message, bool use_colors, uint64_t time_nanoseconds)
Apply format to a log entry and write result to buffer.
Definition log/format.c:496
const char * log_named_format_or_original(const char *message)
Format named objects in a message (thread-local buffer).
Definition log/named.c:875
log_color_t
Color enum for logging - indexes into color arrays.
Definition log/log.h:146
#define log_error(...)
Log an ERROR message.
Definition log/log.h:628
log_level_t
Logging levels enumeration.
Definition types.h:33
void log_template_free(log_template_t *format)
Free compiled format structure.
Definition log/format.c:332
log_template_t * log_template_parse(const char *format_str, bool console_only)
Parse a format string into compiled format structure.
Definition log/format.c:328
#define log_debug(...)
Log a DEBUG message.
Definition log/log.h:589
@ LOG_COLOR_RESET
Definition log/log.h:154
@ LOG_COLOR_GREY
Definition log/log.h:153
@ LOG_COLOR_FATAL
Definition log/log.h:152
@ LOG_COLOR_ERROR
Definition log/log.h:151
@ LOG_COLOR_DEV
Definition log/log.h:147
@ LOG_COLOR_DEBUG
Definition log/log.h:148
@ LOG_COLOR_INFO
Definition log/log.h:149
@ LOG_COLOR_WARN
Definition log/log.h:150
#define NS_PER_SEC_INT
Definition time.h:173
int time_format_now(const char *format_str, char *buf, size_t buf_size)
Format current time using strftime format string.
Definition time.c:712
bool utf8_is_valid(const char *str)
Check if a string is valid UTF-8.
Definition utf8.c:128
const char * extract_project_relative_path(const char *file, const char *const *owned_dirs)
Extract a path relative to the source root embedded by CMake.
Definition path.c:132
⚙️ Common definitions, error codes, macros, and types shared throughout the application
const char * get_level_string_padded(log_level_t level)
Get padded level string for consistent alignment.
#define LOG_FORMAT_FORMAT_BUFFER_SIZE
Definition log/format.c:9
#define LOG_FORMAT_CONTENT_BUFFER_SIZE
Definition log/format.c:11
#define LOG_FORMAT_STRFTIME_LITERAL_MAX
Definition log/format.c:8
#define LOG_FORMAT_LEVEL_NAME_BUFFER_SIZE
Definition log/format.c:10
@ LOG_FORMAT_LEVEL
Definition log/format.h:30
@ LOG_FORMAT_COLOR
Definition log/format.h:40
@ LOG_FORMAT_NEWLINE
Definition log/format.h:45
@ LOG_FORMAT_FILE_RELATIVE
Definition log/format.h:33
@ LOG_FORMAT_LINE
Definition log/format.h:34
@ LOG_FORMAT_MICROSECONDS
Definition log/format.h:42
@ LOG_FORMAT_TID
Definition log/format.h:36
@ LOG_FORMAT_COLORED_MESSAGE
Definition log/format.h:41
@ LOG_FORMAT_LITERAL
Definition log/format.h:28
@ LOG_FORMAT_COLORLOG_LEVEL
Definition log/format.h:39
@ LOG_FORMAT_MESSAGE
Definition log/format.h:38
@ LOG_FORMAT_TIME
Definition log/format.h:29
@ LOG_FORMAT_LEVEL_ALIGNED
Definition log/format.h:31
@ LOG_FORMAT_FUNC
Definition log/format.h:35
@ LOG_FORMAT_NANOSECONDS
Definition log/format.h:43
@ LOG_FORMAT_TNAME
Definition log/format.h:37
@ LOG_FORMAT_STRFTIME_CODE
Definition log/format.h:44
@ LOG_FORMAT_FILE
Definition log/format.h:32
📝 Logging API with multiple log levels and terminal output control
Format named objects in log messages (replaces hex addresses with type/name descriptions).
📂 Path Manipulation Utilities
🔤 String Manipulation and Shell Escaping Utilities
One literal or conversion token in a compiled log format.
Definition log/format.h:59
log_template_type_t type
Definition log/format.h:60
Compiled log format ready for use in log_template_apply().
Definition log/format.h:72
char * original
Definition log/format.h:75
log_format_spec_t * specs
Definition log/format.h:73
size_t spec_count
Definition log/format.h:74
The useful part of named metadata is in its fields: together they describe what is pending,...
⏱️ High-precision timing utilities using sokol_time.h and uthash
#define LOG_DEV
Definition types.h:42
#define LOG_DEBUG
Definition types.h:43
#define LOG_FATAL
Definition types.h:47
#define LOG_ERROR
Definition types.h:46
#define LOG_WARN
Definition types.h:45
#define LOG_INFO
Definition types.h:44
🔤 UTF-8 Encoding and Decoding Utilities