我正在制作一个小型系统,我希望能够在整个系统中切换“详细”文本输出。使控制台输出成为“详细”的最快方法
我做了一个名为globals.h:
namespace REBr{
extern bool console_verbose = false;
}
如果这是真的我希望我所有的类打印一条消息到控制台时,他们正在建设,自毁,复制或做几乎任何东西。
例如:
window(string title="",int width=1280,int height=720):
Width(width),Height(height),title(title)
{
if(console_verbose){
std::cout<<"Generating window #"<<this->instanceCounter;
std::cout<<"-";
}
this->window=SDL_CreateWindow(title.c_str(),0,0,width,height,SDL_WINDOW_OPENGL);
if(console_verbose)
std::cout<<"-";
if(this->window)
{
this->glcontext = SDL_GL_CreateContext(window);
if(console_verbose)
std::cout<<".";
if(this->glcontext==NULL)
{
std::cout<<"FATAL ERROR IN REBr::WINDOW::CONSTR_OPENGLCONTEXT: "<<SDL_GetError()<<std::endl;
}
}
else std::cout<<"FATAL ERROR IN REBr::WINDOW::CONSTR_WINDOW: "<<SDL_GetError()<<std::endl;
if(console_verbose)
std::cout<<">done!"<<endl;
}
现在你可以看到我有很多在构造IFS的。我真的不想这样做,因为这会减慢我的申请。我需要这样做,尽可能快地移除“加载栏”(这有助于我确定程序停止运行的功能)。
什么是最好的/最快的方法来完成这个?
万万在我的系统是命名空间REBr
将''console_verbose'标记为'constexpr'。如果它们的表达式被评估为“false”,那么它显然会导致编译器优化所有的“if”。 – Alexey
此外,如果你想避免输入'if(...)''每一次,只需定义一个宏#define VERB_STREAM if(console_verbose)std :: cout'并使用它像'VERB_STREAM <<“生成窗口#”<< this-> instanceCounter <<' - ';' – chtz
你应该也看到这个问题:https://stackoverflow.com/q/6692238/1632887 – seleciii44