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

Log message colorization for terminal output. More...

Go to the source code of this file.

Macros

#define LOG_COLORIZE_KEY_BUFFER_SIZE   256
#define LOG_COLORIZE_VALUE_BUFFER_SIZE   512
#define LOG_COLORIZE_PATTERN_BUFFER_SIZE   256
#define LOG_COLORIZE_PATH_BUFFER_SIZE   512
#define LOG_COLORIZE_VAR_BUFFER_SIZE   256
#define LOG_COLORIZE_URL_BUFFER_SIZE   2048
#define LOG_COLORIZE_SLOT_COUNT   4

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.

Definition in file colorize.c.

Macro Definition Documentation

◆ LOG_COLORIZE_KEY_BUFFER_SIZE

#define LOG_COLORIZE_KEY_BUFFER_SIZE   256

Definition at line 8 of file colorize.c.

Referenced by colorize_log_message_with_color().

◆ LOG_COLORIZE_PATH_BUFFER_SIZE

#define LOG_COLORIZE_PATH_BUFFER_SIZE   512

Definition at line 11 of file colorize.c.

Referenced by colorize_log_message_with_color().

◆ LOG_COLORIZE_PATTERN_BUFFER_SIZE

#define LOG_COLORIZE_PATTERN_BUFFER_SIZE   256

Definition at line 10 of file colorize.c.

Referenced by colorize_log_message_with_color().

◆ LOG_COLORIZE_SLOT_COUNT

#define LOG_COLORIZE_SLOT_COUNT   4

Definition at line 16 of file colorize.c.

Referenced by colorize_log_message_with_color().

◆ LOG_COLORIZE_URL_BUFFER_SIZE

#define LOG_COLORIZE_URL_BUFFER_SIZE   2048

Definition at line 13 of file colorize.c.

Referenced by colorize_log_message_with_color().

◆ LOG_COLORIZE_VALUE_BUFFER_SIZE

#define LOG_COLORIZE_VALUE_BUFFER_SIZE   512

Definition at line 9 of file colorize.c.

Referenced by colorize_log_message_with_color().

◆ LOG_COLORIZE_VAR_BUFFER_SIZE

#define LOG_COLORIZE_VAR_BUFFER_SIZE   256

Definition at line 12 of file colorize.c.

Referenced by colorize_log_message_with_color().

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

530 {
531 if (!message)
532 return message;
534}
const char * colorize_log_message_with_color(const char *message, bool enabled)
Definition colorize.c:536
#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 536 of file colorize.c.

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

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().