/**************************************************************** * BiffSocko 8/4/2004 * logger.c * * This program essentially acts as a logging devise for other programs. * normally a shell script (or another c/perl/java program) would call this * program with the following arguements: * * [MESSAGE] * where MESSAGE can be any text * NOTE: STOP or START has to be in caps. * * the calling program should call logger.c at the begining of the * program or script with the START parameter. It should then * call logger.c at the end of it's run with the STOP parameter. * * if you are using logger.c with lots of scripts and programs and a * single logfile, it is advised that you use logger.c to update a temp * logfile and then after the program run copy the contents of the temp * logfile into the real one: * * example: * * #!/bin/sh * LOGFILE="/var/log/pgmlog" * TMPLOGFILE="/tmp/pgmlog.$$" * logger $0 $TMPLOGFILE START * # do some stuff * logger $0 $TMPLOGFILE STOP * cat $TMPLOGFILE >> $LOGFILE * rm $TMPLOGFILE * * the output will appear as follows: * * program_name START DATE DATE_in_seconds STOP DATE DATE_in_seconds * * DATE_in_seconds refers to the number of seconds that have passed since * Jan. 1 1970 (standard UNIX format for time) * * this program will exit with 0 upon success and -1 if it fails * * TO COMPILE: * gcc -o logger logger.c ****************************************************************/ #include #include #include #define max 80 #define usage "pslogger [ MESSAGE ]" int main (int argc, char *argv[]) { /******************************** * some variables ********************************/ int flag=0; char LocalTime[max]; /* just a place ta keep the time*/ struct tm *LOCtm; time_t clock; /* holds num of seconds since jan 1 1970 */ time_t error_time; size_t error_chk; char program[max]; char logfile[max]; char state[max]; char message[max]; FILE *Fp=NULL; /* LOGFILE*/ /****************************************** * make sure we get the right parameters ******************************************/ if((argc != 4) && (argc != 5)){ printf("%s\n", usage); exit(-1); } strcpy(program,argv[1]); strcpy(logfile,argv[2]); strcpy(state,argv[3]); if(argc == 5){ strcpy(message, argv[4]); flag=1; } if((strcmp(state, "START") != 0) && (strcmp(state, "STOP") != 0)){ printf("wrong state %s\n", state); exit(-1); } /********************************************************* * get number of seconds from Jan 1, 1970 (time function) * and then convert it to Local Time *********************************************************/ error_time = time(&clock); if(error_time == -1){ fprintf(stderr, "error getting time()\n"); exit( -1); } LOCtm=localtime(&clock); /********************************************************* * strftime just formats the time into something useable *********************************************************/ error_chk = strftime(LocalTime, max,"%d-%b-%y %H:%M", LOCtm); if ( error_chk == 0){ fprintf(stderr, "error getting Local Time\n"); exit( -1); } if(!(Fp=fopen(logfile, "a+"))){ printf("can't open %s to write\n", logfile); exit(-1); } /********************************************************* * just print out the times and exit cleanly *********************************************************/ if(flag == 0){ if(strcmp(state, "START") == 0){ fprintf(Fp,"%s\t%s\t%s\t%i\t", program,state, LocalTime, clock); }else{ fprintf(Fp,"%s\t%s\t%i\n", state, LocalTime, clock); } }else{ if(strcmp(state, "START") == 0){ fprintf(Fp,"%s\t%s\t%s\t%i\t%s", program,state, LocalTime, clock, message); }else{ fprintf(Fp,"%s\t%s\t%i\t%s\n", state, LocalTime, clock, message); } } fclose(Fp); exit ( 0); }