Author: tjansen Date: Tue Oct 27 17:11:34 2009 New Revision: 1353 Log: Cosmetics: Use tabs rather than space for indentation. Modified: trunk/libpisa/log.c Modified: trunk/libpisa/log.c ============================================================================== --- trunk/libpisa/log.c Tue Oct 27 17:03:19 2009 (r1352) +++ trunk/libpisa/log.c Tue Oct 27 17:11:34 2009 (r1353) @@ -35,27 +35,27 @@ */ int init_log(const char *file) { - FILE *f; - char buffer[LENGTH_DATE_BUFFER]; + FILE *f; + char buffer[LENGTH_DATE_BUFFER]; - strncpy(logfile,file,MAX_PATH); + strncpy(logfile,file,MAX_PATH); - f = fopen(logfile,"a"); + f = fopen(logfile,"a"); - if(f==NULL) - { - fprintf(stderr,"Error reading log file.\n"); - logfile[0] = '\0'; - return 0; - } + if(f==NULL) + { + fprintf(stderr,"Error reading log file.\n"); + logfile[0] = '\0'; + return 0; + } - fclose(f); + fclose(f); - get_formatted_current_time(buffer,LENGTH_DATE_BUFFER,DATE_FORMAT); + get_formatted_current_time(buffer,LENGTH_DATE_BUFFER,DATE_FORMAT); - LOG("Logging Start: %s",buffer); + LOG("Logging Start: %s",buffer); - return 1; + return 1; } /** @@ -65,21 +65,21 @@ */ void log_start_connection(const char *hit,struct in_addr *address) { - if(logfile!=NULL) - { - char buffer[LENGTH_DATE_BUFFER]; - char ip_buffer[INET_ADDRSTRLEN]; + if(logfile!=NULL) + { + char buffer[LENGTH_DATE_BUFFER]; + char ip_buffer[INET_ADDRSTRLEN]; - get_formatted_current_time(buffer,LENGTH_DATE_BUFFER,DATE_FORMAT); + get_formatted_current_time(buffer,LENGTH_DATE_BUFFER,DATE_FORMAT); - LOG("New Connection: %s",buffer); - LOG("Hit: %s",hit); + LOG("New Connection: %s",buffer); + LOG("Hit: %s",hit); - inet_ntop(AF_INET, address, ip_buffer, sizeof(ip_buffer)); - LOG("IPv4: %s",ip_buffer); + inet_ntop(AF_INET, address, ip_buffer, sizeof(ip_buffer)); + LOG("IPv4: %s",ip_buffer); - log_nat_mapping_start(hit, address); - } + log_nat_mapping_start(hit, address); + } } /** @@ -90,27 +90,27 @@ */ void log_stop_connection(const char *hit,int number_of_bytes,time_t start) { - if(logfile!=NULL) - { - char buffer[LENGTH_DATE_BUFFER]; - time_t rawtime; - double dif; - - time ( &rawtime ); - convert_time_t_to_formatted_string(&rawtime,buffer,LENGTH_DATE_BUFFER,DATE_FORMAT,0); - - LOG("End of Connection: %s",buffer); - LOG("Hit: %s",hit); - LOG("Number of Bytes transferred: %i",number_of_bytes); - // Calculate Connection Time - dif = difftime(rawtime,start); - rawtime = (time_t) dif; - convert_time_t_to_formatted_string(&rawtime,buffer,LENGTH_DATE_BUFFER,"%H:%M:%S",1); - LOG("Connection length: %s",buffer); - - log_month_connection(hit,number_of_bytes,rawtime); - log_nat_mapping_end(hit); - } + if(logfile!=NULL) + { + char buffer[LENGTH_DATE_BUFFER]; + time_t rawtime; + double dif; + + time ( &rawtime ); + convert_time_t_to_formatted_string(&rawtime,buffer,LENGTH_DATE_BUFFER,DATE_FORMAT,0); + + LOG("End of Connection: %s",buffer); + LOG("Hit: %s",hit); + LOG("Number of Bytes transferred: %i",number_of_bytes); + // Calculate Connection Time + dif = difftime(rawtime,start); + rawtime = (time_t) dif; + convert_time_t_to_formatted_string(&rawtime,buffer,LENGTH_DATE_BUFFER,"%H:%M:%S",1); + LOG("Connection length: %s",buffer); + + log_month_connection(hit,number_of_bytes,rawtime); + log_nat_mapping_end(hit); + } } /** @@ -123,27 +123,27 @@ */ void log_month_connection(const char *hit,int number_of_bytes,time_t length) { - char path[MAX_PATH]; - int old_number_of_bytes = 0; - time_t old_connection_length = 0; - int old_values_available = 0; - - get_month_log_file_for_hit(path,hit); - - old_values_available=read_old_month_values(path,&old_number_of_bytes,&old_connection_length); - - FILE* f=fopen(path,"w"); - if(f!=NULL) - { - if(old_values_available) - { - number_of_bytes += old_number_of_bytes; - length+=old_connection_length; - } - fprintf(f,"%i\n",number_of_bytes); - fprintf(f,"%i\n",(int)(length)); - fclose(f); - } + char path[MAX_PATH]; + int old_number_of_bytes = 0; + time_t old_connection_length = 0; + int old_values_available = 0; + + get_month_log_file_for_hit(path,hit); + + old_values_available=read_old_month_values(path,&old_number_of_bytes,&old_connection_length); + + FILE* f=fopen(path,"w"); + if(f!=NULL) + { + if(old_values_available) + { + number_of_bytes += old_number_of_bytes; + length+=old_connection_length; + } + fprintf(f,"%i\n",number_of_bytes); + fprintf(f,"%i\n",(int)(length)); + fclose(f); + } } /** @@ -154,23 +154,23 @@ */ void log_nat_mapping_start(const char *hit,struct in_addr *address) { - char path[MAX_PATH]; - char buffer[LENGTH_DATE_BUFFER]; - char ip_buffer[INET_ADDRSTRLEN]; - - get_formatted_current_time(buffer,LENGTH_DATE_BUFFER,DATE_FORMAT); - - get_nat_map_file_for_hit(path,hit); - - inet_ntop(AF_INET, address, ip_buffer, sizeof(ip_buffer)); - - FILE* f=fopen(path,"a"); - if(f!=NULL) - { - fprintf(f,"Start: %s\n",buffer); - fprintf(f,"%s\n",ip_buffer); - fclose(f); - } + char path[MAX_PATH]; + char buffer[LENGTH_DATE_BUFFER]; + char ip_buffer[INET_ADDRSTRLEN]; + + get_formatted_current_time(buffer,LENGTH_DATE_BUFFER,DATE_FORMAT); + + get_nat_map_file_for_hit(path,hit); + + inet_ntop(AF_INET, address, ip_buffer, sizeof(ip_buffer)); + + FILE* f=fopen(path,"a"); + if(f!=NULL) + { + fprintf(f,"Start: %s\n",buffer); + fprintf(f,"%s\n",ip_buffer); + fclose(f); + } } /** @@ -179,19 +179,19 @@ */ void log_nat_mapping_end(const char *hit) { - char path[MAX_PATH]; - char buffer[LENGTH_DATE_BUFFER]; + char path[MAX_PATH]; + char buffer[LENGTH_DATE_BUFFER]; - get_formatted_current_time(buffer,LENGTH_DATE_BUFFER,DATE_FORMAT); + get_formatted_current_time(buffer,LENGTH_DATE_BUFFER,DATE_FORMAT); - get_nat_map_file_for_hit(path,hit); + get_nat_map_file_for_hit(path,hit); - FILE* f=fopen(path,"a"); - if(f!=NULL) - { - fprintf(f,"End: %s\n",buffer); - fclose(f); - } + FILE* f=fopen(path,"a"); + if(f!=NULL) + { + fprintf(f,"End: %s\n",buffer); + fclose(f); + } } /** @@ -199,14 +199,14 @@ */ void close_log(void) { - if(logfile!=NULL) - { - char buffer[LENGTH_DATE_BUFFER]; + if(logfile!=NULL) + { + char buffer[LENGTH_DATE_BUFFER]; - get_formatted_current_time(buffer,LENGTH_DATE_BUFFER,DATE_FORMAT); + get_formatted_current_time(buffer,LENGTH_DATE_BUFFER,DATE_FORMAT); - LOG("Logging End: %s",buffer); - } + LOG("Logging End: %s",buffer); + } } /** @@ -216,16 +216,16 @@ */ static void convert_hit_to_fileformat(const char *hit,char *dst) { - char *current; + char *current; - strcpy(dst,hit); + strcpy(dst,hit); - current = strchr(dst,':'); - while(current!=NULL) - { - strncpy(current,".",1); - current = strchr(dst,':'); - } + current = strchr(dst,':'); + while(current!=NULL) + { + strncpy(current,".",1); + current = strchr(dst,':'); + } } /** @@ -237,28 +237,28 @@ */ static int read_old_month_values(const char *path,int *number_of_bytes,time_t *length) { - char buffer[MONTH_FILE_BUFFER_LENGTH]; - FILE* f; + char buffer[MONTH_FILE_BUFFER_LENGTH]; + FILE* f; - f = fopen(path,"r"); + f = fopen(path,"r"); - if(f==NULL) - return 0; + if(f==NULL) + return 0; - if(fgets(buffer,MONTH_FILE_BUFFER_LENGTH,f)==NULL) - { - return 0; - } - *number_of_bytes = atoi(buffer); - if(fgets(buffer,MONTH_FILE_BUFFER_LENGTH,f)==NULL) - { - return 0; - } - *length = (time_t)atoi(buffer); + if(fgets(buffer,MONTH_FILE_BUFFER_LENGTH,f)==NULL) + { + return 0; + } + *number_of_bytes = atoi(buffer); + if(fgets(buffer,MONTH_FILE_BUFFER_LENGTH,f)==NULL) + { + return 0; + } + *length = (time_t)atoi(buffer); - fclose(f); - return 1; + fclose(f); + return 1; } /** @@ -268,14 +268,14 @@ */ static void get_month_log_file_for_hit(char *path,const char *hit) { - char buffer[LENGTH_DATE_BUFFER]; - char tmphit[INET6_ADDRSTRLEN]; + char buffer[LENGTH_DATE_BUFFER]; + char tmphit[INET6_ADDRSTRLEN]; - get_formatted_current_time(buffer,LENGTH_DATE_BUFFER,"%m-%Y"); + get_formatted_current_time(buffer,LENGTH_DATE_BUFFER,"%m-%Y"); - convert_hit_to_fileformat(hit,tmphit); + convert_hit_to_fileformat(hit,tmphit); - snprintf(path,MAX_PATH,LOG_DIR "/%s-%s.log" ,tmphit,buffer); + snprintf(path,MAX_PATH,LOG_DIR "/%s-%s.log" ,tmphit,buffer); } /** @@ -285,14 +285,14 @@ */ static void get_nat_map_file_for_hit(char *path,const char *hit) { - char buffer[LENGTH_DATE_BUFFER]; - char tmphit[INET6_ADDRSTRLEN]; + char buffer[LENGTH_DATE_BUFFER]; + char tmphit[INET6_ADDRSTRLEN]; - get_formatted_current_time(buffer,LENGTH_DATE_BUFFER,"%Y"); + get_formatted_current_time(buffer,LENGTH_DATE_BUFFER,"%Y"); - convert_hit_to_fileformat(hit,tmphit); + convert_hit_to_fileformat(hit,tmphit); - snprintf(path,MAX_PATH,LOG_DIR "/%s-nat-%s.log" ,tmphit,buffer); + snprintf(path,MAX_PATH,LOG_DIR "/%s-nat-%s.log" ,tmphit,buffer); } /** @@ -303,10 +303,10 @@ */ static void get_formatted_current_time(char *buffer,int buffer_size,const char *time_format) { - time_t rawtime; + time_t rawtime; - time(&rawtime); - convert_time_t_to_formatted_string(&rawtime,buffer,buffer_size,time_format,0); + time(&rawtime); + convert_time_t_to_formatted_string(&rawtime,buffer,buffer_size,time_format,0); } /** @@ -320,37 +320,37 @@ */ static int convert_time_t_to_formatted_string(time_t *time,char *buffer,int buffer_size,const char *time_format,int gm) { - struct tm *ts; - // Convert to given date format + struct tm *ts; + // Convert to given date format - // Use local or gmtime - if(!gm) - { - ts = localtime(time); - } - else - { - ts= gmtime(time); - } - return (strftime(buffer, buffer_size,time_format , ts)!=0); + // Use local or gmtime + if(!gm) + { + ts = localtime(time); + } + else + { + ts= gmtime(time); + } + return (strftime(buffer, buffer_size,time_format , ts)!=0); } void print_to_log(const char *fmt,...) { - if(logfile!=NULL) - { - va_list args; - va_start(args,fmt); - - FILE *f = fopen(logfile,"a"); - - if(f!=NULL) - { - vfprintf(f, fmt, args); - fclose(f); - } + if(logfile!=NULL) + { + va_list args; + va_start(args,fmt); + + FILE *f = fopen(logfile,"a"); + + if(f!=NULL) + { + vfprintf(f, fmt, args); + fclose(f); + } - va_end(args); - } + va_end(args); + } }