A
arnuld
PURPOSE: To print the logs according to the value of the global variable
DESIRED_LOG_LEVEL. See comments.
WHAT I GOT: compile-time errors
/* The purpose of this program is to print logs to the stdout. Which logs
will be printed will depend on the first parameter given
* to write_log(). LOG FLAGS are hierarcy based. If global variable
DESIRED_LOG_FLAG is set to PRINT_VAR, then it will execute
* every write_log() call containing PRINT_VAR, PRINT_EXTRA and
PRINT_NIL. If PRINT_EXTRA is set then it will execute the write_log()
* calls containing PRINT_EXTRA and PRINT_NILL but not PRINT_VAR and if
PRINT_NIL is set, it will only execute write_log() calls
* containing PRINT_NIL and nothing else. Means all the flags existing
above the one in use will always be printed. PRINT_NIL will
* actually print nothing on stdout. Its code will only contain a semi-
colon. e.g. if(DESIRED_LOG_LEVEL == PRINT_NIL) ;
*
* Here is a general call to write_log:
*
* write_log( LOG_FLAG, string literal, variable-number-of-arguments)
*
* the first 2 parameter will always be there, only the last parmeter may
or may not be there and it may contain any number of arguments.
*
*
* VERSION 0.0
*
*/
#include <stdio.h>
#include <stdarg.h>
enum LOG_FLAG
{
PRINT_NIL,
PRINT_EXTRA,
PRINT_VAR
};
enum SIZES { ARRSIZE = 50 };
int DESIRED_LOG_FLAG = PRINT_EXTRA;
/* enum LOG_FLAG flag_type[] = { PRINT_NIL, PRINT_EXTRA, PRINT_VAR, 0 };
Last zero will work as NUL, the array terminator */
void write_log(int pflag, const char*, ...);
int main(void)
{
int idx;
char* p = "This is comp.lang.c";
char arrc[] = "This is comp.lang.c";
write_log(PRINT_EXTRA, NULL, "This is a program which prints logs
depending the values given to it");
for( idx = 0; idx != 3; ++idx)
{
write_log(PRINT_VAR, "PRINT_VAR, p = %s", p);
write_log(PRINT_EXTRA, "PRINT_EXTRA: arrc = %s", arrc);
write_log(PRINT_NIL, "Just don't print this");
}
return 0;
}
/* Exmaples:
write_log( PRINT_VAR, "Filename is %s\n", __FILE__);
write_log( PRINT_EXTRA, "FILE: %s, Error occured at LINE: %d\n",
__FILE__, __LINE__);
*/
void write_Log(int log_flag, const char* ptr, ...)
{
char c;
va_list ap;
va_start(ap, ptr);
while((c = *ptr++))
{
switch(c)
{
case PRINT_VAR:
printf("[PRINT_VAR] : %d", va_arg(ap, int));
break;
case PRINT_NIL:
printf("LOG_NIL\n");
break;
default:
break;
}
}
va_end(ap);
}
==================== OUTPUT ============================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra log.c
log.c:65: warning: unused parameter ‘log_flag’
/tmp/ccwxEoR5.o: In function `main':
log.c.text+0x58): undefined reference to `write_log'
log.c.text+0x7c): undefined reference to `write_log'
log.c.text+0x97): undefined reference to `write_log'
log.c.text+0xab): undefined reference to `write_log'
collect2: ld returned 1 exit status
[arnuld@dune programs]$
Since DESIRED_LOG_LEVEL = PRINT_EXTRA, it should print the information in
PRINT_EXTRA and PRINT_NIL but it does not.
DESIRED_LOG_LEVEL. See comments.
WHAT I GOT: compile-time errors
/* The purpose of this program is to print logs to the stdout. Which logs
will be printed will depend on the first parameter given
* to write_log(). LOG FLAGS are hierarcy based. If global variable
DESIRED_LOG_FLAG is set to PRINT_VAR, then it will execute
* every write_log() call containing PRINT_VAR, PRINT_EXTRA and
PRINT_NIL. If PRINT_EXTRA is set then it will execute the write_log()
* calls containing PRINT_EXTRA and PRINT_NILL but not PRINT_VAR and if
PRINT_NIL is set, it will only execute write_log() calls
* containing PRINT_NIL and nothing else. Means all the flags existing
above the one in use will always be printed. PRINT_NIL will
* actually print nothing on stdout. Its code will only contain a semi-
colon. e.g. if(DESIRED_LOG_LEVEL == PRINT_NIL) ;
*
* Here is a general call to write_log:
*
* write_log( LOG_FLAG, string literal, variable-number-of-arguments)
*
* the first 2 parameter will always be there, only the last parmeter may
or may not be there and it may contain any number of arguments.
*
*
* VERSION 0.0
*
*/
#include <stdio.h>
#include <stdarg.h>
enum LOG_FLAG
{
PRINT_NIL,
PRINT_EXTRA,
PRINT_VAR
};
enum SIZES { ARRSIZE = 50 };
int DESIRED_LOG_FLAG = PRINT_EXTRA;
/* enum LOG_FLAG flag_type[] = { PRINT_NIL, PRINT_EXTRA, PRINT_VAR, 0 };
Last zero will work as NUL, the array terminator */
void write_log(int pflag, const char*, ...);
int main(void)
{
int idx;
char* p = "This is comp.lang.c";
char arrc[] = "This is comp.lang.c";
write_log(PRINT_EXTRA, NULL, "This is a program which prints logs
depending the values given to it");
for( idx = 0; idx != 3; ++idx)
{
write_log(PRINT_VAR, "PRINT_VAR, p = %s", p);
write_log(PRINT_EXTRA, "PRINT_EXTRA: arrc = %s", arrc);
write_log(PRINT_NIL, "Just don't print this");
}
return 0;
}
/* Exmaples:
write_log( PRINT_VAR, "Filename is %s\n", __FILE__);
write_log( PRINT_EXTRA, "FILE: %s, Error occured at LINE: %d\n",
__FILE__, __LINE__);
*/
void write_Log(int log_flag, const char* ptr, ...)
{
char c;
va_list ap;
va_start(ap, ptr);
while((c = *ptr++))
{
switch(c)
{
case PRINT_VAR:
printf("[PRINT_VAR] : %d", va_arg(ap, int));
break;
case PRINT_NIL:
printf("LOG_NIL\n");
break;
default:
break;
}
}
va_end(ap);
}
==================== OUTPUT ============================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra log.c
log.c:65: warning: unused parameter ‘log_flag’
/tmp/ccwxEoR5.o: In function `main':
log.c.text+0x58): undefined reference to `write_log'
log.c.text+0x7c): undefined reference to `write_log'
log.c.text+0x97): undefined reference to `write_log'
log.c.text+0xab): undefined reference to `write_log'
collect2: ld returned 1 exit status
[arnuld@dune programs]$
Since DESIRED_LOG_LEVEL = PRINT_EXTRA, it should print the information in
PRINT_EXTRA and PRINT_NIL but it does not.