better error handling

This commit is contained in:
tqchen@graphlab.com 2014-08-25 15:58:52 -07:00
parent 6da62159d0
commit c2484f3134
3 changed files with 51 additions and 20 deletions

View File

@ -11,7 +11,7 @@ else
endif endif
# expose these flags to R CMD SHLIB # expose these flags to R CMD SHLIB
export PKG_CPPFLAGS = $(CFLAGS) export PKG_CPPFLAGS = $(CFLAGS) -DXGBOOST_CUSTOMIZE_ERROR_
# specify tensor path # specify tensor path
BIN = xgboost BIN = xgboost

View File

@ -11,14 +11,10 @@
#include <cstdlib> #include <cstdlib>
#ifdef _MSC_VER #ifdef _MSC_VER
#define fopen64 fopen #define fopen64 fopen
// temporal solution for MSVC // NOTE: sprintf_s is not equivalent to snprintf,
inline int snprintf(char *ptr, size_t sz, const char *fmt, ...) { // they are equivalent when success, which is sufficient for our case
va_list args; #define snprintf sprintf_s
va_start(args, fmt); #define vsnprintf vsprintf_s
int ret = vsprintf(ptr, fmt, args);
va_end(args);
return ret;
}
#else #else
#ifdef _FILE_OFFSET_BITS #ifdef _FILE_OFFSET_BITS
#if _FILE_OFFSET_BITS == 32 #if _FILE_OFFSET_BITS == 32
@ -50,41 +46,65 @@ typedef long int64_t;
namespace xgboost { namespace xgboost {
/*! \brief namespace for helper utils of the project */ /*! \brief namespace for helper utils of the project */
namespace utils { namespace utils {
/*! \brief error message buffer length */
const int kErrorBuffer = 1 << 12;
#ifndef XGBOOST_CUSTOMIZE_ERROR_
/*!
* \brief handling of Assert error, caused by in-apropriate input
* \param msg error message
*/
inline void HandleAssertError(const char *msg) {
fprintf(stderr, "AssertError:%s\n", msg);
exit(-1);
}
/*!
* \brief handling of Check error, caused by in-apropriate input
* \param msg error message
*/
inline void HandleCheckError(const char *msg) {
fprintf(stderr, "%s\n", msg);
exit(-1);
}
#else
// include declarations, some one must implement this
void HandleAssertError(const char *msg);
void HandleCheckError(const char *msg);
#endif
/*! \brief assert an condition is true, use this to handle debug information */ /*! \brief assert an condition is true, use this to handle debug information */
inline void Assert(bool exp, const char *fmt, ...) { inline void Assert(bool exp, const char *fmt, ...) {
if (!exp) { if (!exp) {
std::string msg(kErrorBuffer, '\0');
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
fprintf(stderr, "AssertError:"); vsnprintf(&msg[0], kErrorBuffer, fmt, args);
vfprintf(stderr, fmt, args);
va_end(args); va_end(args);
fprintf(stderr, "\n"); HandleAssertError(msg.c_str());
exit(-1);
} }
} }
/*!\brief same as assert, but this is intended to be used as message for user*/ /*!\brief same as assert, but this is intended to be used as message for user*/
inline void Check(bool exp, const char *fmt, ...) { inline void Check(bool exp, const char *fmt, ...) {
if (!exp) { if (!exp) {
std::string msg(kErrorBuffer, '\0');
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
vfprintf(stderr, fmt, args); vsnprintf(&msg[0], kErrorBuffer, fmt, args);
va_end(args); va_end(args);
fprintf(stderr, "\n"); HandleCheckError(msg.c_str());
exit(-1);
} }
} }
/*! \brief report error message, same as check */ /*! \brief report error message, same as check */
inline void Error(const char *fmt, ...) { inline void Error(const char *fmt, ...) {
{ {
std::string msg(kErrorBuffer, '\0');
va_list args; va_list args;
va_start(args, fmt); va_start(args, fmt);
vfprintf(stderr, fmt, args); vsnprintf(&msg[0], kErrorBuffer, fmt, args);
va_end(args); va_end(args);
fprintf(stderr, "\n"); HandleCheckError(msg.c_str());
exit(-1);
} }
} }

View File

@ -2,13 +2,24 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include <cstring> #include <cstring>
#include "xgboost_wrapper.h"
#include "xgboost_R.h" #include "xgboost_R.h"
#include "xgboost_wrapper.h"
#include "../src/utils/utils.h" #include "../src/utils/utils.h"
#include "../src/utils/omp.h" #include "../src/utils/omp.h"
#include "../src/utils/matrix_csr.h" #include "../src/utils/matrix_csr.h"
using namespace xgboost; using namespace xgboost;
// implements error handling
namespace xgboost {
namespace utils {
void HandleAssertError(const char *msg) {
error("%s", msg);
}
void HandleCheckError(const char *msg) {
error("%s", msg);
}
} // namespace utils
} // namespace xgboost
extern "C" { extern "C" {
void _DMatrixFinalizer(SEXP ext) { void _DMatrixFinalizer(SEXP ext) {