Changeset 5413

Show
Ignore:
Timestamp:
03/15/08 16:22:10 (4 months ago)
Author:
morris
Message:

Increase lock granularity

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • libwired/trunk/libwired/system/wi-log.c

    r5359 r5413  
    155155        int                             priority; 
    156156         
    157         if(wi_lock_trylock(_wi_log_lock)) { 
    158                 string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap); 
    159                 cstring = wi_string_cstring(string); 
    160                 name = wi_string_cstring(wi_process_name(wi_process())); 
     157        string = wi_string_init_with_format_and_arguments(wi_string_alloc(), fmt, ap); 
     158        cstring = wi_string_cstring(string); 
     159        name = wi_string_cstring(wi_process_name(wi_process())); 
     160         
     161        _wi_log_date(date); 
     162 
     163        if(wi_log_stdout || wi_log_stderr) { 
     164                fp = wi_log_stdout ? stdout : stderr; 
     165 
     166                fprintf(fp, "%s %s[%u]: %s\n", date, name, (uint32_t) getpid(), cstring); 
     167        } 
     168        else if(wi_log_startup && level < WI_LOG_INFO) { 
     169                fp = stderr; 
     170 
     171                fprintf(fp, "%s: %s\n", name, cstring); 
     172        } 
     173        else if(wi_log_tool) { 
     174                fp = (level < WI_LOG_INFO) ? stderr : stdout; 
     175 
     176                fprintf(fp, "%s: %s\n", name, cstring); 
     177        } 
     178        else if(wi_log_plain) { 
     179                fp = (level < WI_LOG_INFO) ? stderr : stdout; 
     180 
     181                fprintf(fp, "%s\n", cstring); 
     182        } 
     183 
     184        if(fp) 
     185                fflush(fp); 
     186 
     187        if(wi_log_syslog) { 
     188                switch(level) { 
     189                        default: 
     190                        case WI_LOG_INFO:       priority = LOG_INFO;    break; 
     191                        case WI_LOG_WARN:       priority = LOG_WARNING; break; 
     192                        case WI_LOG_ERR:        priority = LOG_ERR;             break; 
     193                        case WI_LOG_DEBUG:      priority = LOG_DEBUG;   break; 
     194                } 
    161195                 
    162                 _wi_log_date(date); 
    163  
    164                 if(wi_log_stdout || wi_log_stderr) { 
    165                         fp = wi_log_stdout ? stdout : stderr; 
    166  
     196                syslog(priority, "%s", cstring); 
     197        } 
     198 
     199        if(wi_log_file && wi_log_path) { 
     200                wi_lock_lock(_wi_log_lock); 
     201 
     202                path = wi_string_cstring(wi_path_relative_to_root(wi_log_path)); 
     203 
     204                fp = fopen(path, "a"); 
     205 
     206                if(fp) { 
    167207                        fprintf(fp, "%s %s[%u]: %s\n", date, name, (uint32_t) getpid(), cstring); 
    168                 } 
    169                 else if(wi_log_startup && level < WI_LOG_INFO) { 
    170                         fp = stderr; 
    171  
    172                         fprintf(fp, "%s: %s\n", name, cstring); 
    173                 } 
    174                 else if(wi_log_tool) { 
    175                         fp = (level < WI_LOG_INFO) ? stderr : stdout; 
    176  
    177                         fprintf(fp, "%s: %s\n", name, cstring); 
    178                 } 
    179                 else if(wi_log_plain) { 
    180                         fp = (level < WI_LOG_INFO) ? stderr : stdout; 
    181  
    182                         fprintf(fp, "%s\n", cstring); 
    183                 } 
    184  
    185                 if(fp) 
    186                         fflush(fp); 
    187  
    188                 if(wi_log_syslog) { 
    189                         switch(level) { 
    190                                 default: 
    191                                 case WI_LOG_INFO:       priority = LOG_INFO;    break; 
    192                                 case WI_LOG_WARN:       priority = LOG_WARNING; break; 
    193                                 case WI_LOG_ERR:        priority = LOG_ERR;             break; 
    194                                 case WI_LOG_DEBUG:      priority = LOG_DEBUG;   break; 
     208                        fclose(fp); 
     209                         
     210                        if(_wi_log_lines > 0 && wi_log_limit > 0) { 
     211                                if(_wi_log_lines % (int) ((float) wi_log_limit / 10.0f) == 0) { 
     212                                        _wi_log_truncate(path); 
     213                                         
     214                                        _wi_log_lines = wi_log_limit; 
     215                                } 
    195216                        } 
    196217                         
    197                         syslog(priority, "%s", cstring); 
     218                        _wi_log_lines++; 
     219                } else { 
     220                        fprintf(stderr, "%s: %s: %s\n", name, path, strerror(errno)); 
    198221                } 
    199222 
    200                 if(wi_log_file && wi_log_path) { 
    201                         path = wi_string_cstring(wi_path_relative_to_root(wi_log_path)); 
    202  
    203                         fp = fopen(path, "a"); 
    204  
    205                         if(fp) { 
    206                                 fprintf(fp, "%s %s[%u]: %s\n", date, name, (uint32_t) getpid(), cstring); 
    207                                 fclose(fp); 
    208                                  
    209                                 if(_wi_log_lines > 0 && wi_log_limit > 0) { 
    210                                         if(_wi_log_lines % (int) ((float) wi_log_limit / 10.0f) == 0) { 
    211                                                 _wi_log_truncate(path); 
    212                                                  
    213                                                 _wi_log_lines = wi_log_limit; 
    214                                         } 
    215                                 } 
    216                                  
    217                                 _wi_log_lines++; 
    218                         } else { 
    219                                 fprintf(stderr, "%s: %s: %s\n", name, path, strerror(errno)); 
    220                         } 
    221                 } 
    222  
    223                 if(wi_log_callback) 
    224                         (*wi_log_callback)(level, string); 
    225  
    226                 if(wi_log_startup && level == WI_LOG_ERR) 
    227                         exit(1); 
    228          
    229                 wi_release(string); 
    230  
    231223                wi_lock_unlock(_wi_log_lock); 
    232224        } 
     225 
     226        if(wi_log_callback) 
     227                (*wi_log_callback)(level, string); 
     228 
     229        if(wi_log_startup && level == WI_LOG_ERR) 
     230                exit(1); 
     231 
     232        wi_release(string); 
    233233} 
    234234