@@ -12,8 +12,7 @@ extern "C" {
1212// Forward declaration
1313struct ring_buffer ;
1414
15- // Logging context - allows multiple independent logging contexts
16- // Useful for testing and future multi-instance support
15+ // Logging context - encapsulates all logging state
1716typedef struct logging_context {
1817 FILE * logfile ;
1918 int loglevel ;
@@ -23,100 +22,62 @@ typedef struct logging_context {
2322 ev_signal sigusr2 ;
2423} logging_context_t ;
2524
26- // === Context-aware API (new) ===
25+ enum LogSeverity {
26+ LOG_DEBUG ,
27+ LOG_INFO ,
28+ LOG_WARNING ,
29+ LOG_ERROR ,
30+ LOG_STATS ,
31+ LOG_FATAL ,
32+ LOG_MAX
33+ };
34+
35+ // Core logging function - logs to specified context
36+ void logging_context_log (logging_context_t * ctx , const char * file , int line ,
37+ int severity , const char * fmt , ...);
2738
28- // Initialize a logging context with specified parameters
39+ // Context lifecycle management
2940void logging_context_init (logging_context_t * ctx , int fd , int level ,
3041 unsigned flight_recorder_size );
31-
32- // Initialize periodic timer and signal handlers for a context
3342void logging_context_events_init (logging_context_t * ctx , struct ev_loop * loop );
34-
35- // Cleanup events for a context
3643void logging_context_events_cleanup (logging_context_t * ctx );
37-
38- // Cleanup a logging context
3944void logging_context_cleanup (logging_context_t * ctx );
4045
41- // Check if debug logging is enabled for a context
46+ // Context query functions
4247int logging_context_debug_enabled (logging_context_t * ctx );
43-
44- // Dump flight recorder for a context
4548void logging_context_flight_recorder_dump (logging_context_t * ctx );
4649
47- // Log with explicit context
48- void logging_context_log (logging_context_t * ctx , const char * file , int line ,
49- int severity , const char * fmt , ...);
50-
51- // === Legacy API (backwards compatible) ===
52-
53- // Get the default global logging context
50+ // Default global context accessors
5451logging_context_t * logging_get_default_context (void );
5552
56- // Initializes default global logging context
57- // Writes logs to descriptor 'fd' for log levels above or equal to 'level'.
53+ // Convenience wrappers for default context
5854void logging_init (int fd , int level , unsigned flight_recorder_size );
59-
60- // Initialize periodic timer to flush logs (uses default context)
6155void logging_events_init (struct ev_loop * loop );
6256void logging_events_cleanup (struct ev_loop * loop );
63-
64- // Cleans up and flushes open logs (uses default context)
6557void logging_cleanup (void );
66-
67- // Returns 1 if debug logging is enabled (uses default context)
6858int logging_debug_enabled (void );
69-
70- // Dump flight recorder (uses default context)
7159void logging_flight_recorder_dump (void );
7260
73- // Internal. Don't use directly - use macros below
74- void _log (const char * file , int line , int severity , const char * fmt , ...);
75-
7661#ifdef __cplusplus
7762}
7863#endif
7964
80- enum LogSeverity {
81- LOG_DEBUG ,
82- LOG_INFO ,
83- LOG_WARNING ,
84- LOG_ERROR ,
85- LOG_STATS ,
86- LOG_FATAL ,
87- LOG_MAX
88- };
89-
90- // === Legacy macros (use default global context) ===
91-
92- #define LOG (level , ...) _log(__FILENAME__, __LINE__, level, __VA_ARGS__)
93- #define DLOG (...) _log(__FILENAME__, __LINE__, LOG_DEBUG, __VA_ARGS__)
94- #define ILOG (...) _log(__FILENAME__, __LINE__, LOG_INFO, __VA_ARGS__)
95- #define WLOG (...) _log(__FILENAME__, __LINE__, LOG_WARNING, __VA_ARGS__)
96- #define ELOG (...) _log(__FILENAME__, __LINE__, LOG_ERROR, __VA_ARGS__)
97- #define SLOG (...) _log(__FILENAME__, __LINE__, LOG_STATS, __VA_ARGS__)
65+ // Logging macros - all use the default global context
66+ #define LOG (level , ...) \
67+ logging_context_log(logging_get_default_context(), __FILENAME__, __LINE__, level, __VA_ARGS__)
68+ #define DLOG (...) \
69+ logging_context_log(logging_get_default_context(), __FILENAME__, __LINE__, LOG_DEBUG, __VA_ARGS__)
70+ #define ILOG (...) \
71+ logging_context_log(logging_get_default_context(), __FILENAME__, __LINE__, LOG_INFO, __VA_ARGS__)
72+ #define WLOG (...) \
73+ logging_context_log(logging_get_default_context(), __FILENAME__, __LINE__, LOG_WARNING, __VA_ARGS__)
74+ #define ELOG (...) \
75+ logging_context_log(logging_get_default_context(), __FILENAME__, __LINE__, LOG_ERROR, __VA_ARGS__)
76+ #define SLOG (...) \
77+ logging_context_log(logging_get_default_context(), __FILENAME__, __LINE__, LOG_STATS, __VA_ARGS__)
9878#define FLOG (...) do { \
99- _log(__FILENAME__, __LINE__, LOG_FATAL, __VA_ARGS__); \
100- exit(1); /* for clang-tidy! */ \
101- } while (0 )
102-
103- // === Context-aware macros (new, opt-in) ===
104-
105- #define LOG_CTX (ctx , level , ...) \
106- logging_context_log(ctx, __FILENAME__, __LINE__, level, __VA_ARGS__)
107- #define DLOG_CTX (ctx , ...) \
108- logging_context_log(ctx, __FILENAME__, __LINE__, LOG_DEBUG, __VA_ARGS__)
109- #define ILOG_CTX (ctx , ...) \
110- logging_context_log(ctx, __FILENAME__, __LINE__, LOG_INFO, __VA_ARGS__)
111- #define WLOG_CTX (ctx , ...) \
112- logging_context_log(ctx, __FILENAME__, __LINE__, LOG_WARNING, __VA_ARGS__)
113- #define ELOG_CTX (ctx , ...) \
114- logging_context_log(ctx, __FILENAME__, __LINE__, LOG_ERROR, __VA_ARGS__)
115- #define SLOG_CTX (ctx , ...) \
116- logging_context_log(ctx, __FILENAME__, __LINE__, LOG_STATS, __VA_ARGS__)
117- #define FLOG_CTX (ctx , ...) do { \
118- logging_context_log(ctx, __FILENAME__, __LINE__, LOG_FATAL, __VA_ARGS__); \
119- exit(1); /* for clang-tidy! */ \
79+ logging_context_log(logging_get_default_context(), __FILENAME__, __LINE__, LOG_FATAL, __VA_ARGS__); \
80+ exit(1); \
12081} while(0)
12182
12283#endif // _LOGGING_H_
0 commit comments