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/log.h
Go to the documentation of this file.
1
23
24#pragma once
25#ifndef ASCII_CHAT_LOG_LOG_H
26#define ASCII_CHAT_LOG_LOG_H
27
30
31#include <stdarg.h>
32#include <libplatform/atomic.h>
33// Use atomic.h API for all platforms (C and C++)
34// atomic_t is available in both C and C++ contexts
35#define LOG_ATOMIC_UINT64 atomic_t
36// Value initialization is valid for both the C and C++ representations of
37// libplatform's atomic_t, including C++ std::atomic members.
38#define LOG_ATOMIC_UINT64_INIT(val) {}
39#include <stdbool.h>
40#include <stdint.h>
41#include <time.h>
42#include <libplatform/socket.h>
43#include <libplatform/system.h>
44
45/* Forward declarations */
46struct crypto_context_t;
47struct color_scheme_t; /* Opaque - full definition in ui/colors.h, only included in logging.c */
49struct session_log_buffer; /* Opaque - full definition in session/session_log_buffer.h */
51
52/* Include log types (must be before network/log.h to avoid circular dependency) */
53#include "types.h"
54
55static inline bool log_only_enabled(unsigned bitmask, log_level_t level) {
56 return (bitmask & LOG_BIT(level)) != 0;
57}
58
59#include "../network/log.h"
60
61#ifndef NDEBUG
63#define DEFAULT_LOG_LEVEL LOG_DEBUG
64#else
66#define DEFAULT_LOG_LEVEL LOG_INFO
67#endif
68
70#define MAX_LOG_SIZE (3 * 1024 * 1024)
71
73#define MAX_TERMINAL_BUFFER_SIZE (64 * 1024)
74
76#define MAX_TERMINAL_BUFFER_ENTRIES 256
77
78/* ============================================================================
79 * Logging Buffer Size Constants
80 * ============================================================================
81 * Named constants for internal buffer sizes. Messages exceeding these sizes
82 * will be truncated with no error indication.
83 */
84
86#define LOG_MSG_BUFFER_SIZE 4096
87
89#define LOG_MMAP_MSG_BUFFER_SIZE 1024
90
92#define LOG_HEADER_BUFFER_SIZE 512
93
95#define LOG_TIMESTAMP_BUFFER_SIZE 32
96
98#define LOG_TERMINAL_BUFFER_SIZE 16384
99
101#define LOG_TERMINAL_ENTRY_BUFFER_SIZE (LOG_HEADER_BUFFER_SIZE + LOG_TERMINAL_BUFFER_SIZE)
102
103/* ============================================================================
104 * Compile-Time Log Level Stripping
105 * ============================================================================
106 * By default, all log levels are compiled in (LOG_DEV) so that runtime
107 * verbosity flags like -vvv work in both debug and release builds.
108 *
109 * Define LOG_COMPILE_LEVEL to strip logs at compile-time for smaller binaries:
110 * -DLOG_COMPILE_LEVEL=LOG_INFO -> Strip DEV and DEBUG
111 * -DLOG_COMPILE_LEVEL=LOG_WARN -> Strip DEV, DEBUG, and INFO
112 * -DLOG_COMPILE_LEVEL=LOG_DEV -> Keep all log levels (default)
113 */
114#ifndef LOG_COMPILE_LEVEL
116#define LOG_COMPILE_LEVEL LOG_DEV
117#endif
118
127typedef struct {
128 bool use_stderr; /* true for stderr, false for stdout */
129 char *message; /* Formatted message (heap allocated) */
131
132/* Implementation details (g_log struct, color arrays) are in logging.c */
133
134#ifdef __cplusplus
135extern "C" {
136#endif
137
152void log_init(const char *filename, log_level_t level, bool force_stderr, bool use_mmap);
153
158void log_destroy(void);
159
168void log_system_init(void);
169
178void log_system_destroy(void);
179
185void log_set_level(log_level_t level);
186
193
202asciichat_error_t log_set_format(const char *format_str, bool console_only);
203
210
216bool log_get_terminal_output(void);
217
230
236bool log_get_force_stderr(void);
237
245void log_disable_file_output(void);
246
253void log_truncate_if_large(void);
254
265void log_msg(log_level_t level, const char *file, int line, const char *func, const char *fmt, ...);
266
280void log_terminal_msg(log_level_t level, const char *file, int line, const char *func, const char *fmt, ...);
281
293void log_colored_msg_impl(log_level_t level, const char *file, int line, const char *func, const char *colored_message, const char *plain_message);
294#define log_colored_msg(level, colored_message, plain_message) log_colored_msg_impl((level), __FILE__, __LINE__, __func__, (colored_message), (plain_message))
295
305void 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);
306#define log_terminal_colored_msg(level, colored_message, plain_message) log_terminal_colored_msg_impl((level), __FILE__, __LINE__, __func__, (colored_message), (plain_message))
307
316void log_plain_msg(const char *fmt, ...);
317
326void log_plain_stderr_msg(const char *fmt, ...);
327
337void log_plain_stdout_msg(const char *fmt, ...);
338
348void log_plain_stderr_nonewline_msg(const char *fmt, ...);
349
358void log_file_msg(const char *fmt, ...);
359
372void log_labeled(const char *label, log_color_t color, const char *message, ...);
373
380const char *log_level_color(log_color_t color);
381
386const char *log_level_color_for_output(log_color_t color, bool enabled);
387
395const char **log_get_color_array(void);
396
398const char *log_get_foreground_color(void);
399
401int log_get_color_mode(void);
402
410
419void log_init_colors(void);
420
430void log_set_color_scheme(const color_scheme_t *scheme);
431
449bool log_lock_terminal(void);
450
460void log_unlock_terminal(bool previous_state);
461
472void log_set_flush_delay(unsigned int delay_ms);
473
481char *format_message(const char *format, va_list args);
482
489size_t get_current_time_formatted(char *time_buf);
490
491struct acip_transport;
492
505asciichat_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, ...);
506
514
522#ifdef NDEBUG
523#define log_as(level, ...) log_msg(level, NULL, 0, NULL, __VA_ARGS__)
524#else
525#define log_as(level, ...) log_msg(level, __FILE__, __LINE__, __func__, __VA_ARGS__)
526#endif
527
536#define log_only(bitmask, level, ...) \
537 do { \
538 if (log_only_enabled((bitmask), (level))) \
539 log_as(level, __VA_ARGS__); \
540 } while (asciichat_macro_block_continue())
541
550#if LOG_COMPILE_LEVEL <= LOG_DEV
551#define log_dev(...) log_only(LOG_BIT(LOG_DEV), LOG_DEV, __VA_ARGS__)
552#else
553#define log_dev(...) ((void)0)
554#endif
555
564#if LOG_COMPILE_LEVEL <= LOG_DEBUG
565#define log_debug(...) log_only(LOG_BIT(LOG_DEBUG), LOG_DEBUG, __VA_ARGS__)
566#else
567#define log_debug(...) ((void)0)
568#endif
569
577#if LOG_COMPILE_LEVEL <= LOG_INFO
578#define log_info(...) log_only(LOG_BIT(LOG_INFO), LOG_INFO, __VA_ARGS__)
579#else
580#define log_info(...) ((void)0)
581#endif
582
590#if LOG_COMPILE_LEVEL <= LOG_WARN
591#define log_warn(...) log_only(LOG_BIT(LOG_WARN), LOG_WARN, __VA_ARGS__)
592#else
593#define log_warn(...) ((void)0)
594#endif
595
603#if LOG_COMPILE_LEVEL <= LOG_ERROR
604#define log_error(...) log_only(LOG_BIT(LOG_ERROR), LOG_ERROR, __VA_ARGS__)
605#else
606#define log_error(...) ((void)0)
607#endif
608
616#define log_fatal(...) log_only(LOG_BIT(LOG_FATAL), LOG_FATAL, __VA_ARGS__)
617
619
627
629
635#define log_plain(...) log_plain_msg(__VA_ARGS__)
636
642#define log_plain_stderr(...) log_plain_stderr_msg(__VA_ARGS__)
643
649#define log_plain_stdout(...) log_plain_stdout_msg(__VA_ARGS__)
650
656#define log_plain_stderr_nonewline(...) log_plain_stderr_nonewline_msg(__VA_ARGS__)
657
663#define log_file(...) log_file_msg(__VA_ARGS__)
664
666
684#ifdef NDEBUG
685#define log_every(log_level, interval_us, fmt, ...) \
686 do { \
687 static LOG_ATOMIC_UINT64 _log_every_last_time = LOG_ATOMIC_UINT64_INIT(0); \
688 uint64_t _log_every_now = platform_get_monotonic_time_us(); \
689 uint64_t _log_every_last = atomic_load_u64(&_log_every_last_time); \
690 if (_log_every_now - _log_every_last >= (uint64_t)(interval_us)) { \
691 if (atomic_cas_u64(&_log_every_last_time, &_log_every_last, _log_every_now)) { \
692 log_msg(LOG_##log_level, NULL, 0, NULL, fmt, ##__VA_ARGS__); \
693 } \
694 } \
695 } while (asciichat_macro_block_continue())
696#else
697#define log_every(log_level, interval_us, fmt, ...) \
698 do { \
699 static LOG_ATOMIC_UINT64 _log_every_last_time = LOG_ATOMIC_UINT64_INIT(0); \
700 uint64_t _log_every_now = platform_get_monotonic_time_us(); \
701 uint64_t _log_every_last = atomic_load_u64(&_log_every_last_time); \
702 if (_log_every_now - _log_every_last >= (uint64_t)(interval_us)) { \
703 if (atomic_cas_u64(&_log_every_last_time, &_log_every_last, _log_every_now)) { \
704 log_msg(LOG_##log_level, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__); \
705 } \
706 } \
707 } while (asciichat_macro_block_continue())
708#endif
709
714
716#define log_dev_every(interval_us, fmt, ...) log_every(DEV, interval_us, fmt, ##__VA_ARGS__)
717
719#define log_debug_every(interval_us, fmt, ...) log_every(DEBUG, interval_us, fmt, ##__VA_ARGS__)
720
722#define log_info_every(interval_us, fmt, ...) log_every(INFO, interval_us, fmt, ##__VA_ARGS__)
723
725#define log_warn_every(interval_us, fmt, ...) log_every(WARN, interval_us, fmt, ##__VA_ARGS__)
726
728#define log_error_every(interval_us, fmt, ...) log_every(ERROR, interval_us, fmt, ##__VA_ARGS__)
729
731#define log_fatal_every(interval_us, fmt, ...) log_every(FATAL, interval_us, fmt, ##__VA_ARGS__)
732
734
753#ifdef NDEBUG
754#define log_nth(log_level, n, fmt, ...) \
755 do { \
756 static LOG_ATOMIC_UINT64 _log_nth_counter = LOG_ATOMIC_UINT64_INIT(0); \
757 uint64_t _log_nth_count = atomic_load_u64(&_log_nth_counter); \
758 uint64_t _log_nth_new = _log_nth_count + 1; \
759 atomic_store_u64(&_log_nth_counter, _log_nth_new); \
760 if (_log_nth_new % (uint64_t)(n) == 0) { \
761 log_msg(LOG_##log_level, NULL, 0, NULL, fmt, ##__VA_ARGS__); \
762 } \
763 } while (asciichat_macro_block_continue())
764#else
765#define log_nth(log_level, n, fmt, ...) \
766 do { \
767 static LOG_ATOMIC_UINT64 _log_nth_counter = LOG_ATOMIC_UINT64_INIT(0); \
768 uint64_t _log_nth_count = atomic_load_u64(&_log_nth_counter); \
769 uint64_t _log_nth_new = _log_nth_count + 1; \
770 atomic_store_u64(&_log_nth_counter, _log_nth_new); \
771 if (_log_nth_new % (uint64_t)(n) == 0) { \
772 log_msg(LOG_##log_level, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__); \
773 } \
774 } while (asciichat_macro_block_continue())
775#endif
776
794#ifdef NDEBUG
795#define log_once(log_level, fmt, ...) \
796 do { \
797 static LOG_ATOMIC_UINT64 _log_once_counter = LOG_ATOMIC_UINT64_INIT(0); \
798 uint64_t _log_once_count = atomic_load_u64(&_log_once_counter); \
799 if (_log_once_count == 0) { \
800 atomic_store_u64(&_log_once_counter, 1); \
801 log_msg(LOG_##log_level, NULL, 0, NULL, fmt, ##__VA_ARGS__); \
802 } \
803 } while (asciichat_macro_block_continue())
804#else
805#define log_once(log_level, fmt, ...) \
806 do { \
807 static LOG_ATOMIC_UINT64 _log_once_counter = LOG_ATOMIC_UINT64_INIT(0); \
808 uint64_t _log_once_count = atomic_load_u64(&_log_once_counter); \
809 if (_log_once_count == 0) { \
810 atomic_store_u64(&_log_once_counter, 1); \
811 log_msg(LOG_##log_level, __FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__); \
812 } \
813 } while (asciichat_macro_block_continue())
814#endif
815
820
822#define log_dev_nth(n, fmt, ...) log_nth(DEV, n, fmt, ##__VA_ARGS__)
823
825#define log_debug_nth(n, fmt, ...) log_nth(DEBUG, n, fmt, ##__VA_ARGS__)
826
828#define log_info_nth(n, fmt, ...) log_nth(INFO, n, fmt, ##__VA_ARGS__)
829
831#define log_warn_nth(n, fmt, ...) log_nth(WARN, n, fmt, ##__VA_ARGS__)
832
834#define log_error_nth(n, fmt, ...) log_nth(ERROR, n, fmt, ##__VA_ARGS__)
835
837#define log_fatal_nth(n, fmt, ...) log_nth(FATAL, n, fmt, ##__VA_ARGS__)
838
840
845
847#define log_dev_once(fmt, ...) log_once(DEV, fmt, ##__VA_ARGS__)
848
850#define log_debug_once(fmt, ...) log_once(DEBUG, fmt, ##__VA_ARGS__)
851
853#define log_info_once(fmt, ...) log_once(INFO, fmt, ##__VA_ARGS__)
854
856#define log_warn_once(fmt, ...) log_once(WARN, fmt, ##__VA_ARGS__)
857
859#define log_error_once(fmt, ...) log_once(ERROR, fmt, ##__VA_ARGS__)
860
862#define log_fatal_once(fmt, ...) log_once(FATAL, fmt, ##__VA_ARGS__)
863
865
866/* LOGGING_INTERNAL_ERROR macro is defined in logging.c (internal use only) */
867
868/* Network logging convenience macros (log_*_net) are defined in src/server/client.h
869 * since they require access to client_info_t. The base function log_net_message()
870 * is available here for generic use. */
871
872/* ============================================================================
873 * Lock-Free MMAP Logging
874 * ============================================================================ */
875
876/* Forward declaration - full definition in log/mmap.h */
877struct log_mmap_config;
879
900
910
917void log_disable_mmap(void);
918
930void log_shutdown_begin(void);
931
938void log_shutdown_end(void);
939
947void log_cleanup_colors(void);
948
959
969
980
1003size_t log_recolor_plain_entry(const char *plain_line, char *colored_buf, size_t buf_size);
1004
1005/* ============================================================================
1006 * Internal Utilities (used by log formatting modules)
1007 * ============================================================================ */
1008
1015const char *get_level_string_padded(log_level_t level);
1016
1017// Console-only logging helper (text or JSON, no file output)
1018// For signal handlers and initialization paths where file logging is not safe
1019// Automatically chooses between text and JSON based on --json flag
1020// Captures call site (file, line, function) for better debugging
1021void log_console_impl(log_level_t level, const char *file, int line, const char *func, const char *message);
1022#define log_console(level, message) log_console_impl((level), __FILE__, __LINE__, __func__, (message))
1023
1038void *log_get_template(void);
1039
1040/* Include crypto.h at the end to avoid circular dependency with time.h */
1041#include "../crypto/crypto.h"
1042
1043#ifdef __cplusplus
1044}
1045#endif
1046 /* logging */
1048
1049#endif /* ASCII_CHAT_LOG_LOG_H */
Synchronized per-thread pending error stacks and value snapshots. SET_ERRNO pushes,...
log_color_t
Definition color.h:6
bool enabled
Is filtering active?
Definition grep.c:63
asciichat_error_t
Error and exit codes - unified status values (0-255).
Definition error_codes.h:48
void log_truncate_if_large(void)
Manually truncate large log files.
Definition log/log.c:925
void log_plain_stderr_nonewline_msg(const char *fmt,...)
Plain logging to stderr without trailing newline.
Definition log/log.c:1857
void log_plain_msg(const char *fmt,...)
Plain logging without timestamps or levels.
Definition log/log.c:1685
bool log_get_terminal_output(void)
Get current terminal output setting.
Definition log/log.c:836
asciichat_error_t log_enable_mmap(const char *log_path)
Enable lock-free mmap-based logging.
Definition log/log.c:2188
void log_init_colors(void)
Initialize logging color system with current terminal capabilities.
Definition log/log.c:2172
void log_system_init(void)
Initialize the logging system internal state (system-level init).
Definition log/log.c:784
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.
Definition log/log.c:1313
void log_shutdown_end(void)
End shutdown phase - restore previous logging settings.
Definition log/log.c:2231
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).
Definition log/log.c:1998
void log_destroy(void)
Destroy the logging system and close log file.
Definition log/log.c:737
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.
Definition log/log.c:1670
void log_set_force_stderr(bool enabled)
Force all terminal log output to stderr.
Definition log/log.c:840
enum remote_log_direction remote_log_direction_t
Remote log packet direction enumeration.
log_level_t log_get_level(void)
Get the current minimum log level.
Definition log/log.c:814
const char ** log_get_color_array(void)
Get the appropriate color array based on terminal capabilities.
Definition log/log.c:2117
const char * log_level_color_for_output(log_color_t color, bool enabled)
Definition log/log.c:2149
const char * log_level_color(log_color_t color)
Get color string for a given color enum.
Definition log/log.c:2136
struct session_log_buffer session_log_buffer_t
Thread-safe fixed-size history ring for session-screen log lines.
void log_set_flush_delay(unsigned int delay_ms)
Set the delay between flushing buffered log entries.
Definition log/log.c:921
const char * log_get_foreground_color(void)
Definition log/log.c:2131
session_log_buffer_t * log_get_session_log_buffer(void)
Get the currently registered session log buffer.
Definition log/log.c:2287
char * format_message(const char *format, va_list args)
Format a message using va_list.
Definition log/log.c:305
void log_cleanup_colors(void)
Clean up compiled color scheme.
Definition log/log.c:2248
void log_shutdown_begin(void)
Begin shutdown phase - disable console logging but keep file logging.
Definition log/log.c:2219
void log_disable_file_output(void)
Disable file output and use stderr instead.
Definition log/log.c:848
bool log_get_force_stderr(void)
Get current force_stderr setting.
Definition log/log.c:844
void log_set_level(log_level_t level)
Set the minimum log level.
Definition log/log.c:809
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.
Definition log/log.c:2303
void log_clear_session_log_buffer(void)
Unregister the session log buffer.
Definition log/log.c:2272
void log_plain_stdout_msg(const char *fmt,...)
Plain logging to stdout with newline.
Definition log/log.c:1938
void log_set_session_log_buffer(session_log_buffer_t *buf)
Register a session log buffer with the logger.
Definition log/log.c:2263
void log_system_destroy(void)
Shutdown the logging system internal state (system-level cleanup).
Definition log/log.c:801
void log_init(const char *filename, log_level_t level, bool force_stderr, bool use_mmap)
Initialize the logging system.
Definition log/log.c:627
bool log_lock_terminal(void)
Lock terminal output for exclusive access by the calling thread.
Definition log/log.c:905
log_level_t
Logging levels enumeration.
Definition log/types.h:33
void log_labeled(const char *label, log_color_t color, const char *message,...)
Print a labeled message with color.
asciichat_error_t log_set_format(const char *format_str, bool console_only)
Set a custom log format string.
Definition log/log.c:859
size_t get_current_time_formatted(char *time_buf)
Get current time as formatted string.
Definition log/log.c:272
asciichat_error_t log_enable_mmap_sized(const char *log_path, size_t max_size)
Enable lock-free mmap logging with custom file size.
Definition log/log.c:2192
#define LOG_BIT(n)
Logging bit conversion.
Definition log/types.h:23
void log_file_msg(const char *fmt,...)
Log to file only, no stderr output.
Definition log/log.c:1952
void log_disable_mmap(void)
Disable mmap logging and return to mutex-based logging.
Definition log/log.c:2207
void log_set_terminal_output(bool enabled)
Control stderr output to terminal.
Definition log/log.c:818
void * log_get_template(void)
Get the current log format template (opaque pointer).
Definition log/log.c:2607
int log_get_color_mode(void)
Definition log/log.c:2126
void log_set_color_scheme(const color_scheme_t *scheme)
Set the color scheme for logging output.
Definition log/log.c:2179
void log_plain_stderr_msg(const char *fmt,...)
Plain logging to stderr with newline.
Definition log/log.c:1843
void log_redetect_terminal_capabilities(void)
Re-detect terminal capabilities after logging is initialized.
Definition log/log.c:2078
void log_unlock_terminal(bool previous_state)
Release terminal lock and flush buffered messages.
Definition log/log.c:914
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.
Definition log/log.c:1677
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).
Definition log/log.c:1546
session_log_buffer_t * log_get_history_buffer(void)
Definition log/log.c:625
struct log_mmap_config log_mmap_config_t
Definition log/log.h:878
const char * get_level_string_padded(log_level_t level)
Get padded log level string (internal utility for formatting).
Definition log/log.c:203
void log_console_impl(log_level_t level, const char *file, int line, const char *func, const char *message)
Definition log/log.c:2562
Log level types and constants.
action_args_t args
Color scheme definition.
Definition colorscheme.h:81
Endpoint cryptographic state for handshake, packet protection, authentication, and rekeying.
Heap-backed message waiting for the logging output thread.
Definition log/log.h:127
char * message
Definition log/log.h:129
bool use_stderr
Definition log/log.h:128
Path and size limits for the memory-mapped log file.
Definition mmap.h:56
size_t max_size
Definition mmap.h:58
const char * log_path
Definition mmap.h:57
Thread-safe fixed-size history ring for session-screen log lines.
⏱️ High-precision timing utilities using sokol_time.h and uthash