From 393d10d18525e376d8f5e2cf70575826fdfd7092 Mon Sep 17 00:00:00 2001 From: Akihiro Suda Date: Wed, 11 Dec 2024 00:52:44 +0900 Subject: [PATCH] Add log.h Fix issue 106 Signed-off-by: Akihiro Suda --- cli.c | 32 ++++++++++++++------------------ log.h | 17 +++++++++++++++++ main.c | 14 ++------------ 3 files changed, 33 insertions(+), 30 deletions(-) create mode 100644 log.h diff --git a/cli.c b/cli.c index be7a7a5..7ea4fac 100644 --- a/cli.c +++ b/cli.c @@ -10,6 +10,7 @@ #include #include "cli.h" +#include "log.h" #ifndef VERSION #define VERSION "UNKNOWN" @@ -77,7 +78,7 @@ enum { struct cli_options *cli_options_parse(int argc, char *argv[]) { struct cli_options *res = calloc(1, sizeof(*res)); if (res == NULL) { - perror("calloc"); + ERRORN("calloc"); exit(EXIT_FAILURE); } @@ -109,7 +110,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) { } else if (strcmp(optarg, "bridged") == 0) { res->vmnet_mode = VMNET_BRIDGED_MODE; } else { - fprintf(stderr, "Unknown vmnet mode \"%s\"\n", optarg); + ERRORF("Unknown vmnet mode \"%s\"", optarg); goto error; } break; @@ -127,7 +128,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) { break; case CLI_OPT_VMNET_INTERFACE_ID: if (uuid_parse(optarg, res->vmnet_interface_id) < 0) { - fprintf(stderr, "Failed to parse UUID \"%s\"\n", optarg); + ERRORF("Failed to parse UUID \"%s\"", optarg); goto error; } break; @@ -166,7 +167,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) { * is specified) */ struct in_addr sin; if (!inet_aton(res->vmnet_gateway, &sin)) { - perror("inet_aton(res->vmnet_gateway)"); + ERRORN("inet_aton(res->vmnet_gateway)"); goto error; } uint32_t h = ntohl(sin.s_addr); @@ -175,7 +176,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) { sin.s_addr = htonl(h); const char *end_static = inet_ntoa(sin); /* static storage, do not free */ if (end_static == NULL) { - perror("inet_ntoa"); + ERRORN("inet_ntoa"); goto error; } res->vmnet_dhcp_end = strdup(end_static); @@ -189,36 +190,31 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) { /* validate */ if (res->vmnet_mode == VMNET_BRIDGED_MODE && res->vmnet_interface == NULL) { - fprintf( - stderr, - "vmnet mode \"bridged\" require --vmnet-interface to be specified\n"); + ERROR("vmnet mode \"bridged\" require --vmnet-interface to be specified"); goto error; } if (res->vmnet_gateway == NULL) { if (res->vmnet_mode != VMNET_BRIDGED_MODE) { - fprintf(stderr, - "WARNING: --vmnet-gateway=IP should be explicitly specified to " - "avoid conflicting with other applications\n"); + ERROR("WARNING: --vmnet-gateway=IP should be explicitly specified to " + "avoid conflicting with other applications"); } if (res->vmnet_dhcp_end != NULL) { - fprintf(stderr, "--vmnet-dhcp-end=IP requires --vmnet-gateway=IP\n"); + ERROR("--vmnet-dhcp-end=IP requires --vmnet-gateway=IP"); goto error; } if (res->vmnet_mask != NULL) { - fprintf(stderr, "--vmnet-mask=MASK requires --vmnet-gateway=IP\n"); + ERROR("--vmnet-mask=MASK requires --vmnet-gateway=IP"); goto error; } } else { if (res->vmnet_mode == VMNET_BRIDGED_MODE) { - fprintf(stderr, - "vmnet mode \"bridged\" conflicts with --vmnet-gateway\n"); + ERROR("vmnet mode \"bridged\" conflicts with --vmnet-gateway"); goto error; } struct in_addr dummy; if (!inet_aton(res->vmnet_gateway, &dummy)) { - fprintf(stderr, - "invalid address \"%s\" was specified for --vmnet-gateway\n", - res->vmnet_gateway); + ERRORF("invalid address \"%s\" was specified for --vmnet-gateway", + res->vmnet_gateway); goto error; } } diff --git a/log.h b/log.h new file mode 100644 index 0000000..7c5059a --- /dev/null +++ b/log.h @@ -0,0 +1,17 @@ +#ifndef SOCKET_VMNET_LOG_H +#define SOCKET_VMNET_LOG_H +#include +extern bool debug; + +#define DEBUGF(fmt, ...) \ + do { \ + if (debug) \ + fprintf(stderr, "DEBUG| " fmt "\n", __VA_ARGS__); \ + } while (0) + +#define INFOF(fmt, ...) fprintf(stderr, "INFO | " fmt "\n", __VA_ARGS__) +#define ERROR(msg) fprintf(stderr, "ERROR| " msg "\n") +#define ERRORF(fmt, ...) fprintf(stderr, "ERROR| " fmt "\n", __VA_ARGS__) +#define ERRORN(name) ERRORF(name ": %s", strerror(errno)) + +#endif /* SOCKET_VMNET_LOG_H */ diff --git a/main.c b/main.c index 68b381e..a05a2b0 100644 --- a/main.c +++ b/main.c @@ -13,23 +13,13 @@ #include #include "cli.h" +#include "log.h" #if __MAC_OS_X_VERSION_MAX_ALLOWED < 101500 #error "Requires macOS 10.15 or later" #endif -static bool debug = false; - -#define DEBUGF(fmt, ...) \ - do { \ - if (debug) \ - fprintf(stderr, "DEBUG| " fmt "\n", __VA_ARGS__); \ - } while (0) - -#define INFOF(fmt, ...) fprintf(stderr, "INFO | " fmt "\n", __VA_ARGS__) -#define ERROR(msg) fprintf(stderr, "ERROR| " msg "\n") -#define ERRORF(fmt, ...) fprintf(stderr, "ERROR| " fmt "\n", __VA_ARGS__) -#define ERRORN(name) ERRORF(name ": %s", strerror(errno)) +bool debug = false; static const char *vmnet_strerror(vmnet_return_t v) { switch (v) {