MoustacheSpy
MoustacheSpy

Reputation: 763

Fastest way to make console output "verbose" or not

I am making a small system and I want to be able to toggle "verbose" text output in the whole system.

I have made a file called globals.h:

namespace REBr{
    extern bool console_verbose = false;
}

If this is true I want all my classes to print a message to the console when they are constructing, destructing, copying or doing pretty much anything.

For example:

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;
}

Now as you can see I have a lot of ifs in that constructor. And I REALLY dont want that since that will slow down my application. I need this to be as fast as possible without removing the "loading bar" (this helps me determine at which function the program stopped functioning).

What is the best/fastest way to accomplish this?


Everying in my system is under the namespace REBr

Upvotes: 0

Views: 1456

Answers (3)

Alexey
Alexey

Reputation: 1236

Some variants to achieve that:

  1. Use some logger library. It is the best option as it gives you maximum flexibility and some useful experience ;) And you haven't to devise something. For example, look at Google GLOG.
  2. Define some macro, allowing you to turn on/off all these logs by changing only the macro. But it isn't so easy to write such marco correctly.
  3. Mark your conditional flag as constexpr. That way you may switch the flag and, depending on its value, compiler will optimise ifs in compiled program. But ifs will still be in code, so it looks kinda bulky.

Anyway, all these options require program recompilation. W/o recompilation it is impossible to achieve the maximum speed.

Upvotes: 1

Curious
Curious

Reputation: 21560

Google's logging module is widely used in the industry and supports logging levels that you can set from the command line. For example (taken from their documentation)

VLOG(1) << "I'm printed when you run the program with --v=1 or higher";
VLOG(2) << "I'm printed when you run the program with --v=2 or higher";

You can find the code here https://github.com/google/glog and the documentation in the doc/ folder.

Upvotes: 0

Bruce
Bruce

Reputation: 2310

I often use a Logger class that supports debug levels. A call might look like:

logger->Log(debugLevel, "%s %s %d %d", timestamp, msg, value1, value2);

The Logger class supports multiple debug levels so that I can fine tune the debug output. This can be set at any time through the command line or with a debugger. The Log statement uses a variable length argument list much like printf.

Upvotes: 0

Related Questions