Skip to content

[RSDK-11250, RSDK-11251, RSDK-11784, RSDK-11807] - Cpp improvements#27

Merged
seanavery merged 14 commits intoviam-modules:mainfrom
seanavery:RSDK-11250
Sep 3, 2025
Merged

[RSDK-11250, RSDK-11251, RSDK-11784, RSDK-11807] - Cpp improvements#27
seanavery merged 14 commits intoviam-modules:mainfrom
seanavery:RSDK-11250

Conversation

@seanavery
Copy link
Collaborator

@seanavery seanavery commented Aug 28, 2025

Description

  • The cpp-sdk added a logger, this PR replaces cout and cerr calls with the built-in logger.
  • Using throw for bad states instead of exits
  • Implement GetProperties
  • Add linter

Testing

  • logs showing up as expected ✅
8/28/2025, 11:06:48 AM info rdk.modmanager.viam_csi-cam-pi.StdOut   pexec/managed_process.go:411   \_ 2025--08--28 11:06:48: [Viam C++ SDK] <info> [host/main.cpp:33] Device type: Raspberry Pi
8/28/2025, 11:06:47 AM info rdk.modmanager.viam_csi-cam-pi.StdOut   pexec/managed_process.go:411   \_ 2025--08--28 11:06:47: [Viam C++ SDK] <info> [host/main.cpp:24] ### STARTING VIAM CSI CAMERA MODULE
  • tested module with bad configuration fails reconfigure without exiting the process ✅
  • tested GetProperties working ✅

// 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.
Instance inst;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Need to move Instance before any logger use


// Getters
std::string get_name() const;
bool is_debug() const;
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Removing debug attribute

@seanavery seanavery requested a review from hexbabe August 28, 2025 15:14
@seanavery seanavery changed the title RSDK-11250 - Use cpp sdk logger RSDK-11250, RSDK-11251 - Cpp improvements Aug 28, 2025
@seanavery seanavery changed the title RSDK-11250, RSDK-11251 - Cpp improvements [RSDK-11250, RSDK-11251, RSDK-11784] - Cpp improvements Aug 28, 2025
if (image.bytes.empty()) {
std::cerr << "ERROR: no bytes retrieved" << std::endl;
VIAM_SDK_LOG(error) << "no bytes retrieved from get_csi_image";
throw Exception("no bytes retrieved from get_csi_image");
Copy link
Collaborator

Choose a reason for hiding this comment

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

would std::runtime_error be more appropriate of an error to throw?

I'm gonna @ Lia @lia-viam

Choose a reason for hiding this comment

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

honestly not super important given the way we handle exceptions. note that viam::sdk::Exception is a public derived class of runtime_error, which matters in situations where you'd be interested in granular filtering on the exception type. You can skip the java stuff but see here: https://www.geeksforgeeks.org/cpp/catching-base-and-derived-classes-as-exceptions-in-cpp-and-java/

the distinction between Exception and runtime_error might matter if we were writing catch (const viam::sdk::Exception&) blocks and doing something meaningful with them, which we are not currently

the thing I would suggest though is rather than logging a message then putting it in an exception, in main.cpp you can wrap the call to serve in a try/catch block, like

try { my_mod.serve(); }
catch (const std::exception& e) {
VIAM_SDK_LOG(err) << e.what();
return EXIT_FAILURE;
}

Copy link
Collaborator Author

@seanavery seanavery Aug 29, 2025

Choose a reason for hiding this comment

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

@lia-viam Thanks for the explanation. I can wrap serve in a try catch, however, I thought that a throw here would get swallowed somewhere in the SDK/GRPC layer... Are you saying std::runtime_error will bring down the module server? This path gets hit behind GetImage/GetImages GRPC calls.

Copy link
Collaborator Author

@seanavery seanavery Aug 29, 2025

Choose a reason for hiding this comment

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

@lia-viam Should I be using GRPCException here instead? My intention is just to error out on the GRPC call.

Copy link
Collaborator Author

@seanavery seanavery Aug 29, 2025

Choose a reason for hiding this comment

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

It looks like the orbbec module is hitting std::runtime_error (which would be similar to the viam::sdk::Exception use here) behind GRPC calls. See get_image error handling.

Copy link

Choose a reason for hiding this comment

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

hey sean sorry I missed the ping. so my concern here was more just the duplication of throwing an error and having to log the same message. if you're implementing a method on a component then yes that gets handled by the grpc layer, although I think it should result in SDK or RDK (I forget which) logging the text of the exception you threw, so it's maybe not necessary to duplicate the logging.

for exception throws not in a component method implementation (or transitively called by one) then yes a try/catch in main would take care of that. this can indeed be more helpful than letting RDK take care of it, because often if you terminate with an uncaught exception you just get a message like "std::terminate called, uncaught exception of type runtime_error" without printing the message.

The main takeaway is it doesn't matter a ton whether you're throwing Exception or runtime_error, but no I wouldn't recommend throwing GRPCException

Copy link
Collaborator Author

@seanavery seanavery Sep 2, 2025

Choose a reason for hiding this comment

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

if you're implementing a method on a component then yes that gets handled by the grpc layer

Makes sense, thanks.

for exception throws not in a component method implementation (or transitively called by one) then yes a try/catch in main

Ok, added try/catch block around the main function to handle this case better.

Also, removed dup logging, now allowing GRPC swallow to spit out the msg.

@seanavery seanavery changed the title [RSDK-11250, RSDK-11251, RSDK-11784] - Cpp improvements [RSDK-11250, RSDK-11251, RSDK-11784, RSDK-11807] - Cpp improvements Sep 2, 2025
Copy link

@SebastianMunozP SebastianMunozP left a comment

Choose a reason for hiding this comment

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

These changes look good to me.

@seanavery
Copy link
Collaborator Author

@SebastianMunozP

These changes look good to me.
Sounds good, did a final round of manual testing.

  • lint ✅
  • build ✅
  • package ✅
  • test on pi ✅
  • test on jetson ✅
  • reconfigure ✅

@seanavery seanavery merged commit ece9d0c into viam-modules:main Sep 3, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants