Skip to content

Commit

Permalink
Separate traffic debugging control
Browse files Browse the repository at this point in the history
  • Loading branch information
attipaci committed Sep 10, 2024
1 parent 279492d commit ca11201
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -939,6 +939,7 @@ You can also turn debug messages by defining the `DEBUG` constant for the compil
Some obvious ways the library could evolve and grow in the not too distant future:
- Automated regression testing and coverage tracking.
- Keep track of subscription patterns, and automatically resubscribe to them on reconnecting.
- Support for the [RESP3](https://github.com/antirez/RESP3/blob/master/spec.md) standard and Redis `HELLO`.
- Support for [Redis Sentinel](https://redis.io/docs/latest/develop/reference/sentinel-clients/) clients, for high-availability server configurations.
- TLS support (perhaps...)
Expand Down
1 change: 1 addition & 0 deletions include/redisx.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ typedef void (*RedisErrorHandler)(Redis *redis, enum redisx_channel channel, con

void redisxSetVerbose(boolean value);
boolean redisxIsVerbose();
void redisxDebugTraffic(boolean value);

void redisxSetTcpBuf(int size);
int redisxSetTransmitErrorHandler(Redis *redis, RedisErrorHandler f);
Expand Down
12 changes: 8 additions & 4 deletions src/redisx-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
# define SEND_YIELD_COUNT (-1)
#endif

#define trprintf if(debugTraffic) printf ///< Use for debugging Redis bound traffic

int debugTraffic = FALSE; ///< Whether to print excerpts of all traffic to/from the Redis server.

/// \endcond

/**
Expand Down Expand Up @@ -76,7 +80,7 @@ static int rReadChunkAsync(ClientPrivate *cp) {

cp->next = 0;
cp->available = recv(sock, cp->in, REDISX_RCVBUF_SIZE, 0);
xdprintf(" ... read %d bytes from client %d socket.\n", cp->available, cp->idx);
trprintf(" ... read %d bytes from client %d socket.\n", cp->available, cp->idx);
if(cp->available <= 0) {
if(cp->available == 0) errno = ECONNRESET; // 0 return is remote cleared connection. So set ECONNRESET...
return rTransmitError(cp, "read");
Expand Down Expand Up @@ -155,7 +159,7 @@ static int rReadToken(ClientPrivate *cp, char *buf, int length) {
// Terminate string in buffer
buf[L] = '\0';

xdprintf("[%s]\n", buf);
trprintf("[%s]\n", buf);

if(*buf == RESP_ERROR) {
fprintf(stderr, "Redis-X> error message: %s\n", &buf[1]);
Expand Down Expand Up @@ -229,7 +233,7 @@ static int rSendBytesAsync(ClientPrivate *cp, const char *buf, int length, boole

if(!buf) return X_NULL;

xdprintf(" >>> '%s'\n", buf);
trprintf(" >>> '%s'\n", buf);

if(!cp->isEnabled) return X_NO_INIT;
if(sock < 0) return X_NO_INIT;
Expand Down Expand Up @@ -735,7 +739,7 @@ RESP *redisxReadReplyAsync(RedisClient *cl) {
}
else if(resp->value) {
((char *) resp->value)[resp->n] = '\0';
xdprintf("\"%s\"\n", (char *) resp->value);
trprintf("\"%s\"\n", (char *) resp->value);
}

break;
Expand Down
19 changes: 17 additions & 2 deletions src/redisx.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@

#define REDISX_LISTENER_PRIORITY (XPRIO_MIN + (int) (REDISX_LISTENER_REL_PRIORITY * XPRIO_RANGE))

/// \endcond
extern int debugTraffic; ///< Whether to print excerpts of all traffic to/from the Redis server.

/// \cond PRIVATE
/// \endcond

/// \cond PROTECTED

/**
* Waits to get exlusive access to configuring the properties of a Redis instance.
Expand Down Expand Up @@ -69,6 +70,7 @@ void rConfigUnlock(Redis *redis) {
*
* \param value TRUE to enable verbose reporting, or FALSE to disable.
*
* @sa redisxDebugTraffic()
*/
void redisxSetVerbose(boolean value) {
xSetVerbose(value);
Expand All @@ -83,6 +85,19 @@ boolean redisxIsVerbose() {
return xIsVerbose();
}

/**
* Enable or disable verbose reporting of all Redis bound traffic. It may be useful when debugging
* programs that use the redisx interface. Verbose reporting is DISABLED by default.
*
* \param value TRUE to enable verbose reporting, or FALSE to disable.
*
* @sa redisxSetVerbose()
*/
void redisxDebugTraffic(boolean value) {
debugTraffic = value ? TRUE : FALSE;
}


/**
* Sets the user name to use for authenticating on the Redis server after connection. See the
* `AUTH` Redis command for more explanation. Naturally, you need to call this prior to connecting
Expand Down

0 comments on commit ca11201

Please sign in to comment.