Changeset 475

Show
Ignore:
Timestamp:
07/26/2005 08:26:28 AM (3 years ago)
Author:
jan
Message:

cleaned up the errorlog writing, default is now stderr, syslog is requested explicitly

Location:
branches/lighttpd-1.3.x/src
Files:
7 modified

Legend:

Unmodified
Added
Removed
  • branches/lighttpd-1.3.x/src/base.h

    r470 r475  
    408408        unsigned short port; 
    409409        buffer *bindhost; 
    410         buffer *error_logfile; 
     410         
     411        buffer *errorlog_file; 
     412        unsigned short errorlog_use_syslog; 
     413         
    411414        unsigned short dont_daemonize; 
    412415        buffer *changeroot; 
     
    414417        buffer *groupname; 
    415418         
    416         buffer *license; 
    417419        buffer *pid_file; 
    418420         
     
    456458        server_socket_array srv_sockets; 
    457459         
    458         int log_error_fd; 
    459         int log_using_syslog; 
     460        /* the errorlog */ 
     461        int errorlog_fd; 
     462        enum { ERRORLOG_STDERR, ERRORLOG_FILE, ERRORLOG_SYSLOG } errorlog_mode; 
     463        buffer *errorlog_buf; 
     464         
    460465        fdevents *ev, *ev_ins; 
    461466         
     
    463468        void *plugin_slots; 
    464469         
     470        /* counters */ 
    465471        int con_opened; 
    466472        int con_read; 
     
    478484        buffer *parse_full_path; 
    479485        buffer *response_header; 
    480         buffer *error_log; 
    481486        buffer *response_range; 
    482487        buffer *tmp_buf; 
  • branches/lighttpd-1.3.x/src/config.c

    r447 r475  
    7777                 
    7878                { "dir-listing.encoding",        NULL, T_CONFIG_STRING, T_CONFIG_SCOPE_CONNECTION },  /* 41 */ 
     79                { "server.errorlog-use-syslog",  NULL, T_CONFIG_BOOLEAN, T_CONFIG_SCOPE_SERVER },     /* 42 */ 
    7980                 
    8081                { "server.host",                 "use server.bind instead", T_CONFIG_DEPRECATED, T_CONFIG_SCOPE_UNSET }, 
     
    9394        /* 0 */ 
    9495        cv[0].destination = srv->srvconf.bindhost; 
    95         cv[1].destination = srv->srvconf.error_logfile; 
     96        cv[1].destination = srv->srvconf.errorlog_file; 
    9697        cv[3].destination = srv->srvconf.changeroot; 
    9798        cv[4].destination = srv->srvconf.username; 
     
    107108        cv[36].destination = &(srv->srvconf.log_request_header_on_error); 
    108109        cv[37].destination = &(srv->srvconf.log_state_handling); 
     110         
     111        cv[42].destination = &(srv->srvconf.errorlog_use_syslog); 
    109112         
    110113        srv->config_storage = calloc(1, srv->config_context->used * sizeof(specific_config *)); 
  • branches/lighttpd-1.3.x/src/log.c

    r1 r475  
    3333 * open the errorlog 
    3434 *  
     35 * we have 3 possibilities: 
     36 * - stderr (default) 
     37 * - syslog  
     38 * - logfile 
     39 *  
    3540 * if the open failed, report to the user and die 
    36  * if no filename is given, use syslog instead 
    3741 *  
    3842 */ 
     
    4246        int close_stderr = 1; 
    4347         
    44         if (srv->srvconf.error_logfile->used) { 
    45                 const char *logfile = srv->srvconf.error_logfile->ptr; 
    46                  
    47                 if (-1 == (srv->log_error_fd = open(logfile, O_APPEND | O_WRONLY | O_CREAT | O_LARGEFILE, 0644))) { 
     48#ifdef HAVE_SYSLOG_H 
     49        /* perhaps someone wants to use syslog() */ 
     50        openlog("lighttpd", LOG_CONS | LOG_PID, LOG_DAEMON); 
     51#endif 
     52        srv->errorlog_mode = ERRORLOG_STDERR; 
     53         
     54        if (srv->srvconf.errorlog_use_syslog) { 
     55                srv->errorlog_mode = ERRORLOG_SYSLOG; 
     56        } else if (!buffer_is_empty(srv->srvconf.errorlog_file)) { 
     57                const char *logfile = srv->srvconf.errorlog_file->ptr; 
     58                 
     59                if (-1 == (srv->errorlog_fd = open(logfile, O_APPEND | O_WRONLY | O_CREAT | O_LARGEFILE, 0644))) { 
    4860                        log_error_write(srv, __FILE__, __LINE__, "SSSS",  
    4961                                        "opening errorlog '", logfile, 
     
    5466#ifdef FD_CLOEXEC 
    5567                /* close fd on exec (cgi) */ 
    56                 fcntl(srv->log_error_fd, F_SETFD, FD_CLOEXEC); 
    57 #endif 
    58         } else { 
    59                 srv->log_error_fd = -1; 
    60 #ifdef HAVE_SYSLOG_H 
    61                 /* syslog-mode */ 
    62                 srv->log_using_syslog = 1; 
    63                 openlog("lighttpd", LOG_CONS, LOG_LOCAL0); 
    64 #endif 
     68                fcntl(srv->errorlog_fd, F_SETFD, FD_CLOEXEC); 
     69#endif 
     70                srv->errorlog_mode = ERRORLOG_FILE; 
    6571        } 
    6672         
     
    7177        if (RUNNING_ON_VALGRIND) close_stderr = 0; 
    7278#endif 
    73  
     79        if (srv->errorlog_mode == ERRORLOG_STDERR) close_stderr = 0; 
     80         
    7481        /* move stderr to /dev/null */ 
    7582        if (close_stderr && 
     
    93100        /* only cycle if we are not in syslog-mode */ 
    94101         
    95         if (0 == srv->log_using_syslog) { 
    96                 const char *logfile = srv->srvconf.error_logfile->ptr; 
     102        if (srv->errorlog_mode == ERRORLOG_FILE) { 
     103                const char *logfile = srv->srvconf.errorlog_file->ptr; 
    97104                /* already check of opening time */ 
    98105                 
     
    106113                                        ", falling back to syslog()"); 
    107114                         
    108                         close(srv->log_error_fd); 
    109                         srv->log_error_fd = -1; 
     115                        close(srv->errorlog_fd); 
     116                        srv->errorlog_fd = -1; 
    110117#ifdef HAVE_SYSLOG_H     
    111                         /* fall back to syslog() */ 
    112                         srv->log_using_syslog = 1; 
    113                          
    114                         openlog("lighttpd", LOG_CONS, LOG_LOCAL0); 
     118                        srv->errorlog_mode = ERRORLOG_SYSLOG; 
    115119#endif 
    116120                } else { 
    117121                        /* ok, new log is open, close the old one */ 
    118                         close(srv->log_error_fd); 
    119                         srv->log_error_fd = new_fd; 
     122                        close(srv->errorlog_fd); 
     123                        srv->errorlog_fd = new_fd; 
    120124                } 
    121125        } 
     
    129133        log_error_write(srv, __FILE__, __LINE__, "s", "server stopped"); 
    130134         
    131         if (srv->log_error_fd >= 0) { 
    132                 close(srv->log_error_fd); 
    133         } else if(srv->log_using_syslog) { 
     135        switch(srv->errorlog_mode) { 
     136        case ERRORLOG_FILE: 
     137                close(srv->errorlog_fd); 
     138                break; 
     139        case ERRORLOG_SYSLOG: 
    134140#ifdef HAVE_SYSLOG_H 
    135141                closelog(); 
    136142#endif 
     143                break; 
     144        case ERRORLOG_STDERR: 
     145                break; 
    137146        } 
    138147         
     
    143152        va_list ap; 
    144153         
    145         if (srv->log_using_syslog == 0) { 
     154        switch(srv->errorlog_mode) { 
     155        case ERRORLOG_FILE: 
     156        case ERRORLOG_STDERR: 
    146157                /* cache the generated timestamp */ 
    147158                if (srv->cur_ts != srv->last_generated_debug_ts) { 
     
    153164                } 
    154165 
    155                 buffer_copy_string_buffer(srv->error_log, srv->ts_debug_str); 
    156                 BUFFER_APPEND_STRING_CONST(srv->error_log, ": ("); 
    157         } else { 
    158                 BUFFER_COPY_STRING_CONST(srv->error_log, "("); 
    159         } 
    160         buffer_append_string(srv->error_log, filename); 
    161         BUFFER_APPEND_STRING_CONST(srv->error_log, "."); 
    162         buffer_append_long(srv->error_log, line); 
    163         BUFFER_APPEND_STRING_CONST(srv->error_log, ") "); 
     166                buffer_copy_string_buffer(srv->errorlog_buf, srv->ts_debug_str); 
     167                BUFFER_APPEND_STRING_CONST(srv->errorlog_buf, ": ("); 
     168                break; 
     169        case ERRORLOG_SYSLOG: 
     170                /* syslog is generating its own timestamps */ 
     171                BUFFER_COPY_STRING_CONST(srv->errorlog_buf, "("); 
     172                break; 
     173        } 
     174         
     175        buffer_append_string(srv->errorlog_buf, filename); 
     176        BUFFER_APPEND_STRING_CONST(srv->errorlog_buf, "."); 
     177        buffer_append_long(srv->errorlog_buf, line); 
     178        BUFFER_APPEND_STRING_CONST(srv->errorlog_buf, ") "); 
    164179         
    165180         
     
    173188                case 's':           /* string */ 
    174189                        s = va_arg(ap, char *); 
    175                         buffer_append_string(srv->error_log, s); 
    176                         BUFFER_APPEND_STRING_CONST(srv->error_log, " "); 
     190                        buffer_append_string(srv->errorlog_buf, s); 
     191                        BUFFER_APPEND_STRING_CONST(srv->errorlog_buf, " "); 
    177192                        break; 
    178193                case 'b':           /* buffer */ 
    179194                        b = va_arg(ap, buffer *); 
    180                         buffer_append_string_buffer(srv->error_log, b); 
    181                         BUFFER_APPEND_STRING_CONST(srv->error_log, " "); 
     195                        buffer_append_string_buffer(srv->errorlog_buf, b); 
     196                        BUFFER_APPEND_STRING_CONST(srv->errorlog_buf, " "); 
    182197                        break; 
    183198                case 'd':           /* int */ 
    184199                        d = va_arg(ap, int); 
    185                         buffer_append_long(srv->error_log, d); 
    186                         BUFFER_APPEND_STRING_CONST(srv->error_log, " "); 
     200                        buffer_append_long(srv->errorlog_buf, d); 
     201                        BUFFER_APPEND_STRING_CONST(srv->errorlog_buf, " "); 
    187202                        break; 
    188203                case 'o':           /* off_t */ 
    189204                        o = va_arg(ap, off_t); 
    190                         buffer_append_off_t(srv->error_log, o); 
    191                         BUFFER_APPEND_STRING_CONST(srv->error_log, " "); 
     205                        buffer_append_off_t(srv->errorlog_buf, o); 
     206                        BUFFER_APPEND_STRING_CONST(srv->errorlog_buf, " "); 
    192207                        break; 
    193208                case 'x':           /* int (hex) */ 
    194209                        d = va_arg(ap, int); 
    195                         BUFFER_APPEND_STRING_CONST(srv->error_log, "0x"); 
    196                         buffer_append_hex(srv->error_log, d); 
    197                         BUFFER_APPEND_STRING_CONST(srv->error_log, " "); 
     210                        BUFFER_APPEND_STRING_CONST(srv->errorlog_buf, "0x"); 
     211                        buffer_append_hex(srv->errorlog_buf, d); 
     212                        BUFFER_APPEND_STRING_CONST(srv->errorlog_buf, " "); 
    198213                        break; 
    199214                case 'S':           /* string */ 
    200215                        s = va_arg(ap, char *); 
    201                         buffer_append_string(srv->error_log, s); 
     216                        buffer_append_string(srv->errorlog_buf, s); 
    202217                        break; 
    203218                case 'B':           /* buffer */ 
    204219                        b = va_arg(ap, buffer *); 
    205                         buffer_append_string_buffer(srv->error_log, b); 
     220                        buffer_append_string_buffer(srv->errorlog_buf, b); 
    206221                        break; 
    207222                case 'D':           /* int */ 
    208223                        d = va_arg(ap, int); 
    209                         buffer_append_long(srv->error_log, d); 
     224                        buffer_append_long(srv->errorlog_buf, d); 
    210225                        break; 
    211226                case '(': 
     
    215230                case ',': 
    216231                case ' ': 
    217                         buffer_append_string_len(srv->error_log, fmt, 1); 
     232                        buffer_append_string_len(srv->errorlog_buf, fmt, 1); 
    218233                        break; 
    219234                } 
     
    221236        va_end(ap); 
    222237         
    223         BUFFER_APPEND_STRING_CONST(srv->error_log, "\n"); 
    224          
    225         if (srv->log_error_fd >= 0) { 
    226                 write(srv->log_error_fd, srv->error_log->ptr, srv->error_log->used - 1); 
    227         } else if (srv->log_using_syslog == 0) { 
    228                 /* only available at startup time */ 
    229                 write(STDERR_FILENO, srv->error_log->ptr, srv->error_log->used - 1); 
    230         } else { 
    231 #ifdef HAVE_SYSLOG_H 
    232                 syslog(LOG_ERR, "%s", srv->error_log->ptr); 
    233 #endif 
    234         } 
    235          
    236         return 0; 
    237 } 
    238  
     238        switch(srv->errorlog_mode) { 
     239        case ERRORLOG_FILE: 
     240                BUFFER_APPEND_STRING_CONST(srv->errorlog_buf, "\n"); 
     241                write(srv->errorlog_fd, srv->errorlog_buf->ptr, srv->errorlog_buf->used - 1); 
     242                break; 
     243        case ERRORLOG_STDERR: 
     244                BUFFER_APPEND_STRING_CONST(srv->errorlog_buf, "\n"); 
     245                write(STDERR_FILENO, srv->errorlog_buf->ptr, srv->errorlog_buf->used - 1); 
     246                break; 
     247        case ERRORLOG_SYSLOG: 
     248                syslog(LOG_ERR, "%s", srv->errorlog_buf->ptr); 
     249                break; 
     250        } 
     251         
     252        return 0; 
     253} 
     254 
  • branches/lighttpd-1.3.x/src/mod_accesslog.c

    r410 r475  
    446446                 
    447447                if (s->use_syslog) { 
    448                         if (srv->log_using_syslog == 0) { 
    449                                 log_error_write(srv, __FILE__, __LINE__, "s",  
    450                                                 "accesslog can only be written to syslog if errorlog is also sent to syslog. ABORTING."); 
    451                                  
    452                                 return HANDLER_ERROR; 
    453                         } 
    454                          
    455448                        /* ignore the next checks */ 
    456449                        continue; 
  • branches/lighttpd-1.3.x/src/mod_cgi.c

    r365 r475  
    742742                close(to_cgi_fds[1]); 
    743743                 
    744                 if (srv->log_error_fd >= 0) { 
     744                /* HACK:  
     745                 * this is not nice, but it works 
     746                 * 
     747                 * we feed the stderr of the CGI to our errorlog, if possible 
     748                 */ 
     749                if (srv->errorlog_mode == ERRORLOG_FILE) { 
    745750                        close(STDERR_FILENO); 
    746                         dup2(srv->log_error_fd, STDERR_FILENO); 
     751                        dup2(srv->errorlog_fd, STDERR_FILENO); 
    747752                } 
    748753                 
     
    922927                /* we don't need the client socket */ 
    923928                for (i = 3; i < 256; i++) { 
    924                         if (i != srv->log_error_fd) close(i); 
     929                        if (i != srv->errorlog_fd) close(i); 
    925930                } 
    926931                 
  • branches/lighttpd-1.3.x/src/mod_rrdtool.c

    r448 r475  
    133133                 
    134134                close(STDERR_FILENO); 
    135                 if (srv->log_error_fd != -1) { 
    136                         dup2(srv->log_error_fd, STDERR_FILENO); 
    137                         close(srv->log_error_fd); 
     135                 
     136                if (srv->errorlog_mode == ERRORLOG_FILE) { 
     137                        dup2(srv->errorlog_fd, STDERR_FILENO); 
     138                        close(srv->errorlog_fd); 
    138139                } 
    139140                 
  • branches/lighttpd-1.3.x/src/server.c

    r449 r475  
    123123        CLEAN(ts_debug_str); 
    124124        CLEAN(ts_date_str); 
    125         CLEAN(error_log); 
     125        CLEAN(errorlog_buf); 
    126126        CLEAN(response_range); 
    127127        CLEAN(tmp_buf); 
     
    132132        buffer_copy_string(srv->empty_string, ""); 
    133133         
    134         CLEAN(srvconf.error_logfile); 
     134        CLEAN(srvconf.errorlog_file); 
    135135        CLEAN(srvconf.groupname); 
    136136        CLEAN(srvconf.username); 
    137137        CLEAN(srvconf.changeroot); 
    138138        CLEAN(srvconf.bindhost); 
    139         CLEAN(srvconf.license); 
    140139        CLEAN(srvconf.event_handler); 
    141140        CLEAN(srvconf.pid_file); 
     
    173172         
    174173        /* use syslog */ 
    175         srv->log_error_fd = -1; 
     174        srv->errorlog_fd = -1; 
     175        srv->errorlog_mode = ERRORLOG_STDERR; 
    176176 
    177177        srv->split_vals = array_init(); 
     
    204204        CLEAN(ts_debug_str); 
    205205        CLEAN(ts_date_str); 
    206         CLEAN(error_log); 
     206        CLEAN(errorlog_buf); 
    207207        CLEAN(response_range); 
    208208        CLEAN(tmp_buf); 
     
    211211        CLEAN(empty_string); 
    212212         
    213         CLEAN(srvconf.error_logfile); 
     213        CLEAN(srvconf.errorlog_file); 
    214214        CLEAN(srvconf.groupname); 
    215215        CLEAN(srvconf.username); 
    216216        CLEAN(srvconf.changeroot); 
    217217        CLEAN(srvconf.bindhost); 
    218         CLEAN(srvconf.license); 
    219218        CLEAN(srvconf.event_handler); 
    220219        CLEAN(srvconf.pid_file);