1

This is a very n00b question but I'm writing a nix based tool and would like to have verbosity flags, based on the number of vvv's passed in I would go about printing debug/information statements in my program.

My question is how would I go about using opargs for this, since optargs can only parse one character at a time.

Also suppose I know I'm at verbosity level 3, do all my print statements have to be in an if condition? Or there a clever way of going about it using the pre-processor?

Also if someone could point me to some code online which does this, that would be awesome.

Thanks

I figure it out, thought I'd post here if someone else comes across this in the future:

Basically for all my different verbosity statements I defined a special print using the preprocessor like:

#define dprintf \
    if (verbosity == 1) printf

I then put in the statements as needed in the code e.g.

dprintf ("Verbosity is at level 1.");

My opt atgs looks something like this

case 'v':
    verbosity++;
break;
4

3 回答 3

3

详细级别在编译时是未知的,因此您需要准备好代码来处理用户选择的任何级别。

一种简单且易于理解的方法是在不透明的编译单元中分离您的日志记录功能,并使用静态变量跟踪详细程度。然后,您可以使用“set_logging_level(level)”之类的内容对其进行初始化,并编写由该静态变量保护的日志记录函数。然后,您只需公开初始化和日志记录功能,并在代码中根据需要使用它们。

static level = 0;
void set_logging_level(int l) { level = l; }

void log_info(char* msg) {
  // Will always print
}

void log_debug(char *msg) {
  if(level > 0)
    // Write to stdout or stderr, whichever fits
}

void log_details(char *msg) {
  if(level > 1)
    // As above
}

void log_insanity(char *msg) {
  if(level > 2)
    // As above
}

编辑:更健全的记录条件。特别是如果您想要在详细级别上升时包含日志记录......

于 2012-04-16T16:12:48.340 回答
1

条件编译怎么样?

您还可以通过为详细级别设置一个数字而不是传递那么多v来简化。

#if VERBOSE_LEVEL == 3
  print("A verbose message");
#endif
于 2012-04-16T16:06:32.430 回答
0

我不太确定这是否是您的意思,但这就是我在另一个项目中实现它的方式:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define TRUE    1
#define FALSE   0

int usage( char *name, int quit );

int main (int argc, char **argv) {
    int c;
    static int vlevel = 0;

    while ( (c = getopt(argc, argv, ":abc:d:hv012")) != -1) {
        int this_option_optind = optind ? optind : 1;
        switch (c) {

            case 'v':
                vlevel++;
                printf ("verbosity level is %d\n", vlevel);
                break;

            case ':':       /* Option without required operand */
                fprintf(stderr, "option -%c requires an operand\n", optopt);
                break;

            case 'h':
            case '?':
                usage( argv[0], TRUE );
                break;

            default:
                printf ("?? getopt returned character code 0%o ??\n", c);
        }
    }

    if (optind < argc) {
        printf ("non-option ARGV-elements:\n");
        while (optind < argc)
            printf ("\t%s\n", argv[optind++]);
    }

    exit (0);
}

int usage( char *progname, int quit )
{
    printf ( "Usage:\n\t%s [-vh]\n", progname );
    if ( quit ) exit( 1 );
    return 0;
}

这将为您提供以下内容:

eroux@smaug:~$ ./testverbose -h
Usage:
    ./testverbose [-vh]
eroux@smaug:~$ ./testverbose -vvvv
verbosity level is 1
verbosity level is 2
verbosity level is 3
verbosity level is 4
eroux@smaug:~$ 

从那里您应该能够使用 vlevel 变量 [in main()] 在相关详细级别期间打印正确的消息。

于 2012-04-16T17:15:21.930 回答