generated from athackst/vscode_ros2_workspace
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
19 changed files
with
690 additions
and
128 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
add_subdirectory(cmn_hdrs) | ||
add_subdirectory(net_node) | ||
add_subdirectory(protofiles) | ||
add_subdirectory(sailbot_db) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
set(module net_node) | ||
|
||
set(link_libs | ||
) | ||
|
||
set(inc_dirs | ||
) | ||
|
||
set(compile_defs | ||
) | ||
|
||
set(srcs | ||
${CMAKE_CURRENT_LIST_DIR}/src/net_node.cpp | ||
) | ||
make_lib(${module} "${srcs}" "${link_libs}" "${inc_dirs}" "${compile_defs}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#pragma once | ||
#include <rclcpp/rclcpp.hpp> | ||
|
||
/** | ||
* Network Systems custom ROS Node. | ||
* Redirects std::cout and std::cerr streams to ROS logging macros. | ||
* @note An extra newline character is added to ROS logs from redirected streams. Not the prettiest, but it's harmless. | ||
* Errors are extra ugly, with multiple empty newlines. It does make them extra obvious though. | ||
*/ | ||
class NetNode : public rclcpp::Node | ||
{ | ||
public: | ||
/** | ||
* @brief Initialize a NetNode's buffers and stream redirections | ||
* | ||
* @param node_name Name of the node to pass to the base rclcpp::Node | ||
*/ | ||
explicit NetNode(const std::string & node_name); | ||
~NetNode(); | ||
|
||
private: | ||
/** | ||
* @brief Custom string buffer that flushes its contents to ROS logging macros. | ||
* https://stackoverflow.com/a/14232652 | ||
*/ | ||
class LogBuf : public std::stringbuf | ||
{ | ||
public: | ||
/** | ||
* @brief Initialize a LogBuf for the NetNode | ||
* | ||
* @param fd File descriptor. Must be either STDOUT_FILENO or STDERR_FILENO | ||
* @param logger Logger object of from the encompassing NetNode | ||
*/ | ||
LogBuf(int fd, rclcpp::Logger logger); | ||
|
||
/** | ||
* @brief Called when the buffer is flushed. Invokes ROS logging macros and clears buffer. | ||
* | ||
* @return 0 | ||
*/ | ||
virtual int sync(); | ||
|
||
private: | ||
const int fd_; // File descriptor to redirect (either STDOUT_FILENO or STDERR_FILENO) | ||
const rclcpp::Logger logger_; // Logger instance from the encompassing NetNode | ||
}; | ||
|
||
std::streambuf * og_stdout_buf_; // Original buffer for stdout | ||
std::streambuf * og_stderr_buf_; // Original buffer for stderr | ||
LogBuf new_stdout_buf_; // LogBuf to redirect stdout to | ||
LogBuf new_stderr_buf_; // LogBuf to redirect stderr to | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#include "net_node.h" | ||
|
||
#include <rclcpp/rclcpp.hpp> | ||
|
||
NetNode::NetNode(const std::string & node_name) | ||
: rclcpp::Node(node_name), | ||
// When constructing the new buffers, create a new rclcpp::Logger with ":net_node" appended to the name so that | ||
// we can identify that redirected messages are output from this file, rather than the _ros_intf.cpp file. | ||
// Hopefully this will help reduce confusion with respect to the printed line numbers. | ||
new_stdout_buf_(STDOUT_FILENO, rclcpp::get_logger(std::string(this->get_logger().get_name()) + ":net_node")), | ||
new_stderr_buf_(STDERR_FILENO, rclcpp::get_logger(std::string(this->get_logger().get_name()) + ":net_node")) | ||
{ | ||
og_stdout_buf_ = std::cout.rdbuf(); | ||
og_stderr_buf_ = std::cerr.rdbuf(); | ||
std::cout.rdbuf(&new_stdout_buf_); | ||
std::cerr.rdbuf(&new_stderr_buf_); | ||
} | ||
|
||
NetNode::~NetNode() | ||
{ | ||
std::cout.rdbuf(og_stdout_buf_); | ||
std::cerr.rdbuf(og_stderr_buf_); | ||
} | ||
|
||
NetNode::LogBuf::LogBuf(int fd, rclcpp::Logger logger) : fd_(fd), logger_(logger) {} | ||
|
||
int NetNode::LogBuf::sync() | ||
{ | ||
switch (fd_) { | ||
case STDOUT_FILENO: | ||
RCLCPP_INFO(logger_, "%s", this->str().c_str()); | ||
break; | ||
case STDERR_FILENO: | ||
RCLCPP_ERROR(logger_, "%s", this->str().c_str()); | ||
break; | ||
default: | ||
// unreachable | ||
throw std::runtime_error("Invalid file descriptor! " + std::to_string(fd_)); | ||
} | ||
this->str(""); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.