ascii-chat
Binary v0.15.83-53-gb711d8720
Library lib/v0.3.2-701-gb711d8720
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.c.

Macro Definition Documentation

◆ LOG_BUFFER_BUFFER_SIZE

#define LOG_BUFFER_BUFFER_SIZE   8192

Definition at line 3 of file log.c.

◆ LOG_COLOR_COUNT

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

Definition at line 215 of file log.c.

◆ LOG_FILE_PATH_BUFFER_SIZE

#define LOG_FILE_PATH_BUFFER_SIZE   256

Definition at line 7 of file 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.c.

Referenced by log_recolor_plain_entry().

◆ LOG_LEVEL_BUFFER_SIZE

#define LOG_LEVEL_BUFFER_SIZE   16

Definition at line 6 of file 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.c.

◆ LOG_PARSE_TIMESTAMP_BUFFER_SIZE

#define LOG_PARSE_TIMESTAMP_BUFFER_SIZE   64

Definition at line 5 of file 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)
@ LOG_COLOR_ERROR
Definition log/log.h:152
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.
Definition string.c:284

Definition at line 233 of file log.c.

233#define LOGGING_INTERNAL_ERROR(error, message, ...) \
234 do { \
235 errno_set_with_message(error, __FILE__, __LINE__, __func__, message, ##__VA_ARGS__); \
236 static const char *msg_header = "CRITICAL LOGGING SYSTEM ERROR: "; \
237 log_terminal_printf(STDERR_FILENO, "%s %s\n", colored_string(LOG_COLOR_ERROR, msg_header), message); \
238 int _log_fd = atomic_load_int(&g_log.file); \
239 platform_write(_log_fd, msg_header, strlen(msg_header)); \
240 platform_write(_log_fd, message, strlen(message)); \
241 platform_write(_log_fd, "\n", 1); \
242 backtrace_print_simple(0); \
243 } while (0)

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

Function Documentation

◆ __attribute__()

__attribute__ ( (weak) )

Definition at line 65 of file log.c.

65 {
66 (void)level;
67 (void)message;
68 // Default: no-op
69}

◆ 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 183 of file log.c.

183 {
184 const char *result;
185 switch (level) {
186 case LOG_INFO:
187 result = "INFO "; // 5 chars: INFO + 1 space
188 break;
189 case LOG_WARN:
190 result = "WARN "; // 5 chars: WARN + 1 space
191 break;
192 case LOG_DEV:
193 result = "DEV "; // 5 chars: DEV + 2 spaces
194 break;
195 case LOG_DEBUG:
196 result = "DEBUG"; // 5 chars: DEBUG (no padding needed)
197 break;
198 case LOG_ERROR:
199 result = "ERROR"; // 5 chars: ERROR (no padding needed)
200 break;
201 case LOG_FATAL:
202 result = "FATAL"; // 5 chars: FATAL (no padding needed)
203 break;
204 default:
205 result = "?????"; // Invalid level - return 5 question marks
206 }
207 // Verify length
208 if (strlen(result) != 5) {
209 log_terminal_printf(STDERR_FILENO, "ERROR: get_level_string_padded() returned non-5-char string: '%s' (len=%zu)\n",
210 result, strlen(result));
211 }
212 return result;
213}
#define LOG_DEV
Definition types.h:38
#define LOG_DEBUG
Definition types.h:39
#define LOG_FATAL
Definition types.h:43
#define LOG_ERROR
Definition types.h:42
#define LOG_WARN
Definition types.h:41
#define LOG_INFO
Definition types.h:40

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 2334 of file log.c.

2334 {
2335 if (!message) {
2336 return;
2337 }
2338
2339 int fd = terminal_choose_log_fd(level);
2340 if (fd < 0) {
2341 return;
2342 }
2343
2344 // Presentation owns terminal output, but never changes the caller's logging preference.
2345 if (ui_controller_is_presenting() && (ui_controller_is_owner() || platform_isatty(fd))) {
2346 return;
2347 }
2348
2349 // Check if JSON output is enabled (json_file >= 0 means enabled)
2350 int json_fd = atomic_load_int(&g_log.json_file);
2351 bool use_json = (json_fd >= 0);
2352
2353 if (use_json) {
2354 // Use async-safe JSON formatter (safe for signal handlers)
2355 log_json_async_safe(fd, level, file, line, func, message);
2356 } else {
2357 // Text output to console using platform_write_all to handle partial writes
2358 size_t msg_len = strlen(message);
2359 ui_controller_write(fd, message, msg_len);
2360 if (msg_len == 0 || message[msg_len - 1] != '\n') {
2361 ui_controller_write(fd, "\n", 1);
2362 }
2363 }
2364}
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:273
int terminal_choose_log_fd(int level)
Definition log/terminal.c:7
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)

References log_json_async_safe(), 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 565 of file log.c.

565 {
566 return g_history;
567}

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 775 of file log.c.

775 {
776 atomic_store_int(&g_log.json_file, fd);
777}

Referenced by main().

◆ terminal_choose_log_fd()

int terminal_choose_log_fd ( int level)

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

7 {
8 if (log_get_force_stderr() || !terminal_is_interactive())
9 return STDERR_FILENO;
10 return level >= LOG_WARN ? STDERR_FILENO : STDOUT_FILENO;
11}
bool log_get_force_stderr(void)
Get current force_stderr setting.
Definition log.c:771

References log_get_force_stderr(), and LOG_WARN.

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 1189 of file log.c.

1189 {
1190 va_list args;
1191 va_start(args, format);
1192 notice_logv(UI_NOTICE_INFO, true, file, line, func, title, format, args);
1193 va_end(args);
1194}
action_args_t args
Definition actions.c:78
@ UI_NOTICE_INFO
Definition notice.h:10

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 1181 of file log.c.

1182 {
1183 va_list args;
1184 va_start(args, format);
1185 notice_logv(severity, false, file, line, func, title, format, args);
1186 va_end(args);
1187}

References args.

Referenced by ui_notice_confirm_kind().