Skip to content

Instantly share code, notes, and snippets.

@EricCousineau-TRI
Created June 26, 2023 20:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EricCousineau-TRI/ff52a8e3be0ca5fd085307c16ff09dbc to your computer and use it in GitHub Desktop.
Save EricCousineau-TRI/ff52a8e3be0ca5fd085307c16ff09dbc to your computer and use it in GitHub Desktop.
#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