Skip to content

Commit c831444

Browse files
committed
[agroal#662] Add PostgreSQL message logging
1 parent e2f1fa6 commit c831444

File tree

7 files changed

+112
-5
lines changed

7 files changed

+112
-5
lines changed

doc/manual/en/71-git.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,12 @@ and then create a pull request for it
7878

7979
**Format source code**
8080

81-
Use
81+
Use the `clang-format.sh` script from the root directory of the project to apply consistent formatting:
8282

83-
``` sh
83+
```sh
8484
./clang-format.sh
8585
```
8686

87-
to format the source code
88-
8987
**Repeat**
9088

9189
Based on feedback keep making changes, squashing, rebasing and force pushing

src/include/logging.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ extern "C" {
6565
#define pgagroal_log_error(...) pgagroal_log_line(PGAGROAL_LOGGING_LEVEL_ERROR, __FILE__, __LINE__, __VA_ARGS__)
6666
#define pgagroal_log_fatal(...) pgagroal_log_line(PGAGROAL_LOGGING_LEVEL_FATAL, __FILE__, __LINE__, __VA_ARGS__)
6767

68+
#ifdef DEBUG
69+
#define PGAGROAL_LOG_POSTGRES(x) pgagroal_log_postgres(x)
70+
#else
71+
#define PGAGROAL_LOG_POSTGRES(x) ((void)(x))
72+
#endif
73+
6874
/**
6975
* Initialize the logging system
7076
* @return 0 upon success, otherwise 1
@@ -184,6 +190,14 @@ log_file_open(void);
184190
void
185191
log_file_rotate(void);
186192

193+
/**
194+
* The function parses PostgreSQL wire-protocol from
195+
* raw data stream and logs message type and length information.
196+
* @param msg The message containing raw PostgreSQL wire protocol data
197+
*/
198+
void
199+
pgagroal_log_postgres(struct message* msg);
200+
187201
#ifdef __cplusplus
188202
}
189203
#endif

src/libpgagroal/art.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1749,4 +1749,4 @@ static int
17491749
art_iterate(struct art* t, art_callback cb, void* data)
17501750
{
17511751
return art_node_iterate(t->root, cb, data);
1752-
}
1752+
}

src/libpgagroal/logging.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -632,6 +632,98 @@ pgagroal_log_mem(void* data, size_t size)
632632
}
633633
}
634634

635+
static void*
636+
dynamic_buffer_append(void* orig, size_t orig_size,
637+
void* append, size_t append_size,
638+
size_t* new_size)
639+
{
640+
void* d = NULL;
641+
size_t s;
642+
643+
if (append != NULL)
644+
{
645+
s = orig_size + append_size;
646+
d = realloc(orig, s);
647+
memcpy((char*)d + orig_size, append, append_size);
648+
}
649+
else
650+
{
651+
s = orig_size;
652+
d = orig;
653+
}
654+
655+
*new_size = s;
656+
return d;
657+
}
658+
659+
static void* partial_buf = NULL;
660+
static size_t partial_buf_size = 0;
661+
662+
void
663+
pgagroal_log_postgres(struct message* msg)
664+
{
665+
size_t offset = 0;
666+
size_t remaining = 0;
667+
uint8_t* buf = NULL;
668+
void* new_buf = NULL;
669+
670+
uint32_t msg_len = 0;
671+
uint8_t msg_type = ' ';
672+
673+
if (!msg)
674+
{
675+
return;
676+
}
677+
678+
partial_buf = dynamic_buffer_append(partial_buf, partial_buf_size, msg->data, msg->length, &partial_buf_size);
679+
680+
buf = (uint8_t*)partial_buf;
681+
682+
while (offset + 5 <= partial_buf_size)
683+
{
684+
memcpy(&msg_len, buf + offset + 1, 4);
685+
msg_len = ntohl(msg_len);
686+
687+
if (msg_len < 4)
688+
{
689+
partial_buf_size = 0;
690+
break;
691+
}
692+
693+
if (offset + 1 + msg_len > partial_buf_size)
694+
{
695+
break;
696+
}
697+
698+
msg_type = buf[offset];
699+
pgagroal_log_trace("Message type: %c len: %u", msg_type, msg_len);
700+
701+
offset += (1 + msg_len);
702+
}
703+
704+
if (offset > 0)
705+
{
706+
remaining = partial_buf_size - offset;
707+
if (remaining > 0)
708+
{
709+
memmove(partial_buf, buf + offset, remaining);
710+
711+
new_buf = realloc(partial_buf, remaining);
712+
if (new_buf)
713+
{
714+
partial_buf = new_buf;
715+
}
716+
}
717+
else
718+
{
719+
free(partial_buf);
720+
partial_buf = NULL;
721+
}
722+
723+
partial_buf_size = remaining;
724+
}
725+
}
726+
635727
static void
636728
output_log_line(char* l)
637729
{

src/libpgagroal/pipeline_perf.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ performance_client(struct io_watcher* watcher)
120120
wi = (struct worker_io*)watcher;
121121

122122
status = pgagroal_recv_message(watcher, &msg);
123+
PGAGROAL_LOG_POSTGRES(msg);
123124

124125
if (likely(status == MESSAGE_STATUS_OK))
125126
{

src/libpgagroal/status.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ status_details(bool details, struct json* response)
264264

265265
pgagroal_json_create(&js);
266266

267+
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)"*", ValueString);
267268
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_DATABASE, (uintptr_t)"*", ValueString);
268269
pgagroal_json_put(js, MANAGEMENT_ARGUMENT_ENABLED, (uintptr_t)!config->all_disabled, ValueBool);
269270

src/libpgagroal/utils.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ uint8_t
328328
pgagroal_read_uint8(void* data)
329329
{
330330
return (uint8_t)*((char*)data);
331+
return (uint8_t)*((char*)data);
331332
}
332333

333334
int16_t

0 commit comments

Comments
 (0)