Skip to content

Commit ac25b4e

Browse files
committed
update example code to use logging
1 parent f6c21ff commit ac25b4e

File tree

7 files changed

+126
-135
lines changed

7 files changed

+126
-135
lines changed

src/viam/examples/dial/example_dial.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <viam/sdk/common/instance.hpp>
1313
#include <viam/sdk/components/generic.hpp>
14+
#include <viam/sdk/log/logging.hpp>
1415
#include <viam/sdk/robot/client.hpp>
1516
#include <viam/sdk/rpc/dial.hpp>
1617

@@ -36,15 +37,15 @@ int main() {
3637

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

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

5051
return EXIT_SUCCESS;

src/viam/examples/dial_api_key/example_dial_api_key.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ int main(int argc, char* argv[]) {
5050

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

5858
return EXIT_SUCCESS;

src/viam/examples/mlmodel/example_audio_classification_client.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -401,14 +401,11 @@ int main(int argc, char* argv[]) try {
401401
return EXIT_SUCCESS;
402402
}
403403
} catch (const std::exception& ex) {
404-
std::cout << argv[0] << ": "
404+
std::cerr << argv[0] << ": "
405405
<< "Failed: a std::exception was thrown: `" << ex.what() << "``" << std::endl;
406406
return EXIT_FAILURE;
407-
} catch (const std::string& ex) {
408-
std::cout << argv[0] << ": "
409-
<< "Failed: a std::string was thrown: `" << ex << "``" << std::endl;
410407
} catch (...) {
411-
std::cout << argv[0] << ": "
408+
std::cerr << argv[0] << ": "
412409
<< "Failed: an unknown exception was thrown" << std::endl;
413410
return EXIT_FAILURE;
414411
}

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

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
#include <viam/sdk/common/instance.hpp>
1515
#include <viam/sdk/components/motor.hpp>
16+
#include <viam/sdk/log/logging.hpp>
1617
#include <viam/sdk/robot/client.hpp>
1718
#include <viam/sdk/rpc/dial.hpp>
1819

@@ -48,54 +49,61 @@ int main() {
4849
// Connect to robot.
4950
std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);
5051
// Print resources.
51-
std::cout << "Resources" << std::endl;
52+
VIAM_SDK_LOG(info) << "Resources:";
5253
std::vector<Name> resource_names = robot->resource_names();
5354
for (const Name& resource : resource_names) {
54-
std::cout << "\t" << resource << "\n";
55+
VIAM_SDK_LOG(info) << resource;
5556
}
5657

5758
// Exercise Gizmo methods.
5859
auto gc = robot->resource_by_name<Gizmo>("gizmo1");
5960
if (!gc) {
60-
std::cerr << "could not get 'gizmo1' resource from robot" << std::endl;
61+
VIAM_SDK_LOG(error) << "could not get 'gizmo1' resource from robot" << std::endl;
6162
return EXIT_FAILURE;
6263
}
64+
6365
bool do_one_ret = gc->do_one("arg1");
64-
std::cout << "gizmo1 do_one returned: " << do_one_ret << std::endl;
66+
VIAM_SDK_LOG(info) << "gizmo1 do_one returned: " << do_one_ret;
67+
6568
bool do_one_client_stream_ret = gc->do_one_client_stream({"arg1", "arg1", "arg1"});
66-
std::cout << "gizmo1 do_one_client_stream returned: " << do_one_client_stream_ret << std::endl;
69+
VIAM_SDK_LOG(info) << "gizmo1 do_one_client_stream returned: " << do_one_client_stream_ret;
70+
6771
std::string do_two_ret = gc->do_two(false);
68-
std::cout << "gizmo1 do_two returned: " << do_two_ret << std::endl;
72+
VIAM_SDK_LOG(info) << "gizmo1 do_two returned: " << do_two_ret;
73+
6974
std::vector<bool> do_one_server_stream_ret = gc->do_one_server_stream("arg1");
70-
std::cout << "gizmo1 do_one_server_stream returned: " << std::endl;
75+
VIAM_SDK_LOG(info) << "gizmo1 do_one_server_stream returned: ";
7176
for (bool ret : do_one_server_stream_ret) {
72-
std::cout << '\t' << ret << std::endl;
77+
VIAM_SDK_LOG(info) << ret;
7378
}
79+
7480
std::vector<bool> do_one_bidi_stream_ret = gc->do_one_bidi_stream({"arg1", "arg2", "arg3"});
75-
std::cout << "gizmo1 do_one_bidi_stream returned: " << std::endl;
81+
VIAM_SDK_LOG(info) << "gizmo1 do_one_bidi_stream returned: ";
7682
for (bool ret : do_one_bidi_stream_ret) {
77-
std::cout << '\t' << ret << std::endl;
83+
VIAM_SDK_LOG(info) << ret;
7884
}
7985

8086
// Exercise Summation methods.
8187
auto sc = robot->resource_by_name<Summation>("mysum1");
8288
if (!sc) {
83-
std::cerr << "could not get 'mysum1' resource from robot" << std::endl;
89+
VIAM_SDK_LOG(error) << "could not get 'mysum1' resource from robot";
8490
return EXIT_FAILURE;
8591
}
92+
8693
double sum = sc->sum({0, 1, 2, 3, 4, 5, 6, 7, 8, 9});
87-
std::cout << "mysum1 sum of numbers [0, 10) is: " << sum << std::endl;
94+
VIAM_SDK_LOG(info) << "mysum1 sum of numbers [0, 10) is: " << sum;
8895

8996
// Exercise a Base method.
9097
auto mc = robot->resource_by_name<Motor>("motor1");
9198
if (!mc) {
92-
std::cerr << "could not get 'motor1' resource from robot" << std::endl;
99+
VIAM_SDK_LOG(error) << "could not get 'motor1' resource from robot";
93100
return EXIT_FAILURE;
94101
}
102+
95103
if (mc->is_moving()) {
96-
std::cout << "motor1 is moving" << std::endl;
104+
VIAM_SDK_LOG(info) << "motor1 is moving";
97105
} else {
98-
std::cout << "motor1 is not moving" << std::endl;
106+
VIAM_SDK_LOG(info) << "motor1 is not moving";
99107
}
100108

101109
return EXIT_SUCCESS;

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <viam/sdk/common/instance.hpp>
66
#include <viam/sdk/common/proto_value.hpp>
77
#include <viam/sdk/components/sensor.hpp>
8+
#include <viam/sdk/log/logging.hpp>
89
#include <viam/sdk/robot/client.hpp>
910
#include <viam/sdk/rpc/dial.hpp>
1011

@@ -32,40 +33,40 @@ int main() {
3233
std::shared_ptr<RobotClient> robot = RobotClient::at_address(address, options);
3334

3435
// Print resources
35-
std::cout << "Resources\n";
36+
VIAM_SDK_LOG(info) << "Resources";
3637
std::vector<Name> resource_names = robot->resource_names();
3738
for (const Name& resource : resource_names) {
38-
std::cout << "\t" << resource << "\n";
39+
VIAM_SDK_LOG(info) << resource;
3940
}
4041

4142
// Exercise sensor methods
4243
auto sensor = robot->resource_by_name<Sensor>("mysensor");
4344
if (!sensor) {
44-
std::cerr << "could not get 'mysensor' resource from robot\n";
45+
VIAM_SDK_LOG(error) << "could not get 'mysensor' resource from robot";
4546
return EXIT_FAILURE;
4647
}
4748

4849
ProtoStruct command{{"hello", "world"}};
4950
ProtoStruct resp = sensor->do_command(command);
5051

5152
if (command != resp) {
52-
std::cerr << "Got unexpected result from 'mysensor'\n";
53+
VIAM_SDK_LOG(error) << "Got unexpected result from 'mysensor'";
5354
return EXIT_FAILURE;
5455
}
5556

5657
ProtoStruct readings = sensor->get_readings();
5758

5859
auto itr = readings.find("signal");
5960
if (itr == readings.end()) {
60-
std::cerr << "Expected signal not found in sensor readings\n";
61+
VIAM_SDK_LOG(error) << "Expected signal not found in sensor readings";
6162
return EXIT_FAILURE;
6263
}
6364

6465
const double* signal = itr->second.get<double>();
6566
if (signal) {
66-
std::cout << "\t" << itr->first << ": " << *signal << "\n";
67+
VIAM_SDK_LOG(info) << itr->first << ": " << *signal;
6768
} else {
68-
std::cerr << "Unexpected value type for sensor reading\n";
69+
VIAM_SDK_LOG(error) << "Unexpected value type for sensor reading";
6970
return EXIT_FAILURE;
7071
}
7172

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -757,10 +757,10 @@ int serve(const std::string& socket_path) try {
757757

758758
return EXIT_SUCCESS;
759759
} catch (const std::exception& ex) {
760-
std::cout << "ERROR: A std::exception was thrown from `serve`: " << ex.what() << std::endl;
760+
std::cerr << "ERROR: A std::exception was thrown from `serve`: " << ex.what() << std::endl;
761761
return EXIT_FAILURE;
762762
} catch (...) {
763-
std::cout << "ERROR: An unknown exception was thrown from `serve`" << std::endl;
763+
std::cerr << "ERROR: An unknown exception was thrown from `serve`" << std::endl;
764764
return EXIT_FAILURE;
765765
}
766766

@@ -770,8 +770,8 @@ int main(int argc, char* argv[]) {
770770
const std::string usage = "usage: mlmodelservice_tflite /path/to/unix/socket";
771771

772772
if (argc < 2) {
773-
std::cout << "ERROR: insufficient arguments\n";
774-
std::cout << usage << "\n";
773+
std::cerr << "ERROR: insufficient arguments\n";
774+
std::cerr << usage << "\n";
775775
return EXIT_FAILURE;
776776
}
777777

0 commit comments

Comments
 (0)