Skip to content

Commit

Permalink
Add log.h
Browse files Browse the repository at this point in the history
Fix issue 106

Signed-off-by: Akihiro Suda <akihiro.suda.cz@hco.ntt.co.jp>
  • Loading branch information
AkihiroSuda committed Dec 10, 2024
1 parent ab78cb5 commit 393d10d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 30 deletions.
32 changes: 14 additions & 18 deletions cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <uuid/uuid.h>

#include "cli.h"
#include "log.h"

#ifndef VERSION
#define VERSION "UNKNOWN"
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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;
}
}
Expand Down
17 changes: 17 additions & 0 deletions log.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#ifndef SOCKET_VMNET_LOG_H
#define SOCKET_VMNET_LOG_H
#include <errno.h>
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 */
14 changes: 2 additions & 12 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,13 @@
#include <vmnet/vmnet.h>

#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) {
Expand Down

0 comments on commit 393d10d

Please sign in to comment.