Skip to content

Commit 395b39d

Browse files
committed
get grpc logging working
1 parent 4a06676 commit 395b39d

File tree

20 files changed

+379
-27
lines changed

20 files changed

+379
-27
lines changed

src/viam/examples/modules/complex/client.cpp

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,27 +27,36 @@
2727
using namespace viam::sdk;
2828

2929
int main() {
30-
const char* uri = "http://localhost:8080/"; // replace with your URI if connecting securely
31-
DialOptions dial_options;
32-
dial_options.set_allow_insecure_downgrade(true); // set to false if connecting securely
30+
// const char* uri = "http://localhost:8080/"; // replace with your URI if connecting securely
31+
// DialOptions dial_options;
32+
// dial_options.set_allow_insecure_downgrade(true); // set to false if connecting securely
3333

3434
// Uncomment and fill out your credentials details if connecting securely
3535
// std::string type = "<your authentication type>";
3636
// std::string payload = "<your authentication payload>";
3737
// Credentials credentials(type, payload);
3838
// dial_options.set_credentials(credentials);
3939

40-
boost::optional<DialOptions> opts(dial_options);
41-
std::string address(uri);
42-
Options options(1, opts);
40+
std::string host("webrtc-test-main.jkek76kqnh.viam.cloud");
41+
DialOptions dial_opts;
42+
dial_opts.set_entity(std::string("a9dcf212-3397-4318-bb1e-f5c36b3cafc1"));
43+
Credentials credentials("api-key", "2ml60ys1j4i9v0pecpxahe654yxpc1dz");
44+
dial_opts.set_credentials(credentials);
45+
boost::optional<DialOptions> opts(dial_opts);
46+
Options options(0, opts);
47+
48+
// boost::optional<DialOptions> opts(dial_options);
49+
// std::string address(uri);
50+
// Options options(1, opts);
4351

4452
// Register custom gizmo and summation clients so robot client can access resources
4553
// of that type from the server.
4654
Registry::register_resource_client<GizmoClient>();
4755
Registry::register_resource_client<SummationClient>();
56+
std::cout << "We are here!!!!\n\n\n" << std::flush;
4857

4958
// Connect to robot.
50-
std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);
59+
std::shared_ptr<RobotClient> robot = RobotClient::at_address(host, options);
5160
// Print resources.
5261
std::cout << "Resources" << std::endl;
5362
std::vector<Name> resource_names = robot->resource_names();

src/viam/examples/modules/complex/gizmo/api.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
#include <google/protobuf/descriptor.h>
66

7+
#include <boost/log/common.hpp>
8+
#include <boost/log/sources/logger.hpp>
9+
#include <boost/log/trivial.hpp>
710
#include <viam/sdk/common/client_helper.hpp>
811
#include <viam/sdk/common/utils.hpp>
912
#include <viam/sdk/registry/registry.hpp>
@@ -24,12 +27,22 @@ API API::traits<Gizmo>::api() {
2427
return {"viam", "component", "gizmo"};
2528
}
2629

27-
Gizmo::Gizmo(std::string name) : Component(std::move(name)){};
30+
Gizmo::Gizmo(std::string name)
31+
: Component(std::move(name)) {
32+
// boost::log::sources::severity_logger<boost::log::trivial::severity_level> lg;
33+
// BOOST_LOG_SEV(lg, boost::log::trivial::severity_level::debug) << "debug test";
34+
// BOOST_LOG_SEV(lg, boost::log::trivial::severity_level::info) << "info test";
35+
// BOOST_LOG_SEV(lg, boost::log::trivial::severity_level::warning) << "warning test";
36+
// BOOST_LOG_SEV(lg, boost::log::trivial::severity_level::error) << "error test";
37+
// BOOST_LOG_TRIVIAL(error) << "gizmo created error\n";
38+
// BOOST_LOG_TRIVIAL(info) << "gizmo created info\n";
39+
// BOOST_LOG_TRIVIAL(debug) << "gizmo created debug\n";
40+
};
2841

2942
/* Gizmo server methods */
3043

3144
GizmoServer::GizmoServer(std::shared_ptr<ResourceManager> manager)
32-
: ResourceServer(std::move(manager)){};
45+
: ResourceServer(std::move(manager)) {};
3346

3447
grpc::Status GizmoServer::DoOne(grpc::ServerContext* context,
3548
const DoOneRequest* request,
@@ -170,12 +183,25 @@ grpc::Status GizmoServer::DoTwo(::grpc::ServerContext* context,
170183
/* Gizmo client methods */
171184

172185
GizmoClient::GizmoClient(std::string name, std::shared_ptr<grpc::Channel> channel)
173-
: Gizmo(std::move(name)), stub_(GizmoService::NewStub(channel)), channel_(std::move(channel)){};
186+
: Gizmo(std::move(name)),
187+
stub_(GizmoService::NewStub(channel)),
188+
channel_(std::move(channel)) {};
174189

175190
bool GizmoClient::do_one(std::string arg1) {
191+
BOOST_LOG_SEV(*(logger_.underlying()), boost::log::trivial::severity_level::error)
192+
<< "some other new error log";
193+
logger_.info("new info log");
194+
logger_.debug("new debug log");
195+
logger_.error("new error log");
176196
return make_client_helper(this, *stub_, &StubType::DoOne)
177-
.with([&](auto& request) { request.set_arg1(arg1); })
178-
.invoke([](auto& response) { return response.ret1(); });
197+
.with([&](auto& request) {
198+
std::cout << "in with\n" << std::flush;
199+
request.set_arg1(arg1);
200+
})
201+
.invoke([](auto& response) {
202+
std::cout << "in invoke\n" << std::flush;
203+
return response.ret1();
204+
});
179205
}
180206

181207
bool GizmoClient::do_one_client_stream(std::vector<std::string> arg1) {

src/viam/examples/modules/complex/gizmo/impl.cpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313

1414
using namespace viam::sdk;
1515

16-
std::string find_arg1(ResourceConfig cfg) {
17-
auto gizmo_name = cfg.name();
16+
std::string find_arg1(const ResourceConfig& cfg) {
17+
const auto& gizmo_name = cfg.name();
1818
auto arg1 = cfg.attributes().find("arg1");
1919
if (arg1 == cfg.attributes().end()) {
2020
std::ostringstream buffer;
@@ -31,11 +31,11 @@ std::string find_arg1(ResourceConfig cfg) {
3131
return *arg1_string;
3232
}
3333

34-
void MyGizmo::reconfigure(const Dependencies& deps, const ResourceConfig& cfg) {
34+
void MyGizmo::reconfigure(const Dependencies&, const ResourceConfig& cfg) {
3535
arg1_ = find_arg1(cfg);
3636
}
3737

38-
std::vector<std::string> MyGizmo::validate(ResourceConfig cfg) {
38+
std::vector<std::string> MyGizmo::validate(const ResourceConfig& cfg) {
3939
// Custom validation can be done by specifying a validate function at the
4040
// time of resource registration (see complex/main.cpp) like this one.
4141
// Validate functions can `throw` exceptions that will be returned to the
@@ -50,6 +50,14 @@ std::vector<std::string> MyGizmo::validate(ResourceConfig cfg) {
5050
}
5151

5252
bool MyGizmo::do_one(std::string arg1) {
53+
std::cout << "it's a myGizmo!";
54+
// CR erodkin: these logs break things when init_logging_() is called. figure out why!
55+
// also we're now getting an error when stopping the viam-server that the mod manager didn't
56+
// close properly so probably we should figure that out too
57+
// logger_.error("new error log in do_one");
58+
logger_.debug("new debug log in do_one");
59+
std::cout << "did a debug log now let's try an info one \n" << std::flush;
60+
logger_.info("new info log in do_one");
5361
return arg1_ == arg1;
5462
}
5563

src/viam/examples/modules/complex/gizmo/impl.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ using namespace viam::sdk;
1414
// `validate` method that checks config validity.
1515
class MyGizmo : public Gizmo, public Reconfigurable {
1616
public:
17-
MyGizmo(std::string name, std::string arg1) : Gizmo(std::move(name)), arg1_(std::move(arg1)){};
17+
MyGizmo(std::string name, std::string arg1) : Gizmo(std::move(name)), arg1_(std::move(arg1)) {};
1818
MyGizmo(const Dependencies& deps, const ResourceConfig& cfg) : Gizmo(cfg.name()) {
1919
this->reconfigure(deps, cfg);
2020
};
2121
void reconfigure(const Dependencies& deps, const ResourceConfig& cfg) override;
22-
static std::vector<std::string> validate(ResourceConfig cfg);
22+
static std::vector<std::string> validate(const ResourceConfig& cfg);
2323

2424
bool do_one(std::string arg1) override;
2525
bool do_one_client_stream(std::vector<std::string> arg1) override;

src/viam/examples/modules/complex/main.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ using namespace viam::sdk;
3030
int main(int argc, char** argv) {
3131
Model mybase_model("viam", "base", "mybase");
3232

33+
std::cout << "registering resource servers\n" << std::flush;
34+
3335
// Make sure to explicity register resources with custom APIs.
3436
Registry::register_resource_server<GizmoServer>();
3537
Registry::register_resource_server<SummationServer>();
@@ -56,6 +58,7 @@ int main(int argc, char** argv) {
5658

5759
std::vector<std::shared_ptr<ModelRegistration>> mrs = {mybase_mr, mygizmo_mr, mysummation_mr};
5860
auto my_mod = std::make_shared<ModuleService>(argc, argv, mrs);
61+
std::cout << "we have created the module and gotten ready to serve\n" << std::flush;
5962
my_mod->serve();
6063

6164
return EXIT_SUCCESS;

src/viam/sdk/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ target_sources(viamsdk
4949
common/client_helper.cpp
5050
common/exception.cpp
5151
common/linear_algebra.cpp
52+
common/logger.cpp
5253
common/pose.cpp
5354
common/proto_value.cpp
5455
common/service_helper.cpp
@@ -137,6 +138,7 @@ target_sources(viamsdk
137138
../../viam/sdk/common/client_helper.hpp
138139
../../viam/sdk/common/exception.hpp
139140
../../viam/sdk/common/linear_algebra.hpp
141+
../../viam/sdk/common/logger.hpp
140142
../../viam/sdk/common/pose.hpp
141143
../../viam/sdk/common/proto_value.hpp
142144
../../viam/sdk/common/service_helper.hpp

src/viam/sdk/common/client_helper.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ class ClientHelper {
3535
static void default_rsc_(RequestType&) {}
3636
static void default_rhc_(const ResponseType&) {}
3737
static void default_ehc_(const ::grpc::Status& status) {
38+
std::cout << "error is " << status.error_details() << " " << status.error_code() << " "
39+
<< status.error_message() << "\n"
40+
<< std::flush;
3841
throw GRPCException(status);
3942
}
4043

@@ -76,8 +79,11 @@ class ClientHelper {
7679
if (debug_key_ != "") {
7780
ctx.set_debug_key(debug_key_);
7881
}
82+
std::cout << "getting result\n" << std::flush;
7983
const auto result = (stub_->*pfn_)(ctx, request_, &response_);
84+
std::cout << "got result\n" << std::flush;
8085
if (result.ok()) {
86+
std::cout << "got bad result\n" << std::flush;
8187
return std::forward<ResponseHandlerCallable>(rhc)(
8288
const_cast<const ResponseType&>(response_));
8389
}

src/viam/sdk/common/logger.cpp

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#include <viam/sdk/common/logger.hpp>
2+
3+
#include <iostream>
4+
5+
#include <boost/log/core.hpp>
6+
#include <boost/log/trivial.hpp>
7+
8+
namespace viam {
9+
namespace sdk {
10+
11+
namespace logging = boost::log;
12+
using ll = Logger::log_level;
13+
using bll = logging::trivial::severity_level;
14+
15+
struct Logger::impl {
16+
impl()
17+
: logger_(std::make_shared<logging::sources::severity_logger<bll>>()), level_(ll::info) {}
18+
impl(ll level)
19+
: logger_(std::make_shared<logging::sources::severity_logger<bll>>()), level_(level) {}
20+
21+
std::shared_ptr<logging::sources::severity_logger<bll>> logger_;
22+
ll level_;
23+
};
24+
25+
std::shared_ptr<logging::sources::severity_logger<bll>> Logger::underlying() {
26+
return impl_->logger_;
27+
}
28+
29+
Logger::Logger(std::string name)
30+
: name_(std::move(name)), level_(log_level::info), impl_(std::make_unique<impl>()) {};
31+
Logger::Logger(std::string name, log_level level)
32+
: name_(std::move(name)), level_(level), impl_(std::make_unique<impl>(level)) {};
33+
34+
logging::trivial::severity_level _log_level_to_severity_level(Logger::log_level level) {
35+
switch (level) {
36+
case ll::error: {
37+
return bll::error;
38+
}
39+
case ll::warning: {
40+
return bll::warning;
41+
}
42+
case ll::debug: {
43+
return bll::debug;
44+
}
45+
case ll::fatal: {
46+
return bll::fatal;
47+
}
48+
case ll::trace: {
49+
return bll::trace;
50+
}
51+
case ll::info: // fallthrough
52+
default: {
53+
return bll::info;
54+
}
55+
}
56+
}
57+
58+
void Logger::log(const std::string& log, log_level level) const {
59+
if (impl_->level_ > level) {
60+
return;
61+
}
62+
BOOST_LOG_SEV(*(impl_->logger_), _log_level_to_severity_level(level)) << log;
63+
}
64+
void Logger::debug(const std::string& log) const {
65+
this->log(log, ll::debug);
66+
}
67+
void Logger::error(const std::string& log) const {
68+
this->log(log, ll::error);
69+
}
70+
void Logger::fatal(const std::string& log) const {
71+
this->log(log, ll::fatal);
72+
}
73+
void Logger::info(const std::string& log) const {
74+
this->log(log, ll::info);
75+
}
76+
void Logger::trace(const std::string& log) const {
77+
this->log(log, ll::trace);
78+
}
79+
void Logger::warning(const std::string& log) const {
80+
this->log(log, ll::warning);
81+
}
82+
83+
Logger::~Logger() = default;
84+
85+
} // namespace sdk
86+
} // namespace viam

src/viam/sdk/common/logger.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#pragma once
2+
3+
#include <boost/log/core.hpp>
4+
#include <boost/log/trivial.hpp>
5+
#include <cstdint>
6+
#include <string>
7+
8+
namespace viam {
9+
namespace sdk {
10+
11+
class Resource;
12+
13+
class Logger {
14+
public:
15+
~Logger();
16+
enum class log_level : std::int8_t {
17+
trace = -2,
18+
debug = -1,
19+
info = 0, // default value is info
20+
warning = 1,
21+
error = 2,
22+
fatal = 3,
23+
};
24+
25+
explicit Logger(std::string name);
26+
explicit Logger(std::string name, log_level level);
27+
28+
void log(const std::string& log, log_level level) const;
29+
void debug(const std::string& log) const;
30+
void error(const std::string& log) const;
31+
void fatal(const std::string& log) const;
32+
void info(const std::string& log) const;
33+
void trace(const std::string& log) const;
34+
void warning(const std::string& log) const;
35+
36+
void set_log_level(log_level level);
37+
38+
std::shared_ptr<boost::log::sources::severity_logger<boost::log::trivial::severity_level>>
39+
underlying();
40+
41+
private:
42+
std::string name_;
43+
log_level level_;
44+
struct impl;
45+
std::unique_ptr<impl> impl_;
46+
};
47+
48+
} // namespace sdk
49+
} // namespace viam

src/viam/sdk/common/utils.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#include <chrono>
12
#include <viam/sdk/common/utils.hpp>
23

34
#include <random>
@@ -40,6 +41,17 @@ time_point timestamp_to_time_pt(const google::protobuf::Timestamp& timestamp) {
4041
nanos);
4142
}
4243

44+
google::protobuf::Timestamp time_pt_to_timestamp(
45+
const std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>& time_pt) {
46+
const std::chrono::seconds duration_s =
47+
std::chrono::duration_cast<std::chrono::seconds>(time_pt.time_since_epoch());
48+
const std::chrono::nanoseconds duration_ns = time_pt.time_since_epoch() - duration_s;
49+
google::protobuf::Timestamp timestamp;
50+
timestamp.set_seconds(duration_s.count());
51+
timestamp.set_nanos(static_cast<int32_t>(duration_ns.count()));
52+
return timestamp;
53+
}
54+
4355
google::protobuf::Timestamp time_pt_to_timestamp(const time_point& time_pt) {
4456
const std::chrono::seconds duration_s =
4557
std::chrono::duration_cast<std::chrono::seconds>(time_pt.time_since_epoch());

0 commit comments

Comments
 (0)