summaryrefslogtreecommitdiff
path: root/proto.c
diff options
context:
space:
mode:
Diffstat (limited to 'proto.c')
-rw-r--r--proto.c37
1 files changed, 3 insertions, 34 deletions
diff --git a/proto.c b/proto.c
index b1f4e02..47af6c6 100644
--- a/proto.c
+++ b/proto.c
@@ -1,6 +1,7 @@
/* SPDX-License-Identifier: Unlicense */
#include "proto.h"
+#include "debug.h"
#include <assert.h>
#include <stdint.h>
@@ -44,43 +45,11 @@ static inline void SetU32NE(void *buffer, uint32_t v)
((uint8_t *)buffer)[0] = (v >> 24) & 0xff;
}
-static char ByteToPrintableChar(uint32_t byte)
-{
- return (byte >= 0x20 && byte <= 0x7f) ? byte : '.';
-}
-
-static void FPrintRaw(FILE *s, void const *data_arg, size_t size)
-{
- const size_t cols = 16;
- const size_t section_width = 8;
- if (size == 0) {
- return;
- }
- uint8_t const *data = data_arg;
- for (size_t line = 0; line < (size - 1) / cols + 1; line++) {
- fprintf(s, "%08zx: ", line * cols);
- for (size_t c = 0; c < cols; c++) {
- fprintf(s, "%02x ", data[line * cols + c]);
- if (c % section_width == section_width - 1 && c != cols - 1) {
- fprintf(s, " ");
- }
- }
- fprintf(s, " |");
- for (size_t c = 0; c < cols; c++) {
- fprintf(s, "%c", ByteToPrintableChar(data[line * cols + c]));
- if (c % section_width == section_width - 1 && c != cols - 1) {
- fprintf(s, " ");
- }
- }
- fprintf(s, "|\n");
- }
-}
-
struct frame ParseFrame(void const *buffer_arg, size_t buffer_size)
{
assert(buffer_size >= 8);
uint8_t const *buffer = buffer_arg;
- if (LOG_TRACE) {
+ if (g_log_level >= LOG_LEVEL_TRACE) {
FPrintRaw(stderr, buffer, buffer_size);
}
if (GetU16NE(buffer) != 0) {
@@ -189,7 +158,7 @@ size_t SerializeFrame(void *buffer_arg, size_t buffer_size, struct frame const *
SetU16NE(buffer + 2, frame->type);
SetU32NE(buffer + 4, frame->connection_id);
size_t const size = 8 + serializeFrameData(buffer + 8, buffer_size - 8, frame);
- if (LOG_TRACE) {
+ if (g_log_level >= LOG_LEVEL_TRACE) {
FPrintRaw(stderr, buffer, size);
}
return size;