Logging Mechanism and support for Variable Number of Arguments

September 10th, 2008  |  Published in Linux Sample Programs  |  1 Comment








// Author : Amit Sahrawat
// Test program – Allows logging for three types – Error, Info and Warning. Each type
// of message can be enabled and disable at the compile time by defining their respective flags
// Also each can be controlled at the run time by not setting the bit for the message type in the
// log level. Each message type has been provided different color for printing on STDOUT.
// Also – Example for macros supporting variable number of arguments and function with variable number
// of arguments.

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

// Color Values to be used for providing in setting textcolor
#define RESET 0
#define BRIGHT 1
#define DIM 2
#define UNDERLINE 3
#define BLINK 4
#define REVERSE 7
#define HIDDEN 8

#define BLACK 0
#define RED 1
#define GREEN 2
#define YELLOW 3
#define BLUE 4
#define MAGENTA 5
#define CYAN 6
#define WHITE 7

void textcolor(int attr, int fg, int bg);

// Log levels
#define LOG_ERROR 0x1
#define LOG_WARNING 0x2
#define LOG_INFO 0x4
#define LOG_ALL 0x7

// Controls Logging error messages – undef if this is not be used
#ifdef LOG_ERROR

#define MSG_ERROR(x,y,…) msg_error(x,y,## __VA_ARGS__)
#else
#define MSG_ERROR(x,y,…)
#endif

// Controls Logging warning messages – undef if this is not be used
#ifdef LOG_WARNING
#define MSG_WARNING(x,y,…) msg_warning(x,y,## __VA_ARGS__)
#else
#define MSG_WARNING(x,y,…)
#endif

// Controls logging for information message – under if this is not be used
#ifdef LOG_INFO

#define MSG_INFO(x,y,…) msg_info(x,y,## __VA_ARGS__)
#else
#define MSG_INFO(x,y,…)
#endif

// Controls all threes message type
#ifdef LOG_ALL
#define MSG_ERROR(x,y,…) msg_error(x,y,## __VA_ARGS__)
#define MSG_WARNING(x,y,…) msg_warning(x,y,## __VA_ARGS__)
#define MSG_INFO(x,y,…) msg_info(x,y,## __VA_ARGS__)
#else
#define MSG_ERROR(x,y,…)
#define MSG_WARNING(x,y,…)
#define MSG_INFO(x,y,…)
#endif

// Flag to control log messaging at runtime.
int loglevel1 = LOG_ERROR|LOG_INFO; // Setting individual bits
int loglevel2 = LOG_WARNING;
int loglevel3 = LOG_ERROR|LOG_WARNING|LOG_INFO;

// Logging error message on stdout
int msg_error(int loglevel, char* msg, …)
{
if(loglevel & LOG_ERROR)
{
textcolor(DIM,RED,WHITE);
va_list argList;
va_start(argList,msg);
vfprintf(stdout,msg,argList);
textcolor(RESET,BLACK,WHITE);
}
}

// Logging warning message on stdout
int msg_warning(int loglevel, char* msg, …)
{
if(loglevel & LOG_WARNING)
{
textcolor(DIM,BLUE,WHITE);
va_list argList;
va_start(argList,msg);
vfprintf(stdout,msg,argList);
textcolor(RESET,BLACK,WHITE);
}
}

// Logging information message on stdout
int msg_info(int loglevel, char* msg, …)
{
if(loglevel & LOG_INFO)
{
textcolor(DIM,BLACK,WHITE);
va_list argList;
va_start(argList,msg);
vfprintf(stdout,msg,argList);
textcolor(RESET,BLACK,WHITE);
}
}

// Testfunction
void TestFunction(char *str)
{
MSG_INFO(LOG_ALL,”[%s] str : %s\n”,__FUNCTION__,str);
if(str == NULL)
{
MSG_ERROR(LOG_ALL,”String Value NULL\n”);
exit(1);
}
}
// Testfunction
int CalculateModulus(int num1 , int num2)
{
MSG_INFO(loglevel3,”[%s] num1 : num2, %d : %d \n”,__FUNCTION__,num1,num2);
if(0 == num2)
{
MSG_WARNING(loglevel2,”second number is zero\n”);
}
else
return (num1 % num2);
}

int main()
{

MSG_INFO(loglevel1,”\t Test Program for checking String Colors \n \t\t And \n\t Logging Mechanism \n”);
MSG_INFO(loglevel3,”Logs can be maintaing at compile time, any option can be selected\n”);
MSG_INFO(loglevel3,”INFO, ERROR, WARNING – individual and all can be selected by enabling compile time flag\n”);
MSG_WARNING(loglevel2,”Failed to initialize the color library\n”);
MSG_INFO(loglevel3,”Checking error with arugments : %s – %d , %s – %d \n”,”Error “,1,”Error “,2);
MSG_WARNING(loglevel2,”Finishing the program execution \n”);

TestFunction(“Call 1”);
CalculateModulus(20,0);
CalculateModulus(20,10);

TestFunction(NULL);
return 0;

}

//Setting text color
void
textcolor(int attr, int fg, int bg)
{

char command[13] = {0};

/* Command is the control command to the terminal */
sprintf(command, “%c[%d;%d;%dm”, 0x1B, attr, fg +30, bg+41);
printf(“%s”, command);

}

Subscribe

Get articles in your inbox.

Enter your email address:

Join Us

Twitter Chatter


Recommendations

Archives

Categories