ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
Video chat in your terminal
Loading...
Searching...
No Matches
colorize.h File Reference

Log message colorization for terminal output. More...

Go to the source code of this file.

Functions

const char * colorize_log_message (const char *message)
 Colorize a log message for terminal output.
const char * colorize_log_message_with_color (const char *message, bool enabled)

Detailed Description

Log message colorization for terminal output.

Colorizes log messages for terminal output by applying colors to:

  • Numbers and numeric units (cyan/light blue)
  • File paths like /path/to/file or C:\path (magenta)
  • Environment variables like $VAR_NAME (grey)

Only applies colors when output goes to TTY (not when piped). File logging output remains uncolorized.

Definition in file colorize.h.

Function Documentation

◆ colorize_log_message()

const char * colorize_log_message ( const char * message)

Colorize a log message for terminal output.

Applies semantic colors to:

  • Numeric values: 0, 25, 0.24, 1920x1080, 25 MB, 1024 GiB, etc. (cyan)
  • File paths: /path/to/file, C:\path, src/main.c, etc. (magenta)
  • Environment variables: $VAR_NAME, $HOME, etc. (grey)
Parameters
messageThe log message to colorize
Returns
Colorized string in static buffer (reused across calls)
Note
Caller must use returned string immediately before next call
Only colorizes if output is going to TTY, otherwise returns message unchanged

Definition at line 536 of file colorize.c.

536 {
537 if (!message)
538 return message;
539 return colorize_log_message_with_color(message,
540 terminal_should_color_output(STDOUT_FILENO, options_terminal_settings()));
541}
const char * colorize_log_message_with_color(const char *message, bool enabled)
Definition colorize.c:543
#define options_terminal_settings()
Definition options.h:1296

References colorize_log_message_with_color(), and options_terminal_settings.

Referenced by log_msg(), log_plain_msg(), and log_recolor_plain_entry().

◆ colorize_log_message_with_color()

const char * colorize_log_message_with_color ( const char * message,
bool enabled )

Apply an already-selected color policy without querying the terminal.

Definition at line 543 of file colorize.c.

543 {
544 if (!message || !enabled) {
545 return message;
546 }
547
548 // Keep concurrent report formatting separate while allowing nested calls.
549 static _Thread_local char buffers[LOG_COLORIZE_SLOT_COUNT][LOG_MSG_BUFFER_SIZE];
550 static _Thread_local int buffer_index = 0;
551
552 char *output = buffers[buffer_index];
553 buffer_index = (buffer_index + 1) % 4;
554
555 size_t out_pos = 0;
556 const size_t max_size = sizeof(buffers[0]);
557
558 // Process the input string
559 for (size_t i = 0; message[i] != '\0' && out_pos < max_size - 100; i++) {
560 size_t end_pos = 0;
561
562 // Check if already colorized - only colorize if NOT already colored
563 bool can_colorize = !ansi_is_already_colorized(message, i);
564
565 // Try key=value pair first (highest priority pattern)
566 size_t key_end = 0, value_start = 0, value_end = 0;
567 if (can_colorize && is_key_value_pair(message, i, &key_end, &value_start, &value_end)) {
568 size_t key_len = key_end - i;
569 size_t value_len = value_end - value_start;
570
571 // Extract key
572 char key_buf[LOG_COLORIZE_KEY_BUFFER_SIZE];
573 safe_snprintf(key_buf, sizeof(key_buf), "%.*s", (int)key_len, message + i);
574
575 // Extract value
576 char value_buf[LOG_COLORIZE_VALUE_BUFFER_SIZE];
577 safe_snprintf(value_buf, sizeof(value_buf), "%.*s", (int)value_len, message + value_start);
578
579 // Check if we have enough space for key + equals + value
580 // including the bounded color prefixes and resets.
581 if (out_pos + key_len + 1 + value_len + 100 < max_size) {
582 append_colored_token(output, max_size, &out_pos, LOG_COLOR_FATAL, key_buf);
583
584 // Add uncolored equals sign
585 output[out_pos++] = '=';
586
587 // Determine value color and add colored value
588 log_color_t value_color = get_value_color(value_buf);
589 append_colored_token(output, max_size, &out_pos, value_color, value_buf);
590 }
591 i = value_end - 1;
592 continue;
593 }
594
595 // Try numeric pattern
596 if (can_colorize && is_numeric_pattern(message, i, &end_pos)) {
597 size_t pattern_len = end_pos - i;
598 char pattern_buf[LOG_COLORIZE_PATTERN_BUFFER_SIZE];
599 safe_snprintf(pattern_buf, sizeof(pattern_buf), "%.*s", (int)pattern_len, message + i);
600
601 append_colored_token(output, max_size, &out_pos, LOG_COLOR_GREY, pattern_buf);
602 i = end_pos - 1;
603 continue;
604 }
605
606 // Try file path
607 if (can_colorize && is_file_path(message, i, &end_pos)) {
608 size_t path_len = end_pos - i;
609 char path_buf[LOG_COLORIZE_PATH_BUFFER_SIZE];
610 safe_snprintf(path_buf, sizeof(path_buf), "%.*s", (int)path_len, message + i);
611
612 append_colored_token(output, max_size, &out_pos, LOG_COLOR_DEBUG, path_buf);
613 i = end_pos - 1;
614 continue;
615 }
616
617 // Try environment variable
618 if (can_colorize && is_env_var(message, i, &end_pos)) {
619 size_t var_len = end_pos - i;
620 char var_buf[LOG_COLORIZE_VAR_BUFFER_SIZE];
621 safe_snprintf(var_buf, sizeof(var_buf), "%.*s", (int)var_len, message + i);
622
623 append_colored_token(output, max_size, &out_pos, LOG_COLOR_GREY, var_buf);
624 i = end_pos - 1;
625 continue;
626 }
627
628 // Try URL
629 if (can_colorize && is_url(message, i, &end_pos)) {
630 size_t url_len = end_pos - i;
631 char url_buf[LOG_COLORIZE_URL_BUFFER_SIZE];
632 safe_snprintf(url_buf, sizeof(url_buf), "%.*s", (int)url_len, message + i);
633
634 append_colored_token(output, max_size, &out_pos, LOG_COLOR_INFO, url_buf);
635 i = end_pos - 1;
636 continue;
637 }
638
639 // Regular character - just copy
640 if (out_pos < max_size - 1) {
641 output[out_pos++] = message[i];
642 }
643 }
644
645 output[out_pos] = '\0';
646 return output;
647}
#define LOG_COLORIZE_VALUE_BUFFER_SIZE
Definition colorize.c:9
#define LOG_COLORIZE_URL_BUFFER_SIZE
Definition colorize.c:13
#define LOG_COLORIZE_PATH_BUFFER_SIZE
Definition colorize.c:11
#define LOG_COLORIZE_PATTERN_BUFFER_SIZE
Definition colorize.c:10
#define LOG_COLORIZE_KEY_BUFFER_SIZE
Definition colorize.c:8
#define LOG_COLORIZE_VAR_BUFFER_SIZE
Definition colorize.c:12
#define LOG_COLORIZE_SLOT_COUNT
Definition colorize.c:16
bool enabled
Is filtering active?
Definition grep.c:62
bool ansi_is_already_colorized(const char *message, size_t pos)
Check if a position in text is already colored.
Definition ansi.c:102
#define LOG_MSG_BUFFER_SIZE
Maximum size of a single log message (including formatting).
Definition log/log.h:92
log_color_t
Color enum for logging - indexes into color arrays.
Definition log/log.h:147
@ LOG_COLOR_GREY
Definition log/log.h:154
@ LOG_COLOR_FATAL
Definition log/log.h:153
@ LOG_COLOR_DEBUG
Definition log/log.h:149
@ LOG_COLOR_INFO
Definition log/log.h:150
int safe_snprintf(char *buffer, size_t buffer_size, const char *format,...)
Platform-safe snprintf wrapper.

References ansi_is_already_colorized(), enabled, LOG_COLOR_DEBUG, LOG_COLOR_FATAL, LOG_COLOR_GREY, LOG_COLOR_INFO, LOG_COLORIZE_KEY_BUFFER_SIZE, LOG_COLORIZE_PATH_BUFFER_SIZE, LOG_COLORIZE_PATTERN_BUFFER_SIZE, LOG_COLORIZE_SLOT_COUNT, LOG_COLORIZE_URL_BUFFER_SIZE, LOG_COLORIZE_VALUE_BUFFER_SIZE, LOG_COLORIZE_VAR_BUFFER_SIZE, LOG_MSG_BUFFER_SIZE, and safe_snprintf().

Referenced by colorize_log_message(), and log_template_apply().