Author: tjansen Date: Tue Oct 27 17:23:13 2009 New Revision: 1355 Log: Cosmetics: Added spaces between functions arguments and after if, while for improved readability. Modified: trunk/libpisa/log.c Modified: trunk/libpisa/log.c ============================================================================== --- trunk/libpisa/log.c Tue Oct 27 17:17:23 2009 (r1354) +++ trunk/libpisa/log.c Tue Oct 27 17:23:13 2009 (r1355) @@ -21,12 +21,12 @@ char logfile[MAX_PATH]={0}; -static int read_old_month_values(const char *path,int *number_of_bytes,time_t *length); -static void get_month_log_file_for_hit(char *path,const char *hit); -static void convert_hit_to_fileformat(const char *hit,char *dst); -static void get_nat_map_file_for_hit(char *path,const char *hit); -static void get_formatted_current_time(char *buffer,int buffer_size,const char *time_format); -static int convert_time_t_to_formatted_string(time_t *time,char *buffer,int buffer_size,const char *time_format,int gm); +static int read_old_month_values(const char *path, int *number_of_bytes, time_t *length); +static void get_month_log_file_for_hit(char *path, const char *hit); +static void convert_hit_to_fileformat(const char *hit, char *dst); +static void get_nat_map_file_for_hit(char *path, const char *hit); +static void get_formatted_current_time(char *buffer, int buffer_size, const char *time_format); +static int convert_time_t_to_formatted_string(time_t *time, char *buffer, int buffer_size, const char *time_format, int gm); /** * Initializes the logfile and writes the time of the logging start @@ -38,22 +38,22 @@ 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) + if (f == NULL) { - fprintf(stderr,"Error reading log file.\n"); + fprintf(stderr, "Error reading log file.\n"); logfile[0] = '\0'; return 0; } 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; } @@ -63,20 +63,20 @@ * @param hit The HIT of the client * @param ip The IPv4 address of the client */ -void log_start_connection(const char *hit,struct in_addr *address) +void log_start_connection(const char *hit, struct in_addr *address) { - if(logfile!=NULL) + 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); + LOG("IPv4: %s", ip_buffer); log_nat_mapping_start(hit, address); } @@ -88,27 +88,27 @@ * @param number_of_bytes The number of bytes which were transferred through the tunnel * @param start The start-time of the connection */ -void log_stop_connection(const char *hit,int number_of_bytes,time_t start) +void log_stop_connection(const char *hit, int number_of_bytes, time_t start) { - if(logfile!=NULL) + 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); + 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); + 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); + 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); + 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_month_connection(hit, number_of_bytes, rawtime); log_nat_mapping_end(hit); } } @@ -121,27 +121,27 @@ * @param number_of_bytes The number of bytes which were transferred through the tunnel * @param length The connection length */ -void log_month_connection(const char *hit,int number_of_bytes,time_t length) +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); + get_month_log_file_for_hit(path, hit); - old_values_available=read_old_month_values(path,&old_number_of_bytes,&old_connection_length); + old_values_available = read_old_month_values(path, &old_number_of_bytes, &old_connection_length); - FILE* f=fopen(path,"w"); - if(f!=NULL) + FILE* f = fopen(path, "w"); + if (f != NULL) { - if(old_values_available) + if (old_values_available) { number_of_bytes += old_number_of_bytes; - length+=old_connection_length; + length += old_connection_length; } - fprintf(f,"%i\n",number_of_bytes); - fprintf(f,"%i\n",(int)(length)); + fprintf(f, "%i\n", number_of_bytes); + fprintf(f, "%i\n", (int)(length)); fclose(f); } } @@ -152,23 +152,23 @@ * @param hit The Hit of the client * @param ip IPv4 address of the client */ -void log_nat_mapping_start(const char *hit,struct in_addr *address) +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_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); inet_ntop(AF_INET, address, ip_buffer, sizeof(ip_buffer)); - FILE* f=fopen(path,"a"); - if(f!=NULL) + FILE* f = fopen(path, "a"); + if (f != NULL) { - fprintf(f,"Start: %s\n",buffer); - fprintf(f,"%s\n",ip_buffer); + fprintf(f, "Start: %s\n", buffer); + fprintf(f, "%s\n", ip_buffer); fclose(f); } } @@ -182,14 +182,14 @@ 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) + FILE* f = fopen(path, "a"); + if (f != NULL) { - fprintf(f,"End: %s\n",buffer); + fprintf(f, "End: %s\n", buffer); fclose(f); } } @@ -199,13 +199,13 @@ */ void close_log(void) { - if(logfile!=NULL) + 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); } } @@ -214,17 +214,17 @@ * @param hit The Hit which should be replaced * @param dst A pointer to the destination char */ -static void convert_hit_to_fileformat(const char *hit,char *dst) +static void convert_hit_to_fileformat(const char *hit, char *dst) { char *current; - strcpy(dst,hit); + strcpy(dst, hit); - current = strchr(dst,':'); - while(current!=NULL) + current = strchr(dst, ':'); + while (current != NULL) { - strncpy(current,".",1); - current = strchr(dst,':'); + strncpy(current, ".", 1); + current = strchr(dst, ':'); } } @@ -235,23 +235,23 @@ * @param length Pointer to the time_t where the length should be stored * @return 0->error; 1->success */ -static int read_old_month_values(const char *path,int *number_of_bytes,time_t *length) +static int read_old_month_values(const char *path, int *number_of_bytes, time_t *length) { char buffer[MONTH_FILE_BUFFER_LENGTH]; FILE* f; - f = fopen(path,"r"); + f = fopen(path, "r"); - if(f==NULL) + if (f == NULL) return 0; - if(fgets(buffer,MONTH_FILE_BUFFER_LENGTH,f)==NULL) + 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) + if (fgets(buffer, MONTH_FILE_BUFFER_LENGTH, f) == NULL) { return 0; } @@ -266,16 +266,16 @@ * @param path A pointer to the char where the calculated path should be stored * @param hit The HIT of the client */ -static void get_month_log_file_for_hit(char *path,const char *hit) +static void get_month_log_file_for_hit(char *path, const char *hit) { 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); } /** @@ -283,16 +283,16 @@ * @param path A pointer to the char where the calculated path should be stored * @param hit The HIT of the client */ -static void get_nat_map_file_for_hit(char *path,const char *hit) +static void get_nat_map_file_for_hit(char *path, const char *hit) { 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); } /** @@ -301,12 +301,12 @@ * @param buffer_size The size of the buffer * @param time_format The time_format which should be used to format the current time */ -static void get_formatted_current_time(char *buffer,int buffer_size,const char *time_format) +static void get_formatted_current_time(char *buffer, int buffer_size, const char *time_format) { time_t rawtime; time(&rawtime); - convert_time_t_to_formatted_string(&rawtime,buffer,buffer_size,time_format,0); + convert_time_t_to_formatted_string(&rawtime, buffer, buffer_size, time_format, 0); } /** @@ -318,33 +318,33 @@ * @param gm 1->Use gmtime, 0->Use localtime * @return 0->error; 1->success */ -static int convert_time_t_to_formatted_string(time_t *time,char *buffer,int buffer_size,const char *time_format,int gm) +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 // Use local or gmtime - if(!gm) + if (!gm) { ts = localtime(time); } else { - ts= gmtime(time); + ts = gmtime(time); } - return (strftime(buffer, buffer_size,time_format , ts)!=0); + return (strftime(buffer, buffer_size, time_format, ts) != 0); } -void print_to_log(const char *fmt,...) +void print_to_log(const char *fmt, ...) { - if(logfile!=NULL) + if (logfile != NULL) { va_list args; - va_start(args,fmt); + va_start(args, fmt); - FILE *f = fopen(logfile,"a"); + FILE *f = fopen(logfile, "a"); - if(f!=NULL) + if (f != NULL) { vfprintf(f, fmt, args); fclose(f); @@ -353,4 +353,3 @@ va_end(args); } } -