This example_log page provides an example of librnr support for logging and command-line options processing.
Example main():
#include <stdio.h>
#include <string.h>
#include <libgen.h>
static int OptsTest = 1;
{
.
synopsis =
"Example of librnr logging and options processing facilities.",
.long_desc =
"The %P command demonstrates various macros and functions of the "
"librnr logging facilities, plus command-line options processing."
};
{
{
.short_opt = 't',
.has_arg = required_argument,
.has_default= true,
.arg_name = "<test>",
.opt_desc =
"Set the logging test(s) to conduct.\n"
"%A is one of:\n"
" 0 - No tests.\n"
" 1 - Test #1.\n"
" 2 - Test #2.\n"
" 3 - All tests."
},
};
static void logtest_num1(
void *p,
const char *sFruit,
int n,
int half,
double f,
int hex,
bool_t bAreRipe)
{
if( bAreRipe )
{
LOGDIAG2(
"There are %d juicy %s in %s", n, sFruit,
"Tuscany");
}
else
{
LOGDIAG2(
"There are %d green %s in %s", n, sFruit,
"Utah");
}
}
{
FILE *fp;
const char *sFileName = "PackingShedOnStrike.newsflash";
LOGWARN(
"Ebola is your friend, Mr. %s.",
"Sicko");
while( count-- > 0 )
{
LOGERROR(
"Salmonella scare %d...", count);
}
if( (fp = fopen(sFileName, "r")) == NULL )
{
}
else
{
LOGDIAG4(
"%s: what a pleasant surprise", sFileName);
fclose(fp);
}
}
{
short h = 10;
int i = 12;
switch( OptsTest )
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
default:
LOGERROR(
"bug: %d: unexpected test", OptsTest);
return 0;
}
return 1;
}
static int init(
int argc,
char *argv[])
{
FILE *fp;
int i;
Argv0 = basename(argv[0]);
argv =
OptsGet(Argv0, &
PkgInfo, &LogExamplePgmInfo, LogExampleOptsInfo,
true,
&argc, argv);
CHKEXPR_INT(OptsTest, ((OptsTest >= 0) || (OptsTest <= 3)), 0);
{
LOGDIAG1(
"Post options processed non-option arguments:");
for(i=0; i<argc; ++i)
{
fprintf(fp, " argv[%d]=\"%s\"\n", i, argv[i]);
}
}
return 1;
}
int main(
int argc,
char *argv[])
{
printf("arch : %s\n", ARCH);
{
return 3;
}
}
Generated output for run #1:
$ ../loc/bin.x86_64/example_log -l diag2 -t 3 peaches plums 2>example_log.run1
librnr: Diag1: log.c:108: Logging level set to 3
logexample: Diag1: example_log.c:152: Post options processed non-option arguments:
argv[0]="peaches"
argv[1]="plums"
logexample: Diag1: example_log.c:62: logtest_num1(p=0x7ffff37e7bbe,\
sFruit="pears",n=12,half=10,f=-0.015000,hex=0xf1,bAreRipe=true)
logexample: Diag2: example_log.c:67: There are 12 juicy pears in Tuscany
logexample: Diag2: example_log.c:84: logtest_num2(count=3)
logexample: Error: example_log.c:89: Salmonella scare 2...
logexample: Error: example_log.c:89: Salmonella scare 1...
logexample: Error: example_log.c:89: Salmonella scare 0...
logexample: Error: example_log.c:95: No such file or directory(errno=2):\
PackingShedOnStrike.newsflash
Generated output for run #2:
$ ../loc/bin.x86_64/example_log --help
Usage: example_log [OPTIONS]
Example of librnr logging and options processing facilities.
Mandatory arguments to long options are also mandatory for short options.
-t, --test=<test> Set the logging test(s) to conduct.
<test> is one of:
0 - No tests.
1 - Test #1.
2 - Test #2.
3 - All tests.
DEFAULT: 1
-l, --log=<level> Set logging threshold level. All logging events
with priority <= <level> will be logged. All
others will be ignored. Error events are
always logged.
<level> is one of:
'off' or 0 - Disable all logging.
'error' or 1 - Enable error logging.
'diag1' or 2 - Enable diagnostics 1 logging.
'diag2' or 3 - Enable diagnostics 2 logging.
'diag3' or 4 - Enable diagnostics 3 logging.
>4 - Enable user-defined logging.
DEFAULT: off
--logfile=<file> Set log file <file>.
Special <file> names:
'stderr' - log to standard error.
'stdout' - log to standard output.
DEFAULT: stderr
--help Display this help and exit.
--version Output version information and exit.
The example_log command demonstrates various macros and functions of the
librnr logging facilities, plus command-line options processing.