Enhance logging and error handling in Controller class methods. Added checks for uninitialized SportClient and included informational logs for API responses, improving visibility into method execution and error management.

This commit is contained in:
2025-09-09 11:53:30 +08:00
parent 21c5bc2573
commit 54a9f9b544

View File

@@ -73,7 +73,14 @@ bool Controller::stop() {
bool Controller::StandUp() {
LOG_INFO("Standing up");
try {
return sc_ && sc_->StandUp() == 0;
if (!sc_) {
LOG_ERROR("SportClient not initialized");
return false;
}
int32_t result = sc_->StandUp();
LOG_INFO("StandUp API returned: " + std::to_string(result));
return result == 0;
} catch (const std::exception& e) {
LOG_ERROR("Stand up failed: " + std::string(e.what()));
return false;
@@ -83,7 +90,14 @@ bool Controller::StandUp() {
bool Controller::StandDown() {
LOG_INFO("Standing down");
try {
return sc_ && sc_->StandDown() == 0;
if (!sc_) {
LOG_ERROR("SportClient not initialized");
return false;
}
int32_t result = sc_->StandDown();
LOG_INFO("StandDown API returned: " + std::to_string(result));
return result == 0;
} catch (const std::exception& e) {
LOG_ERROR("Stand down failed: " + std::string(e.what()));
return false;
@@ -93,7 +107,14 @@ bool Controller::StandDown() {
bool Controller::Sit() {
LOG_INFO("Sitting");
try {
return sc_ && sc_->Sit() == 0;
if (!sc_) {
LOG_ERROR("SportClient not initialized");
return false;
}
int32_t result = sc_->Sit();
LOG_INFO("Sit API returned: " + std::to_string(result));
return result == 0;
} catch (const std::exception& e) {
LOG_ERROR("Sit failed: " + std::string(e.what()));
return false;
@@ -103,7 +124,14 @@ bool Controller::Sit() {
bool Controller::Damp() {
LOG_INFO("Damping");
try {
return sc_ && sc_->Damp() == 0;
if (!sc_) {
LOG_ERROR("SportClient not initialized");
return false;
}
int32_t result = sc_->Damp();
LOG_INFO("Damp API returned: " + std::to_string(result));
return result == 0;
} catch (const std::exception& e) {
LOG_ERROR("Damp failed: " + std::string(e.what()));
return false;
@@ -114,11 +142,14 @@ bool Controller::RecoveryStand() {
LOG_INFO("Executing recovery stand");
try {
if (sc_) {
int32_t result = sc_->RecoveryStand();
return result == 0;
}
if (!sc_) {
LOG_ERROR("SportClient not initialized");
return false;
}
int32_t result = sc_->RecoveryStand();
LOG_INFO("RecoveryStand API returned: " + std::to_string(result));
return result == 0;
} catch (const std::exception& e) {
LOG_ERROR("Recovery stand failed: " + std::string(e.what()));
return false;
@@ -127,7 +158,14 @@ bool Controller::RecoveryStand() {
bool Controller::StopMove() {
try {
return sc_ && sc_->StopMove() == 0;
if (!sc_) {
LOG_ERROR("SportClient not initialized");
return false;
}
int32_t result = sc_->StopMove();
LOG_INFO("StopMove API returned: " + std::to_string(result));
return result == 0;
} catch (const std::exception& e) {
LOG_ERROR("Stop move failed: " + std::string(e.what()));
return false;
@@ -136,7 +174,14 @@ bool Controller::StopMove() {
bool Controller::BalanceStand() {
try {
return sc_ && sc_->BalanceStand() == 0;
if (!sc_) {
LOG_ERROR("SportClient not initialized");
return false;
}
int32_t result = sc_->BalanceStand();
LOG_INFO("BalanceStand API returned: " + std::to_string(result));
return result == 0;
} catch (const std::exception& e) {
LOG_ERROR("Balance stand failed: " + std::string(e.what()));
return false;
@@ -145,7 +190,14 @@ bool Controller::BalanceStand() {
bool Controller::Dance1() {
try {
return sc_ && sc_->Dance1() == 0;
if (!sc_) {
LOG_ERROR("SportClient not initialized");
return false;
}
int32_t result = sc_->Dance1();
LOG_INFO("Dance1 API returned: " + std::to_string(result));
return result == 0;
} catch (const std::exception& e) {
LOG_ERROR("Dance1 failed: " + std::string(e.what()));
return false;
@@ -154,7 +206,14 @@ bool Controller::Dance1() {
bool Controller::Dance2() {
try {
return sc_ && sc_->Dance2() == 0;
if (!sc_) {
LOG_ERROR("SportClient not initialized");
return false;
}
int32_t result = sc_->Dance2();
LOG_INFO("Dance2 API returned: " + std::to_string(result));
return result == 0;
} catch (const std::exception& e) {
LOG_ERROR("Dance2 failed: " + std::string(e.what()));
return false;
@@ -163,7 +222,14 @@ bool Controller::Dance2() {
bool Controller::Hello() {
try {
return sc_ && sc_->Hello() == 0;
if (!sc_) {
LOG_ERROR("SportClient not initialized");
return false;
}
int32_t result = sc_->Hello();
LOG_INFO("Hello API returned: " + std::to_string(result));
return result == 0;
} catch (const std::exception& e) {
LOG_ERROR("Hello failed: " + std::string(e.what()));
return false;