ascii-chat
Binary v0.16.114-28-gf5913657f
Library lib/v0.3.2-3302-gf5913657f
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 560 of file colorize.c.

560 {
561 if (!message)
562 return message;
564 message,
566}
const char * colorize_log_message_with_color(const char *message, bool enabled)
Definition colorize.c:568
#define options_terminal_settings()
Definition options.h:1287
bool terminal_should_color_output(int fd, const terminal_options_t *options)

References colorize_log_message_with_color(), options_terminal_settings, and terminal_should_color_output().

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 568 of file colorize.c.

568 {
569 if (!message || !enabled) {
570 return message;
571 }
572
573 // Keep concurrent report formatting separate while allowing nested calls.
574 static _Thread_local char buffers[LOG_COLORIZE_SLOT_COUNT]
576 static _Thread_local int buffer_index = 0;
577
578 char *output = buffers[buffer_index];
579 buffer_index = (buffer_index + 1) % 4;
580
581 size_t out_pos = 0;
582 const size_t max_size = sizeof(buffers[0]);
583
584 // Process the input string
585 for (size_t i = 0; message[i] != '\0' && out_pos < max_size - 100; i++) {
586 size_t end_pos = 0;
587
588 // Check if already colorized - only colorize if NOT already colored
589 bool can_colorize = !ansi_is_already_colorized(message, i);
590
591 // Try key=value pair first (highest priority pattern)
592 size_t key_end = 0, value_start = 0, value_end = 0;
593 if (can_colorize &&
594 is_key_value_pair(message, i, &key_end, &value_start, &value_end)) {
595 size_t key_len = key_end - i;
596 size_t value_len = value_end - value_start;
597
598 // Extract key
599 char key_buf[LOG_COLORIZE_KEY_BUFFER_SIZE];
600 safe_snprintf(key_buf, sizeof(key_buf), "%.*s", (int)key_len,
601 message + i);
602
603 // Extract value
604 char value_buf[LOG_COLORIZE_VALUE_BUFFER_SIZE];
605 safe_snprintf(value_buf, sizeof(value_buf), "%.*s", (int)value_len,
606 message + value_start);
607
608 // Check if we have enough space for key + equals + value
609 // including the bounded color prefixes and resets.
610 if (out_pos + key_len + 1 + value_len + 100 < max_size) {
611 append_colored_token(output, max_size, &out_pos, LOG_COLOR_FATAL,
612 key_buf);
613
614 // Add uncolored equals sign
615 output[out_pos++] = '=';
616
617 // Determine value color and add colored value
618 log_color_t value_color = get_value_color(value_buf);
619 append_colored_token(output, max_size, &out_pos, value_color,
620 value_buf);
621 }
622 i = value_end - 1;
623 continue;
624 }
625
626 // Try numeric pattern
627 if (can_colorize && is_numeric_pattern(message, i, &end_pos)) {
628 size_t pattern_len = end_pos - i;
629 char pattern_buf[LOG_COLORIZE_PATTERN_BUFFER_SIZE];
630 safe_snprintf(pattern_buf, sizeof(pattern_buf), "%.*s", (int)pattern_len,
631 message + i);
632
633 append_colored_token(output, max_size, &out_pos, LOG_COLOR_GREY,
634 pattern_buf);
635 i = end_pos - 1;
636 continue;
637 }
638
639 // Try file path
640 if (can_colorize && is_file_path(message, i, &end_pos)) {
641 size_t path_len = end_pos - i;
642 char path_buf[LOG_COLORIZE_PATH_BUFFER_SIZE];
643 safe_snprintf(path_buf, sizeof(path_buf), "%.*s", (int)path_len,
644 message + i);
645
646 append_colored_token(output, max_size, &out_pos, LOG_COLOR_DEBUG,
647 path_buf);
648 i = end_pos - 1;
649 continue;
650 }
651
652 // Try environment variable
653 if (can_colorize && is_env_var(message, i, &end_pos)) {
654 size_t var_len = end_pos - i;
655 char var_buf[LOG_COLORIZE_VAR_BUFFER_SIZE];
656 safe_snprintf(var_buf, sizeof(var_buf), "%.*s", (int)var_len,
657 message + i);
658
659 append_colored_token(output, max_size, &out_pos, LOG_COLOR_GREY, var_buf);
660 i = end_pos - 1;
661 continue;
662 }
663
664 // Try URL
665 if (can_colorize && is_url(message, i, &end_pos)) {
666 size_t url_len = end_pos - i;
667 char url_buf[LOG_COLORIZE_URL_BUFFER_SIZE];
668 safe_snprintf(url_buf, sizeof(url_buf), "%.*s", (int)url_len,
669 message + i);
670
671 append_colored_token(output, max_size, &out_pos, LOG_COLOR_INFO, url_buf);
672 i = end_pos - 1;
673 continue;
674 }
675
676 // Regular character - just copy
677 if (out_pos < max_size - 1) {
678 output[out_pos++] = message[i];
679 }
680 }
681
682 output[out_pos] = '\0';
683 return output;
684}
log_color_t
Definition color.h:6
@ LOG_COLOR_GREY
Definition color.h:13
@ LOG_COLOR_FATAL
Definition color.h:12
@ LOG_COLOR_DEBUG
Definition color.h:8
@ LOG_COLOR_INFO
Definition color.h:9
#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:63
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:86

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, and LOG_MSG_BUFFER_SIZE.

Referenced by colorize_log_message(), and log_template_apply().