Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#473] Fix connection status output #474

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/include/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,6 @@ pgagroal_extract_message(char type, struct message* msg, struct message** extrac
int
pgagroal_extract_error_message(struct message* msg, char** error);

/**
* Get a string for the state
* @param state
* @return The string
*/
char*
pgagroal_get_state_string(signed char state);

/**
* Read a byte
* @param data Pointer to the data
Expand Down Expand Up @@ -524,6 +516,17 @@ parse_command(int argc,
char*
pgagroal_server_state_as_string(signed char state);

/**
* Utility function to convert the status of a connection
* into a descriptive string. Useful to spurt the status
* in command line output.
*
* @param state the actual state of the connection
* @returns the (allocated) buffer with the string
*/
char*
pgagroal_connection_state_as_string(signed char state);

#ifdef __cplusplus
}
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/libpgagroal/management.c
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ pgagroal_management_json_read_status_details(SSL* ssl, int socket, bool include_
cJSON* current_connection_json = cJSON_CreateObject();

cJSON_AddNumberToObject(current_connection_json, "number", i + 1);
cJSON_AddStringToObject(current_connection_json, "state", pgagroal_server_state_as_string(state));
cJSON_AddStringToObject(current_connection_json, "state", pgagroal_connection_state_as_string(state));
cJSON_AddStringToObject(current_connection_json, "time", time > 0 ? ts : "");
cJSON_AddStringToObject(current_connection_json, "pid", pid > 0 ? p : "");
cJSON_AddStringToObject(current_connection_json, "fd", fd > 0 ? f : "");
Expand Down
24 changes: 10 additions & 14 deletions src/libpgagroal/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,8 +249,11 @@ pgagroal_extract_error_message(struct message* msg, char** error)
}

char*
pgagroal_get_state_string(signed char state)
pgagroal_connection_state_as_string(signed char state)
{
char* buf;
int buf_size = strlen("Unknown") + 1 + 4 + 1; // 'unknown' + <space> + <number> + \0

switch (state)
{
case STATE_NOTINIT:
Expand All @@ -273,9 +276,12 @@ pgagroal_get_state_string(signed char state)
return "Validating";
case STATE_REMOVE:
return "Removing";
default:
buf = malloc(buf_size);
memset(buf, 0, buf_size);
snprintf(buf, buf_size, "Unknown %02d", state);
return buf;
}

return "Unknown";
}

signed char
Expand Down Expand Up @@ -1098,16 +1104,6 @@ parse_command(int argc,
#undef EMPTY_STR
}

/**
* Given a server state, it returns a string that
* described the state in a human-readable form.
*
* If the state cannot be determined, the numeric
* form of the state is returned as a string.
*
* @param state the value of the sate for the server
* @returns the string representing the state
*/
char*
pgagroal_server_state_as_string(signed char state)
{
Expand Down Expand Up @@ -1167,4 +1163,4 @@ pgagroal_compare_string(const char* str1, const char* str2)
return false;
}
return strcmp(str1, str2) == 0;
}
}
Loading