1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332
|
// Copyright (c) 2004-2019 greg Landrum and Rational Discovery LLC
//
// @@ All Rights Reserved @@
// This file is part of the RDKit.
// The contents are covered by the terms of the BSD license
// which is included in the file license.txt, found at the root
// of the RDKit source tree.
//
#include <RDBoost/python.h>
#include <iostream>
#include <fstream>
#include <RDBoost/Wrap.h>
#include <RDBoost/python_streambuf.h>
#include <RDGeneral/versions.h>
#include <RDGeneral/Invariant.h>
#include <cstdlib>
#include <RDGeneral/RDLog.h>
#if 0
#include <boost/log/functions.hpp>
#if defined(BOOST_HAS_THREADS)
#include <boost/log/extra/functions_ts.hpp>
#endif
#endif
namespace python = boost::python;
namespace logging = boost::logging;
std::string _version() { return "$Id$"; }
// std::ostream wrapper around Python's stderr stream
struct PyErrStream : std::ostream, std::streambuf {
static thread_local std::string buffer;
PyErrStream() : std::ostream(this) {
// All done!
}
int overflow(int c) override {
write(c);
return 0;
}
void write(char c) {
if (c == '\n') {
PyGILStateHolder h;
PySys_WriteStderr("%s\n", buffer.c_str());
buffer.clear();
} else {
buffer += c;
}
}
};
// std::ostream wrapper around Python's logging module
struct PyLogStream : std::ostream, std::streambuf {
static thread_local std::string buffer;
PyObject *logfn = nullptr;
PyLogStream(std::string level) : std::ostream(this) {
PyObject *module = PyImport_ImportModule("logging");
PyObject *logger = nullptr;
if (module != nullptr) {
logger = PyObject_CallMethod(module, "getLogger", "s", "rdkit");
Py_DECREF(module);
}
if (logger != nullptr) {
logfn = PyObject_GetAttrString(logger, level.c_str());
Py_DECREF(logger);
}
if (PyErr_Occurred()) {
PyErr_Print();
}
}
~PyLogStream() {
if (!_Py_IsFinalizing()) {
Py_XDECREF(logfn);
}
}
int overflow(int c) override {
write(c);
return 0;
}
void write(char c) {
if (logfn == nullptr) {
return;
}
if (c == '\n') {
PyGILStateHolder h;
PyObject *result = PyObject_CallFunction(logfn, "s", buffer.c_str());
Py_XDECREF(result);
buffer.clear();
} else {
buffer += c;
}
}
};
// per-thread buffers for the Python loggers
thread_local std::string PyErrStream::buffer;
thread_local std::string PyLogStream::buffer;
void LogToPythonLogger() {
static PyLogStream debug("debug");
static PyLogStream info("info");
static PyLogStream warning("warning");
static PyLogStream error("error");
rdDebugLog = std::make_shared<logging::rdLogger>(&debug);
rdInfoLog = std::make_shared<logging::rdLogger>(&info);
rdWarningLog = std::make_shared<logging::rdLogger>(&warning);
rdErrorLog = std::make_shared<logging::rdLogger>(&error);
}
void LogToPythonStderr() {
static PyErrStream debug;
static PyErrStream info;
static PyErrStream warning;
static PyErrStream error;
rdDebugLog = std::make_shared<logging::rdLogger>(&debug);
rdInfoLog = std::make_shared<logging::rdLogger>(&info);
rdWarningLog = std::make_shared<logging::rdLogger>(&warning);
rdErrorLog = std::make_shared<logging::rdLogger>(&error);
}
void WrapLogs() {
static PyErrStream debug; //("RDKit DEBUG: ");
static PyErrStream error; //("RDKit ERROR: ");
static PyErrStream warning; //("RDKit WARNING: ");
static PyErrStream info; //("RDKit INFO: ");
if (!rdDebugLog || !rdInfoLog || !rdErrorLog || !rdWarningLog) {
RDLog::InitLogs();
}
rdDebugLog->SetTee(debug);
rdInfoLog->SetTee(info);
rdWarningLog->SetTee(warning);
rdErrorLog->SetTee(error);
}
void EnableLog(std::string spec) { logging::enable_logs(spec); }
void DisableLog(std::string spec) { logging::disable_logs(spec); }
std::string LogStatus() { return logging::log_status(); }
void AttachFileToLog(std::string spec, std::string filename, int delay = 100) {
(void)spec;
(void)filename;
(void)delay;
#if 0
#if defined(BOOST_HAS_THREADS)
logging::manipulate_logs(spec)
.add_appender(logging::ts_appender(logging::write_to_file(filename),
delay));
#else
logging::manipulate_logs(spec)
.add_appender(logging::write_to_file(filename));
#endif
#endif
}
void LogDebugMsg(const std::string &msg) {
// NOGIL nogil;
BOOST_LOG(rdDebugLog) << msg << std::endl;
}
void LogInfoMsg(const std::string &msg) {
// NOGIL nogil;
BOOST_LOG(rdInfoLog) << msg << std::endl;
}
void LogWarningMsg(const std::string &msg) {
// NOGIL nogil;
BOOST_LOG(rdWarningLog) << msg << std::endl;
}
void LogErrorMsg(const std::string &msg) {
// NOGIL nogil;
BOOST_LOG(rdErrorLog) << msg << std::endl;
}
void LogMessage(std::string spec, std::string msg) {
if (spec == "rdApp.error") {
LogErrorMsg(msg);
} else if (spec == "rdApp.warning") {
LogWarningMsg(msg);
} else if (spec == "rdApp.info") {
LogInfoMsg(msg);
} else if (spec == "rdApp.debug") {
LogDebugMsg(msg);
}
}
class BlockLogs : public boost::noncopyable {
public:
BlockLogs() : m_log_setter{new RDLog::LogStateSetter} {}
~BlockLogs() = default;
BlockLogs *enter() { return this; }
void exit(python::object exc_type, python::object exc_val,
python::object traceback) {
RDUNUSED_PARAM(exc_type);
RDUNUSED_PARAM(exc_val);
RDUNUSED_PARAM(traceback);
m_log_setter.reset();
}
private:
std::unique_ptr<RDLog::LogStateSetter> m_log_setter;
};
namespace {
struct python_streambuf_wrapper {
typedef boost_adaptbx::python::streambuf wt;
static void wrap() {
using namespace boost::python;
class_<wt, boost::noncopyable>("streambuf", no_init)
.def(init<object &, std::size_t>(
(arg("python_file_obj"), arg("buffer_size") = 0),
"documentation")[with_custodian_and_ward_postcall<0, 2>()]);
}
};
struct python_ostream_wrapper {
typedef boost_adaptbx::python::ostream wt;
static void wrap() {
using namespace boost::python;
class_<std::ostream, boost::noncopyable>("std_ostream", no_init);
class_<wt, boost::noncopyable, bases<std::ostream>>("ostream", no_init)
.def(init<object &, std::size_t>(
(arg("python_file_obj"), arg("buffer_size") = 0)));
}
};
void seedRNG(unsigned int seed) { std::srand(seed); }
} // namespace
BOOST_PYTHON_MODULE(rdBase) {
python::scope().attr("__doc__") =
"Module containing basic definitions for wrapped C++ code\n"
"\n";
RDLog::InitLogs();
RegisterVectorConverter<int>();
RegisterVectorConverter<unsigned>();
RegisterVectorConverter<double>();
RegisterVectorConverter<std::string>(1);
RegisterVectorConverter<std::vector<int>>();
RegisterVectorConverter<std::vector<unsigned>>();
RegisterVectorConverter<std::vector<double>>();
RegisterListConverter<int>();
RegisterListConverter<std::vector<int>>();
RegisterListConverter<std::vector<unsigned int>>();
python::register_exception_translator<IndexErrorException>(
&translate_index_error);
python::register_exception_translator<ValueErrorException>(
&translate_value_error);
python::register_exception_translator<KeyErrorException>(
&translate_key_error);
#if INVARIANT_EXCEPTION_METHOD
python::register_exception_translator<Invar::Invariant>(
&translate_invariant_error);
#endif
python::def("_version", _version,
"Deprecated, use the constant rdkitVersion instead");
python::scope().attr("rdkitVersion") = RDKit::rdkitVersion;
python::scope().attr("boostVersion") = RDKit::boostVersion;
python::scope().attr("rdkitBuild") = RDKit::rdkitBuild;
python::def("LogToCppStreams", RDLog::InitLogs,
"Initialize RDKit logs with C++ streams");
python::def("LogToPythonLogger", LogToPythonLogger,
"Initialize RDKit logs with Python's logging module");
python::def("LogToPythonStderr", LogToPythonStderr,
"Initialize RDKit logs with Python's stderr stream");
python::def("WrapLogs", WrapLogs,
"Tee the RDKit logs to Python's stderr stream");
python::def("EnableLog", EnableLog);
python::def("DisableLog", DisableLog);
python::def("LogStatus", LogStatus);
python::def("LogDebugMsg", LogDebugMsg,
"Log a message to the RDKit debug logs");
python::def("LogInfoMsg", LogInfoMsg, "Log a message to the RDKit info logs");
python::def("LogWarningMsg", LogWarningMsg,
"Log a message to the RDKit warning logs");
python::def("LogErrorMsg", LogErrorMsg,
"Log a message to the RDKit error logs");
python::def("LogMessage", LogMessage, "Log a message to any rdApp.* log");
python::def("AttachFileToLog", AttachFileToLog,
"Causes the log to write to a file",
(python::arg("spec"), python::arg("filename"),
python::arg("delay") = 100));
python::def("SeedRandomNumberGenerator", seedRNG,
"Provides a seed to the standard C random number generator\n"
"This does not affect pure Python code, but is relevant to some "
"of the RDKit C++ components.",
(python::arg("seed")));
python_streambuf_wrapper::wrap();
python_ostream_wrapper::wrap();
python::class_<BlockLogs, boost::noncopyable>(
"BlockLogs",
"Temporarily block logs from outputting while this instance is in scope.",
python::init<>())
.def("__enter__", &BlockLogs::enter,
python::return_internal_reference<>())
.def("__exit__", &BlockLogs::exit);
}
|