What is good practice for generating verbose output?

前端 未结 5 1063
庸人自扰
庸人自扰 2021-02-19 03:49

what is good practice for generating verbose output? currently, i have a function

bool verbose;
int setVerbose(bool v)
{
    errormsg = \"\";
    verbose = v;
           


        
5条回答
  •  不要未来只要你来
    2021-02-19 04:07

    1. If you are using g++ you could use the -D flag, this allows the compilator to define a macro of your choice.

    Defining the

    For instance :

    #ifdef DEBUG_FLAG
     printf("My error message");
    #endif
    

    2. I agree this isn't elegant either, so to make it a bit nicer :

    void verbose(const char * fmt, ... )
    {
    va_list args;  /* Used as a pointer to the next variable argument. */
    va_start( args, fmt );  /* Initialize the pointer to arguments. */
    
    #ifdef DEBUG_FLAG
    printf(fmt, &args);  
    #endif
    /*This isn't tested, the point is to be able to pass args to 
    printf*/
    }
    

    That you could use like printf :

    verbose("Error number %d\n",errorno);
    

    3. A third solution easier, and more C++ and Unix like is to pass an argument to your program that is going to be used - as the macro earlier - to initialize a particular variable (that could be a global const).

    Example : $ ./myprogram -v

    if(optarg('v')) static const verbose = 1;
    

提交回复
热议问题