/*************************************************************************** * * Copyright (c) 2009 Baidu.com, Inc. All Rights Reserved * $Id: bsl_test_exception_prepare.h_,v 1.2 2009/06/15 06:29:05 chenxm Exp $ * **************************************************************************/ /** * @file bsl_test_exception_prepare.h * @author chenxm(chenxiaoming@baidu.com) * @date 2009/04/25 15:58:28 * @version $Revision: 1.2 $ * @brief * **/ #ifndef __BSL_TEST_EXCEPTION_PREPARE_H_ #define __BSL_TEST_EXCEPTION_PREPARE_H_ /** * @brief 把异常打印到日志的宏函数 * fmrt必须是C风格格式化字符串 * args必须是C风格可变参数 * */ #define BSL_LOG_EXCEPTION(__bsl_bsl_exception_instance__, fmt, args...) \ __BSL_LOG_WARNING__( \ "%s thrown from %s:%d:%s is caught! "fmt" message:%s stack:%s", \ (__bsl_bsl_exception_instance__).name(), \ (__bsl_bsl_exception_instance__).file(), \ (__bsl_bsl_exception_instance__).line(), \ (__bsl_bsl_exception_instance__).function(), \ (__bsl_bsl_exception_instance__).what(), \ (__bsl_bsl_exception_instance__).stack() \ ) \ /** * @brief 将异常转化成错误码并打印日志的宏函数 * * 该宏旨在帮助程序员把C程序库平滑地升级为C++程序库,C++程序之间避免使用该宏。 * 若代码(code)不抛异常,errno会被置为0; * 若代码(code)抛出异常,errno会被置为-1,并且打印详细的错误日志。 * 强烈建议使用此宏后,检查errno * 强烈建议代码(code)只是一句函数调用。 */ #define BSL_E2NO(errno, code) \ do{ \ try{ \ code; \ (errno) = 0; \ }catch(bsl::Exception& __bsl_bsl_exception_instance__){ /*long name to prevent naming confliction*/ \ (errno) = -1; \ BSL_LOG_EXCEPTION(__bsl_bsl_exception_instance__, #errno" is set to -1!"); \ }catch(std::exception& __bsl_std_exception_instance__){ /*long name to prevent naming confliction*/ \ (errno) = -1; \ __BSL_LOG_FATAL__( "std::exception[%s] is caught! "#errno" is set to -1! Definitely a BUG!", \ __bsl_std_exception_instance__.what() ); \ }catch(...){ \ (errno) = -1; \ __BSL_LOG_FATAL__("unknown exception is caught! "#errno" is set to -1! Definitely a FATAL BUG!"); \ } \ }while(0) #define UB_LOG_WARNING( fmt, arg... ) \ do{ \ fprintf( stderr, "UB_LOG_WARNING:"fmt"\n", ##arg ); \ }while(0) #define UB_LOG_FATAL( fmt, arg... ) \ do{ \ fprintf( stderr, "UB_LOG_FATAL:"fmt"\n", ##arg ); \ }while(0) void call_nothrow(){ //do nothing } template void call_throw(){ throw Exception()< #include #define see(x) do{ \ std::cerr<<__FILE__<<":"<<__LINE__<<": " <<(#x)<<" = "<<(x)<<" ("< void throw_func_push( int depth ){ if ( depth <= 0 ){ throw ExceptionT().push(1000000, 'c').push(BSL_EARG).push(bsl::EXCEPTION_LEVEL_FATAL); } throw_func_push( depth - 1 ); } template void throw_func_shift( int depth ){ if ( depth <= 0 ){ throw ExceptionT()<<"acumon"<<7336<<-123<<"abc"<( depth - 1 ); } #endif //__BSL_TEST_EXCEPTION_PREPARE_H_ /* vim: set ts=4 sw=4 sts=4 tw=100 */