Skip to content

Commit 056edfd

Browse files
authored
RSDK-6170 - add debug log support (#299)
1 parent 688d80a commit 056edfd

File tree

10 files changed

+117
-12
lines changed

10 files changed

+117
-12
lines changed

src/viam/examples/dial_api_key/example_dial_api_key.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
#include <viam/sdk/robot/service.hpp>
2323
#include <viam/sdk/rpc/dial.hpp>
2424

25-
using viam::robot::v1::Status;
2625
using namespace viam::sdk;
2726

2827
namespace po = boost::program_options;

src/viam/sdk/common/client_helper.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include <grpcpp/support/sync_stream.h>
55

66
#include <viam/sdk/common/exception.hpp>
7+
#include <viam/sdk/common/private/utils.hpp>
78
#include <viam/sdk/common/proto_value.hpp>
89
#include <viam/sdk/common/utils.hpp>
910

@@ -53,6 +54,11 @@ class ClientHelper {
5354

5455
template <typename RequestSetupCallable>
5556
ClientHelper& with(const ProtoStruct& extra, RequestSetupCallable&& rsc) {
57+
auto key = extra.find(impl::debug_map_key);
58+
if (key != extra.end()) {
59+
ProtoValue value = key->second;
60+
debug_key_ = *value.get<std::string>();
61+
}
5662
*request_.mutable_extra() = map_to_struct(extra);
5763
return with(std::forward<RequestSetupCallable>(rsc));
5864
}
@@ -67,6 +73,9 @@ class ClientHelper {
6773
*request_.mutable_name() = client_->name();
6874
ClientContext ctx;
6975

76+
if (debug_key_ != "") {
77+
ctx.set_debug_key(debug_key_);
78+
}
7079
const auto result = (stub_->*pfn_)(ctx, request_, &response_);
7180
if (result.ok()) {
7281
return std::forward<ResponseHandlerCallable>(rhc)(
@@ -112,6 +121,7 @@ class ClientHelper {
112121
private:
113122
ClientType* client_;
114123
StubType* stub_;
124+
std::string debug_key_;
115125
MethodType pfn_;
116126
RequestType request_;
117127
ResponseType response_;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#pragma once
2+
3+
namespace viam {
4+
namespace sdk {
5+
namespace impl {
6+
7+
const char debug_map_key[] = "com.viam.debug_key_internal";
8+
9+
} // namespace impl
10+
} // namespace sdk
11+
} // namespace viam

src/viam/sdk/common/utils.cpp

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <viam/sdk/common/utils.hpp>
22

3+
#include <random>
34
#include <unordered_map>
45
#include <vector>
56

@@ -12,6 +13,7 @@
1213

1314
#include <viam/api/common/v1/common.pb.h>
1415

16+
#include <viam/sdk/common/private/utils.hpp>
1517
#include <viam/sdk/common/private/version_metadata.hpp>
1618
#include <viam/sdk/components/component.hpp>
1719
#include <viam/sdk/registry/registry.hpp>
@@ -106,6 +108,55 @@ void ClientContext::set_client_ctx_authority_() {
106108
wrapped_context_.set_authority("viam-placeholder");
107109
}
108110

111+
std::string random_debug_key() {
112+
static const char alphanum[] = "abcdefghijklmnopqrstuvwxyz";
113+
static std::default_random_engine generator(
114+
std::chrono::system_clock::now().time_since_epoch().count());
115+
std::uniform_int_distribution<int> distribution(0, sizeof(alphanum) - 1);
116+
117+
std::string key;
118+
key.reserve(6);
119+
120+
for (int i = 0; i < 6; ++i) {
121+
key += alphanum[distribution(generator)];
122+
};
123+
124+
return key;
125+
}
126+
127+
ProtoStruct debug_map() {
128+
return debug_map(random_debug_key());
129+
}
130+
131+
ProtoStruct debug_map(std::string debug_key) {
132+
ProtoStruct map;
133+
map.emplace(impl::debug_map_key, ProtoValue(std::move(debug_key)));
134+
135+
return map;
136+
}
137+
138+
void add_debug_entry(ProtoStruct& map, std::string debug_key) {
139+
map.emplace(impl::debug_map_key, ProtoValue(std::move(debug_key)));
140+
}
141+
142+
void add_debug_entry(ProtoStruct& map) {
143+
add_debug_entry(map, random_debug_key());
144+
}
145+
146+
ProtoStruct with_debug_entry(ProtoStruct&& map, std::string debug_key) {
147+
add_debug_entry(map, std::move(debug_key));
148+
return map;
149+
}
150+
151+
ProtoStruct with_debug_entry(ProtoStruct&& map) {
152+
add_debug_entry(map);
153+
return map;
154+
}
155+
156+
void ClientContext::set_debug_key(const std::string& debug_key) {
157+
wrapped_context_.AddMetadata("dtname", debug_key);
158+
}
159+
109160
void ClientContext::add_viam_client_version_() {
110161
wrapped_context_.AddMetadata("viam_client", impl::k_version);
111162
}

src/viam/sdk/common/utils.hpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
#pragma once
22

3-
#include <cmath>
4-
#include <unordered_map>
5-
63
#include <boost/optional/optional.hpp>
74
#include <grpcpp/client_context.h>
85

@@ -56,13 +53,37 @@ class ClientContext {
5653
ClientContext();
5754
operator grpc::ClientContext*();
5855
operator const grpc::ClientContext*() const;
56+
void set_debug_key(const std::string& debug_key);
5957

6058
private:
6159
void set_client_ctx_authority_();
6260
void add_viam_client_version_();
6361
grpc::ClientContext wrapped_context_;
6462
};
6563

64+
/// @brief Returns a new `ProtoStruct` with a random key for server-side debug logging
65+
ProtoStruct debug_map();
66+
67+
/// @brief Returns a new `ProtoStruct` with @param debug_key for server-side debug logging
68+
/// @throws Exception if the debug_key contains invalid (e.g., uppercase) gRPC characters
69+
ProtoStruct debug_map(std::string debug_key);
70+
71+
/// @brief Adds @param debug_key for server-side debug logging to @param map
72+
/// @throws Exception if the debug_key contains invalid (e.g., uppercase) gRPC characters
73+
void add_debug_entry(ProtoStruct& map, std::string debug_key);
74+
75+
/// @brief Adds a random key to @param map for server-side debug logging
76+
void add_debug_entry(ProtoStruct& map);
77+
78+
/// @brief Adds @param debug_key for server-side debug logging to @param map
79+
/// @throws Exception if the debug_key contains invalid (e.g., uppercase) gRPC characters
80+
/// @returns the new ProtoStruct
81+
ProtoStruct with_debug_entry(ProtoStruct&& map, std::string debug_key);
82+
83+
/// @brief Adds a random key to @param map for server-side debug logging
84+
/// @returns the new ProtoStruct
85+
ProtoStruct with_debug_entry(ProtoStruct&& map);
86+
6687
/// @brief Set the boost trivial logger's severity depending on args.
6788
/// @param argc The number of args.
6889
/// @param argv The commandline arguments to parse.

src/viam/sdk/components/arm.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ class Arm : public Component, public Stoppable {
9393
/// @param extra Any additional arguments to the method.
9494
virtual std::vector<double> get_joint_positions(const ProtoStruct& extra) = 0;
9595

96+
/// @brief Move each joint on the arm to the corresponding angle specified in @param positions
97+
inline void move_to_joint_positions(const std::vector<double>& positions) {
98+
return move_to_joint_positions(positions, {});
99+
}
100+
96101
/// @brief Move each joint on the arm to the corresponding angle specified in @param positions
97102
/// @param extra Any additional arguments to the method.
98103
virtual void move_to_joint_positions(const std::vector<double>& positions,

src/viam/sdk/components/private/motor_client.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
#include <viam/sdk/components/private/motor_client.hpp>
22

3-
#include <algorithm>
43
#include <memory>
5-
#include <stdexcept>
64
#include <string>
75
#include <utility>
86

src/viam/sdk/tests/mocks/mock_motion.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
#include "viam/sdk/services/motion.hpp"
21
#include <viam/sdk/tests/mocks/mock_motion.hpp>
32

43
#include <chrono>
54

5+
#include <viam/sdk/common/private/utils.hpp>
66
#include <viam/sdk/common/utils.hpp>
7+
#include <viam/sdk/services/motion.hpp>
78
#include <viam/sdk/spatialmath/geometry.hpp>
89
#include <viam/sdk/spatialmath/orientation_types.hpp>
910
#include <viam/sdk/tests/test_utils.hpp>
@@ -65,7 +66,12 @@ std::string MockMotion::move_on_globe(
6566
pose_in_frame MockMotion::get_pose(const Name&,
6667
const std::string&,
6768
const std::vector<WorldState::transform>&,
68-
const ProtoStruct&) {
69+
const ProtoStruct& extra) {
70+
auto key = extra.find(impl::debug_map_key);
71+
if (key != extra.end()) {
72+
ProtoValue value = key->second;
73+
peek_debug_key = *value.get<std::string>();
74+
}
6975
return current_location;
7076
}
7177

src/viam/sdk/tests/mocks/mock_motion.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ class MockMotion : public sdk::Motion {
9191
std::string peek_destination_frame;
9292
double peek_heading;
9393
bool peek_stop_plan_called = false;
94+
std::string peek_debug_key;
9495
std::vector<sdk::geo_geometry> peek_obstacles;
9596
std::vector<sdk::GeometryConfig> peek_map_obstacles;
9697
std::shared_ptr<constraints> peek_constraints;

src/viam/sdk/tests/test_motion.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include <boost/test/included/unit_test.hpp>
44

55
#include <viam/sdk/common/pose.hpp>
6+
#include <viam/sdk/common/utils.hpp>
67
#include <viam/sdk/common/world_state.hpp>
78
#include <viam/sdk/services/motion.hpp>
89
#include <viam/sdk/spatialmath/geometry.hpp>
@@ -164,18 +165,20 @@ BOOST_AUTO_TEST_SUITE(test_motion_client_server)
164165

165166
BOOST_AUTO_TEST_CASE(test_move_and_get_pose) {
166167
auto mock = std::make_shared<MockMotion>("mock_motion");
167-
client_to_mock_pipeline<Motion>(mock, [](Motion& client) {
168+
client_to_mock_pipeline<Motion>(mock, [&](Motion& client) {
168169
std::string destination_frame("destination");
169170
std::vector<WorldState::transform> transforms;
170-
ProtoStruct extra = fake_map();
171-
pose_in_frame pose = client.get_pose(fake_component_name(), destination_frame, {}, extra);
171+
std::string debug_key = "debug-key";
172+
pose_in_frame pose = client.get_pose(
173+
fake_component_name(), destination_frame, {}, with_debug_entry(fake_map(), debug_key));
174+
BOOST_CHECK_EQUAL(mock->peek_debug_key, debug_key);
172175
BOOST_CHECK_EQUAL(pose, init_fake_pose());
173176

174177
auto ws = std::make_shared<WorldState>(mock_world_state());
175178
bool success = client.move(fake_pose(), fake_component_name(), ws, nullptr, fake_map());
176179
BOOST_TEST(success);
177180

178-
pose = client.get_pose(fake_component_name(), destination_frame, transforms, extra);
181+
pose = client.get_pose(fake_component_name(), destination_frame, transforms, fake_map());
179182
BOOST_CHECK_EQUAL(pose, fake_pose());
180183
});
181184
}

0 commit comments

Comments
 (0)