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.h File Reference

📝 Logging API with multiple log levels and terminal output control More...

Go to the source code of this file.

Data Structures

struct  log_buffer_entry_t
 Heap-backed message waiting for the logging output thread. More...

Macros

#define ASCII_CHAT_LOG_LOG_H
#define LOG_ATOMIC_UINT64   atomic_t
#define LOG_ATOMIC_UINT64_INIT(val)
#define DEFAULT_LOG_LEVEL   LOG_DEBUG
 Default log level for debug builds (DEBUG and above).
#define MAX_LOG_SIZE   (3 * 1024 * 1024)
 Maximum log file size in bytes (3MB) before rotation.
#define MAX_TERMINAL_BUFFER_SIZE   (64 * 1024)
 Maximum size of terminal output buffer (64KB).
#define MAX_TERMINAL_BUFFER_ENTRIES   256
 Maximum number of buffered log entries.
#define LOG_MSG_BUFFER_SIZE   4096
 Maximum size of a single log message (including formatting).
#define LOG_MMAP_MSG_BUFFER_SIZE   1024
 Maximum size of a log message in mmap mode.
#define LOG_HEADER_BUFFER_SIZE   512
 Maximum size of a log header (timestamp, level, file:line:func).
#define LOG_TIMESTAMP_BUFFER_SIZE   32
 Maximum size of a timestamp string.
#define LOG_TERMINAL_BUFFER_SIZE   16384
#define LOG_TERMINAL_ENTRY_BUFFER_SIZE   (LOG_HEADER_BUFFER_SIZE + LOG_TERMINAL_BUFFER_SIZE)
#define log_colored_msg(level, colored_message, plain_message)
#define log_terminal_colored_msg(level, colored_message, plain_message)
#define log_plain(...)
 Plain logging - writes to both log file and stderr without timestamps or log levels.
#define log_plain_stderr(...)
 Plain logging to stderr with newline.
#define log_plain_stdout(...)
 Plain logging to stdout with newline.
#define log_plain_stderr_nonewline(...)
 Plain logging to stderr without newline - for interactive prompts.
#define log_file(...)
 File-only logging - writes to log file only, no stderr output.
#define log_every(log_level, interval_us, fmt, ...)
 Rate-limited logging macro (thread-safe).
#define log_nth(log_level, n, fmt, ...)
 Log every nth call to this code location (thread-safe).
#define log_once(log_level, fmt, ...)
 Log exactly once per call site (thread-safe).
#define log_console(level, message)
Logging Macros
Note
Compile-time log level stripping: In release builds, log_dev() and log_debug() are compiled out completely (no runtime overhead). Override with LOG_COMPILE_LEVEL.
#define log_as(level, ...)
 Log a message given the associated level.
#define log_only(bitmask, level, ...)
 Log a message given its level and if the bitmask allows so.
#define log_dev(...)
 Log a DEV message (most verbose, development only).
#define log_debug(...)
 Log a DEBUG message.
#define log_info(...)
 Log an INFO message.
#define log_warn(...)
 Log a WARN message.
#define log_error(...)
 Log an ERROR message.
#define log_fatal(...)
 Log a FATAL message.
Rate-Limited Logging Macros
#define log_dev_every(interval_us, fmt, ...)
 Rate-limited DEV logging.
#define log_debug_every(interval_us, fmt, ...)
 Rate-limited DEBUG logging.
#define log_info_every(interval_us, fmt, ...)
 Rate-limited INFO logging.
#define log_warn_every(interval_us, fmt, ...)
 Rate-limited WARN logging.
#define log_error_every(interval_us, fmt, ...)
 Rate-limited ERROR logging.
#define log_fatal_every(interval_us, fmt, ...)
 Rate-limited FATAL logging.
Nth-Call Logging Macros
#define log_dev_nth(n, fmt, ...)
 Log DEV message every nth call.
#define log_debug_nth(n, fmt, ...)
 Log DEBUG message every nth call.
#define log_info_nth(n, fmt, ...)
 Log INFO message every nth call.
#define log_warn_nth(n, fmt, ...)
 Log WARN message every nth call.
#define log_error_nth(n, fmt, ...)
 Log ERROR message every nth call.
#define log_fatal_nth(n, fmt, ...)
 Log FATAL message every nth call.
Once-Only Logging Macros
#define log_dev_once(fmt, ...)
 Log DEV message exactly once.
#define log_debug_once(fmt, ...)
 Log DEBUG message exactly once.
#define log_info_once(fmt, ...)
 Log INFO message exactly once.
#define log_warn_once(fmt, ...)
 Log WARN message exactly once.
#define log_error_once(fmt, ...)
 Log ERROR message exactly once.
#define log_fatal_once(fmt, ...)
 Log FATAL message exactly once.

Typedefs

typedef struct color_scheme_t color_scheme_t
typedef struct log_mmap_config log_mmap_config_t

Functions

void log_init (const char *filename, log_level_t level, bool force_stderr, bool use_mmap)
 Initialize the logging system.
void log_destroy (void)
 Destroy the logging system and close log file.
void log_system_init (void)
 Initialize the logging system internal state (system-level init).
void log_system_destroy (void)
 Shutdown the logging system internal state (system-level cleanup).
void log_set_level (log_level_t level)
 Set the minimum log level.
log_level_t log_get_level (void)
 Get the current minimum log level.
asciichat_error_t log_set_format (const char *format_str, bool console_only)
 Set a custom log format string.
void log_set_terminal_output (bool enabled)
 Control stderr output to terminal.
bool log_get_terminal_output (void)
 Get current terminal output setting.
void log_set_force_stderr (bool enabled)
 Force all terminal log output to stderr.
bool log_get_force_stderr (void)
 Get current force_stderr setting.
void log_disable_file_output (void)
 Disable file output and use stderr instead.
void log_truncate_if_large (void)
 Manually truncate large log files.
void log_msg (log_level_t level, const char *file, int line, const char *func, const char *fmt,...)
 Log a message at a specific level.
void log_terminal_msg (log_level_t level, const char *file, int line, const char *func, const char *fmt,...)
 Log a message to terminal only (no file output).
void log_colored_msg_impl (log_level_t level, const char *file, int line, const char *func, const char *colored_message, const char *plain_message)
 Write semantically colored terminal text and an ANSI-free log equivalent.
void log_terminal_colored_msg_impl (log_level_t level, const char *file, int line, const char *func, const char *colored_message, const char *plain_message)
 Write semantically colored terminal text without persistent logging.
void log_plain_msg (const char *fmt,...)
 Plain logging without timestamps or levels.
void log_plain_stderr_msg (const char *fmt,...)
 Plain logging to stderr with newline.
void log_plain_stdout_msg (const char *fmt,...)
 Plain logging to stdout with newline.
void log_plain_stderr_nonewline_msg (const char *fmt,...)
 Plain logging to stderr without trailing newline.
void log_file_msg (const char *fmt,...)
 Log to file only, no stderr output.
void log_labeled (const char *label, log_color_t color, const char *message,...)
 Print a labeled message with color.
const char * log_level_color (log_color_t color)
 Get color string for a given color enum.
const char * log_level_color_for_output (log_color_t color, bool enabled)
const char ** log_get_color_array (void)
 Get the appropriate color array based on terminal capabilities.
const char * log_get_foreground_color (void)
int log_get_color_mode (void)
void log_redetect_terminal_capabilities (void)
 Re-detect terminal capabilities after logging is initialized.
void log_init_colors (void)
 Initialize logging color system with current terminal capabilities.
void log_set_color_scheme (const color_scheme_t *scheme)
 Set the color scheme for logging output.
bool log_lock_terminal (void)
 Lock terminal output for exclusive access by the calling thread.
void log_unlock_terminal (bool previous_state)
 Release terminal lock and flush buffered messages.
void log_set_flush_delay (unsigned int delay_ms)
 Set the delay between flushing buffered log entries.
char * format_message (const char *format, va_list args)
 Format a message using va_list.
size_t get_current_time_formatted (char *time_buf)
 Get current time as formatted string.
asciichat_error_t log_net_message (struct acip_transport *transport, log_level_t level, remote_log_direction_t direction, const char *file, int line, const char *func, const char *fmt,...)
 Log a message to all destinations (network, file, and terminal).
asciichat_error_t log_enable_mmap (const char *log_path)
 Enable lock-free mmap-based logging.
asciichat_error_t log_enable_mmap_sized (const char *log_path, size_t max_size)
 Enable lock-free mmap logging with custom file size.
void log_disable_mmap (void)
 Disable mmap logging and return to mutex-based logging.
void log_shutdown_begin (void)
 Begin shutdown phase - disable console logging but keep file logging.
void log_shutdown_end (void)
 End shutdown phase - restore previous logging settings.
void log_cleanup_colors (void)
 Clean up compiled color scheme.
void log_set_session_log_buffer (session_log_buffer_t *buf)
 Register a session log buffer with the logger.
void log_clear_session_log_buffer (void)
 Unregister the session log buffer.
session_log_buffer_t * log_get_history_buffer (void)
session_log_buffer_t * log_get_session_log_buffer (void)
 Get the currently registered session log buffer.
size_t log_recolor_plain_entry (const char *plain_line, char *colored_buf, size_t buf_size)
 Recolor a plain (non-colored) log line with proper ANSI codes.
const char * get_level_string_padded (log_level_t level)
 Get padded log level string (internal utility for formatting).
void log_console_impl (log_level_t level, const char *file, int line, const char *func, const char *message)
void * log_get_template (void)
 Get the current log format template (opaque pointer).

Detailed Description

📝 Logging API with multiple log levels and terminal output control

Definition in file log/log.h.

Macro Definition Documentation

◆ log_console

#define log_console ( level,
message )
Value:
log_console_impl((level), __FILE__, __LINE__, __func__, (message))
void log_console_impl(log_level_t level, const char *file, int line, const char *func, const char *message)
Definition log/log.c:2562

Definition at line 1022 of file log/log.h.

◆ log_debug_every

◆ log_debug_nth

#define log_debug_nth ( n,
fmt,
... )
Value:
log_nth(DEBUG, n, fmt, ##__VA_ARGS__)
#define log_nth(log_level, n, fmt,...)
Log every nth call to this code location (thread-safe).
Definition log/log.h:765

Log DEBUG message every nth call.

Definition at line 825 of file log/log.h.

◆ log_debug_once

#define log_debug_once ( fmt,
... )
Value:
log_once(DEBUG, fmt, ##__VA_ARGS__)
#define log_once(log_level, fmt,...)
Log exactly once per call site (thread-safe).
Definition log/log.h:805

Log DEBUG message exactly once.

Definition at line 850 of file log/log.h.

◆ log_dev_every

◆ log_dev_nth

#define log_dev_nth ( n,
fmt,
... )
Value:
log_nth(DEV, n, fmt, ##__VA_ARGS__)

Log DEV message every nth call.

Definition at line 822 of file log/log.h.

◆ log_dev_once

#define log_dev_once ( fmt,
... )
Value:
log_once(DEV, fmt, ##__VA_ARGS__)

Log DEV message exactly once.

Definition at line 847 of file log/log.h.

◆ log_error_every

#define log_error_every ( interval_us,
fmt,
... )
Value:
log_every(ERROR, interval_us, fmt, ##__VA_ARGS__)

Rate-limited ERROR logging.

Definition at line 728 of file log/log.h.

Referenced by client_audio_pipeline_capture(), client_audio_pipeline_playback(), client_audio_render_thread(), crypto_decrypt(), mutex_stack_detect_deadlocks(), and session_pipeline_run_main().

◆ log_error_nth

#define log_error_nth ( n,
fmt,
... )
Value:
log_nth(ERROR, n, fmt, ##__VA_ARGS__)

Log ERROR message every nth call.

Definition at line 834 of file log/log.h.

◆ log_error_once

#define log_error_once ( fmt,
... )
Value:
log_once(ERROR, fmt, ##__VA_ARGS__)

Log ERROR message exactly once.

Definition at line 859 of file log/log.h.

◆ log_fatal_every

#define log_fatal_every ( interval_us,
fmt,
... )
Value:
log_every(FATAL, interval_us, fmt, ##__VA_ARGS__)
#define FATAL(code,...)
Exit with error code and custom message, with stack trace in debug builds.

Rate-limited FATAL logging.

Definition at line 731 of file log/log.h.

◆ log_fatal_nth

#define log_fatal_nth ( n,
fmt,
... )
Value:
log_nth(FATAL, n, fmt, ##__VA_ARGS__)

Log FATAL message every nth call.

Definition at line 837 of file log/log.h.

◆ log_fatal_once

#define log_fatal_once ( fmt,
... )
Value:
log_once(FATAL, fmt, ##__VA_ARGS__)

Log FATAL message exactly once.

Definition at line 862 of file log/log.h.

◆ log_info_every

#define log_info_every ( interval_us,
fmt,
... )
Value:
log_every(INFO, interval_us, fmt, ##__VA_ARGS__)

Rate-limited INFO logging.

Definition at line 722 of file log/log.h.

◆ log_info_nth

#define log_info_nth ( n,
fmt,
... )
Value:
log_nth(INFO, n, fmt, ##__VA_ARGS__)

Log INFO message every nth call.

Definition at line 828 of file log/log.h.

◆ log_info_once

#define log_info_once ( fmt,
... )
Value:
log_once(INFO, fmt, ##__VA_ARGS__)

Log INFO message exactly once.

Definition at line 853 of file log/log.h.

◆ log_warn_every

◆ log_warn_nth

#define log_warn_nth ( n,
fmt,
... )
Value:
log_nth(WARN, n, fmt, ##__VA_ARGS__)

Log WARN message every nth call.

Definition at line 831 of file log/log.h.

◆ log_warn_once

#define log_warn_once ( fmt,
... )
Value:
log_once(WARN, fmt, ##__VA_ARGS__)

Log WARN message exactly once.

Definition at line 856 of file log/log.h.

Referenced by audio_processor_create().

Typedef Documentation

◆ log_mmap_config_t

Definition at line 878 of file log/log.h.

Function Documentation

◆ get_level_string_padded()

const char * get_level_string_padded ( log_level_t level)

Get padded log level string (internal utility for formatting).

Parameters
levelLog level
Returns
Padded level string (5 chars: "DEBUG", "INFO ", "WARN ", "DEV ", "ERROR", "FATAL")
Note
Internal function - do not use directly in application code

Get padded log level string (internal utility for formatting).

Returns level names padded to 5 characters for visual alignment. Used consistently in both colored and plain text formatters to ensure grep pattern matching works correctly.

Parameters
levelLog level
Returns
Padded level string (e.g., "INFO ", "WARN ", "DEBUG")

Definition at line 203 of file log/log.c.

203 {
204 const char *result;
205 switch (level) {
206 case LOG_INFO:
207 result = "INFO "; // 5 chars: INFO + 1 space
208 break;
209 case LOG_WARN:
210 result = "WARN "; // 5 chars: WARN + 1 space
211 break;
212 case LOG_DEV:
213 result = "DEV "; // 5 chars: DEV + 2 spaces
214 break;
215 case LOG_DEBUG:
216 result = "DEBUG"; // 5 chars: DEBUG (no padding needed)
217 break;
218 case LOG_ERROR:
219 result = "ERROR"; // 5 chars: ERROR (no padding needed)
220 break;
221 case LOG_FATAL:
222 result = "FATAL"; // 5 chars: FATAL (no padding needed)
223 break;
224 default:
225 result = "?????"; // Invalid level - return 5 question marks
226 }
227 // Verify length
228 if (strlen(result) != 5) {
229 log_terminal_printf(STDERR_FILENO,
230 "ERROR: get_level_string_padded() returned non-5-char "
231 "string: '%s' (len=%zu)\n",
232 result, strlen(result));
233 }
234 return result;
235}
#define LOG_DEV
Definition log/types.h:42
#define LOG_DEBUG
Definition log/types.h:43
#define LOG_FATAL
Definition log/types.h:47
#define LOG_ERROR
Definition log/types.h:46
#define LOG_WARN
Definition log/types.h:45
#define LOG_INFO
Definition log/types.h:44

References LOG_DEBUG, LOG_DEV, LOG_ERROR, LOG_FATAL, LOG_INFO, and LOG_WARN.

Referenced by log_template_apply().

◆ log_console_impl()

void log_console_impl ( log_level_t level,
const char * file,
int line,
const char * func,
const char * message )

Definition at line 2562 of file log/log.c.

2563 {
2564 if (!message) {
2565 return;
2566 }
2567
2568 int fd = terminal_choose_log_fd(level);
2569 if (fd < 0) {
2570 return;
2571 }
2572
2573 // Presentation owns terminal output, but never changes the caller's logging
2574 // preference.
2577 return;
2578 }
2579
2580 // Check if JSON output is enabled (json_file >= 0 means enabled)
2581 int json_fd = atomic_load_int(&g_log.json_file);
2582 bool use_json = (json_fd >= 0);
2583
2584 if (use_json) {
2585 // Use async-safe JSON formatter (safe for signal handlers)
2586 log_json_async_safe(fd, level, file, line, func, message);
2587 } else {
2588 // Text output to console using platform_write_all to handle partial writes
2589 size_t msg_len = strlen(message);
2590 ui_controller_write(fd, message, msg_len);
2591 if (msg_len == 0 || message[msg_len - 1] != '\n') {
2592 ui_controller_write(fd, "\n", 1);
2593 }
2594 }
2595}
int atomic_load_int(atomic_t *a)
Atomically load an int value.
void log_json_async_safe(int fd, log_level_t level, const char *file, int line, const char *func, const char *message)
Async-safe JSON logging for signal handlers.
Definition log/json.c:282
int terminal_choose_log_fd(int level)
bool ui_controller_is_presenting(void)
bool ui_controller_is_owner(void)
asciichat_error_t ui_controller_write(int fd, const char *data, size_t len)
int platform_isatty(int fd)
Definition util.c:70

References atomic_load_int(), log_json_async_safe(), platform_isatty(), terminal_choose_log_fd(), ui_controller_is_owner(), ui_controller_is_presenting(), and ui_controller_write().

◆ log_get_history_buffer()

session_log_buffer_t * log_get_history_buffer ( void )

Borrowed history retained from log_init through log_system_destroy, independent of screens.

Definition at line 625 of file log/log.c.

625{ return g_history; }

Referenced by log_search_gather_and_filter_logs(), terminal_screen_log_init(), and ui_session_create().