Created
June 26, 2023 20:26
-
-
Save EricCousineau-TRI/ff52a8e3be0ca5fd085307c16ff09dbc to your computer and use it in GitHub Desktop.
Workaround for https://github.com/RobotLocomotion/drake/issues/19680
This file contains hidden or 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
#include <pybind11/pybind11.h> | |
#include <spdlog/sinks/base_sink.h> | |
#include "drake/common/drake_throw.h" | |
#include "drake/common/text_logging.h" | |
namespace py = pybind11; | |
namespace anzu { | |
namespace { | |
// No-op sink to ensure that pydrake sink does not get installed. | |
class noop_sink final | |
: public spdlog::sinks::base_sink<spdlog::details::null_mutex> { | |
public: | |
noop_sink() {} | |
protected: | |
void sink_it_(const spdlog::details::log_msg&) final {} | |
void flush_() final {} | |
}; | |
void FailIfPydrakeImported() { | |
py::dict modules = py::module::import("sys").attr("modules"); | |
if (modules.contains("pydrake")) { | |
throw std::runtime_error("This module must be imported before pydrake."); | |
} | |
} | |
void InstallExtraRootSink() { | |
FailIfPydrakeImported(); | |
std::vector<std::shared_ptr<spdlog::sinks::sink> >& root_sinks = | |
drake::log()->sinks(); | |
DRAKE_DEMAND(root_sinks.size() == 1); | |
root_sinks.push_back(std::make_shared<noop_sink>()); | |
} | |
PYBIND11_MODULE(no_pydrake_spdlog_sink, m) { | |
InstallExtraRootSink(); | |
} | |
} // namespace | |
} // namespace anzu |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment