librnr  1.14.5
RoadNarrows Robotics Common Library 1
example_log.c File Reference

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.

Functions

static void logtest_num1 (void *p, const char *sFruit, int n, int half, double f, int hex, bool_t bAreRipe)
 Log test number 1. More...
 
static void logtest_num2 (int count)
 Log test number 2. More...
 
static int runlogtests ()
 Run log test(s). More...
 
static int init (int argc, char *argv[])
 Main initialization. More...
 
int main (int argc, char *argv[])
 Example main. More...
 

Variables

static char * Argv0
 the command
 
static int OptsTest = 1
 test option value
 
static OptsPgmInfo_T LogExamplePgmInfo
 Program information. More...
 
static OptsInfo_T LogExampleOptsInfo []
 Command line options information. More...
 

Detailed Description

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)

Definition in file example_log.c.

Function Documentation

static int init ( int  argc,
char *  argv[] 
)
static

Main initialization.

Parameters
argcCommand-line argument count.
argvCommand-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().

202 {
203  FILE *fp;
204  int i;
205 
206  // name of this process
207  Argv0 = basename(argv[0]);
208 
209  // parse input options
211  &argc, argv);
212 
213  // test log helper macro
214  CHKEXPR_INT(OptsTest, ((OptsTest >= 0) || (OptsTest <= 3)), 0);
215 
216  // test complicated logging
217  if( LOGABLE(LOG_LEVEL_DIAG1) )
218  {
219  LOGDIAG1("Post options processed non-option arguments:");
220  fp = LOG_GET_LOGFP();
221  for(i=0; i<argc; ++i)
222  {
223  fprintf(fp, " argv[%d]=\"%s\"\n", i, argv[i]);
224  }
225  }
226 
227  return 1;
228 }
#define CHKEXPR_INT(val, expr,...)
check integer
Definition: log.h:688
FILE * LOG_GET_LOGFP()
Get current logging output stream file pointer.
Definition: log.c:301
static OptsInfo_T LogExampleOptsInfo[]
Command line options information.
Definition: example_log.c:76
#define LOGDIAG1(fmt,...)
Standard Diagnostic Level 1 logging.
Definition: log.h:407
static int OptsTest
test option value
Definition: example_log.c:59
#define LOGABLE(level)
Test if given level is logable at current threshold.
Definition: log.h:195
static const PkgInfo_T PkgInfo
Definition: version.h:45
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.
Definition: opts.c:861
#define LOG_LEVEL_DIAG1
diagnostic level 1
Definition: log.h:181
static OptsPgmInfo_T LogExamplePgmInfo
Program information.
Definition: example_log.c:64
static char * Argv0
the command
Definition: example_log.c:58
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
pVoid pointer.
sFruitCharacter pointer.
nInteger.
halfShort.
fDouble.
hexInteger in hex.
bAreRipeBoolean.

Definition at line 111 of file example_log.c.

References _TBOOL, _TFLOAT, _THEX, _TINT, _TPTR, _TSHORT, _TSTR, LOGDIAG1CALL, and LOGDIAG2.

Referenced by runlogtests().

113 {
114  // test log function call diagnostics 1
115  LOGDIAG1CALL(_TPTR(p), _TSTR(sFruit), _TINT(n), _TSHORT(half), _TFLOAT(f),
116  _THEX(hex), _TBOOL(bAreRipe));
117 
118  // test diagnostic 2 test
119  if( bAreRipe )
120  {
121  LOGDIAG2("There are %d juicy %s in %s", n, sFruit, "Tuscany");
122  }
123  else
124  {
125  LOGDIAG2("There are %d green %s in %s", n, sFruit, "Utah");
126  }
127 }
#define _THEX(var)
int (hex)
Definition: log.h:581
#define _TINT(var)
int (decimal)
Definition: log.h:582
#define _TSHORT(var)
short int (decimal)
Definition: log.h:584
#define LOGDIAG2(fmt,...)
Standard Diagnostic Level 2 logging.
Definition: log.h:400
#define _TBOOL(var)
boolean
Definition: log.h:588
#define _TSTR(var)
string variable
Definition: log.h:578
#define _TPTR(var)
pointer
Definition: log.h:587
#define _TFLOAT(var)
float
Definition: log.h:586
#define LOGDIAG1CALL(...)
Standard Diagnostic Level 1 function call tracing.
Definition: log.h:460
static void logtest_num2 ( int  count)
static

Log test number 2.

Parameters
countCount.

Definition at line 134 of file example_log.c.

References _TINT, LOGDIAG2CALL, LOGDIAG4, LOGERROR, LOGSYSERROR, LOGWARN, and NULL.

Referenced by runlogtests().

135 {
136  FILE *fp;
137  const char *sFileName = "PackingShedOnStrike.newsflash";
138 
139  // test log function call diagnostics 2
140  LOGDIAG2CALL(_TINT(count));
141 
142  LOGWARN("Ebola is your friend, Mr. %s.", "Sicko");
143 
144  // test error logging
145  while( count-- > 0 )
146  {
147  LOGERROR("Salmonella scare %d...", count);
148  }
149 
150  // test system error logging
151  if( (fp = fopen(sFileName, "r")) == NULL )
152  {
153  LOGSYSERROR("%s", sFileName);
154  }
155  else
156  {
157  LOGDIAG4("%s: what a pleasant surprise", sFileName);
158  fclose(fp);
159  }
160 }
#define _TINT(var)
int (decimal)
Definition: log.h:582
#define NULL
null pointer
Definition: rnrconfig.h:199
#define LOGSYSERROR(fmt,...)
Standard System Error logging.
Definition: log.h:509
#define LOGERROR(fmt,...)
Standard Error logging.
Definition: log.h:488
#define LOGWARN(fmt,...)
Standard Warning logging.
Definition: log.h:467
#define LOGDIAG4(fmt,...)
Standard Diagnostic Level 4 logging.
Definition: log.h:386
#define LOGDIAG2CALL(...)
Standard Diagnostic Level 2 function call tracing.
Definition: log.h:454
int main ( int  argc,
char *  argv[] 
)

Example main.

Parameters
argcCommand-line argument count.
argvCommand-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().

240 {
241  printf("arch : %s\n", ARCH);
242 
243  if( !init(argc, argv) )
244  {
245  return 3;
246  }
247 
248  return runlogtests()? 0: 4;
249 }
static int runlogtests()
Run log test(s).
Definition: example_log.c:167
static int init(int argc, char *argv[])
Main initialization.
Definition: example_log.c:201
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().

168 {
169  short h = 10;
170  int i = 12;
171 
172  switch( OptsTest )
173  {
174  case 0:
175  break;
176  case 1:
177  logtest_num1(&i, "apples", 53, h, 123.5, 0xdead, false);
178  break;
179  case 2:
180  logtest_num2(3);
181  break;
182  case 3:
183  logtest_num1(&h, "pears", 12, h, -1.5E-2, 0xf1, true);
184  logtest_num2(3);
185  break;
186  default:
187  LOGERROR("bug: %d: unexpected test", OptsTest);
188  return 0;
189  }
190  return 1;
191 }
static void logtest_num2(int count)
Log test number 2.
Definition: example_log.c:134
static int OptsTest
test option value
Definition: example_log.c:59
static void logtest_num1(void *p, const char *sFruit, int n, int half, double f, int hex, bool_t bAreRipe)
Log test number 1.
Definition: example_log.c:111
#define LOGERROR(fmt,...)
Standard Error logging.
Definition: log.h:488

Variable Documentation

OptsInfo_T LogExampleOptsInfo[]
static
Initial value:
=
{
{
.long_opt = "test",
.short_opt = 't',
.has_arg = required_argument,
.has_default= true,
.opt_addr = &OptsTest,
.fn_cvt = OptsCvtArgInt,
.fn_fmt = OptsFmtInt,
.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."
},
{NULL, }
}
char * OptsFmtInt(char *buf, size_t buflen, void *pOptVal)
Integer option value string formatter.
Definition: opts.c:1215
#define NULL
null pointer
Definition: rnrconfig.h:199
static int OptsTest
test option value
Definition: example_log.c:59
int OptsCvtArgInt(const char *argv0, const char *sOptName, char *optarg, void *pOptVal)
Convert options integer argument to integer.
Definition: opts.c:1065

Command line options information.

Definition at line 76 of file example_log.c.

OptsPgmInfo_T LogExamplePgmInfo
static
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.