Skip to content

Commit ddb5aea

Browse files
committed
Migrate to unified logging API: remove dual legacy/context API
- Simplified logging.h: single set of macros using default context internally - Removed _log() function and all _CTX macro variants - All macros (DLOG, ILOG, etc.) now call logging_context_log() directly - Tests updated to use logging_context_log() for explicit contexts - Zero call site changes required; fully backwards compatible
1 parent dccf16a commit ddb5aea

File tree

3 files changed

+42
-92
lines changed

3 files changed

+42
-92
lines changed

src/logging.c

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -175,9 +175,12 @@ void logging_context_log(logging_context_t *ctx, const char *file, int line,
175175
va_end(args);
176176
}
177177

178-
// === Legacy API (uses default global context) ===
178+
// === Default context wrappers ===
179179

180180
logging_context_t* logging_get_default_context(void) {
181+
if (!g_default_initialized) {
182+
logging_init(STDERR_FILENO, LOG_ERROR, 0);
183+
}
181184
return &g_default_context;
182185
}
183186

@@ -188,7 +191,6 @@ void logging_init(int fd, int level, unsigned flight_recorder_size) {
188191

189192
void logging_events_init(struct ev_loop *loop) {
190193
if (!g_default_initialized) {
191-
// Initialize with default stderr if not already initialized
192194
logging_init(STDERR_FILENO, LOG_ERROR, 0);
193195
}
194196
logging_context_events_init(&g_default_context, loop);
@@ -211,16 +213,3 @@ int logging_debug_enabled(void) {
211213
void logging_flight_recorder_dump(void) {
212214
logging_context_flight_recorder_dump(&g_default_context);
213215
}
214-
215-
// Legacy _log function for backwards compatibility with existing macros
216-
void _log(const char *file, int line, int severity, const char *fmt, ...) {
217-
if (!g_default_initialized) {
218-
// Auto-initialize to stderr if not already initialized
219-
logging_init(STDERR_FILENO, LOG_ERROR, 0);
220-
}
221-
222-
va_list args;
223-
va_start(args, fmt);
224-
logging_vlog_impl(&g_default_context, file, line, severity, fmt, args);
225-
va_end(args);
226-
}

src/logging.h

Lines changed: 33 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ extern "C" {
1212
// Forward declaration
1313
struct 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
1716
typedef 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
2940
void 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
3342
void logging_context_events_init(logging_context_t *ctx, struct ev_loop *loop);
34-
35-
// Cleanup events for a context
3643
void logging_context_events_cleanup(logging_context_t *ctx);
37-
38-
// Cleanup a logging context
3944
void logging_context_cleanup(logging_context_t *ctx);
4045

41-
// Check if debug logging is enabled for a context
46+
// Context query functions
4247
int logging_context_debug_enabled(logging_context_t *ctx);
43-
44-
// Dump flight recorder for a context
4548
void 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
5451
logging_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
5854
void logging_init(int fd, int level, unsigned flight_recorder_size);
59-
60-
// Initialize periodic timer to flush logs (uses default context)
6155
void logging_events_init(struct ev_loop *loop);
6256
void logging_events_cleanup(struct ev_loop *loop);
63-
64-
// Cleans up and flushes open logs (uses default context)
6557
void logging_cleanup(void);
66-
67-
// Returns 1 if debug logging is enabled (uses default context)
6858
int logging_debug_enabled(void);
69-
70-
// Dump flight recorder (uses default context)
7159
void 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_

tests/unit/test_logging.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ void test_multiple_contexts(void) {
4444
logging_context_init(&ctx2, dup(fd2), LOG_DEBUG, 0);
4545

4646
// Log to different contexts
47-
ILOG_CTX(&ctx1, "Message to context 1");
48-
ILOG_CTX(&ctx2, "Message to context 2");
49-
DLOG_CTX(&ctx2, "Debug message to context 2");
47+
logging_context_log(&ctx1, __FILE__, __LINE__, LOG_INFO, "Message to context 1");
48+
logging_context_log(&ctx2, __FILE__, __LINE__, LOG_INFO, "Message to context 2");
49+
logging_context_log(&ctx2, __FILE__, __LINE__, LOG_DEBUG, "Debug message to context 2");
5050

5151
// Cleanup
5252
logging_context_cleanup(&ctx1);
@@ -147,8 +147,8 @@ void test_flight_recorder(void) {
147147
logging_context_init(&ctx, dup(fd), LOG_ERROR, 100);
148148

149149
// Log some debug messages (won't be written due to level, but will be recorded)
150-
DLOG_CTX(&ctx, "Debug message 1");
151-
DLOG_CTX(&ctx, "Debug message 2");
150+
logging_context_log(&ctx, __FILE__, __LINE__, LOG_DEBUG, "Debug message 1");
151+
logging_context_log(&ctx, __FILE__, __LINE__, LOG_DEBUG, "Debug message 2");
152152

153153
// Now dump the flight recorder
154154
logging_context_flight_recorder_dump(&ctx);

0 commit comments

Comments
 (0)