1919#include  "include/glide/command_request.pb-c.h" 
2020#include  "include/glide/response.pb-c.h" 
2121#include  "include/glide_bindings.h" 
22+ #include  "logger.h" 
2223#include  "valkey_glide_commands_common.h" 
2324
2425#define  DEBUG_COMMAND_RESPONSE_TO_ZVAL  0
@@ -162,7 +163,7 @@ uint8_t* create_route_bytes_from_route(cluster_route_t* route, size_t* route_byt
162163        case  ROUTE_TYPE_KEY :
163164            /* Validate key data */ 
164165            if  (!route -> data .key_route .key  ||  route -> data .key_route .key_len  ==  0 ) {
165-                 printf ( "Error:  Invalid key data for route\n " );
166+                 VALKEY_LOG_ERROR ( "route_processing" ,  " Invalid key data for route" );
166167                * route_bytes_len  =  0 ;
167168                return  NULL ;
168169            }
@@ -195,33 +196,36 @@ uint8_t* create_route_bytes_from_route(cluster_route_t* route, size_t* route_byt
195196            routes .simple_routes  =  simple_route ;
196197            break ;
197198
198-         default :
199+         default : { 
199200            /* Unknown route type */ 
200-             printf ( "Error:  Unknown route type: %d\n " , route -> type );
201+             VALKEY_LOG_ERROR_FMT ( "route_processing" ,  " Unknown route type: %d" , route -> type );
201202            * route_bytes_len  =  0 ;
202203            return  NULL ;
204+         }
203205    }
204206
205207    /* Get serialized size and allocate buffer */ 
206208    * route_bytes_len  =  command_request__routes__get_packed_size (& routes );
207209    if  (* route_bytes_len  ==  0 ) {
208-         printf ( "Error:  Failed to get packed size for route\n " );
210+         VALKEY_LOG_ERROR ( "route_processing" ,  " Failed to get packed size for route" );
209211        return  NULL ;
210212    }
211213
212214    route_bytes  =  (uint8_t * ) emalloc (* route_bytes_len );
213215
214216    if  (!route_bytes ) {
215-         printf ( "Error:  Failed to allocate memory for route bytes\n " );
217+         VALKEY_LOG_ERROR ( "memory_allocation" ,  " Failed to allocate memory for route bytes" );
216218        * route_bytes_len  =  0 ;
217219        return  NULL ;
218220    }
219221
220222    /* Serialize the routes */ 
221223    size_t  packed_size  =  command_request__routes__pack (& routes , route_bytes );
222224    if  (packed_size  !=  * route_bytes_len ) {
223-         printf (
224-             "Error: Packed size mismatch: expected %zu, got %zu\n" , * route_bytes_len , packed_size );
225+         VALKEY_LOG_ERROR_FMT ("route_processing" ,
226+                              "Packed size mismatch: expected %zu, got %zu" ,
227+                              * route_bytes_len ,
228+                              packed_size );
225229        efree (route_bytes );
226230        * route_bytes_len  =  0 ;
227231        return  NULL ;
@@ -240,7 +244,7 @@ CommandResult* execute_command_with_route(const void*          glide_client,
240244                                          zval *                 arg_route ) {
241245    /* Validate route parameter */ 
242246    if  (!arg_route ) {
243-         printf ( "Error:  arg_route is NULL\n " );
247+         VALKEY_LOG_ERROR ( "route_processing" ,  " arg_route is NULL" );
244248        return  NULL ;
245249    }
246250
@@ -249,15 +253,15 @@ CommandResult* execute_command_with_route(const void*          glide_client,
249253    memset (& route , 0 , sizeof (cluster_route_t ));
250254    if  (!parse_cluster_route (arg_route , & route )) {
251255        /* Failed to parse the route */ 
252-         printf ( "Error:  Failed to parse cluster route\n " );
256+         VALKEY_LOG_ERROR ( "route_processing" ,  " Failed to parse cluster route" );
253257        return  NULL ;
254258    }
255259
256260    /* Create serialized route bytes */ 
257261    size_t    route_bytes_len  =  0 ;
258262    uint8_t *  route_bytes      =  create_route_bytes_from_route (& route , & route_bytes_len );
259263    if  (!route_bytes ) {
260-         printf ( "Error:  Failed to create route bytes\n " );
264+         VALKEY_LOG_ERROR ( "route_processing" ,  " Failed to create route bytes" );
261265        /* Free dynamically allocated key if needed before returning */ 
262266        if  (route .type  ==  ROUTE_TYPE_KEY  &&  route .data .key_route .key_allocated ) {
263267            efree (route .data .key_route .key );
@@ -267,24 +271,28 @@ CommandResult* execute_command_with_route(const void*          glide_client,
267271
268272    /* Validate all parameters before FFI call */ 
269273    if  (!glide_client ) {
270-         printf ( "ERROR:  glide_client is NULL\n " );
274+         VALKEY_LOG_ERROR ( "parameter_validation" ,  " glide_client is NULL" );
271275        return  NULL ;
272276    }
273277
274278    if  (arg_count  >  0 ) {
275279        if  (!args ) {
276-             printf ("ERROR: args is NULL but arg_count is %lu\n" , arg_count );
280+             VALKEY_LOG_ERROR_FMT (
281+                 "parameter_validation" , "args is NULL but arg_count is %lu" , arg_count );
277282            return  NULL ;
278283        }
279284        if  (!args_len ) {
280-             printf ("ERROR: args_len is NULL but arg_count is %lu\n" , arg_count );
285+             VALKEY_LOG_ERROR_FMT (
286+                 "parameter_validation" , "args_len is NULL but arg_count is %lu" , arg_count );
281287            return  NULL ;
282288        }
283289    }
284290
285291    if  (route_bytes_len  >  0 ) {
286292        if  (!route_bytes ) {
287-             printf ("ERROR: route_bytes is NULL but route_bytes_len is %zu\n" , route_bytes_len );
293+             VALKEY_LOG_ERROR_FMT ("parameter_validation" ,
294+                                  "route_bytes is NULL but route_bytes_len is %zu" ,
295+                                  route_bytes_len );
288296            return  NULL ;
289297        }
290298    }
@@ -313,12 +321,13 @@ CommandResult* execute_command_with_route(const void*          glide_client,
313321
314322    /* Validate result before returning */ 
315323    if  (!result ) {
316-         printf ( "Error:  Command execution returned NULL result\n " );
324+         VALKEY_LOG_ERROR ( "command_response" ,  " Command execution returned NULL result" );
317325    } else  if  (result -> command_error ) {
318-         printf ("Error: Command execution failed: %s\n" ,
319-                result -> command_error -> command_error_message 
320-                    ? result -> command_error -> command_error_message 
321-                    : "Unknown error" );
326+         VALKEY_LOG_ERROR_FMT ("command_response" ,
327+                              "Command execution failed: %s" ,
328+                              result -> command_error -> command_error_message 
329+                                  ? result -> command_error -> command_error_message 
330+                                  : "Unknown command error" );
322331    }
323332
324333    return  result ;
@@ -359,6 +368,11 @@ int handle_string_response(CommandResult* result, char** output, size_t* output_
359368
360369    /* Check if there was an error */ 
361370    if  (result -> command_error ) {
371+         VALKEY_LOG_ERROR_FMT ("command_response" ,
372+                              "Command execution failed with error: %s" ,
373+                              result -> command_error -> command_error_message 
374+                                  ? result -> command_error -> command_error_message 
375+                                  : "Unknown error" );
362376        return  -1 ;
363377    }
364378
@@ -432,7 +446,7 @@ int command_response_to_zval(CommandResponse* response,
432446    switch  (response -> response_type ) {
433447        case  Null :
434448#if  DEBUG_COMMAND_RESPONSE_TO_ZVAL 
435-             printf ( "%s:%d -  CommandResponse is NULL\n" ,  __FILE__ ,  __LINE__ );
449+             VALKEY_LOG_DEBUG ( "response_processing" ,  " CommandResponse is NULL"  );
436450#endif 
437451            if  (use_false_if_null ) {
438452                // printf("%s:%d - CommandResponse is converted to false\n", __FILE__, __LINE__); 
@@ -444,54 +458,42 @@ int command_response_to_zval(CommandResponse* response,
444458            return  0 ;
445459        case  Int :
446460#if  DEBUG_COMMAND_RESPONSE_TO_ZVAL 
447- 
448-             printf (
449-                 "%s:%d - CommandResponse is Int: %ld\n" , __FILE__ , __LINE__ , response -> int_value );
461+             VALKEY_LOG_DEBUG_FMT (
462+                 "response_processing" , "CommandResponse is Int: %ld" , response -> int_value );
450463#endif 
451464            ZVAL_LONG (output , response -> int_value );
452465            return  1 ;
453466        case  Float :
454467#if  DEBUG_COMMAND_RESPONSE_TO_ZVAL 
455- 
456-             printf ("%s:%d - CommandResponse is Float: %f\n" ,
457-                    __FILE__ ,
458-                    __LINE__ ,
459-                    response -> float_value );
468+             VALKEY_LOG_DEBUG_FMT (
469+                 "response_processing" , "CommandResponse is Float: %f" , response -> float_value );
460470#endif 
461471            ZVAL_DOUBLE (output , response -> float_value );
462472#if  DEBUG_COMMAND_RESPONSE_TO_ZVAL 
463- 
464-             printf ("%s:%d - Converted CommandResponse to double: %f\n" ,
465-                    __FILE__ ,
466-                    __LINE__ ,
467-                    Z_DVAL_P (output ));
473+             VALKEY_LOG_DEBUG_FMT (
474+                 "response_processing" , "Converted CommandResponse to double: %f" , Z_DVAL_P (output ));
468475#endif 
469476            return  1 ;
470477        case  Bool :
471478#if  DEBUG_COMMAND_RESPONSE_TO_ZVAL 
472- 
473-             printf (
474-                 "%s:%d - CommandResponse is Bool: %d\n" , __FILE__ , __LINE__ , response -> bool_value );
479+             VALKEY_LOG_DEBUG_FMT (
480+                 "response_processing" , "CommandResponse is Bool: %d" , response -> bool_value );
475481#endif 
476482            ZVAL_BOOL (output , response -> bool_value );
477483            return  1 ;
478484        case  String :
479485#if  DEBUG_COMMAND_RESPONSE_TO_ZVAL 
480- 
481-             printf ("%s:%d - CommandResponse is String with length: %ld\n" ,
482-                    __FILE__ ,
483-                    __LINE__ ,
484-                    response -> string_value_len );
486+             VALKEY_LOG_DEBUG_FMT ("response_processing" ,
487+                                  "CommandResponse is String with length: %ld" ,
488+                                  response -> string_value_len );
485489#endif 
486490            ZVAL_STRINGL (output , response -> string_value , response -> string_value_len );
487491            return  1 ;
488492        case  Array :
489493#if  DEBUG_COMMAND_RESPONSE_TO_ZVAL 
490- 
491-             printf (
492-                 "%s:%d - CommandResponse is Array with length: %ld, use_associative_array = %d\n " ,
493-                 __FILE__ ,
494-                 __LINE__ ,
494+             VALKEY_LOG_DEBUG_FMT (
495+                 "response_processing" ,
496+                 "CommandResponse is Array with length: %ld, use_associative_array = %d" ,
495497                response -> array_value_len ,
496498                use_associative_array );
497499#endif 
@@ -520,10 +522,9 @@ int command_response_to_zval(CommandResponse* response,
520522                }
521523            } else  if  (use_associative_array  ==  COMMAND_RESPONSE_ARRAY_ASSOCIATIVE ) {
522524#if  DEBUG_COMMAND_RESPONSE_TO_ZVAL 
523-                 printf ("%s:%d - response->array_value[0]->command_response_type = %d\n " ,
524-                        __FILE__ ,
525-                        __LINE__ ,
526-                        response -> array_value [0 ].response_type );
525+                 VALKEY_LOG_DEBUG_FMT ("response_processing" ,
526+                                      "response->array_value[0]->command_response_type = %d" ,
527+                                      response -> array_value [0 ].response_type );
527528#endif 
528529                array_init (output );
529530                for  (int64_t  i  =  0 ; i  <  response -> array_value_len ; ++ i ) {
0 commit comments