mirror of
https://github.com/rdkit/rdkit.git
synced 2026-06-03 21:44:30 +08:00
* Add RDLog::CaptureLog for capturing log messages Adds an RAII `CaptureLog` class to `namespace RDLog` (alongside the existing `LogStateSetter`) that redirects an RDKit logger's output to an internal `std::stringstream` for the duration of its lifetime. On destruction the original stream destination and enabled state are fully restored. Nesting is supported: an inner capture shadows the outer one and each collects its own messages independently. The default constructor captures `rdErrorLog`; an explicit constructor accepts any `RDLogger`. Both enable the logger if it was previously disabled and restore that state on destruction. Python bindings expose `rdBase.CaptureLog` as a context manager with a `messages` read-only property, mirroring the existing `rdBase.BlockLogs` pattern. Messages remain accessible after the `with` block exits. C++ tests are added to `catch_logs.cpp` (6 Catch2 sections covering basic capture, empty state, enable/restore, stream restore, explicit logger, and nested captures). Python tests are added to `UnitTestLogging.py` (6 unittest cases covering the same scenarios). Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * CaptureLog: add per-level properties (error_messages, warning_messages, etc.) The Python CaptureLog wrapper now captures all four log levels simultaneously. Per-level properties (error_messages, warning_messages, info_messages, debug_messages) give access to messages from each logger independently; the existing messages property returns them all combined. The C++ RDLog::CaptureLog class is unchanged — it remains a clean single-logger RAII type. The Python wrapper composes four instances of it, one per log level. Suggested by bp-kelley in PR review. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Refactor CaptureLog: add named per-level subclasses Add CaptureErrorLog, CaptureWarningLog, CaptureInfoLog, and CaptureDebugLog as named convenience subclasses of CaptureLog, each capturing a specific logger. Update Python bindings to expose the four named classes directly (dropping the combined multi-capture approach), and update tests accordingly. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Simplify CaptureLog: no argument, captures rdErrorLog only Remove the RDLogger argument overload, the four named subclasses, and the PyCaptureLog template in favor of a single no-argument CaptureLog that mirrors the Schrödinger CaptureRDErrorLog from which it was inspired. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * CaptureLog tests: add dp_dest restoration and LogStateSetter interaction Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Rename CaptureLog to CaptureErrorLog The name CaptureLog was ambiguous; CaptureErrorLog is explicit about which logger it captures and avoids redundancy within namespace RDLog. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> * Generalize CaptureErrorLog into CaptureLog with logger parameter Replace CaptureErrorLog with CaptureLog, which accepts any RDLogger in its constructor (e.g. rdErrorLog, rdWarningLog). Add CaptureErrorLog as a convenience subclass that pre-fills rdErrorLog, preserving backward compatibility for existing callers. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> --------- Co-authored-by: Claude Sonnet 4.6 <noreply@anthropic.com>
254 lines
7.0 KiB
C++
254 lines
7.0 KiB
C++
//
|
|
// Copyright (C) 2005-2022 Greg Landrum and other RDKit contributors
|
|
//
|
|
// @@ 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 "RDLog.h"
|
|
|
|
#if 1
|
|
#include <iomanip>
|
|
#include <string>
|
|
#include <ctime>
|
|
#include <iostream>
|
|
#include <sstream>
|
|
|
|
RDLogger rdAppLog = nullptr;
|
|
RDLogger rdDebugLog = nullptr;
|
|
RDLogger rdInfoLog = nullptr;
|
|
RDLogger rdErrorLog = nullptr;
|
|
RDLogger rdWarningLog = nullptr;
|
|
RDLogger rdStatusLog = nullptr;
|
|
namespace RDLog {
|
|
|
|
namespace {
|
|
const std::vector<RDLogger *> allLogs = {&rdAppLog, &rdDebugLog,
|
|
&rdInfoLog, &rdErrorLog,
|
|
&rdWarningLog, &rdStatusLog};
|
|
}
|
|
|
|
LogStateSetter::LogStateSetter() {
|
|
for (auto i = 0u; i < allLogs.size(); ++i) {
|
|
if (*allLogs[i] && (*allLogs[i])->df_enabled) {
|
|
d_origState |= 1 << i;
|
|
(*allLogs[i])->df_enabled = false;
|
|
}
|
|
}
|
|
}
|
|
|
|
LogStateSetter::LogStateSetter(RDLoggerList toEnable) : LogStateSetter() {
|
|
for (auto i = 0u; i < allLogs.size(); ++i) {
|
|
if (*allLogs[i] && std::find(toEnable.begin(), toEnable.end(),
|
|
*allLogs[i]) != toEnable.end()) {
|
|
d_origState ^= 1 << i;
|
|
(*allLogs[i])->df_enabled = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
LogStateSetter::~LogStateSetter() {
|
|
for (auto i = 0u; i < allLogs.size(); ++i) {
|
|
if (*allLogs[i]) {
|
|
(*allLogs[i])->df_enabled ^= d_origState >> i & 1;
|
|
}
|
|
}
|
|
}
|
|
} // namespace RDLog
|
|
namespace boost {
|
|
namespace logging {
|
|
|
|
void enable_logs(const char *arg) { enable_logs(std::string(arg)); };
|
|
void enable_logs(const std::string &arg) {
|
|
// Yes... this is extremely crude
|
|
if (arg == "rdApp.debug" || arg == "rdApp.*") {
|
|
if (rdDebugLog) {
|
|
rdDebugLog->df_enabled = true;
|
|
}
|
|
}
|
|
if (arg == "rdApp.info" || arg == "rdApp.*") {
|
|
if (rdInfoLog) {
|
|
rdInfoLog->df_enabled = true;
|
|
}
|
|
}
|
|
if (arg == "rdApp.warning" || arg == "rdApp.*") {
|
|
if (rdWarningLog) {
|
|
rdWarningLog->df_enabled = true;
|
|
}
|
|
}
|
|
if (arg == "rdApp.error" || arg == "rdApp.*") {
|
|
if (rdErrorLog) {
|
|
rdErrorLog->df_enabled = true;
|
|
}
|
|
}
|
|
};
|
|
void disable_logs(const char *arg) { disable_logs(std::string(arg)); };
|
|
void disable_logs(const std::string &arg) {
|
|
// Yes... this is extremely crude
|
|
if (arg == "rdApp.debug" || arg == "rdApp.*") {
|
|
if (rdDebugLog) {
|
|
rdDebugLog->df_enabled = false;
|
|
}
|
|
}
|
|
if (arg == "rdApp.info" || arg == "rdApp.*") {
|
|
if (rdInfoLog) {
|
|
rdInfoLog->df_enabled = false;
|
|
}
|
|
}
|
|
if (arg == "rdApp.warning" || arg == "rdApp.*") {
|
|
if (rdWarningLog) {
|
|
rdWarningLog->df_enabled = false;
|
|
}
|
|
}
|
|
if (arg == "rdApp.error" || arg == "rdApp.*") {
|
|
if (rdErrorLog) {
|
|
rdErrorLog->df_enabled = false;
|
|
}
|
|
}
|
|
};
|
|
|
|
bool is_log_enabled(RDLogger log) { return log && log->df_enabled; }
|
|
|
|
void get_log_status(std::ostream &ss, const std::string &name, RDLogger log) {
|
|
ss << name << ":";
|
|
if (log) {
|
|
if (log->df_enabled) {
|
|
ss << "enabled";
|
|
} else {
|
|
ss << "disabled";
|
|
}
|
|
} else {
|
|
ss << "unitialized";
|
|
}
|
|
}
|
|
|
|
std::string log_status() {
|
|
std::stringstream ss;
|
|
get_log_status(ss, "rdApp.debug", rdDebugLog);
|
|
ss << std::endl;
|
|
get_log_status(ss, "rdApp.info", rdInfoLog);
|
|
ss << std::endl;
|
|
get_log_status(ss, "rdApp.warning", rdWarningLog);
|
|
ss << std::endl;
|
|
get_log_status(ss, "rdApp.error", rdErrorLog);
|
|
return ss.str();
|
|
}
|
|
|
|
} // namespace logging
|
|
} // namespace boost
|
|
|
|
namespace RDLog {
|
|
void InitLogs() {
|
|
rdDebugLog = std::make_shared<boost::logging::rdLogger>(&std::cerr);
|
|
rdDebugLog->df_enabled = false;
|
|
rdInfoLog = std::make_shared<boost::logging::rdLogger>(&std::cout);
|
|
rdInfoLog->df_enabled = false;
|
|
rdWarningLog = std::make_shared<boost::logging::rdLogger>(&std::cerr);
|
|
rdErrorLog = std::make_shared<boost::logging::rdLogger>(&std::cerr);
|
|
}
|
|
|
|
CaptureLog::CaptureLog(RDLogger log) : d_log(std::move(log)) {
|
|
if (!d_log) {
|
|
return;
|
|
}
|
|
d_logWasEnabled = d_log->df_enabled;
|
|
d_log->df_enabled = true;
|
|
d_savedDest = d_log->dp_dest;
|
|
d_log->dp_dest = &d_messages;
|
|
d_savedTeestream = d_log->teestream;
|
|
d_log->teestream = nullptr;
|
|
}
|
|
|
|
CaptureLog::~CaptureLog() {
|
|
if (!d_log) {
|
|
return;
|
|
}
|
|
d_log->dp_dest = d_savedDest;
|
|
d_log->teestream = d_savedTeestream;
|
|
d_log->df_enabled = d_logWasEnabled;
|
|
}
|
|
|
|
std::string CaptureLog::messages() const { return d_messages.str(); }
|
|
|
|
std::ostream &toStream(std::ostream &logstrm) {
|
|
char buffer[16];
|
|
time_t t = time(nullptr);
|
|
// localtime() is thread safe on windows, but not on *nix
|
|
#ifdef WIN32
|
|
strftime(buffer, 16, "[%T] ", localtime(&t));
|
|
#else
|
|
struct tm buf;
|
|
strftime(buffer, 16, "[%T] ", localtime_r(&t, &buf));
|
|
#endif
|
|
return logstrm << buffer;
|
|
}
|
|
} // namespace RDLog
|
|
|
|
#else
|
|
#include <boost/log/functions.hpp>
|
|
#if defined(BOOST_HAS_THREADS2)
|
|
#include <boost/log/extra/functions_ts.hpp>
|
|
#endif
|
|
#include <iostream>
|
|
namespace logging = boost::logging;
|
|
|
|
BOOST_DEFINE_LOG(rdAppLog, "rdApp")
|
|
BOOST_DEFINE_LOG(rdDebugLog, "rdApp.debug")
|
|
BOOST_DEFINE_LOG(rdInfoLog, "rdApp.info")
|
|
BOOST_DEFINE_LOG(rdErrorLog, "rdApp.error")
|
|
BOOST_DEFINE_LOG(rdWarningLog, "rdApp.warning")
|
|
BOOST_DEFINE_LOG(rdStatusLog, "rdApp.status")
|
|
|
|
namespace RDLog {
|
|
void write_to_cout(const std::string &, const std::string &msg) {
|
|
std::cout << msg;
|
|
std::cout.flush();
|
|
}
|
|
void write_to_cerr(const std::string &, const std::string &msg) {
|
|
std::cerr << msg;
|
|
std::cerr.flush();
|
|
}
|
|
|
|
void InitLogs() {
|
|
static bool callOnce = true;
|
|
if (!callOnce) return;
|
|
callOnce = false;
|
|
|
|
// turn off caching:
|
|
logging::set_log_caching(false);
|
|
logging::manipulate_logs("rdApp.*").add_modifier(
|
|
logging::prepend_time("[$hh:$mm:$ss] "), logging::DEFAULT_INDEX - 10);
|
|
logging::manipulate_logs("rdApp.info")
|
|
.add_appender(write_to_cout, logging::DEFAULT_INDEX + 1);
|
|
#if defined(BOOST_HAS_THREADS2)
|
|
logging::manipulate_logs("rdApp.error")
|
|
.add_appender(logging::ts_appender(write_to_cerr, 100),
|
|
logging::DEFAULT_INDEX + 1);
|
|
logging::manipulate_logs("rdApp.warning")
|
|
.add_appender(logging::ts_appender(write_to_cerr, 100),
|
|
logging::DEFAULT_INDEX + 1);
|
|
logging::manipulate_logs("rdApp.status")
|
|
.add_appender(logging::ts_appender(write_to_cerr, 100),
|
|
logging::DEFAULT_INDEX + 1);
|
|
logging::manipulate_logs("rdApp.debug")
|
|
.add_appender(logging::ts_appender(write_to_cerr, 100),
|
|
logging::DEFAULT_INDEX + 1);
|
|
#else
|
|
logging::manipulate_logs("rdApp.error")
|
|
.add_appender(write_to_cerr, logging::DEFAULT_INDEX + 1);
|
|
logging::manipulate_logs("rdApp.warning")
|
|
.add_appender(write_to_cerr, logging::DEFAULT_INDEX + 1);
|
|
logging::manipulate_logs("rdApp.status")
|
|
.add_appender(write_to_cerr, logging::DEFAULT_INDEX + 1);
|
|
logging::manipulate_logs("rdApp.debug")
|
|
.add_appender(write_to_cerr, logging::DEFAULT_INDEX + 1);
|
|
#endif
|
|
// start with the debug log disabled:
|
|
logging::disable_logs("rdApp.debug");
|
|
};
|
|
|
|
} // namespace RDLog
|
|
#endif
|