|
ascii-chat 0.6.0
Real-time terminal-based video chat with ASCII art conversion
|
📝 Logging API with multiple log levels and terminal output control More...
Files | |
| file | logging.c |
| 📝 Multi-level logging with terminal color support, file rotation, and async output | |
| file | logging.h |
| 📝 Logging API with multiple log levels and terminal output control | |
| file | mmap.c |
| Lock-free memory-mapped text logging implementation. | |
| file | mmap.h |
| Lock-free memory-mapped text logging with crash safety. | |
| file | logging.h |
| Network logging macros and remote log direction enumeration. | |
Data Structures | |
| struct | log_buffer_entry_t |
| A single buffered log entry. More... | |
Macros | |
| #define | LOG_ATOMIC_UINT64 _Atomic uint64_t |
| #define | LOG_ATOMIC_UINT64_INIT(val) 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_COMPILE_LEVEL LOG_DEV |
| Compile-time minimum log level (DEV keeps all, allowing runtime -vvv) | |
| #define | log_every(log_level, interval_us, fmt, ...) |
| Rate-limited logging macro (thread-safe) | |
| #define | LOG_CLIENT_IMPL(client, level, fmt, ...) |
| #define | log_debug_client(client, fmt, ...) LOG_CLIENT_IMPL(client, LOG_DEBUG, fmt, ##__VA_ARGS__) |
| Server sends DEBUG log message to client. | |
| #define | log_info_client(client, fmt, ...) LOG_CLIENT_IMPL(client, LOG_INFO, fmt, ##__VA_ARGS__) |
| Server sends INFO log message to client. | |
| #define | log_warn_client(client, fmt, ...) LOG_CLIENT_IMPL(client, LOG_WARN, fmt, ##__VA_ARGS__) |
| Server sends WARN log message to client. | |
| #define | log_error_client(client, fmt, ...) LOG_CLIENT_IMPL(client, LOG_ERROR, fmt, ##__VA_ARGS__) |
| Server sends ERROR log message to client. | |
| #define | log_fatal_client(client, fmt, ...) LOG_CLIENT_IMPL(client, LOG_FATAL, fmt, ##__VA_ARGS__) |
| Server sends FATAL log message to client. | |
| #define | LOG_SERVER_IMPL(sockfd, crypto_ctx, level, fmt, ...) |
| #define | log_debug_server(sockfd, crypto_ctx, fmt, ...) LOG_SERVER_IMPL(sockfd, crypto_ctx, LOG_DEBUG, fmt, ##__VA_ARGS__) |
| Client sends DEBUG log message to server. | |
| #define | log_info_server(sockfd, crypto_ctx, fmt, ...) LOG_SERVER_IMPL(sockfd, crypto_ctx, LOG_INFO, fmt, ##__VA_ARGS__) |
| Client sends INFO log message to server. | |
| #define | log_warn_server(sockfd, crypto_ctx, fmt, ...) LOG_SERVER_IMPL(sockfd, crypto_ctx, LOG_WARN, fmt, ##__VA_ARGS__) |
| Client sends WARN log message to server. | |
| #define | log_error_server(sockfd, crypto_ctx, fmt, ...) LOG_SERVER_IMPL(sockfd, crypto_ctx, LOG_ERROR, fmt, ##__VA_ARGS__) |
| Client sends ERROR log message to server. | |
| #define | log_fatal_server(sockfd, crypto_ctx, fmt, ...) LOG_SERVER_IMPL(sockfd, crypto_ctx, LOG_FATAL, fmt, ##__VA_ARGS__) |
| Client sends FATAL log message to server. | |
Typedefs | |
| typedef struct log_mmap_config | log_mmap_config_t |
| typedef enum remote_log_direction | remote_log_direction_t |
| Remote log packet direction enumeration. | |
Enumerations | |
| enum | log_level_t { LOG_DEV = 0 , LOG_DEBUG , LOG_INFO , LOG_WARN , LOG_ERROR , LOG_FATAL } |
| Logging levels enumeration. More... | |
| enum | log_color_t { LOG_COLOR_DEV = 0 , LOG_COLOR_DEBUG = 1 , LOG_COLOR_INFO = 2 , LOG_COLOR_WARN = 3 , LOG_COLOR_ERROR = 4 , LOG_COLOR_FATAL = 5 , LOG_COLOR_RESET = 6 } |
| Color enum for logging - indexes into color arrays. More... | |
| enum | remote_log_direction { REMOTE_LOG_DIRECTION_UNKNOWN = 0 , REMOTE_LOG_DIRECTION_SERVER_TO_CLIENT = 1 , REMOTE_LOG_DIRECTION_CLIENT_TO_SERVER = 2 } |
| Remote log packet direction enumeration. More... | |
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_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_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_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_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_get_color_array (void) |
| Get the appropriate color array based on terminal capabilities. | |
| void | log_redetect_terminal_capabilities (void) |
| Re-detect terminal capabilities after logging is initialized. | |
| 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_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). | |
| 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. | |
Logging Macros | |
| |
| #define | log_dev(...) log_msg(LOG_DEV, __FILE__, __LINE__, __func__, __VA_ARGS__) |
| Log a DEV message (most verbose, development only) | |
| #define | log_debug(...) log_msg(LOG_DEBUG, __FILE__, __LINE__, __func__, __VA_ARGS__) |
| Log a DEBUG message. | |
| #define | log_info(...) log_msg(LOG_INFO, __FILE__, __LINE__, __func__, __VA_ARGS__) |
| Log an INFO message. | |
| #define | log_warn(...) log_msg(LOG_WARN, __FILE__, __LINE__, __func__, __VA_ARGS__) |
| Log a WARN message. | |
| #define | log_error(...) log_msg(LOG_ERROR, __FILE__, __LINE__, __func__, __VA_ARGS__) |
| Log an ERROR message. | |
| #define | log_fatal(...) log_msg(LOG_FATAL, __FILE__, __LINE__, __func__, __VA_ARGS__) |
| Log a FATAL message. | |
| #define | log_plain(...) log_plain_msg(__VA_ARGS__) |
| Plain logging - writes to both log file and stderr without timestamps or log levels. | |
| #define | log_plain_stderr(...) log_plain_stderr_msg(__VA_ARGS__) |
| Plain logging to stderr with newline. | |
| #define | log_plain_stderr_nonewline(...) log_plain_stderr_nonewline_msg(__VA_ARGS__) |
| Plain logging to stderr without newline - for interactive prompts. | |
| #define | log_file(...) log_file_msg(__VA_ARGS__) |
| File-only logging - writes to log file only, no stderr output. | |
Rate-Limited Logging Macros | |
| #define | log_dev_every(interval_us, fmt, ...) log_every(DEV, interval_us, fmt, ##__VA_ARGS__) |
| Rate-limited DEV logging. | |
| #define | log_debug_every(interval_us, fmt, ...) log_every(DEBUG, interval_us, fmt, ##__VA_ARGS__) |
| Rate-limited DEBUG logging. | |
| #define | log_info_every(interval_us, fmt, ...) log_every(INFO, interval_us, fmt, ##__VA_ARGS__) |
| Rate-limited INFO logging. | |
| #define | log_warn_every(interval_us, fmt, ...) log_every(WARN, interval_us, fmt, ##__VA_ARGS__) |
| Rate-limited WARN logging. | |
| #define | log_error_every(interval_us, fmt, ...) log_every(ERROR, interval_us, fmt, ##__VA_ARGS__) |
| Rate-limited ERROR logging. | |
| #define | log_fatal_every(interval_us, fmt, ...) log_every(FATAL, interval_us, fmt, ##__VA_ARGS__) |
| Rate-limited FATAL logging. | |
📝 Logging API with multiple log levels and terminal output control
This header provides a comprehensive logging system with:
Welcome to the logging system! If you've ever tried to debug a complex program without good logging, you know the pain. The logging system is your debugging best friend—it helps you understand what's happening in your application, track down bugs, and monitor behavior in production.
Think of logging as your application's diary. It writes down everything that happens: when clients connect, when errors occur, when packets are processed. You can configure how chatty it should be (from super verbose DEBUG mode to quiet ERROR-only mode), and you can send the output to the console, to a file, or both!
Implementation: lib/logging.h
The logging system has six levels, from most verbose to least:
DEV (0) - The super chatty one:
DEBUG (1) - The chatty one:
INFO (2) - The reporter:
WARN (3) - The concerned citizen:
ERROR (4) - Houston, we have a problem:
FATAL (5) - The showstopper:
Log Messages:
Format Specifiers:
Rate Limiting (prevents log spam in tight loops):
Available Rate-Limited Macros:
Runtime Configuration:
Environment Variable:
Configure File Logging:
Command-line Option:
Log messages have a consistent format that makes them easy to read and understand. Every message includes a timestamp, level, source location, and the actual message text. Let's look at the format:
Log Message Format:
The format is simple and consistent—timestamp first (so you can see when things happened), level next (so you know how important it is), source location (so you know where in the code it came from), and the message itself.
Example Output:
Notice how you can see exactly when things happened, what level they are, where in the code they came from, and what the message is. This makes debugging much easier!
Console Colors (ANSI terminals):
Colors make it easy to scan logs—errors jump out in red, warnings in yellow. If you're in a terminal that supports colors, this makes debugging much easier.
Mutex Protection:
Thread Identification:
Example Multi-threaded Logging:
Logging performance is important—you don't want logging to slow down your application. We've designed the system to be efficient, but there are some things to keep in mind:
Rate Limiting:
Rate limiting is your friend in tight loops—you can log every iteration without actually logging every iteration!
File I/O:
File logging is pretty fast—we use buffered I/O, so most of the time you won't notice the overhead. If you're logging hundreds of messages per second, you might want to consider rate limiting or async logging.
String Formatting:
sprintf()) can be slow if you're doing it thousands of times per secondString formatting is usually pretty fast, but if you're doing it millions of times per second, it can add up. Use rate limiting for frequent messages, and remember that debug logs are compiled out in release builds!
DO:
DON'T:
Examples:
Automatic Error Logging:
Example:
| #define DEFAULT_LOG_LEVEL LOG_DEBUG |
#include <logging.h>
Default log level for debug builds (DEBUG and above)
Definition at line 75 of file log/logging.h.
| #define LOG_ATOMIC_UINT64 _Atomic uint64_t |
#include <logging.h>
Definition at line 42 of file log/logging.h.
| #define LOG_ATOMIC_UINT64_INIT | ( | val | ) | val |
#include <logging.h>
Definition at line 44 of file log/logging.h.
| #define LOG_CLIENT_IMPL | ( | client, | |
| level, | |||
| fmt, | |||
| ... | |||
| ) |
#include <logging.h>
Definition at line 55 of file network/logging.h.
| #define LOG_COMPILE_LEVEL LOG_DEV |
#include <logging.h>
Compile-time minimum log level (DEV keeps all, allowing runtime -vvv)
Definition at line 119 of file log/logging.h.
#include <logging.h>
Log a DEBUG message.
| ... | Format string and arguments (printf-style) |
Definition at line 450 of file log/logging.h.
| #define log_debug_client | ( | client, | |
| fmt, | |||
| ... | |||
| ) | LOG_CLIENT_IMPL(client, LOG_DEBUG, fmt, ##__VA_ARGS__) |
#include <logging.h>
Server sends DEBUG log message to client.
Definition at line 66 of file network/logging.h.
| #define log_debug_every | ( | interval_us, | |
| fmt, | |||
| ... | |||
| ) | log_every(DEBUG, interval_us, fmt, ##__VA_ARGS__) |
| #define log_debug_server | ( | sockfd, | |
| crypto_ctx, | |||
| fmt, | |||
| ... | |||
| ) | LOG_SERVER_IMPL(sockfd, crypto_ctx, LOG_DEBUG, fmt, ##__VA_ARGS__) |
#include <logging.h>
Client sends DEBUG log message to server.
Definition at line 92 of file network/logging.h.
#include <logging.h>
Log a DEV message (most verbose, development only)
| ... | Format string and arguments (printf-style) |
Definition at line 432 of file log/logging.h.
| #define log_dev_every | ( | interval_us, | |
| fmt, | |||
| ... | |||
| ) | log_every(DEV, interval_us, fmt, ##__VA_ARGS__) |
#include <logging.h>
Log an ERROR message.
| ... | Format string and arguments (printf-style) |
Definition at line 501 of file log/logging.h.
| #define log_error_client | ( | client, | |
| fmt, | |||
| ... | |||
| ) | LOG_CLIENT_IMPL(client, LOG_ERROR, fmt, ##__VA_ARGS__) |
#include <logging.h>
Server sends ERROR log message to client.
Definition at line 75 of file network/logging.h.
| #define log_error_every | ( | interval_us, | |
| fmt, | |||
| ... | |||
| ) | log_every(ERROR, interval_us, fmt, ##__VA_ARGS__) |
| #define log_error_server | ( | sockfd, | |
| crypto_ctx, | |||
| fmt, | |||
| ... | |||
| ) | LOG_SERVER_IMPL(sockfd, crypto_ctx, LOG_ERROR, fmt, ##__VA_ARGS__) |
#include <logging.h>
Client sends ERROR log message to server.
Definition at line 102 of file network/logging.h.
| #define log_every | ( | log_level, | |
| interval_us, | |||
| fmt, | |||
| ... | |||
| ) |
#include <logging.h>
Rate-limited logging macro (thread-safe)
Logs at most once per specified time interval. Useful for threads that have an FPS and functions they call to prevent spammy logs.
| log_level | Log level (DEV, DEBUG, INFO, WARN, ERROR, FATAL) |
| interval_us | Minimum microseconds between log messages |
| fmt | Format string (printf-style) |
| ... | Format arguments |
Definition at line 581 of file log/logging.h.
#include <logging.h>
Log a FATAL message.
| ... | Format string and arguments (printf-style) |
Definition at line 517 of file log/logging.h.
| #define log_fatal_client | ( | client, | |
| fmt, | |||
| ... | |||
| ) | LOG_CLIENT_IMPL(client, LOG_FATAL, fmt, ##__VA_ARGS__) |
#include <logging.h>
Server sends FATAL log message to client.
Definition at line 78 of file network/logging.h.
| #define log_fatal_server | ( | sockfd, | |
| crypto_ctx, | |||
| fmt, | |||
| ... | |||
| ) | LOG_SERVER_IMPL(sockfd, crypto_ctx, LOG_FATAL, fmt, ##__VA_ARGS__) |
#include <logging.h>
Client sends FATAL log message to server.
Definition at line 106 of file network/logging.h.
| #define log_file | ( | ... | ) | log_file_msg(__VA_ARGS__) |
#include <logging.h>
File-only logging - writes to log file only, no stderr output.
| ... | Format string and arguments (printf-style) |
Definition at line 546 of file log/logging.h.
| #define LOG_HEADER_BUFFER_SIZE 512 |
#include <logging.h>
Maximum size of a log header (timestamp, level, file:line:func)
Definition at line 101 of file log/logging.h.
#include <logging.h>
Log an INFO message.
| ... | Format string and arguments (printf-style) |
Definition at line 467 of file log/logging.h.
| #define log_info_client | ( | client, | |
| fmt, | |||
| ... | |||
| ) | LOG_CLIENT_IMPL(client, LOG_INFO, fmt, ##__VA_ARGS__) |
#include <logging.h>
Server sends INFO log message to client.
Definition at line 69 of file network/logging.h.
| #define log_info_every | ( | interval_us, | |
| fmt, | |||
| ... | |||
| ) | log_every(INFO, interval_us, fmt, ##__VA_ARGS__) |
| #define log_info_server | ( | sockfd, | |
| crypto_ctx, | |||
| fmt, | |||
| ... | |||
| ) | LOG_SERVER_IMPL(sockfd, crypto_ctx, LOG_INFO, fmt, ##__VA_ARGS__) |
#include <logging.h>
Client sends INFO log message to server.
Definition at line 96 of file network/logging.h.
| #define LOG_MMAP_MSG_BUFFER_SIZE 1024 |
#include <logging.h>
Maximum size of a log message in mmap mode.
Definition at line 98 of file log/logging.h.
| #define LOG_MSG_BUFFER_SIZE 4096 |
#include <logging.h>
Maximum size of a single log message (including formatting)
Definition at line 95 of file log/logging.h.
| #define log_plain | ( | ... | ) | log_plain_msg(__VA_ARGS__) |
#include <logging.h>
Plain logging - writes to both log file and stderr without timestamps or log levels.
| ... | Format string and arguments (printf-style) |
Definition at line 525 of file log/logging.h.
| #define log_plain_stderr | ( | ... | ) | log_plain_stderr_msg(__VA_ARGS__) |
#include <logging.h>
Plain logging to stderr with newline.
| ... | Format string and arguments (printf-style) |
Definition at line 532 of file log/logging.h.
| #define log_plain_stderr_nonewline | ( | ... | ) | log_plain_stderr_nonewline_msg(__VA_ARGS__) |
#include <logging.h>
Plain logging to stderr without newline - for interactive prompts.
| ... | Format string and arguments (printf-style) |
Definition at line 539 of file log/logging.h.
| #define LOG_SERVER_IMPL | ( | sockfd, | |
| crypto_ctx, | |||
| level, | |||
| fmt, | |||
| ... | |||
| ) |
#include <logging.h>
Definition at line 86 of file network/logging.h.
| #define LOG_TIMESTAMP_BUFFER_SIZE 32 |
#include <logging.h>
Maximum size of a timestamp string.
Definition at line 104 of file log/logging.h.
#include <logging.h>
Log a WARN message.
| ... | Format string and arguments (printf-style) |
Definition at line 484 of file log/logging.h.
| #define log_warn_client | ( | client, | |
| fmt, | |||
| ... | |||
| ) | LOG_CLIENT_IMPL(client, LOG_WARN, fmt, ##__VA_ARGS__) |
#include <logging.h>
Server sends WARN log message to client.
Definition at line 72 of file network/logging.h.
| #define log_warn_every | ( | interval_us, | |
| fmt, | |||
| ... | |||
| ) | log_every(WARN, interval_us, fmt, ##__VA_ARGS__) |
| #define log_warn_server | ( | sockfd, | |
| crypto_ctx, | |||
| fmt, | |||
| ... | |||
| ) | LOG_SERVER_IMPL(sockfd, crypto_ctx, LOG_WARN, fmt, ##__VA_ARGS__) |
#include <logging.h>
Client sends WARN log message to server.
Definition at line 99 of file network/logging.h.
| #define MAX_LOG_SIZE (3 * 1024 * 1024) |
#include <logging.h>
Maximum log file size in bytes (3MB) before rotation.
Definition at line 79 of file log/logging.h.
| #define MAX_TERMINAL_BUFFER_ENTRIES 256 |
#include <logging.h>
Maximum number of buffered log entries.
Definition at line 85 of file log/logging.h.
| #define MAX_TERMINAL_BUFFER_SIZE (64 * 1024) |
#include <logging.h>
Maximum size of terminal output buffer (64KB)
Definition at line 82 of file log/logging.h.
| typedef struct log_mmap_config log_mmap_config_t |
#include <logging.h>
Definition at line 632 of file log/logging.h.
| typedef enum remote_log_direction remote_log_direction_t |
| enum log_color_t |
#include <logging.h>
Color enum for logging - indexes into color arrays.
These values directly index into level_colors arrays. Order matches DEV, DEBUG, INFO, WARN, ERROR, FATAL, RESET.
Definition at line 137 of file log/logging.h.
| enum log_level_t |
#include <logging.h>
Logging levels enumeration.
Definition at line 59 of file log/logging.h.
| enum remote_log_direction |
#include <logging.h>
Remote log packet direction enumeration.
Indicates the originator of a remote log message so receivers can annotate logs clearly.
| Enumerator | |
|---|---|
| REMOTE_LOG_DIRECTION_UNKNOWN | |
| REMOTE_LOG_DIRECTION_SERVER_TO_CLIENT | |
| REMOTE_LOG_DIRECTION_CLIENT_TO_SERVER | |
Definition at line 20 of file network/logging.h.
| char * format_message | ( | const char * | format, |
| va_list | args | ||
| ) |
#include <logging.h>
Format a message using va_list.
| format | Format string |
| args | Variable arguments list |
Definition at line 157 of file log/logging.c.
References ERROR_INVALID_STATE, LOGGING_INTERNAL_ERROR, SAFE_FREE, and SAFE_MALLOC.
Referenced by asciichat_fatal_with_context(), asciichat_set_errno_with_message(), asciichat_set_errno_with_system_error_and_message(), and log_labeled().
| size_t get_current_time_formatted | ( | char * | time_buf | ) |
#include <logging.h>
Get current time as formatted string.
| time_buf | Output buffer for formatted time |
Definition at line 126 of file log/logging.c.
References ERROR_INVALID_STATE, LOGGING_INTERNAL_ERROR, and platform_localtime().
Referenced by log_msg().
| void log_destroy | ( | void | ) |
#include <logging.h>
Destroy the logging system and close log file.
Definition at line 459 of file log/logging.c.
References log_mmap_destroy(), log_mmap_is_active(), mutex_destroy(), and platform_close().
Referenced by main(), and server_main().
| void log_disable_mmap | ( | void | ) |
#include <logging.h>
Disable mmap logging and return to mutex-based logging.
Flushes remaining entries and closes the mmap file.
Definition at line 1129 of file log/logging.c.
References log_info, log_mmap_destroy(), and log_mmap_is_active().
| asciichat_error_t log_enable_mmap | ( | const char * | log_path | ) |
#include <logging.h>
Enable lock-free mmap-based logging.
When enabled, log messages bypass the mutex and use atomic operations to write directly to a memory-mapped log file as human-readable text.
Benefits:
| log_path | Path to the log file (will be memory-mapped) |
Definition at line 1110 of file log/logging.c.
References log_enable_mmap_sized().
| asciichat_error_t log_enable_mmap_sized | ( | const char * | log_path, |
| size_t | max_size | ||
| ) |
#include <logging.h>
Enable lock-free mmap logging with custom file size.
| log_path | Path to the log file |
| max_size | Maximum file size in bytes (0 = default 4MB) |
Definition at line 1114 of file log/logging.c.
References ASCIICHAT_OK, ERROR_INVALID_PARAM, log_info, log_mmap_init_simple(), and SET_ERRNO.
Referenced by log_enable_mmap().
| void log_file_msg | ( | const char * | fmt, |
| ... | |||
| ) |
#include <logging.h>
Log to file only, no stderr output.
| fmt | Format string (printf-style) |
| ... | Format arguments |
Writes to log file only, without terminal output.
Definition at line 923 of file log/logging.c.
References LOG_INFO, log_mmap_is_active(), log_mmap_write(), and LOG_MSG_BUFFER_SIZE.
Referenced by client_main().
| const char ** log_get_color_array | ( | void | ) |
#include <logging.h>
Get the appropriate color array based on terminal capabilities.
Automatically detects terminal capabilities and returns the appropriate color array.
Definition at line 1079 of file log/logging.c.
References terminal_capabilities_t::color_level, LOG_CMODE_16, LOG_CMODE_256, LOG_CMODE_TRUECOLOR, TERM_COLOR_256, and TERM_COLOR_TRUECOLOR.
Referenced by log_level_color(), log_msg(), and platform_is_binary_in_path().
| bool log_get_force_stderr | ( | void | ) |
#include <logging.h>
Get current force_stderr setting.
Definition at line 510 of file log/logging.c.
| log_level_t log_get_level | ( | void | ) |
#include <logging.h>
Get the current minimum log level.
Definition at line 485 of file log/logging.c.
Referenced by options_init().
| bool log_get_terminal_output | ( | void | ) |
#include <logging.h>
Get current terminal output setting.
Definition at line 502 of file log/logging.c.
| void log_init | ( | const char * | filename, |
| log_level_t | level, | ||
| bool | force_stderr, | ||
| bool | use_mmap | ||
| ) |
#include <logging.h>
Initialize the logging system.
| filename | Log file path (or NULL for no file logging) |
| level | Minimum log level to output |
| force_stderr | If true, route ALL logs to stderr (for client mode to keep stdout clean) |
| use_mmap | If true, use fully lock-free mmap logging (recommended). If mmap fails, uses stderr only (no mutex fallback). |
Definition at line 386 of file log/logging.c.
References ASCIICHAT_OK, terminal_capabilities_t::detection_reliable, FILE_PERM_PRIVATE, log_mmap_init_simple(), log_redetect_terminal_capabilities(), mutex_init(), platform_close(), platform_open(), safe_fprintf(), SAFE_GETENV, and SAFE_STRNCPY.
Referenced by asciichat_shared_init(), and main().
| void log_labeled | ( | const char * | label, |
| log_color_t | color, | ||
| const char * | message, | ||
| ... | |||
| ) |
#include <logging.h>
Print a labeled message with color.
| label | The label text to print (appears before the message) |
| color | Color for the label (from log_color_t enum) |
| message | Format string (printf-style) for the message |
| ... | Format arguments |
Used for consistent formatting of section headers and labeled output. The label is colored, followed by the message content. Output goes to both stderr and log file.
Definition at line 98 of file asciichat_errno.c.
References format_message(), LOG_COLOR_RESET, log_file, log_level_color(), safe_fprintf(), and SAFE_FREE.
Referenced by asciichat_fatal_with_context().
| const char * log_level_color | ( | log_color_t | color | ) |
#include <logging.h>
Get color string for a given color enum.
| color | Color enum value |
Definition at line 1095 of file log/logging.c.
References LOG_COLOR_RESET, and log_get_color_array().
Referenced by asciichat_print_error_context(), and log_labeled().
| bool log_lock_terminal | ( | void | ) |
#include <logging.h>
Lock terminal output for exclusive access by the calling thread.
Call this before interactive prompts (like password entry, yes/no questions) to ensure only the calling thread can output to the terminal. Other threads' log messages will be buffered and flushed when the terminal is unlocked.
While locked:
Must be paired with log_unlock_terminal().
Definition at line 514 of file log/logging.c.
References asciichat_thread_self().
Referenced by client_crypto_handshake(), client_main(), discovery_tui_select(), prompt_password(), and prompt_unknown_host().
| void log_msg | ( | log_level_t | level, |
| const char * | file, | ||
| int | line, | ||
| const char * | func, | ||
| const char * | fmt, | ||
| ... | |||
| ) |
#include <logging.h>
Log a message at a specific level.
| level | Log level (LOG_DEV, LOG_DEBUG, LOG_INFO, LOG_WARN, LOG_ERROR, LOG_FATAL) |
| file | Source file name (or NULL to omit) |
| line | Source line number (or 0 to omit) |
| func | Function name (or NULL to omit) |
| fmt | Format string (printf-style) |
| ... | Format arguments |
Definition at line 695 of file log/logging.c.
References ERROR_INVALID_STATE, get_current_time_formatted(), LOG_COLOR_RESET, LOG_ERROR, LOG_FATAL, log_get_color_array(), log_mmap_is_active(), LOG_MMAP_MSG_BUFFER_SIZE, log_mmap_write(), LOG_MSG_BUFFER_SIZE, LOG_TIMESTAMP_BUFFER_SIZE, LOG_WARN, LOGGING_INTERNAL_ERROR, and safe_fprintf().
Referenced by handle_remote_log_packet_from_client().
| 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, | ||
| ... | |||
| ) |
#include <logging.h>
Log a message to all destinations (network, file, and terminal).
| sockfd | Destination socket |
| crypto_ctx | Optional crypto context for encryption (NULL if not ready) |
| level | Log severity used for remote and local logging |
| direction | Remote log direction metadata |
| file | Source file name (or NULL to omit) |
| line | Source line number (or 0 to omit) |
| func | Function name (or NULL to omit) |
| fmt | Format string (printf-style) |
| ... | Format arguments |
Definition at line 1010 of file log/logging.c.
| 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, | ||
| ... | |||
| ) |
#include <logging.h>
Send a formatted log message over the network.
| sockfd | Destination socket |
| crypto_ctx | Optional crypto context for encryption (NULL if not ready) |
| level | Log severity used for remote and local logging |
| direction | Remote log direction metadata |
| fmt | Format string (printf-style) |
| ... | Format arguments |
Definition at line 1000 of file log/logging.c.
Referenced by disconnect_client_for_bad_data(), tcp_client_send_join(), and threaded_send_client_join_packet().
| void log_plain_msg | ( | const char * | fmt, |
| ... | |||
| ) |
#include <logging.h>
Plain logging without timestamps or levels.
| fmt | Format string (printf-style) |
| ... | Format arguments |
Writes to both log file and stderr without timestamps or log levels.
Definition at line 807 of file log/logging.c.
References asciichat_thread_self(), LOG_INFO, log_mmap_is_active(), log_mmap_write(), LOG_MSG_BUFFER_SIZE, safe_fprintf(), and shutdown_is_requested().
| void log_plain_stderr_msg | ( | const char * | fmt, |
| ... | |||
| ) |
#include <logging.h>
Plain logging to stderr with newline.
| fmt | Format string (printf-style) |
| ... | Format arguments |
Writes to both log file and stderr without timestamps or log levels, with trailing newline.
Definition at line 895 of file log/logging.c.
References shutdown_is_requested().
| void log_plain_stderr_nonewline_msg | ( | const char * | fmt, |
| ... | |||
| ) |
#include <logging.h>
Plain logging to stderr without trailing newline.
| fmt | Format string (printf-style) |
| ... | Format arguments |
Writes to both log file and stderr without timestamps, log levels, or trailing newline. Useful for interactive prompts where the user's response should be on the same line.
Definition at line 909 of file log/logging.c.
References shutdown_is_requested().
| void log_redetect_terminal_capabilities | ( | void | ) |
#include <logging.h>
Re-detect terminal capabilities after logging is initialized.
Useful when terminal capabilities change or need to be refreshed.
Definition at line 1054 of file log/logging.c.
References terminal_capabilities_t::capabilities, terminal_capabilities_t::color_level, terminal_capabilities_t::desired_fps, detect_terminal_capabilities(), terminal_capabilities_t::detection_reliable, log_debug, and terminal_capabilities_t::utf8_support.
Referenced by log_init().
| void log_set_flush_delay | ( | unsigned int | delay_ms | ) |
#include <logging.h>
Set the delay between flushing buffered log entries.
When terminal output is re-enabled after an interactive prompt, buffered log entries are flushed to the terminal. This setting adds a delay between each entry for a visual animation effect.
| delay_ms | Delay in milliseconds between each log entry (0 = no delay) |
Definition at line 527 of file log/logging.c.
| void log_set_force_stderr | ( | bool | enabled | ) |
#include <logging.h>
Force all terminal log output to stderr.
| enabled | true to force all logs to stderr, false for normal routing |
When enabled, all log messages (including INFO, DEBUG, DEV) go to stderr instead of the default behavior where INFO/DEBUG/DEV go to stdout and WARN/ERROR/FATAL go to stderr. This is used by the client to keep stdout clean for ASCII art output.
Definition at line 506 of file log/logging.c.
| void log_set_level | ( | log_level_t | level | ) |
#include <logging.h>
Set the minimum log level.
| level | Minimum log level to output |
Definition at line 480 of file log/logging.c.
Referenced by options_init().
| void log_set_terminal_output | ( | bool | enabled | ) |
#include <logging.h>
Control stderr output to terminal.
| enabled | true to enable terminal output, false to disable |
Definition at line 489 of file log/logging.c.
References log_error, options_get(), and options_state::quiet.
Referenced by asciichat_shared_init(), client_main(), display_disable_logging_for_first_frame(), mirror_main(), server_connection_cleanup(), server_connection_close(), server_connection_establish(), server_connection_lost(), and server_main().
| void log_shutdown_begin | ( | void | ) |
#include <logging.h>
Begin shutdown phase - disable console logging but keep file logging.
Call this before logging shutdown messages. Disables console output but keeps file logging so messages are recorded for debugging.
Useful when you want final messages (like "no servers found") to go to the log file only, not to stdout where it might interfere with output.
Definition at line 1140 of file log/logging.c.
| void log_shutdown_end | ( | void | ) |
#include <logging.h>
End shutdown phase - restore previous logging settings.
Call after shutdown messages have been logged to restore console output.
Definition at line 1151 of file log/logging.c.
| void log_truncate_if_large | ( | void | ) |
#include <logging.h>
Manually truncate large log files.
Checks if the log file exceeds MAX_LOG_SIZE and truncates it if necessary.
Definition at line 531 of file log/logging.c.
References MAX_LOG_SIZE.
Referenced by asciichat_shared_init().
| void log_unlock_terminal | ( | bool | previous_state | ) |
#include <logging.h>
Release terminal lock and flush buffered messages.
Call this after interactive prompts complete to release the terminal lock. Buffered log messages from other threads will be flushed to terminal.
| previous_state | The value returned by log_lock_terminal() |
Definition at line 520 of file log/logging.c.
Referenced by client_crypto_handshake(), discovery_tui_select(), prompt_password(), and prompt_unknown_host().