morph_logger.c 1.15 KB
Newer Older
priyank's avatar
priyank committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include<stdio.h>
#include<time.h>
#include<unistd.h>

char * myasctime(); // to generate time stamp, current time in string format

void print_log(FILE *log_file, char *message, char *program_name, char *file_name, int line_no, char *function_name)
{
	fprintf(log_file, "%s:%s[%d]:%s:%d:%s:%s\n", 
						myasctime(), 
						program_name, 
						getpid(),
						file_name, 
						line_no, 
						function_name,
						message);
	fflush(log_file);
}



void print_err(FILE *log_file, char *message, char *program_name)
{
	fprintf(log_file, "%s %s[%d] %s\n", myasctime(), program_name, getpid(), message);
	fflush(log_file);
}

char *myasctime()
{ 
	
	static char mon_name[12][3] = { "Jan", 
									"Feb", 
									"Mar", 
									"Apr", 
									"May", 
									"Jun", 
									"Jul", 
									"Aug", 
									"Sep", 
									"Oct", 
									"Nov", 
									"Dec" 
								}; 
	
	static char result[26]; 
	
	struct tm *timeptr;

	time_t t = time('\0');
	timeptr = localtime(&t);

	sprintf(result, "%.3s%3d %.2d:%.2d:%.2d", 
					mon_name[timeptr->tm_mon], 
					timeptr->tm_mday, 
					timeptr->tm_hour, 
					timeptr->tm_min, 
					timeptr->tm_sec); 
	return result; 
}