Example of librnr support for logging and command-line options processing.
More...
#include <stdio.h>
#include <string.h>
#include <libgen.h>
#include "rnr/rnrconfig.h"
#include "rnr/log.h"
#include "rnr/opts.h"
#include "version.h"
Go to the source code of this file.
Example of librnr support for logging and command-line options processing.
- LastChangedDate
- 2012-10-31 09:53:31 -0600 (Wed, 31 Oct 2012)
- Rev
- 2497
- Author
- Robin Knight (robin.nosp@m..kni.nosp@m.ght@r.nosp@m.oadn.nosp@m.arrow.nosp@m.s.co.nosp@m.m)
- Copyright
- © 2007-2018. RoadNarrows LLC..
http://www.roadnarrows.com
All Rights Reserved
Definition in file example_log.c.
| static int init |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
|
static |
Main initialization.
- Parameters
-
| argc | Command-line argument count. |
| argv | Command-line argument list. |
- Returns
- Returns 1 on success, exits on failure.
Definition at line 201 of file example_log.c.
References Argv0, CHKEXPR_INT, LOG_GET_LOGFP(), LOG_LEVEL_DIAG1, LOGABLE, LOGDIAG1, OptsGet(), OptsTest, and PkgInfo.
Referenced by main().
207 Argv0 = basename(argv[0]);
219 LOGDIAG1(
"Post options processed non-option arguments:");
221 for(i=0; i<argc; ++i)
223 fprintf(fp,
" argv[%d]=\"%s\"\n", i, argv[i]);
#define CHKEXPR_INT(val, expr,...)
check integer
FILE * LOG_GET_LOGFP()
Get current logging output stream file pointer.
static OptsInfo_T LogExampleOptsInfo[]
Command line options information.
#define LOGDIAG1(fmt,...)
Standard Diagnostic Level 1 logging.
static int OptsTest
test option value
#define LOGABLE(level)
Test if given level is logable at current threshold.
static const PkgInfo_T PkgInfo
char ** OptsGet(const char *argv0, const PkgInfo_T *pPkgInfo, OptsPgmInfo_T *pPgmInfo, OptsInfo_T *pOptsInfo, bool_t bHasLogging, int *pargc, char *argv[])
Gets, validates, and sets all command line options.
#define LOG_LEVEL_DIAG1
diagnostic level 1
static OptsPgmInfo_T LogExamplePgmInfo
Program information.
static char * Argv0
the command
| static void logtest_num1 |
( |
void * |
p, |
|
|
const char * |
sFruit, |
|
|
int |
n, |
|
|
int |
half, |
|
|
double |
f, |
|
|
int |
hex, |
|
|
bool_t |
bAreRipe |
|
) |
| |
|
static |
Log test number 1.
- Parameters
-
| p | Void pointer. |
| sFruit | Character pointer. |
| n | Integer. |
| half | Short. |
| f | Double. |
| hex | Integer in hex. |
| bAreRipe | Boolean. |
Definition at line 111 of file example_log.c.
References _TBOOL, _TFLOAT, _THEX, _TINT, _TPTR, _TSHORT, _TSTR, LOGDIAG1CALL, and LOGDIAG2.
Referenced by runlogtests().
121 LOGDIAG2(
"There are %d juicy %s in %s", n, sFruit,
"Tuscany");
125 LOGDIAG2(
"There are %d green %s in %s", n, sFruit,
"Utah");
#define _THEX(var)
int (hex)
#define _TINT(var)
int (decimal)
#define _TSHORT(var)
short int (decimal)
#define LOGDIAG2(fmt,...)
Standard Diagnostic Level 2 logging.
#define _TBOOL(var)
boolean
#define _TSTR(var)
string variable
#define _TPTR(var)
pointer
#define _TFLOAT(var)
float
#define LOGDIAG1CALL(...)
Standard Diagnostic Level 1 function call tracing.
| static void logtest_num2 |
( |
int |
count | ) |
|
|
static |
Log test number 2.
- Parameters
-
Definition at line 134 of file example_log.c.
References _TINT, LOGDIAG2CALL, LOGDIAG4, LOGERROR, LOGSYSERROR, LOGWARN, and NULL.
Referenced by runlogtests().
137 const char *sFileName =
"PackingShedOnStrike.newsflash";
142 LOGWARN(
"Ebola is your friend, Mr. %s.",
"Sicko");
147 LOGERROR(
"Salmonella scare %d...", count);
151 if( (fp = fopen(sFileName,
"r")) ==
NULL )
157 LOGDIAG4(
"%s: what a pleasant surprise", sFileName);
#define _TINT(var)
int (decimal)
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
#define LOGERROR(fmt,...)
Standard Error logging.
#define LOGWARN(fmt,...)
Standard Warning logging.
#define LOGDIAG4(fmt,...)
Standard Diagnostic Level 4 logging.
#define LOGDIAG2CALL(...)
Standard Diagnostic Level 2 function call tracing.
| int main |
( |
int |
argc, |
|
|
char * |
argv[] |
|
) |
| |
Example main.
- Parameters
-
| argc | Command-line argument count. |
| argv | Command-line argument list. |
- Exit Status:
- Program exits with 0 success, > 0 on failure.
Definition at line 239 of file example_log.c.
References init(), and runlogtests().
241 printf(
"arch : %s\n", ARCH);
243 if( !
init(argc, argv) )
static int runlogtests()
Run log test(s).
static int init(int argc, char *argv[])
Main initialization.
| static int runlogtests |
( |
| ) |
|
|
static |
Run log test(s).
- Returns
- 1 on success, > 0 on failure.
Definition at line 167 of file example_log.c.
References LOGERROR, logtest_num1(), logtest_num2(), and OptsTest.
Referenced by main().
177 logtest_num1(&i,
"apples", 53, h, 123.5, 0xdead,
false);
static void logtest_num2(int count)
Log test number 2.
static int OptsTest
test option value
static void logtest_num1(void *p, const char *sFruit, int n, int half, double f, int hex, bool_t bAreRipe)
Log test number 1.
#define LOGERROR(fmt,...)
Standard Error logging.
Initial value:=
{
{
.long_opt = "test",
.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."
},
}
char * OptsFmtInt(char *buf, size_t buflen, void *pOptVal)
Integer option value string formatter.
static int OptsTest
test option value
int OptsCvtArgInt(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options integer argument to integer.
Command line options information.
Definition at line 76 of file example_log.c.
Initial value:=
{
.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."
}
Program information.
Definition at line 64 of file example_log.c.