Changeset 5054

Show
Ignore:
Timestamp:
12/02/07 20:53:07 (1 year ago)
Author:
morris
Message:

Use wi_lock_trylock() to prevent deadlock when trying to log assertions from within the log method

Files:

Legend:

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

    r4679 r5054  
    156156        cstring = wi_string_cstring(string); 
    157157 
    158         wi_lock_lock(_wi_log_lock); 
    159          
    160         name = wi_string_cstring(wi_process_name(wi_process())); 
    161          
    162         _wi_log_date(date); 
    163  
    164         if(wi_log_stdout || wi_log_stderr) { 
    165                 fp = wi_log_stdout ? stdout : stderr; 
    166  
    167                 fprintf(fp, "%s %s[%u]: %s\n", date, name, (uint32_t) getpid(), cstring); 
    168         } 
    169         else if(wi_log_startup && priority < LOG_INFO) { 
    170                 fp = stderr; 
    171  
    172                 fprintf(fp, "%s: %s\n", name, cstring); 
    173         } 
    174         else if(wi_log_tool) { 
    175                 fp = (priority < LOG_INFO) ? stderr : stdout; 
    176  
    177                 fprintf(fp, "%s: %s\n", name, cstring); 
    178         } 
    179         else if(wi_log_plain) { 
    180                 fp = (priority < 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                 syslog(priority, "%s", cstring); 
    190  
    191         if(wi_log_file && wi_log_path) { 
    192                 path = wi_string_cstring(wi_full_path(wi_log_path)); 
    193  
    194                 fp = fopen(path, "a"); 
    195  
    196                 if(fp) { 
     158        if(wi_lock_trylock(_wi_log_lock)) { 
     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 
    197166                        fprintf(fp, "%s %s[%u]: %s\n", date, name, (uint32_t) getpid(), cstring); 
    198                         fclose(fp); 
    199                          
    200                         if(_wi_log_lines > 0 && wi_log_limit > 0) { 
    201                                 if(_wi_log_lines % (int) ((float) wi_log_limit / 10.0f) == 0) { 
    202                                         _wi_log_truncate(path); 
    203                                          
    204                                         _wi_log_lines = wi_log_limit; 
     167                } 
     168                else if(wi_log_startup && priority < LOG_INFO) { 
     169                        fp = stderr; 
     170 
     171                        fprintf(fp, "%s: %s\n", name, cstring); 
     172                } 
     173                else if(wi_log_tool) { 
     174                        fp = (priority < LOG_INFO) ? stderr : stdout; 
     175 
     176                        fprintf(fp, "%s: %s\n", name, cstring); 
     177                } 
     178                else if(wi_log_plain) { 
     179                        fp = (priority < 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                        syslog(priority, "%s", cstring); 
     189 
     190                if(wi_log_file && wi_log_path) { 
     191                        path = wi_string_cstring(wi_full_path(wi_log_path)); 
     192 
     193                        fp = fopen(path, "a"); 
     194 
     195                        if(fp) { 
     196                                fprintf(fp, "%s %s[%u]: %s\n", date, name, (uint32_t) getpid(), cstring); 
     197                                fclose(fp); 
     198                                 
     199                                if(_wi_log_lines > 0 && wi_log_limit > 0) { 
     200                                        if(_wi_log_lines % (int) ((float) wi_log_limit / 10.0f) == 0) { 
     201                                                _wi_log_truncate(path); 
     202                                                 
     203                                                _wi_log_lines = wi_log_limit; 
     204                                        } 
    205205                                } 
     206                                 
     207                                _wi_log_lines++; 
     208                        } else { 
     209                                fprintf(stderr, "%s: %s: %s\n", name, path, strerror(errno)); 
    206210                        } 
    207                          
    208                         _wi_log_lines++; 
    209                 } else { 
    210                         fprintf(stderr, "%s: %s: %s\n", name, path, strerror(errno)); 
    211                 } 
    212         } 
    213  
    214         if(wi_log_callback) 
    215                 (*wi_log_callback)(string); 
    216  
    217         if(wi_log_startup && priority == LOG_ERR) 
    218                 exit(1); 
    219  
    220         wi_lock_unlock(_wi_log_lock); 
     211                } 
     212 
     213                if(wi_log_callback) 
     214                        (*wi_log_callback)(string); 
     215 
     216                if(wi_log_startup && priority == LOG_ERR) 
     217                        exit(1); 
     218 
     219                wi_lock_unlock(_wi_log_lock); 
     220        } 
    221221         
    222222        wi_release(string);