Add callback interface to re-direct console output (#3438)

* Add callback interface to re-direct console output

* Exempt TrackerLogger from custom logging

* Fix lint
This commit is contained in:
Philip Hyunsu Cho
2018-07-05 11:32:30 -07:00
committed by GitHub
parent 45bf4fbffb
commit 48d6e68690
7 changed files with 64 additions and 5 deletions

View File

@@ -96,6 +96,15 @@ XGB_EXTERN_C typedef int XGBCallbackDataIterNext( // NOLINT(*)
*/
XGB_DLL const char *XGBGetLastError(void);
/*!
* \brief register callback function for LOG(INFO) messages -- helpful messages
* that are not errors.
* Note: this function can be called by multiple threads. The callback function
* will run on the thread that registered it
* \return 0 for success, -1 for failure
*/
XGB_DLL int XGBRegisterLogCallback(void (*callback)(const char*));
/*!
* \brief load a data matrix
* \param fname the name of the file

View File

@@ -9,6 +9,7 @@
#define XGBOOST_LOGGING_H_
#include <dmlc/logging.h>
#include <dmlc/thread_local.h>
#include <sstream>
#include "./base.h"
@@ -37,6 +38,23 @@ class TrackerLogger : public BaseLogger {
~TrackerLogger();
};
class LogCallbackRegistry {
public:
using Callback = void (*)(const char*);
LogCallbackRegistry()
: log_callback_([] (const char* msg) { std::cerr << msg << std::endl; }) {}
inline void Register(Callback log_callback) {
this->log_callback_ = log_callback;
}
inline Callback Get() const {
return log_callback_;
}
private:
Callback log_callback_;
};
using LogCallbackRegistryStore = dmlc::ThreadLocalStore<LogCallbackRegistry>;
// redefines the logging macro if not existed
#ifndef LOG
#define LOG(severity) LOG_##severity.stream()