chuniio-brokenithm/chuniio/src/servers/common.c

72 lines
1.3 KiB
C

//
// Created by beerpsi on 12/31/2023.
//
#include "common.h"
#include <time.h>
#include "util/dprintf.h"
void print_err(const char *fmt, ...) {
const time_t lt = time(NULL);
const struct tm *local = localtime(&lt);
char tmpbuf[32];
strftime(tmpbuf, 32, "%Y-%m-%d %H:%M:%S", local);
dprintf("brokenithm_server: [%s] ", tmpbuf);
va_list ap;
va_start(ap, fmt);
dprintfv(fmt, ap);
va_end(ap);
}
void dump_bytes(const void *ptr, const size_t nbytes, const bool hex_string) {
size_t i;
size_t j;
if (nbytes == 0) {
dprintf("\t--- Empty ---\n");
}
const uint8_t *bytes = ptr;
if (hex_string) {
for (i = 0; i < nbytes; i++) {
dprintf("%02x", bytes[i]);
}
dprintf("\n");
return;
}
for (i = 0; i < nbytes; i += 16) {
dprintf(" %08x:", (int)i);
for (j = 0; i + j < nbytes && j < 16; j++) {
dprintf(" %02x", bytes[i + j]);
}
while (j < 16) {
dprintf(" ");
j++;
}
dprintf(" ");
for (j = 0; i + j < nbytes && j < 16; j++) {
uint8_t c = bytes[i + j];
if (c < 0x20 || c >= 0x7F) {
c = '.';
}
dprintf("%c", c);
}
dprintf("\n");
}
dprintf("\n");
}