Skip to content

Commit 2560dfb

Browse files
lia-viamstuqdog
andauthored
RSDK-5986: Resource-level logging for the C++ SDK (#383)
Co-authored-by: Ethan <[email protected]>
1 parent 83c031b commit 2560dfb

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+1057
-355
lines changed

.github/workflows/test.yml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ jobs:
1414
runs-on: ubuntu-latest
1515
container: ghcr.io/viamrobotics/cpp-base:bullseye-amd64
1616
strategy:
17+
fail-fast: false
1718
matrix:
1819
include:
1920
- BUILD_SHARED: ON

CMakeLists.txt

+12-1
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,18 @@ if (VIAMCPPSDK_BUILD_TESTS)
345345
set(VIAMCPPSDK_BOOST_TEST "unit_test_framework")
346346
endif()
347347

348-
find_package(Boost ${VIAMCPPSDK_BOOST_VERSION_MINIMUM} REQUIRED COMPONENTS headers log program_options ${VIAMCPPSDK_BOOST_TEST})
348+
if (VIAMCPPSDK_BUILD_EXAMPLES)
349+
set(VIAMCPPSDK_BOOST_PROGRAM_OPTIONS "program_options")
350+
endif()
351+
352+
find_package(Boost ${VIAMCPPSDK_BOOST_VERSION_MINIMUM} REQUIRED
353+
COMPONENTS
354+
headers
355+
log
356+
log_setup
357+
${VIAMCPPSDK_BOOST_PROGRAM_OPTIONS}
358+
${VIAMCPPSDK_BOOST_TEST}
359+
)
349360

350361
# Time to find `protobuf` and `gRPC[++]`. Normally this would just be
351362
# something like `find_package(gRPC <version> CONFIG REQUIRED)`, and

README.md

+9
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,15 @@ above) should resolve them. And please file a bug report! We will
8888
endeavor to be as responsive as possible, and resolve issues as
8989
quickly as possible.
9090

91+
## A note on logging
92+
93+
Users should only interact with logging via the macros, classes, and functions in
94+
[`viam/sdk/log/logging.hpp`](src/viam/sdk/log/logging.hpp). Logging is
95+
implemented using Boost.Log, but this is an implementation detail subject
96+
to change without warning. In particular, using Boost.Log macros such as
97+
`BOOST_LOG_TRIVIAL` or `BOOST_LOG_SEV` is undefined behavior which will likely
98+
fail to output log messages.
99+
91100
## License
92101
Copyright 2022 Viam Inc.
93102

etc/docker/tests/run_test.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ popd
3434
if [ ${BUILD_SHARED} = "ON" ]
3535
then
3636
pushd pkg-config
37-
PKG_CONFIG_PATH=${INSTALL_DIR}/lib/pkgconfig make all
37+
PKG_CONFIG_PATH=${INSTALL_DIR}/lib/pkgconfig CXXFLAGS="-DBOOST_LOG_DYN_LINK" make all
3838
run_module
3939
popd
4040
fi

src/viam/config/viam-cpp-sdkConfig.cmake.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
include(CMakeFindDependencyMacro)
44

5-
find_dependency(Boost @Boost_VERSION_MAJOR@.@Boost_VERSION_MINOR@ COMPONENTS headers log)
5+
find_dependency(Boost @Boost_VERSION_MAJOR@.@Boost_VERSION_MINOR@ COMPONENTS headers log log_setup)
66

77
if (@gRPC_FOUND@)
88
find_dependency(gRPC @gRPC_VERSION_MAJOR@.@gRPC_VERSION_MINOR@ CONFIG)

src/viam/examples/camera/example_camera.cpp

+69-86
Original file line numberDiff line numberDiff line change
@@ -4,97 +4,80 @@
44

55
#include <viam/sdk/common/instance.hpp>
66
#include <viam/sdk/components/camera.hpp>
7+
#include <viam/sdk/log/logging.hpp>
78
#include <viam/sdk/robot/client.hpp>
89
#include <viam/sdk/rpc/dial.hpp>
910

10-
int main() {
11-
using std::cerr;
12-
using std::cout;
13-
using std::endl;
11+
int main() try {
1412
namespace vs = ::viam::sdk;
15-
try {
16-
// Every Viam C++ SDK program must have one and only one Instance object which is created
17-
// before
18-
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
19-
vs::Instance inst;
20-
21-
// If you want to connect to a remote robot, this should be the url of the robot
22-
// Ex: xxx.xxx.viam.cloud
23-
std::string robot_address("localhost:8080");
24-
// If you want to connect to a remote robot, you need some authentication secret
25-
// You can find this on app.viam.com
26-
vs::Credentials credentials("", "");
27-
28-
vs::DialOptions dial_options;
29-
30-
// If you have credentials, use this to pass them to the robot
31-
// dial_options.credentials = credentials;
32-
33-
// This is for an example. Care should be taken before exercising this option in production.
34-
dial_options.set_allow_insecure_downgrade(
35-
(credentials.type().empty() && credentials.payload().empty()));
36-
37-
// Set the refresh interval of the robot (in seconds) (0 = auto refresh) and the dial
38-
// options
39-
vs::Options options = vs::Options(1, dial_options);
40-
41-
std::shared_ptr<vs::RobotClient> robot;
42-
try {
43-
robot = vs::RobotClient::at_address(robot_address, options);
44-
cout << "Successfully connected to the robot" << endl;
45-
} catch (const std::exception& e) {
46-
cerr << "Failed to connect to the robot. Exiting." << endl;
47-
throw;
48-
}
49-
50-
std::vector<vs::Name> resource_names = robot->resource_names();
51-
52-
cout << "Resources of the robot:" << endl;
53-
for (const auto& resource : resource_names) {
54-
cout << " - " << resource.name() << " (" << resource.api().resource_subtype() << ")"
55-
<< endl;
56-
}
57-
58-
std::string camera_name("camera1");
59-
60-
cout << "Getting camera: " << camera_name << endl;
61-
std::shared_ptr<vs::Camera> camera;
62-
try {
63-
camera = robot->resource_by_name<vs::Camera>(camera_name);
64-
} catch (const std::exception& e) {
65-
cerr << "Failed to find " << camera_name << ". Exiting." << endl;
66-
throw;
67-
}
68-
vs::Camera::properties props = camera->get_properties();
69-
vs::Camera::intrinsic_parameters intrinsics = props.intrinsic_parameters;
70-
cout << "Image dimensions: " << intrinsics.width_px << " x " << intrinsics.height_px
71-
<< endl;
72-
73-
std::string output_file("img.png");
74-
std::string image_mime_type("image/png");
75-
76-
cout << "Getting image from camera " << endl;
77-
vs::Camera::raw_image img = camera->get_image(image_mime_type);
78-
cout << "Got image of mime type: " << img.mime_type << endl;
79-
80-
cout << "Getting and saving image to " << output_file << endl;
81-
std::ofstream fout;
82-
fout.open(output_file, std::ios::binary | std::ios::out);
83-
if (fout.fail()) {
84-
throw std::runtime_error("Failed to open output file " + output_file);
85-
}
86-
fout.write(reinterpret_cast<char*>(img.bytes.data()), img.bytes.size());
87-
fout.close();
88-
if (fout.fail()) {
89-
throw std::runtime_error("Failed to write and close output file " + output_file);
90-
}
91-
} catch (const std::exception& ex) {
92-
cerr << "Program failed. Exception: " << std::string(ex.what()) << endl;
93-
return EXIT_FAILURE;
94-
} catch (...) {
95-
cerr << "Program failed without exception message." << endl;
96-
return EXIT_FAILURE;
13+
// Every Viam C++ SDK program must have one and only one Instance object which is created
14+
// before
15+
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
16+
vs::Instance inst;
17+
18+
// If you want to connect to a remote robot, this should be the url of the robot
19+
// Ex: xxx.xxx.viam.cloud
20+
std::string robot_address("localhost:8080");
21+
// If you want to connect to a remote robot, you need some authentication secret
22+
// You can find this on app.viam.com
23+
vs::Credentials credentials("", "");
24+
25+
vs::DialOptions dial_options;
26+
27+
// If you have credentials, use this to pass them to the robot
28+
// dial_options.credentials = credentials;
29+
30+
// This is for an example. Care should be taken before exercising this option in production.
31+
dial_options.set_allow_insecure_downgrade(
32+
(credentials.type().empty() && credentials.payload().empty()));
33+
34+
// Set the refresh interval of the robot (in seconds) (0 = auto refresh) and the dial
35+
// options
36+
vs::Options options = vs::Options(1, dial_options);
37+
38+
std::shared_ptr<vs::RobotClient> robot = vs::RobotClient::at_address(robot_address, options);
39+
VIAM_SDK_LOG(info) << "Successfully connected to the robot";
40+
41+
std::vector<vs::Name> resource_names = robot->resource_names();
42+
43+
VIAM_SDK_LOG(info) << "Resources of the robot:";
44+
for (const auto& resource : resource_names) {
45+
VIAM_SDK_LOG(info) << " - " << resource.name() << " (" << resource.api().resource_subtype()
46+
<< ")";
9747
}
9848

49+
std::string camera_name("camera1");
50+
51+
VIAM_SDK_LOG(info) << "Getting camera: " << camera_name;
52+
std::shared_ptr<vs::Camera> camera = robot->resource_by_name<vs::Camera>(camera_name);
53+
54+
vs::Camera::properties props = camera->get_properties();
55+
vs::Camera::intrinsic_parameters intrinsics = props.intrinsic_parameters;
56+
VIAM_SDK_LOG(info) << "Image dimensions: " << intrinsics.width_px << " x "
57+
<< intrinsics.height_px;
58+
59+
std::string output_file("img.png");
60+
std::string image_mime_type("image/png");
61+
62+
VIAM_SDK_LOG(info) << "Getting image from camera ";
63+
64+
vs::Camera::raw_image img = camera->get_image(image_mime_type);
65+
66+
VIAM_SDK_LOG(info) << "Got image of mime type: " << img.mime_type;
67+
68+
VIAM_SDK_LOG(info) << "Getting and saving image to " << output_file;
69+
70+
std::ofstream fout;
71+
72+
// Operations on the ofstream will throw on failure.
73+
fout.exceptions(std::ofstream::failbit);
74+
fout.open(output_file, std::ios::binary | std::ios::out);
75+
76+
fout.write(reinterpret_cast<char*>(img.bytes.data()), img.bytes.size());
77+
fout.close();
78+
9979
return EXIT_SUCCESS;
80+
} catch (const std::exception& ex) {
81+
std::cerr << "Program failed. Exception: " << std::string(ex.what()) << "\n";
82+
return EXIT_FAILURE;
10083
}

src/viam/examples/dial/example_dial.cpp

+4-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <viam/sdk/common/instance.hpp>
1212
#include <viam/sdk/components/generic.hpp>
13+
#include <viam/sdk/log/logging.hpp>
1314
#include <viam/sdk/robot/client.hpp>
1415
#include <viam/sdk/rpc/dial.hpp>
1516

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

3637
// ensure we can query resources
3738
std::vector<Name> resource_names = robot->resource_names();
38-
std::cout << "Resources" << std::endl;
39+
VIAM_SDK_LOG(info) << "Resources:";
3940
for (const Name& resource : resource_names) {
40-
std::cout << "\t" << resource << "\n";
41+
VIAM_SDK_LOG(info) << resource;
4142
}
4243

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

4950
return EXIT_SUCCESS;

src/viam/examples/dial_api_key/example_dial_api_key.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ int main(int argc, char* argv[]) {
4949

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

5757
return EXIT_SUCCESS;

0 commit comments

Comments
 (0)