sync dmlc headers

This commit is contained in:
tqchen 2017-03-16 10:16:23 -07:00
parent 21b5e12913
commit a764d45cfb
4 changed files with 126 additions and 30 deletions

View File

@ -37,6 +37,14 @@
#define DMLC_LOG_CUSTOMIZE 0 #define DMLC_LOG_CUSTOMIZE 0
#endif #endif
/*!
* \brief Wheter to print stack trace for fatal error,
* enabled on linux when using gcc.
*/
#if (!defined(DMLC_LOG_STACK_TRACE) && defined(__GNUC__) && !defined(__MINGW32__))
#define DMLC_LOG_STACK_TRACE 1
#endif
/*! \brief whether compile with hdfs support */ /*! \brief whether compile with hdfs support */
#ifndef DMLC_USE_HDFS #ifndef DMLC_USE_HDFS
#define DMLC_USE_HDFS 0 #define DMLC_USE_HDFS 0
@ -54,13 +62,39 @@
/*! \brief whether or not use c++11 support */ /*! \brief whether or not use c++11 support */
#ifndef DMLC_USE_CXX11 #ifndef DMLC_USE_CXX11
#define DMLC_USE_CXX11 (defined(__GXX_EXPERIMENTAL_CXX0X__) ||\ #if defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(_MSC_VER)
__cplusplus >= 201103L || defined(_MSC_VER)) #define DMLC_USE_CXX11 1
#else
#define DMLC_USE_CXX11 (__cplusplus >= 201103L)
#endif
#endif #endif
/*! \brief strict CXX11 support */ /*! \brief strict CXX11 support */
#ifndef DMLC_STRICT_CXX11 #ifndef DMLC_STRICT_CXX11
#define DMLC_STRICT_CXX11 (__cplusplus >= 201103L || defined(_MSC_VER)) #if defined(_MSC_VER)
#define DMLC_STRICT_CXX11 1
#else
#define DMLC_STRICT_CXX11 (__cplusplus >= 201103L)
#endif
#endif
/*! \brief Whether cxx11 thread local is supported */
#ifndef DMLC_CXX11_THREAD_LOCAL
#if defined(_MSC_VER)
#if (_MSC_VER >= 1900)
#define DMLC_CXX11_THREAD_LOCAL 1
#else
#define DMLC_CXX11_THREAD_LOCAL 0
#endif
#else
#define DMLC_CXX11_THREAD_LOCAL (__cplusplus >= 201103L)
#endif
#endif
/*! \brief whether RTTI is enabled */
#ifndef DMLC_ENABLE_RTTI
#define DMLC_ENABLE_RTTI 1
#endif #endif
/// check if g++ is before 4.6 /// check if g++ is before 4.6
@ -85,7 +119,7 @@
/*! \brief whether enable regex support, actually need g++-4.9 or higher*/ /*! \brief whether enable regex support, actually need g++-4.9 or higher*/
#ifndef DMLC_USE_REGEX #ifndef DMLC_USE_REGEX
#define DMLC_USE_REGEX (__cplusplus >= 201103L || defined(_MSC_VER)) #define DMLC_USE_REGEX DMLC_STRICT_CXX11
#endif #endif
/*! \brief helper macro to supress unused warning */ /*! \brief helper macro to supress unused warning */
@ -201,7 +235,7 @@ inline T *BeginPtr(std::vector<T> &vec) { // NOLINT(*)
} }
} }
/*! /*!
* \brief get the beginning address of a vector * \brief get the beginning address of a const vector
* \param vec input vector * \param vec input vector
* \return beginning address of a vector * \return beginning address of a vector
*/ */
@ -214,7 +248,7 @@ inline const T *BeginPtr(const std::vector<T> &vec) {
} }
} }
/*! /*!
* \brief get the beginning address of a vector * \brief get the beginning address of a string
* \param str input string * \param str input string
* \return beginning address of a string * \return beginning address of a string
*/ */
@ -223,7 +257,7 @@ inline char* BeginPtr(std::string &str) { // NOLINT(*)
return &str[0]; return &str[0];
} }
/*! /*!
* \brief get the beginning address of a vector * \brief get the beginning address of a const string
* \param str input string * \param str input string
* \return beginning address of a string * \return beginning address of a string
*/ */

View File

@ -11,6 +11,7 @@
#include <istream> #include <istream>
#include <ostream> #include <ostream>
#include <streambuf> #include <streambuf>
#include "./logging.h"
// include uint64_t only to make io standalone // include uint64_t only to make io standalone
#ifdef _MSC_VER #ifdef _MSC_VER
@ -149,6 +150,8 @@ class InputSplit {
* \param chunk_size the chunk size * \param chunk_size the chunk size
*/ */
virtual void HintChunkSize(size_t chunk_size) {} virtual void HintChunkSize(size_t chunk_size) {}
/*! \brief get the total size of the InputSplit */
virtual size_t GetTotalSize(void) = 0;
/*! \brief reset the position of InputSplit to beginning */ /*! \brief reset the position of InputSplit to beginning */
virtual void BeforeFirst(void) = 0; virtual void BeforeFirst(void) = 0;
/*! /*!
@ -243,7 +246,7 @@ class ostream : public std::basic_ostream<char> {
this->set_stream(stream); this->set_stream(stream);
} }
// explictly synchronize the buffer // explictly synchronize the buffer
virtual ~ostream() { virtual ~ostream() DMLC_NO_EXCEPTION {
buf_.pubsync(); buf_.pubsync();
} }
/*! /*!
@ -313,7 +316,7 @@ class istream : public std::basic_istream<char> {
: std::basic_istream<char>(NULL), buf_(buffer_size) { : std::basic_istream<char>(NULL), buf_(buffer_size) {
this->set_stream(stream); this->set_stream(stream);
} }
virtual ~istream() {} virtual ~istream() DMLC_NO_EXCEPTION {}
/*! /*!
* \brief set internal stream to be stream, reset states * \brief set internal stream to be stream, reset states
* \param stream new stream as output * \param stream new stream as output

View File

@ -14,6 +14,10 @@
#include <stdexcept> #include <stdexcept>
#include "./base.h" #include "./base.h"
#if DMLC_LOG_STACK_TRACE
#include <execinfo.h>
#endif
namespace dmlc { namespace dmlc {
/*! /*!
* \brief exception class that will be thrown by * \brief exception class that will be thrown by
@ -28,16 +32,6 @@ struct Error : public std::runtime_error {
}; };
} // namespace dmlc } // namespace dmlc
#if defined(_MSC_VER) && _MSC_VER < 1900
#define noexcept(a)
#endif
#if DMLC_USE_CXX11
#define DMLC_THROW_EXCEPTION noexcept(false)
#else
#define DMLC_THROW_EXCEPTION
#endif
#if DMLC_USE_GLOG #if DMLC_USE_GLOG
#include <glog/logging.h> #include <glog/logging.h>
@ -63,21 +57,54 @@ inline void InitLogging(const char* argv0) {
#endif #endif
namespace dmlc { namespace dmlc {
inline void InitLogging(const char* argv0) { inline void InitLogging(const char*) {
// DO NOTHING // DO NOTHING
} }
class LogCheckError {
public:
LogCheckError() : str(nullptr) {}
explicit LogCheckError(const std::string& str_) : str(new std::string(str_)) {}
~LogCheckError() { if (str != nullptr) delete str; }
operator bool() {return str != nullptr; }
std::string* str;
};
#define DEFINE_CHECK_FUNC(name, op) \
template <typename X, typename Y> \
inline LogCheckError LogCheck##name(const X& x, const Y& y) { \
if (x op y) return LogCheckError(); \
std::ostringstream os; \
os << " (" << x << " vs. " << y << ") "; /* CHECK_XX(x, y) requires x and y can be serialized to string. Use CHECK(x OP y) otherwise. NOLINT(*) */ \
return LogCheckError(os.str()); \
} \
inline LogCheckError LogCheck##name(int x, int y) { \
return LogCheck##name<int, int>(x, y); \
}
#define CHECK_BINARY_OP(name, op, x, y) \
if (dmlc::LogCheckError _check_err = dmlc::LogCheck##name(x, y)) \
dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \
<< "Check failed: " << #x " " #op " " #y << *(_check_err.str)
DEFINE_CHECK_FUNC(_LT, <)
DEFINE_CHECK_FUNC(_GT, >)
DEFINE_CHECK_FUNC(_LE, <=)
DEFINE_CHECK_FUNC(_GE, >=)
DEFINE_CHECK_FUNC(_EQ, ==)
DEFINE_CHECK_FUNC(_NE, !=)
// Always-on checking // Always-on checking
#define CHECK(x) \ #define CHECK(x) \
if (!(x)) \ if (!(x)) \
dmlc::LogMessageFatal(__FILE__, __LINE__).stream() << "Check " \ dmlc::LogMessageFatal(__FILE__, __LINE__).stream() \
"failed: " #x << ' ' << "Check failed: " #x << ' '
#define CHECK_LT(x, y) CHECK((x) < (y)) #define CHECK_LT(x, y) CHECK_BINARY_OP(_LT, <, x, y)
#define CHECK_GT(x, y) CHECK((x) > (y)) #define CHECK_GT(x, y) CHECK_BINARY_OP(_GT, >, x, y)
#define CHECK_LE(x, y) CHECK((x) <= (y)) #define CHECK_LE(x, y) CHECK_BINARY_OP(_LE, <=, x, y)
#define CHECK_GE(x, y) CHECK((x) >= (y)) #define CHECK_GE(x, y) CHECK_BINARY_OP(_GE, >=, x, y)
#define CHECK_EQ(x, y) CHECK((x) == (y)) #define CHECK_EQ(x, y) CHECK_BINARY_OP(_EQ, ==, x, y)
#define CHECK_NE(x, y) CHECK((x) != (y)) #define CHECK_NE(x, y) CHECK_BINARY_OP(_NE, !=, x, y)
#define CHECK_NOTNULL(x) \ #define CHECK_NOTNULL(x) \
((x) == NULL ? dmlc::LogMessageFatal(__FILE__, __LINE__).stream() << "Check notnull: " #x << ' ', (x) : (x)) // NOLINT(*) ((x) == NULL ? dmlc::LogMessageFatal(__FILE__, __LINE__).stream() << "Check notnull: " #x << ' ', (x) : (x)) // NOLINT(*)
// Debug-only checking. // Debug-only checking.
@ -221,6 +248,20 @@ class LogMessageFatal : public LogMessage {
public: public:
LogMessageFatal(const char* file, int line) : LogMessage(file, line) {} LogMessageFatal(const char* file, int line) : LogMessage(file, line) {}
~LogMessageFatal() { ~LogMessageFatal() {
#if DMLC_LOG_STACK_TRACE
const int MAX_STACK_SIZE = 10;
void *stack[MAX_STACK_SIZE];
int nframes = backtrace(stack, MAX_STACK_SIZE);
log_stream_ << "\n\n" << "Stack trace returned " << nframes << " entries:\n";
char **msgs = backtrace_symbols(stack, nframes);
if (msgs != nullptr) {
for (int i = 0; i < nframes; ++i) {
log_stream_ << "[bt] (" << i << ") " << msgs[i] << "\n";
}
}
#endif
log_stream_ << "\n"; log_stream_ << "\n";
abort(); abort();
} }
@ -238,6 +279,20 @@ class LogMessageFatal {
} }
std::ostringstream &stream() { return log_stream_; } std::ostringstream &stream() { return log_stream_; }
~LogMessageFatal() DMLC_THROW_EXCEPTION { ~LogMessageFatal() DMLC_THROW_EXCEPTION {
#if DMLC_LOG_STACK_TRACE
const int MAX_STACK_SIZE = 10;
void *stack[MAX_STACK_SIZE];
int nframes = backtrace(stack, MAX_STACK_SIZE);
log_stream_ << "\n\n" << "Stack trace returned " << nframes << " entries:\n";
char **msgs = backtrace_symbols(stack, nframes);
if (msgs != nullptr) {
for (int i = 0; i < nframes; ++i) {
log_stream_ << "[bt] (" << i << ") " << msgs[i] << "\n";
}
}
#endif
// throwing out of destructor is evil // throwing out of destructor is evil
// hopefully we can do it here // hopefully we can do it here
// also log the message before throw // also log the message before throw

View File

@ -15,9 +15,13 @@
// whether or not use c++11 support // whether or not use c++11 support
#ifndef DMLC_USE_CXX11 #ifndef DMLC_USE_CXX11
#define DMLC_USE_CXX11 (defined(__GXX_EXPERIMENTAL_CXX0X__) ||\ #if defined(__GXX_EXPERIMENTAL_CXX0X__) || defined(_MSC_VER)
__cplusplus >= 201103L || defined(_MSC_VER)) #define DMLC_USE_CXX11 1
#else
#define DMLC_USE_CXX11 (__cplusplus >= 201103L)
#endif #endif
#endif
// optionally support of lambda functions in C++11, if available // optionally support of lambda functions in C++11, if available
#if DMLC_USE_CXX11 #if DMLC_USE_CXX11
#include <functional> #include <functional>