Skip to content

Commit 6b83aed

Browse files
committed
handle fallback console logging for grpc log failures
1 parent ad079c8 commit 6b83aed

File tree

3 files changed

+34
-3
lines changed

3 files changed

+34
-3
lines changed

src/viam/sdk/log/logging.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <viam/sdk/common/instance.hpp>
1414
#include <viam/sdk/common/private/instance.hpp>
15+
#include <viam/sdk/log/private/log_backend.hpp>
1516

1617
namespace viam {
1718
namespace sdk {
@@ -136,7 +137,18 @@ void LogManager::init_logging() {
136137

137138
void LogManager::disable_console_logging() {
138139
VIAM_LOG(debug) << "Disabling console logging";
139-
boost::log::core::get()->remove_sink(console_sink_);
140+
141+
// Set a filter which ignores all console logs unless they contain a console force flag which is
142+
// set to true.
143+
console_sink_->set_filter(
144+
[filter = Filter{this}](const boost::log::attribute_value_set& attrs) {
145+
auto force = attrs[impl::attr_console_force_type{}];
146+
if (force && *force) {
147+
return filter(attrs);
148+
}
149+
150+
return false;
151+
});
140152
}
141153

142154
namespace log_detail {

src/viam/sdk/log/private/log_backend.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ struct LogBackend : boost::log::sinks::basic_sink_backend<boost::log::sinks::syn
2525
RobotClient* parent;
2626
};
2727

28+
// TBD if we want to expose this to users, but for now it is an implementation detail.
29+
// Disabling console logging is implemented by changing the filter on the console sink to reject all
30+
// messages unless they have a "force" flag set to true.
31+
BOOST_LOG_ATTRIBUTE_KEYWORD_TYPE(attr_console_force, "force", bool);
32+
2833
} // namespace impl
2934
} // namespace sdk
3035
} // namespace viam

src/viam/sdk/robot/client.cpp

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,15 @@ void RobotClient::connect_logging() {
101101
auto& sink = impl_->log_sink;
102102
if (!sink) {
103103
sink = sdk::impl::LogBackend::create(this);
104-
sink->set_filter(LogManager::Filter{&LogManager::get()});
104+
sink->set_filter([filter = LogManager::Filter{&LogManager::get()}](
105+
const boost::log::attribute_value_set& attrs) {
106+
auto force = attrs[sdk::impl::attr_console_force_type{}];
107+
if (force && *force) {
108+
return false;
109+
}
110+
111+
return filter(attrs);
112+
});
105113

106114
LogManager::get().disable_console_logging();
107115
boost::log::core::get()->add_sink(sink);
@@ -270,7 +278,13 @@ void RobotClient::log(const std::string& name,
270278
robot::v1::LogResponse resp;
271279
ClientContext ctx;
272280
const auto response = impl_->stub_->Log(ctx, req, &resp);
273-
(void)response;
281+
if (is_error_response(response)) {
282+
// Manually override to force this to get logged to console so we don't set off an infinite
283+
// loop
284+
VIAM_LOG(error) << boost::log::add_value(sdk::impl::attr_console_force_type{}, true)
285+
<< "Error sending log message over grpc: " << response.error_message()
286+
<< response.error_details();
287+
}
274288
}
275289

276290
std::shared_ptr<RobotClient> RobotClient::with_channel(std::shared_ptr<ViamChannel> channel,

0 commit comments

Comments
 (0)