diff --git a/cxx/dbenv.cpp b/cxx/dbenv.cpp index ec83c96ef44..1f6b33423ff 100644 --- a/cxx/dbenv.cpp +++ b/cxx/dbenv.cpp @@ -1,5 +1,6 @@ #include #include +#include DbEnv::DbEnv (u_int32_t flags) : do_no_exceptions((flags&DB_CXX_NO_EXCEPTIONS)!=0) @@ -67,6 +68,21 @@ int DbEnv::maybe_throw_error(int err) { if (err==0) return 0; if (do_no_exceptions) return err; DbException e(err); - e.set_env(the_env); + e.set_env(this); throw e; } + +extern "C" { + void toku_db_env_err_vararg(const DB_ENV * env, int error, const char *fmt, va_list ap); +}; + +void DbEnv::err(int error, const char *fmt, ...) { + va_list ap; + va_start(ap, fmt); + toku_db_env_err_vararg(the_env, error, fmt, ap); + va_end(ap); +} + +void DbEnv::set_errfile(FILE *errfile) { + the_env->set_errfile(the_env, errfile); +} diff --git a/cxx/exception.cpp b/cxx/exception.cpp index 4d4f7bbe813..8f8fc5f47bc 100644 --- a/cxx/exception.cpp +++ b/cxx/exception.cpp @@ -36,3 +36,8 @@ DbEnv *DbException::get_env() const { return the_env; } + +void DbException::set_env(DbEnv *new_env) +{ + the_env = new_env; +} diff --git a/cxx/tests/test1.cpp b/cxx/tests/test1.cpp index 202a19edd19..1a8ca932cc7 100644 --- a/cxx/tests/test1.cpp +++ b/cxx/tests/test1.cpp @@ -27,7 +27,10 @@ int cmp(DB *db, const DBT *dbt1, const DBT *dbt2) { } void test_db(void) { - Db db(NULL, 0); + DbEnv env(DB_CXX_NO_EXCEPTIONS); + env.open(NULL, DB_PRIVATE, 0666); + Db db(&env, 0); + int r; r = db.set_bt_compare(cmp); assert(r == 0); @@ -35,13 +38,15 @@ void test_db(void) { } void test_db_env(void) { - DbEnv dbenv(0); + DbEnv dbenv(DB_CXX_NO_EXCEPTIONS); int r; r = dbenv.set_data_dir("."); assert(r == 0); r = dbenv.set_data_dir(".."); assert(r == 0); r = dbenv.set_data_dir(NULL); assert(r == EINVAL); dbenv.set_errpfx("Prefix"); + dbenv.set_errfile(stdout); + dbenv.err(0, "Hello %s!\n", "Name"); } int main() diff --git a/include/db_cxx.h b/include/db_cxx.h index 3bac879b838..f4dc833dba6 100644 --- a/include/db_cxx.h +++ b/include/db_cxx.h @@ -99,6 +99,8 @@ class DbEnv { int txn_begin(DbTxn *, DbTxn **, u_int32_t); int set_data_dir(const char *dir); void set_errpfx(const char *errpfx); + void err(int error, const char *fmt, ...); + void set_errfile(FILE *errfile); private: int do_no_exceptions; @@ -146,6 +148,6 @@ class DbException : public std::exception int the_err; DbEnv *the_env; void FillTheWhat(void); - void set_env(DB_ENV *); + void set_env(DbEnv *); }; diff --git a/src/ydb.c b/src/ydb.c index b88284fe713..961eda6bcc4 100644 --- a/src/ydb.c +++ b/src/ydb.c @@ -59,12 +59,17 @@ static void do_error (DB_ENV *dbenv, const char *string) { fprintf(dbenv->i->errfile, "%s\n", string); } +void toku_db_env_err_vararg(const DB_ENV * env, int error, const char *fmt, va_list ap) { + FILE* ferr = env->i->errfile ? env->i->errfile : stderr; + if (env->i->errpfx && env->i->errpfx[0] != '\0') fprintf(stderr, "%s: ", env->i->errpfx); + fprintf(ferr, "YDB Error %d: ", error); + vfprintf(ferr, fmt, ap); +} + static void toku_db_env_err(const DB_ENV * env, int error, const char *fmt, ...) { va_list ap; va_start(ap, fmt); - if (env->i->errpfx && env->i->errpfx[0] != '\0') fprintf(stderr, "%s: ", env->i->errpfx); - fprintf(stderr, "YDB Error %d: ", error); - vfprintf(stderr, fmt, ap); + toku_db_env_err_vararg(env, error, fmt, ap); va_end(ap); }