Skip to content

Commit

Permalink
refactor(shell): use fmt::print to implement macro `PRINT_AND_RETUR…
Browse files Browse the repository at this point in the history
…N_FALSE_IF_NOT` instead of `fprintf` (apache#1924)
  • Loading branch information
empiredan authored Feb 29, 2024
1 parent 9f19d48 commit 4a05fae
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 24 deletions.
8 changes: 4 additions & 4 deletions src/shell/command_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,10 @@ DEFINE_TASK_CODE(LPC_SCAN_DATA, TASK_PRIORITY_COMMON, ::dsn::THREAD_POOL_DEFAULT
DEFINE_TASK_CODE(LPC_GET_METRICS, TASK_PRIORITY_COMMON, ::dsn::THREAD_POOL_DEFAULT)

#define RETURN_FALSE_IF_SAMPLE_INTERVAL_MS_INVALID() \
verify_logged(dsn::buf2uint32(optarg, sample_interval_ms), \
"parse sample_interval_ms(%s) failed\n", \
optarg); \
verify_logged(sample_interval_ms > 0, "sample_interval_ms should be > 0\n")
PRINT_AND_RETURN_FALSE_IF_NOT(dsn::buf2uint32(optarg, sample_interval_ms), \
"parse sample_interval_ms({}) failed\n", \
optarg); \
PRINT_AND_RETURN_FALSE_IF_NOT(sample_interval_ms > 0, "sample_interval_ms should be > 0\n")

enum scan_data_operator
{
Expand Down
7 changes: 4 additions & 3 deletions src/shell/command_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ bool validate_ip(shell_context *sc,
/*out*/ dsn::rpc_address &target_address,
/*out*/ std::string &err_info);

#define verify_logged(exp, ...) \
// Print messages to stderr and return false if `exp` is evaluated to false.
#define PRINT_AND_RETURN_FALSE_IF_NOT(exp, ...) \
do { \
if (!(exp)) { \
fprintf(stderr, __VA_ARGS__); \
if (dsn_unlikely(!(exp))) { \
fmt::print(stderr, __VA_ARGS__); \
return false; \
} \
} while (0)
Expand Down
6 changes: 3 additions & 3 deletions src/shell/commands/node_management.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,9 @@ bool ls_nodes(command_executor *e, shell_context *sc, arguments args)
s = type_from_string(dsn::replication::_node_status_VALUES_TO_NAMES,
std::string("ns_") + status,
::dsn::replication::node_status::NS_INVALID);
verify_logged(s != ::dsn::replication::node_status::NS_INVALID,
"parse %s as node_status::type failed",
status.c_str());
PRINT_AND_RETURN_FALSE_IF_NOT(s != ::dsn::replication::node_status::NS_INVALID,
"parse {} as node_status::type failed",
status);
}

std::map<dsn::rpc_address, dsn::replication::node_status::type> nodes;
Expand Down
22 changes: 11 additions & 11 deletions src/shell/commands/rebalance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,9 @@ bool set_meta_level(command_executor *e, shell_context *sc, arguments args)
l = type_from_string(_meta_function_level_VALUES_TO_NAMES,
std::string("fl_") + args.argv[1],
meta_function_level::fl_invalid);
verify_logged(l != meta_function_level::fl_invalid,
"parse %s as meta function level failed\n",
args.argv[1]);
PRINT_AND_RETURN_FALSE_IF_NOT(l != meta_function_level::fl_invalid,
"parse {} as meta function level failed\n",
args.argv[1]);

configuration_meta_control_response resp = sc->ddl_client->control_meta_function_level(l);
if (resp.err == dsn::ERR_OK) {
Expand Down Expand Up @@ -106,32 +106,32 @@ bool propose(command_executor *e, shell_context *sc, arguments args)
break;
case 'g':
ans = request.gpid.parse_from(optarg);
verify_logged(ans, "parse %s as gpid failed\n", optarg);
PRINT_AND_RETURN_FALSE_IF_NOT(ans, "parse {} as gpid failed\n", optarg);
break;
case 'p':
proposal_type += optarg;
break;
case 't':
target = dsn::rpc_address::from_host_port(optarg);
verify_logged(target, "parse %s as target_address failed\n", optarg);
PRINT_AND_RETURN_FALSE_IF_NOT(target, "parse {} as target_address failed\n", optarg);
break;
case 'n':
node = dsn::rpc_address::from_host_port(optarg);
verify_logged(node, "parse %s as node failed\n", optarg);
PRINT_AND_RETURN_FALSE_IF_NOT(node, "parse {} as node failed\n", optarg);
break;
default:
return false;
}
}

verify_logged(!target.is_invalid(), "need set target by -t\n");
verify_logged(!node.is_invalid(), "need set node by -n\n");
verify_logged(request.gpid.get_app_id() != -1, "need set gpid by -g\n");
PRINT_AND_RETURN_FALSE_IF_NOT(!target.is_invalid(), "need set target by -t\n");
PRINT_AND_RETURN_FALSE_IF_NOT(!node.is_invalid(), "need set node by -n\n");
PRINT_AND_RETURN_FALSE_IF_NOT(request.gpid.get_app_id() != -1, "need set gpid by -g\n");

config_type::type tp =
type_from_string(_config_type_VALUES_TO_NAMES, proposal_type, config_type::CT_INVALID);
verify_logged(
tp != config_type::CT_INVALID, "parse %s as config_type failed.\n", proposal_type.c_str());
PRINT_AND_RETURN_FALSE_IF_NOT(
tp != config_type::CT_INVALID, "parse {} as config_type failed.\n", proposal_type);
request.action_list = {new_proposal_action(target, node, tp)};
dsn::error_code err = sc->ddl_client->send_balancer_proposal(request);
std::cout << "send proposal response: " << err << std::endl;
Expand Down
5 changes: 2 additions & 3 deletions src/shell/commands/table_management.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ bool ls_apps(command_executor *e, shell_context *sc, arguments args)
s = type_from_string(::dsn::_app_status_VALUES_TO_NAMES,
std::string("as_") + status,
::dsn::app_status::AS_INVALID);
verify_logged(s != ::dsn::app_status::AS_INVALID,
"parse %s as app_status::type failed",
status.c_str());
PRINT_AND_RETURN_FALSE_IF_NOT(
s != ::dsn::app_status::AS_INVALID, "parse {} as app_status::type failed", status);
}
::dsn::error_code err = sc->ddl_client->list_apps(s, show_all, detailed, json, output_file);
if (err != ::dsn::ERR_OK)
Expand Down

0 comments on commit 4a05fae

Please sign in to comment.