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
|
//
// Copyright (C) 2022 Greg Landrum
//
// @@ 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 <string>
#include <sstream>
#include "catch.hpp"
#include "RDLog.h"
TEST_CASE("LogStateSetter") {
RDLog::RDLoggerList allLogs({rdErrorLog, rdWarningLog, rdInfoLog});
SECTION("disable all") {
for (auto strm : allLogs) {
// explicitly enable the stream so that we know something is happening
std::stringstream ostrm;
strm->df_enabled = true;
strm->SetTee(ostrm);
{
RDLog::LogStateSetter disabler;
BOOST_LOG(strm) << "should be silent" << std::endl;
auto txt = ostrm.str();
CHECK(txt.find("should") == std::string::npos);
}
BOOST_LOG(strm) << "should not be silent" << std::endl;
auto txt = ostrm.str();
CHECK(txt.find("should") != std::string::npos);
strm->ClearTee();
}
}
SECTION("enable one") {
for (auto strm : allLogs) {
strm->df_enabled = true;
RDLog::LogStateSetter disabler;
{
RDLog::LogStateSetter justone(RDLog::RDLoggerList({strm}));
for (auto strm2 : allLogs) {
std::stringstream ostrm;
strm2->SetTee(ostrm);
BOOST_LOG(strm2) << "should not be silent" << std::endl;
auto txt = ostrm.str();
if (strm == strm2) {
CHECK(txt.find("should") != std::string::npos);
} else {
CHECK(txt.find("should") == std::string::npos);
}
strm2->ClearTee();
}
}
// should be disabled again
std::stringstream ostrm;
strm->SetTee(ostrm);
BOOST_LOG(strm) << "should be silent" << std::endl;
auto txt = ostrm.str();
CHECK(txt.find("should") == std::string::npos);
strm->ClearTee();
}
}
}
TEST_CASE("GitHub Issue #5172", "[bug][logging]") {
std::stringstream err_ostrm;
std::stringstream warn_ostrm;
rdErrorLog->df_enabled = true;
rdWarningLog->df_enabled = true;
rdErrorLog->SetTee(err_ostrm);
rdWarningLog->SetTee(warn_ostrm);
{
RDLog::LogStateSetter disabler;
BOOST_LOG(rdErrorLog) << "should be silent" << std::endl;
auto txt = err_ostrm.str();
CHECK(txt.find("should") == std::string::npos);
BOOST_LOG(rdWarningLog) << "should be silent" << std::endl;
txt = warn_ostrm.str();
CHECK(txt.find("should") == std::string::npos);
{
// The second setter overrides the first one:
RDLog::LogStateSetter disabler2({rdWarningLog});
BOOST_LOG(rdErrorLog) << "should be silent" << std::endl;
txt = err_ostrm.str();
CHECK(txt.find("should") == std::string::npos);
BOOST_LOG(rdWarningLog) << "should not be silent" << std::endl;
txt = warn_ostrm.str();
CHECK(txt.find("should") != std::string::npos);
warn_ostrm.clear();
}
}
// Both setters are destroyed, and we revert to initial state
BOOST_LOG(rdErrorLog) << "should not be silent" << std::endl;
auto txt = err_ostrm.str();
CHECK(txt.find("should") != std::string::npos);
BOOST_LOG(rdWarningLog) << "should not be silent" << std::endl;
txt = warn_ostrm.str();
CHECK(txt.find("should") != std::string::npos);
rdErrorLog->ClearTee();
rdWarningLog->ClearTee();
}
|