Skip to content

Commit e0217dc

Browse files
committed
Debug options variables converted to a structure DebugOptions
1 parent 96f9557 commit e0217dc

5 files changed

Lines changed: 39 additions & 24 deletions

File tree

src/flags.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,12 @@ Options options_init(void)
1818
return o;
1919
}
2020

21-
Options opt;
21+
DebugOptions debug_options_init(void)
22+
{
23+
DebugOptions o = { 0 };
24+
25+
return o;
26+
}
27+
28+
Options opt;
29+
DebugOptions dbg_opt;

src/flags.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,19 @@ typedef struct {
5656
int icmp_request_typ;
5757
} Options;
5858

59+
typedef struct {
60+
/* debug switches */
61+
int trace_on;
62+
int randomly_lose_on;
63+
int print_per_system_on;
64+
int lose_factor;
65+
} DebugOptions;
66+
67+
5968
Options options_init(void);
69+
DebugOptions debug_options_init(void);
6070

6171
extern Options opt;
72+
extern DebugOptions dbg_opt;
6273

6374
#endif

src/fping.c

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -333,12 +333,6 @@ int64_t end_time;
333333
int64_t last_send_time; /* time last ping was sent */
334334
int64_t next_report_time; /* time next -Q report is expected */
335335

336-
337-
#if defined(DEBUG) || defined(_DEBUG)
338-
int opt_debug_randomly_lose_on, opt_debug_trace_on, opt_debug_print_per_system_on;
339-
int lose_factor;
340-
#endif /* DEBUG || _DEBUG */
341-
342336
unsigned int fwmark = 0;
343337

344338
char *filename = NULL; /* file containing hosts to ping */
@@ -427,11 +421,13 @@ double strtod_strict(const char *arg)
427421

428422
int main(int argc, char **argv)
429423
{
430-
/* Debug: CPU Performance */
424+
/* Debug: debug_options_init() and CPU Performance */
431425
#if defined(DEBUG) || defined(_DEBUG)
432426
clock_t perf_cpu_start, perf_cpu_end;
433427
double perf_cpu_time_used;
434428
perf_cpu_start = clock();
429+
430+
dbg_opt = debug_options_init();
435431
#endif /* DEBUG || _DEBUG */
436432

437433
int c;
@@ -1032,25 +1028,25 @@ int main(int argc, char **argv)
10321028

10331029
#if defined(DEBUG) || defined(_DEBUG)
10341030
if (debugging & DBG_TRACE)
1035-
opt_debug_trace_on = 1;
1031+
dbg_opt.trace_on = 1;
10361032

10371033
if (debugging & DBG_RANDOM_LOSE_FEW) {
1038-
opt_debug_randomly_lose_on = 1;
1039-
lose_factor = 1; /* ie, 1/4 */
1034+
dbg_opt.randomly_lose_on = 1;
1035+
dbg_opt.lose_factor = 1; /* ie, 1/4 */
10401036
}
10411037

10421038
if (debugging & DBG_RANDOM_LOSE_MANY) {
1043-
opt_debug_randomly_lose_on = 1;
1044-
lose_factor = 5; /* ie, 3/4 */
1039+
dbg_opt.randomly_lose_on = 1;
1040+
dbg_opt.lose_factor = 5; /* ie, 3/4 */
10451041
}
10461042

10471043
if (debugging & DBG_PRINT_PER_SYSTEM)
1048-
opt_debug_print_per_system_on = 1;
1044+
dbg_opt.print_per_system_on = 1;
10491045

10501046
if ((debugging & DBG_REPORT_ALL_RTTS) && !opt.loop_on)
10511047
opt.report_all_rtts_on = 1;
10521048

1053-
if (opt_debug_trace_on) {
1049+
if (dbg_opt.trace_on) {
10541050
fprintf(stderr, "%s:\n opt.count: %u, opt.retry: %u, opt.interval: %.0f ms\n",
10551051
prog, opt.count, opt.retry, opt.interval / 1e6);
10561052
fprintf(stderr, " opt.perhost_interval: %.0f ms, opt.timeout: %.0f\n",
@@ -1087,10 +1083,10 @@ int main(int argc, char **argv)
10871083
fprintf(stderr, " opt.per_recv_on set\n");
10881084
if (opt.report_all_rtts_on)
10891085
fprintf(stderr, " opt.report_all_rtts_on set\n");
1090-
if (opt_debug_randomly_lose_on)
1091-
fprintf(stderr, " opt_debug_randomly_lose_on set\n");
1092-
if (opt_debug_print_per_system_on)
1093-
fprintf(stderr, " opt_debug_print_per_system_on set\n");
1086+
if (dbg_opt.randomly_lose_on)
1087+
fprintf(stderr, " dbg_opt.randomly_lose_on set\n");
1088+
if (dbg_opt.print_per_system_on)
1089+
fprintf(stderr, " dbg_opt.print_per_system_on set\n");
10941090
if (opt.outage_on)
10951091
fprintf(stderr, " opt.outage_on set\n");
10961092
if (opt.print_netdata_on)
@@ -1895,7 +1891,7 @@ void finish()
18951891
print_per_system_stats();
18961892
}
18971893
#if defined(DEBUG) || defined(_DEBUG)
1898-
else if (opt_debug_print_per_system_on) {
1894+
else if (dbg_opt.print_per_system_on) {
18991895
if (opt.print_json_on)
19001896
print_per_system_stats_json();
19011897
else
@@ -2157,8 +2153,8 @@ int receive_packet(int64_t wait_time,
21572153
}
21582154

21592155
#if defined(DEBUG) || defined(_DEBUG)
2160-
if (opt_debug_randomly_lose_on) {
2161-
if ((random() & 0x07) <= lose_factor)
2156+
if (dbg_opt.randomly_lose_on) {
2157+
if ((random() & 0x07) <= dbg_opt.lose_factor)
21622158
return 0;
21632159
}
21642160
#endif

src/fping.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ extern int64_t max_reply, min_reply, total_replies, sum_replies;
8080

8181
/* this requires variadic macros, part of C99 */
8282
#if (defined(DEBUG) || defined(_DEBUG))
83-
extern int opt_debug_trace_on;
84-
#define dbg_printf(fmt, ...) do { if (opt_debug_trace_on) { fprintf(stderr, "[%10.5f] ", (double)(current_time_ns / 1000)/1000000); fprintf(stderr, fmt, __VA_ARGS__); } } while (0)
83+
#define dbg_printf(fmt, ...) do { if (dbg_opt.trace_on) { fprintf(stderr, "[%10.5f] ", (double)(current_time_ns / 1000)/1000000); fprintf(stderr, fmt, __VA_ARGS__); } } while (0)
8584

8685
#else
8786
#define dbg_printf(fmt, ...)

src/seqmap.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "seqmap.h"
4040
#include "limits.h"
4141
#include "options.h"
42+
#include "flags.h"
4243
#include "fping.h"
4344

4445
#include <inttypes.h>

0 commit comments

Comments
 (0)