Remove use of std::cout from R package (#5261)

This commit is contained in:
Philip Hyunsu Cho 2020-02-01 05:52:19 -08:00 committed by Hyunsu Philip Cho
parent 8e39a675be
commit e7e522fb06
2 changed files with 27 additions and 18 deletions

View File

@ -15,12 +15,23 @@
#include "xgboost/base.h" #include "xgboost/base.h"
#include "xgboost/tree_model.h" #include "xgboost/tree_model.h"
#if defined(XGBOOST_STRICT_R_MODE)
#define OBSERVER_PRINT LOG(INFO)
#define OBSERVER_ENDL ""
#define OBSERVER_NEWLINE ""
#else
#define OBSERVER_PRINT std::cout
#define OBSERVER_ENDL std::endl
#define OBSERVER_NEWLINE "\n"
#endif // defined(XGBOOST_STRICT_R_MODE)
namespace xgboost { namespace xgboost {
/*\brief An observer for logging internal data structures. /*\brief An observer for logging internal data structures.
* *
* This class is designed to be `diff` tool friendly, which means it uses plain * This class is designed to be `diff` tool friendly, which means it uses plain
* `std::cout` for printing to avoid the time information emitted by `LOG(DEBUG)` or * `std::cout` for printing to avoid the time information emitted by `LOG(DEBUG)` or
* similiar facilities. * similiar facilities. Exception: use `LOG(INFO)` for the R package, to comply
* with CRAN policy.
*/ */
class TrainingObserver { class TrainingObserver {
#if defined(XGBOOST_USE_DEBUG_OUTPUT) #if defined(XGBOOST_USE_DEBUG_OUTPUT)
@ -32,17 +43,17 @@ class TrainingObserver {
public: public:
void Update(int32_t iter) const { void Update(int32_t iter) const {
if (XGBOOST_EXPECT(!observe_, true)) { return; } if (XGBOOST_EXPECT(!observe_, true)) { return; }
std::cout << "Iter: " << iter << std::endl; OBSERVER_PRINT << "Iter: " << iter << OBSERVER_ENDL;
} }
/*\brief Observe tree. */ /*\brief Observe tree. */
void Observe(RegTree const& tree) { void Observe(RegTree const& tree) {
if (XGBOOST_EXPECT(!observe_, true)) { return; } if (XGBOOST_EXPECT(!observe_, true)) { return; }
std::cout << "Tree:" << std::endl; OBSERVER_PRINT << "Tree:" << OBSERVER_ENDL;
Json j_tree {Object()}; Json j_tree {Object()};
tree.SaveModel(&j_tree); tree.SaveModel(&j_tree);
std::string str; std::string str;
Json::Dump(j_tree, &str, true); Json::Dump(j_tree, &str, true);
std::cout << str << std::endl; OBSERVER_PRINT << str << OBSERVER_ENDL;
} }
/*\brief Observe tree. */ /*\brief Observe tree. */
void Observe(RegTree const* p_tree) { void Observe(RegTree const* p_tree) {
@ -54,15 +65,15 @@ class TrainingObserver {
template <typename T> template <typename T>
void Observe(std::vector<T> const& h_vec, std::string name) const { void Observe(std::vector<T> const& h_vec, std::string name) const {
if (XGBOOST_EXPECT(!observe_, true)) { return; } if (XGBOOST_EXPECT(!observe_, true)) { return; }
std::cout << "Procedure: " << name << std::endl; OBSERVER_PRINT << "Procedure: " << name << OBSERVER_ENDL;
for (size_t i = 0; i < h_vec.size(); ++i) { for (size_t i = 0; i < h_vec.size(); ++i) {
std::cout << h_vec[i] << ", "; OBSERVER_PRINT << h_vec[i] << ", ";
if (i % 8 == 0) { if (i % 8 == 0) {
std::cout << '\n'; OBSERVER_PRINT << OBSERVER_NEWLINE;
} }
} }
std::cout << std::endl; OBSERVER_PRINT << OBSERVER_ENDL;
} }
/*\brief Observe data hosted by `HostDeviceVector'. */ /*\brief Observe data hosted by `HostDeviceVector'. */
template <typename T> template <typename T>
@ -85,16 +96,16 @@ class TrainingObserver {
if (XGBOOST_EXPECT(!observe_, true)) { return; } if (XGBOOST_EXPECT(!observe_, true)) { return; }
Json obj {toJson(p)}; Json obj {toJson(p)};
std::cout << "Parameter: " << name << ":\n" << obj << std::endl; OBSERVER_PRINT << "Parameter: " << name << ":\n" << obj << OBSERVER_ENDL;
} }
/*\brief Observe parameters provided by users. */ /*\brief Observe parameters provided by users. */
void Observe(Args const& args) const { void Observe(Args const& args) const {
if (XGBOOST_EXPECT(!observe_, true)) { return; } if (XGBOOST_EXPECT(!observe_, true)) { return; }
for (auto kv : args) { for (auto kv : args) {
std::cout << kv.first << ": " << kv.second << "\n"; OBSERVER_PRINT << kv.first << ": " << kv.second << OBSERVER_NEWLINE;
} }
std::cout << std::endl; OBSERVER_PRINT << OBSERVER_ENDL;
} }
/*\brief Get a global instance. */ /*\brief Get a global instance. */

View File

@ -89,10 +89,10 @@ void Monitor::PrintStatistics(StatMap const& statistics) const {
"Timer for " << kv.first << " did not get stopped properly."; "Timer for " << kv.first << " did not get stopped properly.";
continue; continue;
} }
std::cout << kv.first << ": " << static_cast<double>(kv.second.second) / 1e+6 LOG(CONSOLE) << kv.first << ": " << static_cast<double>(kv.second.second) / 1e+6
<< "s, " << kv.second.first << " calls @ " << "s, " << kv.second.first << " calls @ "
<< kv.second.second << kv.second.second
<< "us" << std::endl; << "us" << std::endl;
} }
} }
@ -107,10 +107,9 @@ void Monitor::Print() const {
if (rabit::GetRank() == 0) { if (rabit::GetRank() == 0) {
LOG(CONSOLE) << "======== Monitor: " << label << " ========"; LOG(CONSOLE) << "======== Monitor: " << label << " ========";
for (size_t i = 0; i < world.size(); ++i) { for (size_t i = 0; i < world.size(); ++i) {
std::cout << "From rank: " << i << ": " << std::endl; LOG(CONSOLE) << "From rank: " << i << ": " << std::endl;
auto const& statistic = world[i]; auto const& statistic = world[i];
this->PrintStatistics(statistic); this->PrintStatistics(statistic);
std::cout << std::endl;
} }
} }
} else { } else {
@ -123,7 +122,6 @@ void Monitor::Print() const {
LOG(CONSOLE) << "======== Monitor: " << label << " ========"; LOG(CONSOLE) << "======== Monitor: " << label << " ========";
this->PrintStatistics(stat_map); this->PrintStatistics(stat_map);
} }
std::cout << std::endl;
} }
} // namespace common } // namespace common