Skip to content

Commit 684324b

Browse files
authored
Logger support
Enable the logger
1 parent d9fc367 commit 684324b

File tree

3 files changed

+103
-56
lines changed

3 files changed

+103
-56
lines changed

logger.c

Lines changed: 99 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616

1717
static bool logger_initialized = false;
1818
static int current_log_level = VALKEY_LOG_LEVEL_DEFAULT;
19-
#if LOGGER_FFI_ENABLED
20-
static enum Level current_ffi_log_level = LEVEL_WARN; /* FFI level tracking */
21-
#endif
19+
20+
static enum Level current_ffi_log_level = WARN; /* FFI level tracking */
21+
2222

2323
/* Simple mutex simulation using static variable for initialization protection */
2424
static volatile bool initialization_in_progress = false;
@@ -27,26 +27,25 @@ static volatile bool initialization_in_progress = false;
2727
* Level Conversion Functions
2828
* ============================================================================ */
2929

30-
#if LOGGER_FFI_ENABLED
3130
/**
3231
* Convert C integer log level to FFI Level enum
3332
*/
3433
static enum Level int_to_ffi_level(int level) {
3534
switch (level) {
3635
case VALKEY_LOG_LEVEL_ERROR:
37-
return LEVEL_ERROR;
36+
return ERROR;
3837
case VALKEY_LOG_LEVEL_WARN:
39-
return LEVEL_WARN;
38+
return WARN;
4039
case VALKEY_LOG_LEVEL_INFO:
41-
return LEVEL_INFO;
40+
return INFO;
4241
case VALKEY_LOG_LEVEL_DEBUG:
43-
return LEVEL_DEBUG;
42+
return DEBUG;
4443
case VALKEY_LOG_LEVEL_TRACE:
45-
return LEVEL_TRACE;
44+
return TRACE;
4645
case VALKEY_LOG_LEVEL_OFF:
47-
return LEVEL_OFF;
46+
return OFF;
4847
default:
49-
return LEVEL_WARN; /* Default fallback */
48+
return WARN; /* Default fallback */
5049
}
5150
}
5251

@@ -55,26 +54,40 @@ static enum Level int_to_ffi_level(int level) {
5554
*/
5655
static int ffi_level_to_int(enum Level level) {
5756
switch (level) {
58-
case LEVEL_ERROR:
57+
case ERROR:
5958
return VALKEY_LOG_LEVEL_ERROR;
60-
case LEVEL_WARN:
59+
case WARN:
6160
return VALKEY_LOG_LEVEL_WARN;
62-
case LEVEL_INFO:
61+
case INFO:
6362
return VALKEY_LOG_LEVEL_INFO;
64-
case LEVEL_DEBUG:
63+
case DEBUG:
6564
return VALKEY_LOG_LEVEL_DEBUG;
66-
case LEVEL_TRACE:
65+
case TRACE:
6766
return VALKEY_LOG_LEVEL_TRACE;
68-
case LEVEL_OFF:
67+
case OFF:
6968
return VALKEY_LOG_LEVEL_OFF;
7069
default:
7170
return VALKEY_LOG_LEVEL_WARN; /* Default fallback */
7271
}
7372
}
74-
#endif
73+
7574
/* ============================================================================
7675
* Utility Functions
7776
* ============================================================================ */
77+
int valkey_glide_log_wrapper(enum Level level, const char* identifier, const char* message) {
78+
struct LogResult* log_result = glide_log(level, identifier, message);
79+
if (log_result != NULL) {
80+
if (log_result->log_error != NULL) {
81+
if (level == ERROR) {
82+
fprintf(stderr, "Log error: %s\n", log_result->log_error);
83+
} else {
84+
valkey_glide_log_wrapper(ERROR, identifier, "Failed to log message");
85+
}
86+
}
87+
free_log_result(log_result);
88+
}
89+
}
90+
7891

7992
int valkey_glide_logger_level_from_string(const char* level_str) {
8093
if (level_str == NULL) {
@@ -116,7 +129,6 @@ int valkey_glide_logger_get_level(void) {
116129
* This centralizes the FFI call and state management.
117130
*/
118131
static int internal_init_logger(const char* level, const char* filename) {
119-
#if LOGGER_FFI_ENABLED
120132
/* Prevent concurrent initialization attempts */
121133
if (initialization_in_progress) {
122134
return -1;
@@ -127,18 +139,35 @@ static int internal_init_logger(const char* level, const char* filename) {
127139
int level_int = valkey_glide_logger_level_from_string(level);
128140
enum Level ffi_level = int_to_ffi_level(level_int);
129141

130-
/* Call the new FFI init function */
142+
/* Call the FFI init function */
143+
struct LogResult* log_result = init(&ffi_level, filename);
144+
145+
if (log_result == NULL) {
146+
initialization_in_progress = false;
147+
fprintf(stderr, "Failed to initialize logger: NULL result\n");
148+
return -1; /* Failed to get result */
149+
}
131150

132-
enum Level actual_ffi_level = init(&ffi_level, filename);
151+
/* Check for initialization error */
152+
if (log_result->log_error != NULL) {
153+
/* Initialization failed */
154+
fprintf(stderr, "Failed to initialize logger: ERROR result, %s\n", log_result->log_error);
155+
free_log_result(log_result);
156+
initialization_in_progress = false;
157+
return -1;
158+
}
133159

134160
/* Update our state with the actual level set by FFI */
135-
current_ffi_log_level = actual_ffi_level;
136-
current_log_level = ffi_level_to_int(actual_ffi_level);
161+
current_ffi_log_level = log_result->level;
162+
current_log_level = ffi_level_to_int(log_result->level);
137163
logger_initialized = true;
138164

165+
/* Clean up the LogResult */
166+
free_log_result(log_result);
167+
139168
initialization_in_progress = false;
140-
#endif
141-
return 0; /* FFI init always succeeds */
169+
170+
return 0; /* Success */
142171
}
143172

144173
/**
@@ -191,13 +220,16 @@ void valkey_glide_logger_log(const char* level, const char* identifier, const ch
191220
}
192221

193222
int level_int = valkey_glide_logger_level_from_string(level);
194-
#if LOGGER_FFI_ENABLED
223+
195224
enum Level ffi_level = int_to_ffi_level(level_int);
196225

226+
/* Check if message level is at or above current log level */
227+
if (current_ffi_log_level == OFF || ffi_level > current_ffi_log_level) {
228+
return; /* Don't log if level is below threshold or logging is off */
229+
}
197230

198-
/* Call the new FFI log function with current logger level */
199-
valkey_log(ffi_level, current_ffi_log_level, identifier, message);
200-
#endif
231+
/* Call the FFI log function and handle result */
232+
valkey_glide_log_wrapper(ffi_level, identifier, message);
201233
}
202234

203235
/* ============================================================================
@@ -232,10 +264,13 @@ void valkey_glide_c_log_error(const char* identifier, const char* message) {
232264
return;
233265
}
234266

235-
/* Call the new FFI log function directly */
236-
#if LOGGER_FFI_ENABLED
237-
valkey_log(LEVEL_ERROR, current_ffi_log_level, identifier, message);
238-
#endif
267+
/* Check if message level is at or above current log level */
268+
if (current_ffi_log_level == OFF || ERROR > current_ffi_log_level) {
269+
return; /* Don't log if level is below threshold or logging is off */
270+
}
271+
272+
/* Call the FFI log function and handle result */
273+
valkey_glide_log_wrapper(ERROR, identifier, message);
239274
}
240275

241276
void valkey_glide_c_log_warn(const char* identifier, const char* message) {
@@ -246,10 +281,13 @@ void valkey_glide_c_log_warn(const char* identifier, const char* message) {
246281
return;
247282
}
248283

249-
#if LOGGER_FFI_ENABLED
250-
/* Call the new FFI log function directly */
251-
valkey_log(LEVEL_WARN, current_ffi_log_level, identifier, message);
252-
#endif
284+
/* Check if message level is at or above current log level */
285+
if (current_ffi_log_level == OFF || WARN > current_ffi_log_level) {
286+
return; /* Don't log if level is below threshold or logging is off */
287+
}
288+
289+
/* Call the FFI log function and handle result */
290+
valkey_glide_log_wrapper(WARN, identifier, message);
253291
}
254292

255293
void valkey_glide_c_log_info(const char* identifier, const char* message) {
@@ -259,10 +297,14 @@ void valkey_glide_c_log_info(const char* identifier, const char* message) {
259297
if (identifier == NULL || message == NULL) {
260298
return;
261299
}
262-
#if LOGGER_FFI_ENABLED
263-
/* Call the new FFI log function directly */
264-
valkey_log(LEVEL_INFO, current_ffi_log_level, identifier, message);
265-
#endif
300+
301+
/* Check if message level is at or above current log level */
302+
if (current_ffi_log_level == OFF || INFO > current_ffi_log_level) {
303+
return; /* Don't log if level is below threshold or logging is off */
304+
}
305+
306+
/* Call the FFI log function and handle result */
307+
valkey_glide_log_wrapper(INFO, identifier, message);
266308
}
267309

268310
void valkey_glide_c_log_debug(const char* identifier, const char* message) {
@@ -272,10 +314,14 @@ void valkey_glide_c_log_debug(const char* identifier, const char* message) {
272314
if (identifier == NULL || message == NULL) {
273315
return;
274316
}
275-
#if LOGGER_FFI_ENABLED
276-
/* Call the new FFI log function directly */
277-
valkey_log(LEVEL_DEBUG, current_ffi_log_level, identifier, message);
278-
#endif
317+
318+
/* Check if message level is at or above current log level */
319+
if (current_ffi_log_level == OFF || DEBUG > current_ffi_log_level) {
320+
return; /* Don't log if level is below threshold or logging is off */
321+
}
322+
323+
/* Call the FFI log function and handle result */
324+
valkey_glide_log_wrapper(DEBUG, identifier, message);
279325
}
280326

281327
void valkey_glide_c_log_trace(const char* identifier, const char* message) {
@@ -285,8 +331,12 @@ void valkey_glide_c_log_trace(const char* identifier, const char* message) {
285331
if (identifier == NULL || message == NULL) {
286332
return;
287333
}
288-
#if LOGGER_FFI_ENABLED
289-
/* Call the new FFI log function directly */
290-
valkey_log(LEVEL_TRACE, current_ffi_log_level, identifier, message);
291-
#endif
334+
335+
/* Check if message level is at or above current log level */
336+
if (current_ffi_log_level == OFF || TRACE > current_ffi_log_level) {
337+
return; /* Don't log if level is below threshold or logging is off */
338+
}
339+
340+
/* Call the FFI log function and handle result */
341+
valkey_glide_log_wrapper(TRACE, identifier, message);
292342
}

tests/ValkeyGlideFeaturesTest.php

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,7 @@ private function findLogSuffix(string $baseFilePath): ?string
635635
}
636636

637637
public function testLoggerBasicFunctionality()
638-
{
639-
return; // Skip this test for now, as it requires logger functionality to be implemented in ValkeyGlide.
638+
{
640639
// Test comprehensive logger functionality with file output and verification
641640
$logFile = $this->createTempLogFile();
642641

@@ -710,8 +709,7 @@ public function testLoggerBasicFunctionality()
710709
}
711710

712711
public function testLoggerLevelFiltering()
713-
{
714-
return; // Skip this test for now, as it requires logger functionality to be implemented in ValkeyGlide.
712+
{
715713
// Test that log level filtering works correctly at info level
716714
$logFile = $this->createTempLogFile();
717715

@@ -764,8 +762,7 @@ public function testLoggerLevelFiltering()
764762
}
765763

766764
public function testLoggerWithValkeyGlideIntegration()
767-
{
768-
return; // Skip this test for now, as it requires logger functionality to be implemented in ValkeyGlide.
765+
{
769766
// Test that ValkeyGlide client integration works with logger system
770767
$logFile = $this->createTempLogFile();
771768

valkey-glide

Submodule valkey-glide updated 309 files

0 commit comments

Comments
 (0)