Skip to content

RSDK-5986: Resource-level logging for the C++ SDK #383

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 103 commits into from
Apr 16, 2025
Merged
Changes from all commits
Commits
Show all changes
103 commits
Select commit Hold shift + click to select a range
31437c3
initial commit of instance object
lia-viam Feb 18, 2025
932ebaa
wip: initial commit to get sdk building with instance
lia-viam Feb 18, 2025
04d0158
add missing implementation
lia-viam Feb 18, 2025
a105cef
update examples to use instance
lia-viam Feb 18, 2025
403f316
only instance can construct registry
lia-viam Feb 18, 2025
27eeb13
make tests work with registry member approach
lia-viam Feb 21, 2025
ce2aa8c
pimpl registry and update examples
lia-viam Feb 24, 2025
e5cf6d0
add instance management infra
lia-viam Feb 25, 2025
85194da
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into regi…
lia-viam Feb 25, 2025
5024d3a
update tflite module
lia-viam Feb 25, 2025
ef8ec8e
silence spurious const warning
lia-viam Feb 25, 2025
16113fd
make method static
lia-viam Feb 25, 2025
216e5e2
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into regi…
lia-viam Feb 26, 2025
dbf1647
update cml boost links
lia-viam Feb 26, 2025
d1dffb8
add logger to resource
lia-viam Feb 27, 2025
4771390
use static current to populate registry member
lia-viam Feb 27, 2025
18d26c9
revert and adapt examples
lia-viam Feb 27, 2025
6018323
make instance more of a black box
lia-viam Feb 27, 2025
d4aeb86
Merge branch 'registry-member' into logger
lia-viam Feb 27, 2025
5535616
add impl instance to git
lia-viam Feb 27, 2025
4d2582c
Merge branch 'registry-member' into logger
lia-viam Feb 27, 2025
eeac195
Add instance comment boilerplate
lia-viam Mar 3, 2025
7560f6d
Add instance comment boilerplate
lia-viam Mar 3, 2025
af08403
connect robot client logging when parent of module service
lia-viam Mar 3, 2025
4e25d35
remove old ctor arg
lia-viam Mar 3, 2025
55baa6c
prohibit multi instance
lia-viam Mar 3, 2025
10cd711
remove initialized flag
lia-viam Mar 3, 2025
c745422
meyers singleton registry
lia-viam Mar 3, 2025
75ab450
remove registry member and fix member spacing/ordering
lia-viam Mar 3, 2025
fc21c35
remove unused include
lia-viam Mar 3, 2025
1758ee9
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into regi…
lia-viam Mar 3, 2025
cd1d8c5
Merge branch 'registry-member' of github.com:lia-viam/viam-cpp-sdk in…
lia-viam Mar 3, 2025
679f1b6
conditionally connect robot logging to backend
lia-viam Mar 3, 2025
795578c
first attempt full logger machinery
lia-viam Mar 4, 2025
1b88d1c
update log to have keywords in header
lia-viam Mar 7, 2025
5eb243b
init grpc logging in module service impl ready, and disable console l…
lia-viam Mar 7, 2025
6e311bc
remove set severity from args
lia-viam Mar 7, 2025
f84fe6e
update boost_log_trivial invocations
lia-viam Mar 7, 2025
9311990
remove now-unused boost includes and uses of boost log trivial
lia-viam Mar 7, 2025
a09901f
remove unused log trivial include
lia-viam Mar 7, 2025
f9b1cb3
readability member arrangement/spacing
lia-viam Mar 7, 2025
68add9d
readability spacing
lia-viam Mar 7, 2025
05eddd0
add from string
lia-viam Mar 7, 2025
5debae1
add log level
lia-viam Mar 7, 2025
6866567
add basic logging tests
lia-viam Mar 10, 2025
6e154ce
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into regi…
lia-viam Mar 12, 2025
f02ce0f
reorder includes
lia-viam Mar 12, 2025
e80bf80
conditionally find unit test framework and link it to tests
lia-viam Mar 12, 2025
185121a
registry get cannot create instance by default
lia-viam Mar 12, 2025
7a747ec
instance models fixture
lia-viam Mar 12, 2025
9c4dc97
create if needed instance in fixture
lia-viam Mar 12, 2025
30fe47e
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into regi…
lia-viam Mar 12, 2025
7409dc7
revert namespace ws
lia-viam Mar 12, 2025
7fafee8
Merge branch 'registry-member' into logger
lia-viam Mar 13, 2025
0dbd491
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into logger
lia-viam Mar 13, 2025
c67166b
move cout redirect to test_utils
lia-viam Mar 13, 2025
0745e16
implement set resource log level
lia-viam Mar 13, 2025
04edd0d
implement set resource log level for resource itself
lia-viam Mar 13, 2025
537ea67
test resource level logging in unit tests
lia-viam Mar 13, 2025
56e706c
set resource log level from config in start/reconfigure
lia-viam Mar 13, 2025
818825a
conditionally find program options as well
lia-viam Mar 14, 2025
6d43bbd
remove trailing semicolon
lia-viam Mar 14, 2025
de5e33a
private/friend decls
lia-viam Mar 14, 2025
a286508
docs
lia-viam Mar 14, 2025
587d1f6
Merge branch 'main' into logger
lia-viam Mar 14, 2025
e45bafc
update method name to avoid conflict
lia-viam Mar 17, 2025
14a6049
const qualify method and member ptr
lia-viam Mar 17, 2025
216df1d
silence tidy error
lia-viam Mar 17, 2025
f4b0f11
fix file logging
lia-viam Mar 17, 2025
e3e2697
handle empty string and clarify level formatting
lia-viam Mar 17, 2025
bdebbab
log the logging
lia-viam Mar 17, 2025
6755cd6
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into logger
lia-viam Mar 17, 2025
8c372cc
linter warnings...........
lia-viam Mar 17, 2025
1744939
linter.....
lia-viam Mar 17, 2025
d7842c1
add log_setup
lia-viam Mar 17, 2025
9829e44
remove sdk::
lia-viam Mar 17, 2025
e10df01
global resource is const char*
lia-viam Mar 17, 2025
192b29f
log log as debug
lia-viam Mar 17, 2025
2de9f40
fix out of date comment
lia-viam Mar 17, 2025
d681f6f
rename to log manager and global logger
lia-viam Mar 17, 2025
3b8d7dc
rename files to logging
lia-viam Mar 17, 2025
82eb403
use has_log_configuration
lia-viam Mar 18, 2025
ad079c8
unit test trim filename
lia-viam Mar 18, 2025
6b83aed
handle fallback console logging for grpc log failures
lia-viam Mar 18, 2025
f9f39a1
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into logger
lia-viam Mar 18, 2025
4eb8643
add logging to simple module example
lia-viam Mar 19, 2025
418f882
add logging to complex module example
lia-viam Mar 19, 2025
ee85b28
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into logger
lia-viam Mar 19, 2025
40b5400
use VIAM_SDK macro prefix
lia-viam Mar 20, 2025
22d29e3
disable fail-fast
lia-viam Mar 20, 2025
ba154d1
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into logger
lia-viam Apr 3, 2025
2e9d015
link boost log and log_setup in public libs
lia-viam Apr 3, 2025
b8dcf7c
set boost log dyn link in test script
lia-viam Apr 3, 2025
f6c21ff
clean up example camera and use logging instead of cout
lia-viam Apr 4, 2025
ac25b4e
update example code to use logging
lia-viam Apr 4, 2025
04c92ce
add test and use set_global_resource_name
lia-viam Apr 4, 2025
9297062
silence const warnings
lia-viam Apr 7, 2025
b1aa44b
add remark on logging enums
lia-viam Apr 16, 2025
6018b94
add explanatory comment
lia-viam Apr 16, 2025
743699d
Merge branch 'main' of github.com:viamrobotics/viam-cpp-sdk into logger
lia-viam Apr 16, 2025
bc5f165
format file/line into rdk log message
lia-viam Apr 16, 2025
82f7104
document use of boost log
lia-viam Apr 16, 2025
2fa0860
remove period
lia-viam Apr 16, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@ jobs:
runs-on: ubuntu-latest
container: ghcr.io/viamrobotics/cpp-base:bullseye-amd64
strategy:
fail-fast: false
matrix:
include:
- BUILD_SHARED: ON
13 changes: 12 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -345,7 +345,18 @@ if (VIAMCPPSDK_BUILD_TESTS)
set(VIAMCPPSDK_BOOST_TEST "unit_test_framework")
endif()

find_package(Boost ${VIAMCPPSDK_BOOST_VERSION_MINIMUM} REQUIRED COMPONENTS headers log program_options ${VIAMCPPSDK_BOOST_TEST})
if (VIAMCPPSDK_BUILD_EXAMPLES)
set(VIAMCPPSDK_BOOST_PROGRAM_OPTIONS "program_options")
endif()

find_package(Boost ${VIAMCPPSDK_BOOST_VERSION_MINIMUM} REQUIRED
COMPONENTS
headers
log
log_setup
${VIAMCPPSDK_BOOST_PROGRAM_OPTIONS}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this was a drive-by

${VIAMCPPSDK_BOOST_TEST}
)

# Time to find `protobuf` and `gRPC[++]`. Normally this would just be
# something like `find_package(gRPC <version> CONFIG REQUIRED)`, and
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -88,6 +88,15 @@ above) should resolve them. And please file a bug report! We will
endeavor to be as responsive as possible, and resolve issues as
quickly as possible.

## A note on logging

Users should only interact with logging via the macros, classes, and functions in
[`viam/sdk/log/logging.hpp`](src/viam/sdk/log/logging.hpp). Logging is
implemented using Boost.Log, but this is an implementation detail subject
to change without warning. In particular, using Boost.Log macros such as
`BOOST_LOG_TRIVIAL` or `BOOST_LOG_SEV` is undefined behavior which will likely
fail to output log messages.

## License
Copyright 2022 Viam Inc.

2 changes: 1 addition & 1 deletion etc/docker/tests/run_test.sh
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ popd
if [ ${BUILD_SHARED} = "ON" ]
then
pushd pkg-config
PKG_CONFIG_PATH=${INSTALL_DIR}/lib/pkgconfig make all
PKG_CONFIG_PATH=${INSTALL_DIR}/lib/pkgconfig CXXFLAGS="-DBOOST_LOG_DYN_LINK" make all
run_module
popd
fi
2 changes: 1 addition & 1 deletion src/viam/config/viam-cpp-sdkConfig.cmake.in
Original file line number Diff line number Diff line change
@@ -2,7 +2,7 @@

include(CMakeFindDependencyMacro)

find_dependency(Boost @Boost_VERSION_MAJOR@.@Boost_VERSION_MINOR@ COMPONENTS headers log)
find_dependency(Boost @Boost_VERSION_MAJOR@.@Boost_VERSION_MINOR@ COMPONENTS headers log log_setup)

if (@gRPC_FOUND@)
find_dependency(gRPC @gRPC_VERSION_MAJOR@.@gRPC_VERSION_MINOR@ CONFIG)
155 changes: 69 additions & 86 deletions src/viam/examples/camera/example_camera.cpp
Original file line number Diff line number Diff line change
@@ -4,97 +4,80 @@

#include <viam/sdk/common/instance.hpp>
#include <viam/sdk/components/camera.hpp>
#include <viam/sdk/log/logging.hpp>
#include <viam/sdk/robot/client.hpp>
#include <viam/sdk/rpc/dial.hpp>

int main() {
using std::cerr;
using std::cout;
using std::endl;
int main() try {
namespace vs = ::viam::sdk;
try {
// Every Viam C++ SDK program must have one and only one Instance object which is created
// before
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
vs::Instance inst;

// If you want to connect to a remote robot, this should be the url of the robot
// Ex: xxx.xxx.viam.cloud
std::string robot_address("localhost:8080");
// If you want to connect to a remote robot, you need some authentication secret
// You can find this on app.viam.com
vs::Credentials credentials("", "");

vs::DialOptions dial_options;

// If you have credentials, use this to pass them to the robot
// dial_options.credentials = credentials;

// This is for an example. Care should be taken before exercising this option in production.
dial_options.set_allow_insecure_downgrade(
(credentials.type().empty() && credentials.payload().empty()));

// Set the refresh interval of the robot (in seconds) (0 = auto refresh) and the dial
// options
vs::Options options = vs::Options(1, dial_options);

std::shared_ptr<vs::RobotClient> robot;
try {
robot = vs::RobotClient::at_address(robot_address, options);
cout << "Successfully connected to the robot" << endl;
} catch (const std::exception& e) {
cerr << "Failed to connect to the robot. Exiting." << endl;
throw;
}

std::vector<vs::Name> resource_names = robot->resource_names();

cout << "Resources of the robot:" << endl;
for (const auto& resource : resource_names) {
cout << " - " << resource.name() << " (" << resource.api().resource_subtype() << ")"
<< endl;
}

std::string camera_name("camera1");

cout << "Getting camera: " << camera_name << endl;
std::shared_ptr<vs::Camera> camera;
try {
camera = robot->resource_by_name<vs::Camera>(camera_name);
} catch (const std::exception& e) {
cerr << "Failed to find " << camera_name << ". Exiting." << endl;
throw;
}
vs::Camera::properties props = camera->get_properties();
vs::Camera::intrinsic_parameters intrinsics = props.intrinsic_parameters;
cout << "Image dimensions: " << intrinsics.width_px << " x " << intrinsics.height_px
<< endl;

std::string output_file("img.png");
std::string image_mime_type("image/png");

cout << "Getting image from camera " << endl;
vs::Camera::raw_image img = camera->get_image(image_mime_type);
cout << "Got image of mime type: " << img.mime_type << endl;

cout << "Getting and saving image to " << output_file << endl;
std::ofstream fout;
fout.open(output_file, std::ios::binary | std::ios::out);
if (fout.fail()) {
throw std::runtime_error("Failed to open output file " + output_file);
}
fout.write(reinterpret_cast<char*>(img.bytes.data()), img.bytes.size());
fout.close();
if (fout.fail()) {
throw std::runtime_error("Failed to write and close output file " + output_file);
}
} catch (const std::exception& ex) {
cerr << "Program failed. Exception: " << std::string(ex.what()) << endl;
return EXIT_FAILURE;
} catch (...) {
cerr << "Program failed without exception message." << endl;
return EXIT_FAILURE;
// Every Viam C++ SDK program must have one and only one Instance object which is created
// before
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
vs::Instance inst;

// If you want to connect to a remote robot, this should be the url of the robot
// Ex: xxx.xxx.viam.cloud
std::string robot_address("localhost:8080");
// If you want to connect to a remote robot, you need some authentication secret
// You can find this on app.viam.com
vs::Credentials credentials("", "");

vs::DialOptions dial_options;

// If you have credentials, use this to pass them to the robot
// dial_options.credentials = credentials;

// This is for an example. Care should be taken before exercising this option in production.
dial_options.set_allow_insecure_downgrade(
(credentials.type().empty() && credentials.payload().empty()));

// Set the refresh interval of the robot (in seconds) (0 = auto refresh) and the dial
// options
vs::Options options = vs::Options(1, dial_options);

std::shared_ptr<vs::RobotClient> robot = vs::RobotClient::at_address(robot_address, options);
VIAM_SDK_LOG(info) << "Successfully connected to the robot";

std::vector<vs::Name> resource_names = robot->resource_names();

VIAM_SDK_LOG(info) << "Resources of the robot:";
for (const auto& resource : resource_names) {
VIAM_SDK_LOG(info) << " - " << resource.name() << " (" << resource.api().resource_subtype()
<< ")";
}

std::string camera_name("camera1");

VIAM_SDK_LOG(info) << "Getting camera: " << camera_name;
std::shared_ptr<vs::Camera> camera = robot->resource_by_name<vs::Camera>(camera_name);

vs::Camera::properties props = camera->get_properties();
vs::Camera::intrinsic_parameters intrinsics = props.intrinsic_parameters;
VIAM_SDK_LOG(info) << "Image dimensions: " << intrinsics.width_px << " x "
<< intrinsics.height_px;

std::string output_file("img.png");
std::string image_mime_type("image/png");

VIAM_SDK_LOG(info) << "Getting image from camera ";

vs::Camera::raw_image img = camera->get_image(image_mime_type);

VIAM_SDK_LOG(info) << "Got image of mime type: " << img.mime_type;

VIAM_SDK_LOG(info) << "Getting and saving image to " << output_file;

std::ofstream fout;

// Operations on the ofstream will throw on failure.
fout.exceptions(std::ofstream::failbit);
fout.open(output_file, std::ios::binary | std::ios::out);

fout.write(reinterpret_cast<char*>(img.bytes.data()), img.bytes.size());
fout.close();

return EXIT_SUCCESS;
} catch (const std::exception& ex) {
std::cerr << "Program failed. Exception: " << std::string(ex.what()) << "\n";
return EXIT_FAILURE;
}
7 changes: 4 additions & 3 deletions src/viam/examples/dial/example_dial.cpp
Original file line number Diff line number Diff line change
@@ -10,6 +10,7 @@

#include <viam/sdk/common/instance.hpp>
#include <viam/sdk/components/generic.hpp>
#include <viam/sdk/log/logging.hpp>
#include <viam/sdk/robot/client.hpp>
#include <viam/sdk/rpc/dial.hpp>

@@ -35,15 +36,15 @@ int main() {

// ensure we can query resources
std::vector<Name> resource_names = robot->resource_names();
std::cout << "Resources" << std::endl;
VIAM_SDK_LOG(info) << "Resources:";
for (const Name& resource : resource_names) {
std::cout << "\t" << resource << "\n";
VIAM_SDK_LOG(info) << resource;
}

// ensure we can create clients to the robot
auto gc = robot->resource_by_name<GenericComponent>("generic1");
if (gc) {
std::cout << "got generic component client named " << gc->name() << std::endl;
VIAM_SDK_LOG(info) << "got generic component client named " << gc->name();
}

return EXIT_SUCCESS;
4 changes: 2 additions & 2 deletions src/viam/examples/dial_api_key/example_dial_api_key.cpp
Original file line number Diff line number Diff line change
@@ -49,9 +49,9 @@ int main(int argc, char* argv[]) {

// ensure we can query resources
std::vector<Name> resource_names = robot->resource_names();
std::cout << "Resources" << std::endl;
VIAM_SDK_LOG(info) << "Resources:";
for (const Name& resource : resource_names) {
std::cout << "\t" << resource << "\n";
VIAM_SDK_LOG(info) << resource;
}

return EXIT_SUCCESS;
Loading