[pisa-src] r1358 - trunk/libpisa/log.c

  • From: Thomas Jansen <mithi@xxxxxxxxx>
  • To: pisa-src@xxxxxxxxxxxxx
  • Date: Tue, 27 Oct 2009 17:40:54 +0100

Author: tjansen
Date: Tue Oct 27 17:40:54 2009
New Revision: 1358

Log:
Simplified several fopen calls.

Modified:
   trunk/libpisa/log.c

Modified: trunk/libpisa/log.c
==============================================================================
--- trunk/libpisa/log.c Tue Oct 27 17:29:58 2009        (r1357)
+++ trunk/libpisa/log.c Tue Oct 27 17:40:54 2009        (r1358)
@@ -40,9 +40,7 @@
 
        strncpy(logfile, file, MAX_PATH);
 
-       f = fopen(logfile, "a");
-
-       if (f == NULL) {
+       if (!(f = fopen(logfile, "a"))) {
                fprintf(stderr, "Error reading log file.\n");
                logfile[0] = '\0';
                return 0;
@@ -126,13 +124,13 @@
        int old_number_of_bytes = 0;
        time_t old_connection_length = 0;
        int old_values_available = 0;
+       FILE* f;
 
        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 ((f = fopen(path, "w"))) {
                if (old_values_available) {
                        number_of_bytes += old_number_of_bytes;
                        length += old_connection_length;
@@ -154,6 +152,7 @@
        char path[MAX_PATH];
        char buffer[LENGTH_DATE_BUFFER];
        char ip_buffer[INET_ADDRSTRLEN];
+       FILE* f;
 
        get_formatted_current_time(buffer, LENGTH_DATE_BUFFER, DATE_FORMAT);
 
@@ -161,8 +160,7 @@
 
        inet_ntop(AF_INET, address, ip_buffer, sizeof(ip_buffer));
 
-       FILE* f = fopen(path, "a");
-       if (f != NULL) {
+       if ((f = fopen(path, "a"))) {
                fprintf(f, "Start: %s\n", buffer);
                fprintf(f, "%s\n", ip_buffer);
                fclose(f);
@@ -177,13 +175,13 @@
 {
        char path[MAX_PATH];
        char buffer[LENGTH_DATE_BUFFER];
+       FILE *f;
 
        get_formatted_current_time(buffer, LENGTH_DATE_BUFFER, DATE_FORMAT);
 
        get_nat_map_file_for_hit(path, hit);
 
-       FILE* f = fopen(path, "a");
-       if (f != NULL) {
+       if ((f = fopen(path, "a"))) {
                fprintf(f, "End: %s\n", buffer);
                fclose(f);
        }
@@ -233,12 +231,9 @@
        char buffer[MONTH_FILE_BUFFER_LENGTH];
        FILE* f;
 
-       f = fopen(path, "r");
-
-       if (f == NULL)
+       if ((f = fopen(path, "r")))
                return 0;
 
-
        if (fgets(buffer, MONTH_FILE_BUFFER_LENGTH, f) == NULL)
                return 0;
        *number_of_bytes = atoi(buffer);
@@ -323,15 +318,14 @@
 void print_to_log(const char *fmt, ...)
 {
        va_list args;
+       FILE *f; 
 
        if (logfile == NULL)
                return;
 
        va_start(args, fmt);
 
-       FILE *f = fopen(logfile, "a");
-
-       if (f != NULL) {
+       if ((f = fopen(logfile, "a"))) {
                vfprintf(f, fmt, args);
                fclose(f);
        }

Other related posts:

  • » [pisa-src] r1358 - trunk/libpisa/log.c - Thomas Jansen