-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Switch to custom logging macros to avoid deadlocks (#223)
* Add simple console logger Signed-off-by: Yadunund <[email protected]> * Rely on rcutils_logging_console_output_handler Signed-off-by: Yadunund <[email protected]> * Move log_named impl to cpp Signed-off-by: Yadunund <[email protected]> * Implement rmw_set_log_severity Signed-off-by: Yadunund <[email protected]> * Add github action to check for logging macros Signed-off-by: Yadunund <[email protected]> --------- Signed-off-by: Yadunund <[email protected]>
- Loading branch information
Showing
13 changed files
with
255 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# Ensure that PR diff does not include RCUTILS_LOG_* macros as these induce deadlocks. | ||
# See https://github.com/ros2/rmw_zenoh/issues/182 for more details. | ||
name: "Check logging macros" | ||
on: [pull_request] | ||
jobs: | ||
check_logging: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Check logging macros | ||
uses: JJ/github-pr-contains-action@releases/v14.1 | ||
with: | ||
github-token: ${{github.token}} | ||
diffDoesNotContain: "RCUTILS_LOG_|rcutils/logging_macros.h" |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
// Copyright 2024 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "logging.hpp" | ||
|
||
#include <sstream> | ||
|
||
namespace rmw_zenoh_cpp | ||
{ | ||
///============================================================================= | ||
Logger & Logger::get() | ||
{ | ||
static Logger logger(RCUTILS_LOG_SEVERITY_INFO); | ||
return logger; | ||
} | ||
|
||
///============================================================================= | ||
Logger::Logger(RCUTILS_LOG_SEVERITY threshold_level) | ||
: threshold_level_(threshold_level) | ||
{} | ||
|
||
///============================================================================= | ||
void Logger::set_log_level(RCUTILS_LOG_SEVERITY new_level) | ||
{ | ||
threshold_level_ = new_level; | ||
} | ||
|
||
void Logger::log_named( | ||
RCUTILS_LOG_SEVERITY level, | ||
const char * name, | ||
const char * message, | ||
...) const | ||
{ | ||
if (level >= this->threshold_level_) { | ||
rcutils_time_point_value_t now; | ||
rcutils_ret_t ret = rcutils_system_time_now(&now); | ||
if (ret != RCUTILS_RET_OK) { | ||
RCUTILS_SAFE_FWRITE_TO_STDERR("Failed to get timestamp while doing a console logging.\n"); | ||
return; | ||
} | ||
static rcutils_log_location_t log_location = {__func__, __FILE__, __LINE__}; | ||
va_list args; | ||
va_start(args, message); | ||
rcutils_logging_console_output_handler( | ||
&log_location, | ||
level, | ||
name, | ||
now, | ||
message, | ||
&args | ||
); | ||
} | ||
} | ||
} // namespace rmw_zenoh_cpp |
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,50 @@ | ||
// Copyright 2024 Open Source Robotics Foundation, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef DETAIL__LOGGING_HPP_ | ||
#define DETAIL__LOGGING_HPP_ | ||
|
||
#include <rcutils/logging.h> | ||
|
||
#include <iostream> | ||
#include <mutex> | ||
#include <string> | ||
#include <sstream> | ||
|
||
namespace rmw_zenoh_cpp | ||
{ | ||
///============================================================================= | ||
class Logger | ||
{ | ||
public: | ||
// Get a static reference to the logger. | ||
static Logger & get(); | ||
|
||
// Set the threshold log level. | ||
void set_log_level(RCUTILS_LOG_SEVERITY new_level); | ||
|
||
// Log to the console. | ||
void log_named( | ||
RCUTILS_LOG_SEVERITY level, | ||
const char * name, | ||
const char * message, | ||
...) const; | ||
|
||
private: | ||
RCUTILS_LOG_SEVERITY threshold_level_; | ||
explicit Logger(RCUTILS_LOG_SEVERITY threshold_level); | ||
}; | ||
} // namespace rmw_zenoh_cpp | ||
|
||
#endif // DETAIL__LOGGING_HPP_ |
Oops, something went wrong.