Skip to content

Commit 9933557

Browse files
committed
add debug log support
1 parent 331fd12 commit 9933557

File tree

7 files changed

+96
-4
lines changed

7 files changed

+96
-4
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: 12 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_type.hpp>
89
#include <viam/sdk/common/utils.hpp>
910

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

5455
template <typename RequestSetupCallable>
5556
ClientHelper& with(const AttributeMap& extra, RequestSetupCallable&& rsc) {
57+
if (extra) {
58+
auto key = extra->find(impl::debug_map_key);
59+
if (key != extra->end()) {
60+
ProtoType value = *(key->second);
61+
debug_key_ = *value.get<std::string>();
62+
}
63+
}
5664
*request_.mutable_extra() = map_to_struct(extra);
5765
return with(std::forward<RequestSetupCallable>(rsc));
5866
}
@@ -67,6 +75,9 @@ class ClientHelper {
6775
*request_.mutable_name() = client_->name();
6876
ClientContext ctx;
6977

78+
if (debug_key_ != "") {
79+
ctx.set_debug_key(debug_key_);
80+
}
7081
const auto result = (stub_->*pfn_)(ctx, request_, &response_);
7182
if (result.ok()) {
7283
return std::forward<ResponseHandlerCallable>(rhc)(
@@ -112,6 +123,7 @@ class ClientHelper {
112123
private:
113124
ClientType* client_;
114125
StubType* stub_;
126+
std::string debug_key_;
115127
MethodType pfn_;
116128
RequestType request_;
117129
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: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include <viam/api/common/v1/common.pb.h>
1616

17+
#include <viam/sdk/common/private/utils.hpp>
1718
#include <viam/sdk/common/private/version_metadata.hpp>
1819
#include <viam/sdk/components/component.hpp>
1920
#include <viam/sdk/registry/registry.hpp>
@@ -108,6 +109,53 @@ void ClientContext::set_client_ctx_authority_() {
108109
wrapped_context_.set_authority("viam-placeholder");
109110
}
110111

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

src/viam/sdk/common/utils.hpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,32 @@ class ClientContext {
5858
ClientContext();
5959
operator grpc::ClientContext*();
6060
operator const grpc::ClientContext*() const;
61+
void set_debug_key(const std::string& debug_key);
6162

6263
private:
6364
void set_client_ctx_authority_();
6465
void add_viam_client_version_();
6566
grpc::ClientContext wrapped_context_;
6667
};
6768

69+
/// @brief Returns a new `AttributeMap` with a random key for server-side debug logging
70+
AttributeMap debug_map();
71+
72+
/// @brief Returns a new `AttributeMap` with @param debug_key for server-side debug logging
73+
/// @throws Exception if the debug_key contains invalid (e.g., uppercase) gRPC characters
74+
AttributeMap debug_map(std::string debug_key);
75+
76+
/// @brief Adds @param debug_key for server-side debug logging to @param map
77+
/// @throws Exception if the debug_key contains invalid (e.g., uppercase )gRPC characters
78+
AttributeMap add_debug_entry(AttributeMap&& map, std::string debug_key);
79+
80+
/// @brief Adds @param debug_key for server-side debug logging to @param map
81+
/// @throws Exception if the debug_key contains invalid (e.g., uppercase )gRPC characters
82+
AttributeMap add_debug_entry(AttributeMap map, std::string debug_key);
83+
84+
/// @brief Adds a random key to @param map for server-side debug logging
85+
AttributeMap add_debug_entry(AttributeMap&& map);
86+
6887
/// @brief Set the boost trivial logger's severity depending on args.
6988
/// @param argc The number of args.
7089
/// @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 AttributeMap& 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: 1 addition & 3 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

@@ -21,7 +19,7 @@ namespace impl {
2119
MotorClient::MotorClient(std::string name, std::shared_ptr<grpc::Channel> channel)
2220
: Motor(std::move(name)),
2321
stub_(viam::component::motor::v1::MotorService::NewStub(channel)),
24-
channel_(std::move(channel)){};
22+
channel_(std::move(channel)) {};
2523

2624
void MotorClient::set_power(double power_pct, const AttributeMap& extra) {
2725
return make_client_helper(this, *stub_, &StubType::SetPower)

0 commit comments

Comments
 (0)