Skip to content

Commit

Permalink
Remove colors from logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dmaivel committed May 27, 2024
1 parent e64aa6d commit 4ea28e8
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 93 deletions.
53 changes: 13 additions & 40 deletions inc/sharedgl.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,47 +11,20 @@
#define GLAPI
#endif

#define COLOR_ESC "\x1b["
#define COLOR_RESET COLOR_ESC "0m"
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <string.h>

#define COLOR_ATTR_RESET "0;"
#define COLOR_ATTR_BOLD "1;"
#define COLOR_ATTR_DIM "2;"
#define COLOR_ATTR_UNDERLINE "3;"
#define COLOR_ATTR_BLINK "5;"
#define COLOR_ATTR_REVERSE "7;"
#define COLOR_ATTR_HIDDEN "8;"

#define COLOR_FG_BLACK "30"
#define COLOR_FG_RED "31"
#define COLOR_FG_GREEN "32"
#define COLOR_FG_YELLOW "33"
#define COLOR_FG_BLUE "34"
#define COLOR_FG_MAGENTA "35"
#define COLOR_FG_CYAN "36"
#define COLOR_FG_WHITE "37"

#define COLOR_BG_BLACK ";40m"
#define COLOR_BG_RED ";41m"
#define COLOR_BG_GREEN ";42m"
#define COLOR_BG_YELLOW ";43m"
#define COLOR_BG_BLUE ";44m"
#define COLOR_BG_MAGENTA ";45m"
#define COLOR_BG_CYAN ";46m"
#define COLOR_BG_WHITE ";47m"

#define COLOR_BG_NONE "m"

#ifndef NO_VERBOSE_COLOR_OUTPUT
#define COLOR(...) COLOR_ESC __VA_ARGS__
#else
#define COLOR(...) ""
#endif

#define COLOR_INFO COLOR(COLOR_ATTR_BOLD COLOR_FG_BLUE COLOR_BG_NONE)
#define COLOR_NUMB COLOR(COLOR_ATTR_BOLD COLOR_FG_GREEN COLOR_BG_NONE)
#define COLOR_ERRO COLOR(COLOR_ATTR_BOLD COLOR_FG_RED COLOR_BG_NONE)
#define COLOR_UNIQ COLOR(COLOR_ATTR_BOLD COLOR_FG_CYAN COLOR_BG_NONE)
#define PRINT_LOG(...) \
{ \
time_t current_time; \
time(&current_time); \
char *time_cstr = ctime(&current_time); \
time_cstr[strlen(time_cstr) - 1] = 0; \
fprintf(stderr, "[%s] ", time_cstr); \
fprintf(stderr, __VA_ARGS__); \
}

#define SGL_OFFSET_REGISTER_COMMIT (sizeof(int) * 0)
#define SGL_OFFSET_REGISTER_RETVAL (sizeof(int) * 1)
Expand Down
43 changes: 22 additions & 21 deletions src/server/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ static void generate_virtual_machine_arguments(size_t m)
static const char *qemu_string =
"-object memory-backend-file,size=%ldM,share,mem-path=/dev/shm/" SGL_SHARED_MEMORY_NAME ",id=" SGL_SHARED_MEMORY_NAME "\n";

printf("%slibvirt%s:\n", COLOR_UNIQ, COLOR_RESET);
printf(libvirt_string, m);
printf("\n%sqemu%s:\n", COLOR_UNIQ, COLOR_RESET);
printf(qemu_string, m);
printf("\n");
fprintf(stderr, "\nlibvirt:\n");
fprintf(stderr, libvirt_string, m);
fprintf(stderr, "\nqemu:\n");
fprintf(stderr, qemu_string, m);
fprintf(stderr, "\n");
}

static void term_handler(int sig)
Expand All @@ -65,24 +65,20 @@ static void term_handler(int sig)
case SIGINT:
break;
case SIGSEGV:
printf("%sfatal%s: server stopped: segmentation fault on %s%s%s (%s%d%s)",
COLOR_ERRO, COLOR_RESET,
COLOR_INFO, sgl_cmd2str(icmd), COLOR_RESET,
COLOR_NUMB, icmd, COLOR_RESET);
PRINT_LOG("server stopped! segmentation fault on %s (%d)\n", sgl_cmd2str(icmd), icmd);
break;
// shouldn't ever reach here
case SIGPIPE:
printf("%sfatal%s: socket unexpectedly closed",
COLOR_ERRO, COLOR_RESET);
PRINT_LOG("server stopped! socket unexpectedly closed\n");
break;
}

puts("");
exit(1);
}

static void arg_parser_protector(int sig)
{
printf("%sfatal%s: expected second argument\n", COLOR_ERRO, COLOR_RESET);
PRINT_LOG("expected second argument\n");
exit(1);
}

Expand Down Expand Up @@ -116,6 +112,7 @@ int main(int argc, char **argv)
break;
case 'x':
shm_unlink(SGL_SHARED_MEMORY_NAME);
PRINT_LOG("unlinked shared memory '%s'\n", SGL_SHARED_MEMORY_NAME);
return 0;
case 'g':
major = argv[i + 1][0] - '0';
Expand Down Expand Up @@ -143,39 +140,36 @@ int main(int argc, char **argv)
i++;
break;
default:
printf("%serr%s: unknown argument \"%s\"\n", COLOR_ERRO, COLOR_RESET, argv[i]);
PRINT_LOG("unrecognized command-line option '%s'\n", argv[i]);
}
}

signal(SIGINT, term_handler);
signal(SIGSEGV, term_handler);
signal(SIGPIPE, SIG_IGN);

printf("%sinfo%s: press %sCTRL+C%s to terminate server\n\n", COLOR_INFO, COLOR_RESET, COLOR_NUMB, COLOR_RESET);
printf("%sinfo%s: reporting gl version %s%d%s.%s%d%s\n\n", COLOR_INFO, COLOR_RESET, COLOR_NUMB, major, COLOR_RESET, COLOR_NUMB, minor, COLOR_RESET);
PRINT_LOG("press CTRL+C to terminate server\n");

if (print_virtual_machine_arguments) {
if (!network_over_shared)
generate_virtual_machine_arguments(shm_size);
else
printf("%sinfo%s: command line argument '-v' ignored as networking is enabled\n\n", COLOR_INFO, COLOR_RESET);
PRINT_LOG("command line argument '-v' ignored as networking is enabled\n");
}

printf("%sinfo%s: using %s%ld%s MiB of memory\n", COLOR_INFO, COLOR_RESET, COLOR_NUMB, shm_size, COLOR_RESET);

/*
* allocate memory, only create a shared memory file if using shared memory
*/
shm_size *= 1024 * 1024;
if (!network_over_shared) {
int shm_fd = shm_open(SGL_SHARED_MEMORY_NAME, O_CREAT | O_RDWR, S_IRWXU);
if (shm_fd == -1) {
printf("%serr%s: failed to open shared memory '%s'\n", COLOR_ERRO, COLOR_RESET, SGL_SHARED_MEMORY_NAME);
PRINT_LOG("failed to open shared memory '%s'\n", SGL_SHARED_MEMORY_NAME);
return -1;
}

if (ftruncate(shm_fd, shm_size) == -1) {
printf("%serr%s: failed to truncate shared memory\n", COLOR_ERRO, COLOR_RESET);
PRINT_LOG("failed to truncate shared memory\n");
return -2;
}

Expand All @@ -185,6 +179,8 @@ int main(int argc, char **argv)
shm_ptr = mmap(NULL, shm_size, PROT_READ | PROT_WRITE, MAP_ANON | MAP_SHARED, -1, 0);
}

PRINT_LOG("reserved %ld MiB of memory\n", shm_size / 1024 / 1024);

struct sgl_cmd_processor_args args = {
.base_address = shm_ptr,
.memory_size = shm_size,
Expand All @@ -198,5 +194,10 @@ int main(int argc, char **argv)
.internal_cmd_ptr = &internal_cmd_ptr,
};

int mw, mh;
sgl_get_max_resolution(&mw, &mh);
PRINT_LOG("maximum resolution set to %dx%d\n", mw, mh);
PRINT_LOG("reporting gl version %d.%d\n", major, minor);

sgl_cmd_processor_start(args);
}
48 changes: 16 additions & 32 deletions src/server/processor.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,10 @@ void sgl_cmd_processor_start(struct sgl_cmd_processor_args args)
struct net_context *net_ctx = NULL;

if ((signed)fifo_size < 0) {
printf("%sfatal%s: framebuffer too big! try increasing memory\n", COLOR_ERRO, COLOR_RESET);
PRINT_LOG("framebuffer too big, try increasing memory!\n");
return;
}

printf("%sinfo%s: [%s0x%08lx%s - %s0x%08lx%s] [%s0x%08lx%s - %s0x%08lx%s] [%s0x%08lx%s - %s0x%08lx%s]\n",
COLOR_INFO, COLOR_RESET,
COLOR_NUMB, (size_t)0, COLOR_RESET,
COLOR_NUMB, (size_t)SGL_OFFSET_COMMAND_START - 1, COLOR_RESET,
COLOR_NUMB, (size_t)SGL_OFFSET_COMMAND_START, COLOR_RESET,
COLOR_NUMB, SGL_OFFSET_COMMAND_START + fifo_size - 1, COLOR_RESET,
COLOR_NUMB, SGL_OFFSET_COMMAND_START + fifo_size, COLOR_RESET,
COLOR_NUMB, args.memory_size, COLOR_RESET
);
printf("%sinfo%s: %-26s%-26s%-26s\n\n", COLOR_INFO, COLOR_RESET, "registers", "fifo buffer", "framebuffer");

memset(p + SGL_OFFSET_COMMAND_START, 0, fifo_size);

*(int*)(p + SGL_OFFSET_REGISTER_FBSTART) = SGL_OFFSET_COMMAND_START + fifo_size;
Expand All @@ -169,16 +158,17 @@ void sgl_cmd_processor_start(struct sgl_cmd_processor_args args)
if (args.network_over_shared) {
char *res = net_init_server(&net_ctx, args.port);
if (res != NULL) {
printf("%sfatal%s: could not initialize server (%s)\n", COLOR_ERRO, COLOR_RESET, res);
PRINT_LOG("failed to start server (reason: %s)\n", res);
return;
}

printf("%sinfo%s: running server on %s%s%s:%s%d%s\n\n",
COLOR_INFO, COLOR_RESET,
COLOR_NUMB, net_get_ip(), COLOR_RESET,
COLOR_NUMB, args.port, COLOR_RESET);

PRINT_LOG("--------------------------------------------------------\n");
PRINT_LOG("running server on %s:%d\n", net_get_ip(), args.port);
PRINT_LOG("ensure SGL_NET_OVER_SHARED is set before running clients\n");
}

PRINT_LOG("--------------------------------------------------------\n");

bool network_expecting_retval = true;

while (1) {
Expand Down Expand Up @@ -208,7 +198,7 @@ void sgl_cmd_processor_start(struct sgl_cmd_processor_args args)
*/
connection_add(creg, 0);

printf("%sinfo%s: client %s%d%s connected\n", COLOR_INFO, COLOR_RESET, COLOR_NUMB, creg, COLOR_RESET);
PRINT_LOG("client %d connected\n", creg);

/*
* prevent the same client from connecting more
Expand Down Expand Up @@ -253,7 +243,7 @@ void sgl_cmd_processor_start(struct sgl_cmd_processor_args args)

*((int*)(p + SGL_OFFSET_REGISTER_CLAIM_ID)) += 1;

printf("%sinfo%s: client %s%d%s connected\n", COLOR_INFO, COLOR_RESET, COLOR_NUMB, id, COLOR_RESET);
PRINT_LOG("client %d connected\n", id);
}

/*
Expand Down Expand Up @@ -316,7 +306,7 @@ void sgl_cmd_processor_start(struct sgl_cmd_processor_args args)
struct sgl_packet_fifo_upload initial_upload_packet, *the_rest_of_the_packets = NULL;
if (!net_recv_tcp_timeout(net_ctx, i, &initial_upload_packet, sizeof(initial_upload_packet), 500)) {
int id = get_id_from_fd(i);
printf("%sinfo%s: client %s%d%s timed out, disconnected\n", COLOR_INFO, COLOR_RESET, COLOR_NUMB, id, COLOR_RESET);
PRINT_LOG("client %d timed out, disconnected\n", id);
connection_rem(id, net_ctx);
memset(p + SGL_OFFSET_COMMAND_START, 0, fifo_size);
break;
Expand Down Expand Up @@ -370,7 +360,7 @@ void sgl_cmd_processor_start(struct sgl_cmd_processor_args args)
break;
case SGL_CMD_GOODBYE_WORLD: {
int id = *pb++;
printf("%sinfo%s: client %s%d%s disconnected\n", COLOR_INFO, COLOR_RESET, COLOR_NUMB, id, COLOR_RESET);
PRINT_LOG("client %d disconnected\n", id);
connection_rem(id, net_ctx);
memset(p + SGL_OFFSET_COMMAND_START, 0, fifo_size);
network_expecting_retval = false;
Expand Down Expand Up @@ -4690,17 +4680,11 @@ void sgl_cmd_processor_start(struct sgl_cmd_processor_args args)
break;
}
}

if (!begun) {
int error = glGetError();
if (error != GL_NO_ERROR)
printf("%sglerr%s: client %s%d%s opengl error: %s%d%s (%s0x%04x%s) from %s%s%s (%s%d%s)\n",
COLOR_ERRO, COLOR_RESET,
COLOR_NUMB, client_id, COLOR_RESET,
COLOR_NUMB, error, COLOR_RESET,
COLOR_NUMB, error, COLOR_RESET,
COLOR_INFO, sgl_cmd2str(cmd), COLOR_RESET,
COLOR_NUMB, cmd, COLOR_RESET
);
int error;
while ((error = glGetError()) != GL_NO_ERROR)
PRINT_LOG("gl error (%d / 0x%04x) on client %d from %s (%d)\n", error, error, client_id, sgl_cmd2str(cmd), cmd);
}
}

Expand Down

0 comments on commit 4ea28e8

Please sign in to comment.