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

📝 Multi-level logging with terminal color support, file rotation, and async output More...

Go to the source code of this file.

Data Structures

struct  log_system_t

Macros

#define LOG_BUFFER_BUFFER_SIZE   8192
#define LOG_LOG_MSG_BUFFER_SIZE   256
#define LOG_PARSE_TIMESTAMP_BUFFER_SIZE   64
#define LOG_LEVEL_BUFFER_SIZE   16
#define LOG_FILE_PATH_BUFFER_SIZE   256
#define LOG_FUNC_NAME_BUFFER_SIZE   256
#define LOG_COLOR_COUNT    8 /* DEV, DEBUG, WARN, INFO, ERROR, FATAL, GREY, RESET */
#define LOGGING_INTERNAL_ERROR(error, message, ...)

Functions

int terminal_choose_log_fd (int level)
 __attribute__ ((weak))
const char * get_level_string_padded (log_level_t level)
 Get padded level string for consistent alignment.
size_t get_current_time_formatted (char *time_buf)
 Get current time as formatted string.
char * format_message (const char *format, va_list args)
 Format a message using va_list.
session_log_buffer_t * log_get_history_buffer (void)
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.
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_set_json_output (int fd)
 Set the JSON output file descriptor.
void log_disable_file_output (void)
 Disable file output and use stderr instead.
asciichat_error_t log_set_format (const char *format_str, bool console_only)
 Set a custom log format string.
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.
void log_truncate_if_large (void)
 Manually truncate large log files.
void ui_notice_log (ui_notice_severity_t severity, const char *file, int line, const char *func, const char *title, const char *format,...)
void ui_notice_announce (const char *file, int line, const char *func, const char *title, const char *format,...)
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_stderr_nonewline_msg (const char *fmt,...)
 Plain logging to stderr without trailing newline.
void log_plain_stdout_msg (const char *fmt,...)
 Plain logging to stdout with newline.
void log_file_msg (const char *fmt,...)
 Log to file only, no stderr output.
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).
void log_redetect_terminal_capabilities (void)
 Re-detect terminal capabilities after logging is initialized.
const char ** log_get_color_array (void)
 Get the appropriate color array based on terminal capabilities.
int log_get_color_mode (void)
const char * log_get_foreground_color (void)
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)
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.
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_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.
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.

Detailed Description

📝 Multi-level logging with terminal color support, file rotation, and async output

Definition in file log/log.c.

Macro Definition Documentation

◆ LOG_BUFFER_BUFFER_SIZE

#define LOG_BUFFER_BUFFER_SIZE   8192

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

◆ LOG_COLOR_COUNT

#define LOG_COLOR_COUNT    8 /* DEV, DEBUG, WARN, INFO, ERROR, FATAL, GREY, RESET */

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

237#define LOG_COLOR_COUNT \
238 8 /* DEV, DEBUG, WARN, INFO, ERROR, FATAL, GREY, RESET */

◆ LOG_FILE_PATH_BUFFER_SIZE

#define LOG_FILE_PATH_BUFFER_SIZE   256

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

Referenced by log_recolor_plain_entry().

◆ LOG_FUNC_NAME_BUFFER_SIZE

#define LOG_FUNC_NAME_BUFFER_SIZE   256

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

Referenced by log_recolor_plain_entry().

◆ LOG_LEVEL_BUFFER_SIZE

#define LOG_LEVEL_BUFFER_SIZE   16

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

Referenced by log_recolor_plain_entry().

◆ LOG_LOG_MSG_BUFFER_SIZE

#define LOG_LOG_MSG_BUFFER_SIZE   256

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

◆ LOG_PARSE_TIMESTAMP_BUFFER_SIZE

#define LOG_PARSE_TIMESTAMP_BUFFER_SIZE   64

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

Referenced by log_recolor_plain_entry().

◆ LOGGING_INTERNAL_ERROR

#define LOGGING_INTERNAL_ERROR ( error,
message,
... )
Value:
do { \
errno_set_with_message(error, __FILE__, __LINE__, __func__, \
message, ##__VA_ARGS__); \
static const char *msg_header = "CRITICAL LOGGING SYSTEM ERROR: "; \
log_terminal_printf(STDERR_FILENO, "%s %s\n", \
colored_string(LOG_COLOR_ERROR, msg_header), message); \
int _log_fd = atomic_load_int(&g_log.file); \
platform_write(_log_fd, msg_header, strlen(msg_header)); \
platform_write(_log_fd, message, strlen(message)); \
platform_write(_log_fd, "\n", 1); \
backtrace_print_simple(0); \
} while (0)
int atomic_load_int(atomic_t *a)
Atomically load an int value.
@ LOG_COLOR_ERROR
Definition color.h:11
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.

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

257#define LOGGING_INTERNAL_ERROR(error, message, ...) \
258 do { \
259 errno_set_with_message(error, __FILE__, __LINE__, __func__, \
260 message, ##__VA_ARGS__); \
261 static const char *msg_header = "CRITICAL LOGGING SYSTEM ERROR: "; \
262 log_terminal_printf(STDERR_FILENO, "%s %s\n", \
263 colored_string(LOG_COLOR_ERROR, msg_header), message); \
264 int _log_fd = atomic_load_int(&g_log.file); \
265 platform_write(_log_fd, msg_header, strlen(msg_header)); \
266 platform_write(_log_fd, message, strlen(message)); \
267 platform_write(_log_fd, "\n", 1); \
268 backtrace_print_simple(0); \
269 } while (0)

Referenced by format_message(), get_current_time_formatted(), and log_msg().

Function Documentation

◆ __attribute__()

__attribute__ ( (weak) )

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

70 {
71 (void)level;
72 (void)message;
73 // Default: no-op
74}

◆ get_level_string_padded()

const char * get_level_string_padded ( log_level_t level)

Get padded level string for consistent alignment.

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

◆ log_set_json_output()

void log_set_json_output ( int fd)

Set the JSON output file descriptor.

Sets which file descriptor should receive JSON-formatted logs. Pass -1 to disable JSON output.

Parameters
fdFile descriptor (typically STDERR_FILENO for terminal, or an open log file)
Note
This function is thread-safe (uses atomic operations)

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

846{ atomic_store_int(&g_log.json_file, fd); }
void atomic_store_int(atomic_t *a, int value)
Atomically store an int value.

References atomic_store_int().

◆ terminal_choose_log_fd()

int terminal_choose_log_fd ( int level)

Definition at line 6 of file lib/log/terminal.c.

6 {
8 return STDERR_FILENO;
9 return level >= LOG_WARN ? STDERR_FILENO : STDOUT_FILENO;
10}
bool log_get_force_stderr(void)
Get current force_stderr setting.
Definition log/log.c:844
bool terminal_is_interactive(void)

References log_get_force_stderr(), LOG_WARN, and terminal_is_interactive().

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

◆ ui_notice_announce()

void ui_notice_announce ( const char * file,
int line,
const char * func,
const char * title,
const char * format,
... )

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

1306 {
1307 va_list args;
1308 va_start(args, format);
1309 notice_logv(UI_NOTICE_INFO, true, file, line, func, title, format, args);
1310 va_end(args);
1311}
@ UI_NOTICE_INFO
Definition notice.h:14
action_args_t args

References args, and UI_NOTICE_INFO.

◆ ui_notice_log()

void ui_notice_log ( ui_notice_severity_t severity,
const char * file,
int line,
const char * func,
const char * title,
const char * format,
... )

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

1298 {
1299 va_list args;
1300 va_start(args, format);
1301 notice_logv(severity, false, file, line, func, title, format, args);
1302 va_end(args);
1303}

References args.

Referenced by ui_notice_confirm_kind().