Skip to content

Commit abdbc35

Browse files
authored
Merge pull request #109 from AkihiroSuda/dev2
Add log.h
2 parents fac3207 + 58e3862 commit abdbc35

File tree

3 files changed

+38
-34
lines changed

3 files changed

+38
-34
lines changed

cli.c

+14-18
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <uuid/uuid.h>
1111

1212
#include "cli.h"
13+
#include "log.h"
1314

1415
#ifndef VERSION
1516
#define VERSION "UNKNOWN"
@@ -77,7 +78,7 @@ enum {
7778
struct cli_options *cli_options_parse(int argc, char *argv[]) {
7879
struct cli_options *res = calloc(1, sizeof(*res));
7980
if (res == NULL) {
80-
perror("calloc");
81+
ERRORN("calloc");
8182
exit(EXIT_FAILURE);
8283
}
8384

@@ -109,7 +110,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
109110
} else if (strcmp(optarg, "bridged") == 0) {
110111
res->vmnet_mode = VMNET_BRIDGED_MODE;
111112
} else {
112-
fprintf(stderr, "Unknown vmnet mode \"%s\"\n", optarg);
113+
ERRORF("Unknown vmnet mode \"%s\"", optarg);
113114
goto error;
114115
}
115116
break;
@@ -127,7 +128,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
127128
break;
128129
case CLI_OPT_VMNET_INTERFACE_ID:
129130
if (uuid_parse(optarg, res->vmnet_interface_id) < 0) {
130-
fprintf(stderr, "Failed to parse UUID \"%s\"\n", optarg);
131+
ERRORF("Failed to parse UUID \"%s\"", optarg);
131132
goto error;
132133
}
133134
break;
@@ -166,7 +167,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
166167
* is specified) */
167168
struct in_addr sin;
168169
if (!inet_aton(res->vmnet_gateway, &sin)) {
169-
perror("inet_aton(res->vmnet_gateway)");
170+
ERRORN("inet_aton(res->vmnet_gateway)");
170171
goto error;
171172
}
172173
uint32_t h = ntohl(sin.s_addr);
@@ -175,7 +176,7 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
175176
sin.s_addr = htonl(h);
176177
const char *end_static = inet_ntoa(sin); /* static storage, do not free */
177178
if (end_static == NULL) {
178-
perror("inet_ntoa");
179+
ERRORN("inet_ntoa");
179180
goto error;
180181
}
181182
res->vmnet_dhcp_end = strdup(end_static);
@@ -189,36 +190,31 @@ struct cli_options *cli_options_parse(int argc, char *argv[]) {
189190

190191
/* validate */
191192
if (res->vmnet_mode == VMNET_BRIDGED_MODE && res->vmnet_interface == NULL) {
192-
fprintf(
193-
stderr,
194-
"vmnet mode \"bridged\" require --vmnet-interface to be specified\n");
193+
ERROR("vmnet mode \"bridged\" require --vmnet-interface to be specified");
195194
goto error;
196195
}
197196
if (res->vmnet_gateway == NULL) {
198197
if (res->vmnet_mode != VMNET_BRIDGED_MODE) {
199-
fprintf(stderr,
200-
"WARNING: --vmnet-gateway=IP should be explicitly specified to "
201-
"avoid conflicting with other applications\n");
198+
WARN("--vmnet-gateway=IP should be explicitly specified to "
199+
"avoid conflicting with other applications");
202200
}
203201
if (res->vmnet_dhcp_end != NULL) {
204-
fprintf(stderr, "--vmnet-dhcp-end=IP requires --vmnet-gateway=IP\n");
202+
ERROR("--vmnet-dhcp-end=IP requires --vmnet-gateway=IP");
205203
goto error;
206204
}
207205
if (res->vmnet_mask != NULL) {
208-
fprintf(stderr, "--vmnet-mask=MASK requires --vmnet-gateway=IP\n");
206+
ERROR("--vmnet-mask=MASK requires --vmnet-gateway=IP");
209207
goto error;
210208
}
211209
} else {
212210
if (res->vmnet_mode == VMNET_BRIDGED_MODE) {
213-
fprintf(stderr,
214-
"vmnet mode \"bridged\" conflicts with --vmnet-gateway\n");
211+
ERROR("vmnet mode \"bridged\" conflicts with --vmnet-gateway");
215212
goto error;
216213
}
217214
struct in_addr dummy;
218215
if (!inet_aton(res->vmnet_gateway, &dummy)) {
219-
fprintf(stderr,
220-
"invalid address \"%s\" was specified for --vmnet-gateway\n",
221-
res->vmnet_gateway);
216+
ERRORF("invalid address \"%s\" was specified for --vmnet-gateway",
217+
res->vmnet_gateway);
222218
goto error;
223219
}
224220
}

log.h

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef SOCKET_VMNET_LOG_H
2+
#define SOCKET_VMNET_LOG_H
3+
#include <errno.h>
4+
5+
extern bool debug;
6+
7+
#define DEBUGF(fmt, ...) \
8+
do { \
9+
if (debug) \
10+
fprintf(stderr, "DEBUG| " fmt "\n", __VA_ARGS__); \
11+
} while (0)
12+
13+
#define INFOF(fmt, ...) fprintf(stderr, "INFO | " fmt "\n", __VA_ARGS__)
14+
#define ERROR(msg) fprintf(stderr, "ERROR| " msg "\n")
15+
#define ERRORF(fmt, ...) fprintf(stderr, "ERROR| " fmt "\n", __VA_ARGS__)
16+
#define ERRORN(name) ERRORF(name ": %s", strerror(errno))
17+
#define WARN(msg) fprintf(stderr, "WARN | " msg "\n")
18+
#define WARNF(fmt, ...) fprintf(stderr, "WARN | " fmt "\n", __VA_ARGS__)
19+
20+
#endif /* SOCKET_VMNET_LOG_H */

main.c

+4-16
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,13 @@
1313
#include <vmnet/vmnet.h>
1414

1515
#include "cli.h"
16+
#include "log.h"
1617

1718
#if __MAC_OS_X_VERSION_MAX_ALLOWED < 101500
1819
#error "Requires macOS 10.15 or later"
1920
#endif
2021

21-
static bool debug = false;
22-
23-
#define DEBUGF(fmt, ...) \
24-
do { \
25-
if (debug) \
26-
fprintf(stderr, "DEBUG| " fmt "\n", __VA_ARGS__); \
27-
} while (0)
28-
29-
#define INFOF(fmt, ...) fprintf(stderr, "INFO | " fmt "\n", __VA_ARGS__)
30-
#define ERROR(msg) fprintf(stderr, "ERROR| " msg "\n")
31-
#define ERRORF(fmt, ...) fprintf(stderr, "ERROR| " fmt "\n", __VA_ARGS__)
32-
#define ERRORN(name) ERRORF(name ": %s", strerror(errno))
22+
bool debug = false;
3323

3424
static const char *vmnet_strerror(vmnet_return_t v) {
3525
switch (v) {
@@ -410,12 +400,10 @@ int main(int argc, char *argv[]) {
410400
struct cli_options *cliopt = cli_options_parse(argc, argv);
411401
assert(cliopt != NULL);
412402
if (geteuid() != 0) {
413-
ERROR("WARNING: Running without root. This is very unlikely to "
414-
"work: See README.md");
403+
WARN("Running without root. This is very unlikely to work: See README.md");
415404
}
416405
if (geteuid() != getuid()) {
417-
ERROR("WARNING: Seems running with SETUID. This is insecure and "
418-
"highly discouraged: See README.md");
406+
WARN("Seems running with SETUID. This is insecure and highly discouraged: See README.md");
419407
}
420408

421409
if (sigsetjmp(jmpbuf, 1) != 0) {

0 commit comments

Comments
 (0)