Skip to content

Commit 04c92ce

Browse files
committed
add test and use set_global_resource_name
1 parent ac25b4e commit 04c92ce

File tree

4 files changed

+80
-52
lines changed

4 files changed

+80
-52
lines changed

src/viam/examples/mlmodel/example_audio_classification_client.cpp

Lines changed: 55 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ int main(int argc, char* argv[]) try {
8383
// any other C++ SDK objects and stays alive until all Viam C++ SDK objects are destroyed.
8484
viam::sdk::Instance inst;
8585

86+
// By default, log messages with VIAM_SDK_LOG have "Viam C++ SDK" as the resource name.
87+
// Here we override it with the name of the executable, trimming the leading path if present.
88+
89+
std::string exe_name{argv[0]};
90+
91+
const auto last_slash_pos = exe_name.find_last_of('/');
92+
if (last_slash_pos != std::string::npos) {
93+
exe_name = exe_name.substr(last_slash_pos);
94+
}
95+
96+
viam::sdk::LogManager::get().set_global_resource_name(exe_name);
97+
8698
// Build up our command line options. The example operates in two
8799
// modes. In the "--generate" mode, it takes command line
88100
// parameters needed to satisfy the interpolation points in the
@@ -168,35 +180,33 @@ int main(int argc, char* argv[]) try {
168180
if (opt_generating) {
169181
// Validate that we have the right options for generation.
170182
if (opt_robot_host || opt_api_key || opt_api_key_id) {
171-
std::cout << argv[0]
172-
<< ": With `--generate`, do not provide `--robot-{host,api-key,api-key-id}`"
173-
<< std::endl;
183+
VIAM_SDK_LOG(error)
184+
<< "With `--generate`, do not provide `--robot-{host,api-key,api-key-id}`";
174185
return EXIT_FAILURE;
175186
}
176187

177188
if (!opt_model_path) {
178-
std::cout << argv[0] << ": With `--generate`, a `--model-path` is required"
179-
<< std::endl;
189+
VIAM_SDK_LOG(error) << "With `--generate`, a `--model-path` is required";
180190
return EXIT_FAILURE;
181191
}
192+
182193
const bf::path model_path(opt_model_path.get());
183194
if (!bf::is_regular_file(model_path)) {
184-
std::cout << argv[0] << ": The path `" << model_path.c_str()
185-
<< "` provided for `--model-path` is not an existing regular file"
186-
<< std::endl;
195+
VIAM_SDK_LOG(error) << "The path `" << model_path.c_str()
196+
<< "` provided for `--model-path` is not an existing regular file";
187197
return EXIT_FAILURE;
188198
}
189199

190200
if (!opt_tflite_module_path) {
191-
std::cout << argv[0] << ": With `--generate`, a `--tflite-module-path` is required"
192-
<< std::endl;
201+
VIAM_SDK_LOG(error) << "With `--generate`, a `--tflite-module-path` is required";
193202
return EXIT_FAILURE;
194203
}
204+
195205
const bf::path tflite_module_path(opt_tflite_module_path.get());
196206
if (!bf::is_regular_file(tflite_module_path)) {
197-
std::cout << argv[0] << ": The path `" << tflite_module_path.c_str()
198-
<< "` provided for `--tflite-module-path` is not an existing regular file"
199-
<< std::endl;
207+
VIAM_SDK_LOG(error)
208+
<< "The path `" << tflite_module_path.c_str()
209+
<< "` provided for `--tflite-module-path` is not an existing regular file";
200210
return EXIT_FAILURE;
201211
}
202212

@@ -207,28 +217,25 @@ int main(int argc, char* argv[]) try {
207217
const auto config = boost::format(kRobotConfigTemplate) % bf::absolute(model_path).c_str() %
208218
bf::absolute(tflite_module_path).c_str();
209219

210-
std::cout << config << std::endl;
220+
VIAM_SDK_LOG(info) << config << std::endl;
211221

212222
} else {
213223
// Validate that we have the right options for classification mode.
214224
if (opt_model_path || opt_tflite_module_path) {
215-
std::cout << argv[0] << ": Without `--generate`, do not provide `--*path*` arguments"
216-
<< std::endl;
225+
VIAM_SDK_LOG(error) << "Without `--generate`, do not provide `--*path*` arguments";
217226
return EXIT_FAILURE;
218227
}
219228

220229
if (!opt_robot_host) {
221-
std::cout << argv[0]
222-
<< ": The `--robot-host` argument is required when connecting to a robot"
223-
<< std::endl;
230+
VIAM_SDK_LOG(error)
231+
<< "The `--robot-host` argument is required when connecting to a robot";
224232
return EXIT_FAILURE;
225233
}
226234

227235
if (!opt_api_key || !opt_api_key_id) {
228-
std::cout << argv[0]
229-
<< ": The `--robot-api-key` and the `--robot-api-key-id` argument are "
230-
"required when connecting to a robot"
231-
<< std::endl;
236+
VIAM_SDK_LOG(error)
237+
<< "The `--robot-api-key` and the `--robot-api-key-id` argument are "
238+
"required when connecting to a robot";
232239
return EXIT_FAILURE;
233240
}
234241

@@ -248,10 +255,9 @@ int main(int argc, char* argv[]) try {
248255
auto yamnet_service =
249256
robot->resource_by_name<vsdk::MLModelService>("yamnet_classification_tflite");
250257
if (!yamnet_service) {
251-
std::cout << argv[0] << ": "
252-
<< "Failed: did not find the `yamnet_classification_tflite` resource, cannot "
253-
"continue"
254-
<< std::endl;
258+
VIAM_SDK_LOG(error)
259+
<< " did not find the `yamnet_classification_tflite` resource, cannot "
260+
"continue";
255261
return EXIT_FAILURE;
256262
}
257263

@@ -296,8 +302,7 @@ int main(int argc, char* argv[]) try {
296302
// the robot configuration.
297303
auto categories = result->find("categories");
298304
if (categories == result->end()) {
299-
std::cout << argv[0] << ": "
300-
<< "Failed: a `categories` tensor was not returned" << std::endl;
305+
VIAM_SDK_LOG(error) << "A `categories` tensor was not returned";
301306
return EXIT_FAILURE;
302307
}
303308

@@ -311,10 +316,8 @@ int main(int argc, char* argv[]) try {
311316
const auto* const categories_float =
312317
boost::get<vsdk::MLModelService::tensor_view<float>>(&categories->second);
313318
if (!categories_float) {
314-
std::cout
315-
<< argv[0] << ": "
316-
<< "Failed: a `categories` tensor was returned, but it was not of type `float`"
317-
<< std::endl;
319+
VIAM_SDK_LOG(error)
320+
<< "A `categories` tensor was returned, but it was not of type `float`";
318321
return EXIT_FAILURE;
319322
}
320323

@@ -323,23 +326,23 @@ int main(int argc, char* argv[]) try {
323326
// results and print out the label and score.
324327
if (!opt_model_label_path) {
325328
for (const auto& val : *categories_float) {
326-
std::cout << val << std::endl;
329+
VIAM_SDK_LOG(info) << val;
327330
}
328331
} else {
329332
// Ensure that the label path is something we can actually read from.
330333
const bf::path model_label_path(opt_model_label_path.get());
331334
if (!bf::is_regular_file(model_label_path)) {
332-
std::cout << argv[0] << ": Failed: The path `" << model_label_path.c_str()
333-
<< "` provided for `--model-label-path` is not an existing regular file"
334-
<< std::endl;
335+
VIAM_SDK_LOG(error)
336+
<< "The path `" << model_label_path.c_str()
337+
<< "` provided for `--model-label-path` is not an existing regular file";
335338
return EXIT_FAILURE;
336339
}
337340

338341
// Open the labels file, or bail.
339342
std::ifstream labels_stream(model_label_path.c_str());
340343
if (!labels_stream) {
341-
std::cout << argv[0] << ": Failed: Unable to open label path `"
342-
<< model_label_path.c_str() << "`" << std::endl;
344+
VIAM_SDK_LOG(error)
345+
<< "Unable to open label path `" << model_label_path.c_str() << "`";
343346
return EXIT_FAILURE;
344347
}
345348

@@ -353,9 +356,8 @@ int main(int argc, char* argv[]) try {
353356
// If the tensor size doesn't match the labels file size, then the labels file
354357
// is probably incorrect.
355358
if (categories_float->size() != labels.size()) {
356-
std::cout << argv[0]
357-
<< ": Failed: Size mismatch between category scores and label files"
358-
<< std::endl;
359+
VIAM_SDK_LOG(error)
360+
<< "Size mismatch between category scores and label files" << std::endl;
359361
return EXIT_FAILURE;
360362
}
361363

@@ -364,48 +366,49 @@ int main(int argc, char* argv[]) try {
364366
const std::string* label;
365367
float score;
366368
};
369+
367370
std::vector<scored_label> scored_labels;
368371
for (size_t i = 0; i != labels.size(); ++i) {
369372
scored_labels.push_back({&labels[i], (*categories_float)[i]});
370373
}
374+
371375
std::sort(begin(scored_labels), end(scored_labels), [](const auto& l, const auto& r) {
372376
return l.score > r.score;
373377
});
374378

375379
// Print out the top 5 (or fewer) label/score pairs.
376380
for (size_t i = 0; i != std::min(5UL, scored_labels.size()); ++i) {
377381
// TODO: Avoid hardcoding the width here.
378-
std::cout << boost::format("%1%: %2% %|40t|%3%\n") % i % *scored_labels[i].label %
379-
scored_labels[i].score;
382+
VIAM_SDK_LOG(info) << boost::format("%1%: %2% %|40t|%3%\n") % i %
383+
*scored_labels[i].label % scored_labels[i].score;
380384
}
381-
std::cout.flush();
382385
}
383386

384387
// Run 100 rounds of inference, accumulate some descriptive
385388
// statistics, and report them.
386-
std::cout << "\nMeasuring inference latency ...\n";
389+
VIAM_SDK_LOG(info) << "\nMeasuring inference latency ...";
387390
bacc::accumulator_set<double, bacc::stats<bacc::tag::mean, bacc::tag::moment<2>>>
388391
accumulator;
392+
389393
for (std::size_t i = 0; i != 100; ++i) {
390394
const auto start = std::chrono::steady_clock::now();
391395
static_cast<void>(yamnet_service->infer(inputs));
392396
const auto finish = std::chrono::steady_clock::now();
393397
const std::chrono::duration<double> elapsed = finish - start;
394-
;
395398
accumulator(elapsed.count());
396399
}
397-
std::cout << "Inference latency (seconds), Mean: " << bacc::mean(accumulator) << std::endl;
398-
std::cout << "Inference latency (seconds), Var : " << bacc::moment<2>(accumulator)
399-
<< std::endl;
400+
401+
VIAM_SDK_LOG(info) << "Inference latency (seconds), Mean: " << bacc::mean(accumulator);
402+
VIAM_SDK_LOG(info) << "Inference latency (seconds), Var : " << bacc::moment<2>(accumulator);
400403

401404
return EXIT_SUCCESS;
402405
}
403406
} catch (const std::exception& ex) {
404407
std::cerr << argv[0] << ": "
405-
<< "Failed: a std::exception was thrown: `" << ex.what() << "``" << std::endl;
408+
<< "Failed: a std::exception was thrown: `" << ex.what() << "``\n";
406409
return EXIT_FAILURE;
407410
} catch (...) {
408411
std::cerr << argv[0] << ": "
409-
<< "Failed: an unknown exception was thrown" << std::endl;
412+
<< "Failed: an unknown exception was thrown\n";
410413
return EXIT_FAILURE;
411414
}

src/viam/sdk/log/logging.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ LogManager& LogManager::get() {
9191
return result;
9292
}
9393

94+
void LogManager::set_global_resource_name(std::string name) {
95+
sdk_logger_.channel(std::move(name));
96+
VIAM_SDK_LOG(debug) << "Overrode global resource name";
97+
}
98+
9499
LogSource& LogManager::global_logger() {
95100
return sdk_logger_;
96101
}

src/viam/sdk/log/logging.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,9 @@ class LogManager {
6969
/// This is the only way to access the logger.
7070
static LogManager& get();
7171

72+
/// @brief Override the channel name of general log messages not originating from resources.
73+
void set_global_resource_name(std::string);
74+
7275
/// @brief Set the global logger severity.
7376
void set_global_log_level(log_level);
7477

src/viam/sdk/tests/test_log.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,23 @@ BOOST_AUTO_TEST_CASE(test_cout_logging) {
2929
}
3030
}
3131

32+
BOOST_AUTO_TEST_CASE(test_global_name) {
33+
cout_redirect redirect;
34+
35+
sdk::LogManager::get().set_global_resource_name("My Channel");
36+
37+
VIAM_SDK_LOG(info) << "after";
38+
39+
const std::string rec = redirect.os.str();
40+
redirect.release();
41+
42+
for (const char* s : {"My Channel", "after"}) {
43+
BOOST_CHECK(rec.find(s) != std::string::npos);
44+
}
45+
46+
BOOST_CHECK(rec.find(sdk::global_resource_name()) == std::string::npos);
47+
}
48+
3249
BOOST_AUTO_TEST_CASE(test_global_filter) {
3350
cout_redirect redirect;
3451

0 commit comments

Comments
 (0)