ascii-chat 0.11.33
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_COLOR_COUNT   8 /* DEV, DEBUG, WARN, INFO, ERROR, FATAL, GREY, RESET */
 
#define LOGGING_INTERNAL_ERROR(error, message, ...)
 

Functions

 __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.
 
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 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_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_network_message (socket_t sockfd, const struct crypto_context_t *crypto_ctx, log_level_t level, remote_log_direction_t direction, const char *fmt,...)
 Send a formatted log message over the network.
 
asciichat_error_t log_net_message (socket_t sockfd, const struct crypto_context_t *crypto_ctx, 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.
 
const char * log_level_color (log_color_t color)
 Get color string for a given color enum.
 
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_COLOR_COUNT

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

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

◆ LOGGING_INTERNAL_ERROR

#define LOGGING_INTERNAL_ERROR (   error,
  message,
  ... 
)
Value:
do { \
asciichat_set_errno_with_message(error, __FILE__, __LINE__, __func__, message, ##__VA_ARGS__); \
static const char *msg_header = "CRITICAL LOGGING SYSTEM ERROR: "; \
safe_fprintf(stderr, "%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.
Definition atomic.c:194
@ LOG_COLOR_ERROR
Definition log/log.h:135
const char * colored_string(log_color_t color, const char *text)
Build a colored string for terminal output.

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

202 { \
203 asciichat_set_errno_with_message(error, __FILE__, __LINE__, __func__, message, ##__VA_ARGS__); \
204 static const char *msg_header = "CRITICAL LOGGING SYSTEM ERROR: "; \
205 safe_fprintf(stderr, "%s %s\n", colored_string(LOG_COLOR_ERROR, msg_header), message); \
206 int _log_fd = atomic_load_int(&g_log.file); \
207 platform_write(_log_fd, msg_header, strlen(msg_header)); \
208 platform_write(_log_fd, message, strlen(message)); \
209 platform_write(_log_fd, "\n", 1); \
210 backtrace_print_simple(0); \
211 } while (0)

Function Documentation

◆ __attribute__()

__attribute__ ( (weak)  )

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

42 {
43 (void)level;
44 (void)message;
45 // Default: no-op
46}

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

149 {
150 const char *result;
151 switch (level) {
152 case LOG_INFO:
153 result = "INFO "; // 5 chars: INFO + 1 space
154 break;
155 case LOG_WARN:
156 result = "WARN "; // 5 chars: WARN + 1 space
157 break;
158 case LOG_DEV:
159 result = "DEV "; // 5 chars: DEV + 2 spaces
160 break;
161 case LOG_DEBUG:
162 result = "DEBUG"; // 5 chars: DEBUG (no padding needed)
163 break;
164 case LOG_ERROR:
165 result = "ERROR"; // 5 chars: ERROR (no padding needed)
166 break;
167 case LOG_FATAL:
168 result = "FATAL"; // 5 chars: FATAL (no padding needed)
169 break;
170 default:
171 result = "?????"; // Invalid level - return 5 question marks
172 }
173 // Verify length
174 if (strlen(result) != 5) {
175 fprintf(stderr, "ERROR: get_level_string_padded() returned non-5-char string: '%s' (len=%zu)\n", result,
176 strlen(result));
177 }
178 return result;
179}
#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 2120 of file log/log.c.

2120 {
2121 if (!message) {
2122 return;
2123 }
2124
2125 int fd = terminal_choose_log_fd(level);
2126 if (fd < 0) {
2127 return;
2128 }
2129
2130 // Check if JSON output is enabled (json_file >= 0 means enabled)
2131 int json_fd = atomic_load_int(&g_log.json_file);
2132 bool use_json = (json_fd >= 0);
2133
2134 if (use_json) {
2135 // Use async-safe JSON formatter (safe for signal handlers)
2136 log_json_async_safe(fd, level, file, line, func, message);
2137 } else {
2138 // Text output to console using platform_write_all to handle partial writes
2139 size_t msg_len = strlen(message);
2140 platform_write_all(fd, (const uint8_t *)message, msg_len);
2141 if (msg_len == 0 || message[msg_len - 1] != '\n') {
2142 platform_write_all(fd, (const uint8_t *)"\n", 1);
2143 }
2144 }
2145}
unsigned char uint8_t
Definition common.h:56
size_t platform_write_all(int fd, const void *buf, size_t count)
Write all bytes to a file descriptor, handling partial writes.
Definition system.c:224
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 json.c:250
int terminal_choose_log_fd(log_level_t level)
Choose output file descriptor for logging based on level and interactivity.

References atomic_load_int(), log_json_async_safe(), platform_write_all(), and terminal_choose_log_fd().

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

729 {
730 atomic_store_int(&g_log.json_file, fd);
731}
void atomic_store_int(atomic_t *a, int value)
Atomically store an int value.
Definition atomic.c:202

References atomic_store_int().

Referenced by main().