This is an automated email from the ASF dual-hosted git repository.

gehafearless pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-pegasus.git


The following commit(s) were added to refs/heads/master by this push:
     new 4a05fae27 refactor(shell): use `fmt::print` to implement macro 
`PRINT_AND_RETURN_FALSE_IF_NOT` instead of `fprintf` (#1924)
4a05fae27 is described below

commit 4a05fae2779f734fd0c715abbb8ec540fe1caebc
Author: Dan Wang <[email protected]>
AuthorDate: Thu Feb 29 18:58:17 2024 +0800

    refactor(shell): use `fmt::print` to implement macro 
`PRINT_AND_RETURN_FALSE_IF_NOT` instead of `fprintf` (#1924)
---
 src/shell/command_helper.h              |  8 ++++----
 src/shell/command_utils.h               |  7 ++++---
 src/shell/commands/node_management.cpp  |  6 +++---
 src/shell/commands/rebalance.cpp        | 22 +++++++++++-----------
 src/shell/commands/table_management.cpp |  5 ++---
 5 files changed, 24 insertions(+), 24 deletions(-)

diff --git a/src/shell/command_helper.h b/src/shell/command_helper.h
index de2e8b9dc..afc2579e3 100644
--- a/src/shell/command_helper.h
+++ b/src/shell/command_helper.h
@@ -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
 {
diff --git a/src/shell/command_utils.h b/src/shell/command_utils.h
index 441a228e0..f4a126df8 100644
--- a/src/shell/command_utils.h
+++ b/src/shell/command_utils.h
@@ -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)
diff --git a/src/shell/commands/node_management.cpp 
b/src/shell/commands/node_management.cpp
index b12e66d81..b81c76f8a 100644
--- a/src/shell/commands/node_management.cpp
+++ b/src/shell/commands/node_management.cpp
@@ -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;
diff --git a/src/shell/commands/rebalance.cpp b/src/shell/commands/rebalance.cpp
index 5fb68ed8f..74c5f4349 100644
--- a/src/shell/commands/rebalance.cpp
+++ b/src/shell/commands/rebalance.cpp
@@ -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) {
@@ -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;
diff --git a/src/shell/commands/table_management.cpp 
b/src/shell/commands/table_management.cpp
index 9edb44149..8fd1e76f9 100644
--- a/src/shell/commands/table_management.cpp
+++ b/src/shell/commands/table_management.cpp
@@ -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)


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to