Mercurial > hg > nginx
diff src/core/ngx_log.c @ 2743:d8316f307b6a
issue start up errors and warning on both stderr and error_log
author | Igor Sysoev <igor@sysoev.ru> |
---|---|
date | Thu, 23 Apr 2009 11:13:12 +0000 |
parents | e138f820b5dc |
children | ccaf43725ff6 |
line wrap: on
line diff
--- a/src/core/ngx_log.c Thu Apr 23 09:46:14 2009 +0000 +++ b/src/core/ngx_log.c Thu Apr 23 11:13:12 2009 +0000 @@ -48,7 +48,8 @@ static ngx_log_t ngx_log; -static ngx_open_file_t ngx_stderr; +static ngx_open_file_t ngx_log_file; +ngx_uint_t ngx_use_stderr = 1; static ngx_str_t err_levels[] = { @@ -86,7 +87,8 @@ #if (NGX_HAVE_VARIADIC_MACROS) va_list args; #endif - u_char errstr[NGX_MAX_ERROR_STR], *p, *last; + u_char *p, *last, *msg; + u_char errstr[NGX_MAX_ERROR_STR]; if (log->file->fd == NGX_INVALID_FILE) { return; @@ -109,6 +111,8 @@ p = ngx_snprintf(p, last - p, "*%uA ", log->connection); } + msg = p; + #if (NGX_HAVE_VARIADIC_MACROS) va_start(args, fmt); @@ -158,6 +162,19 @@ ngx_linefeed(p); (void) ngx_write_fd(log->file->fd, errstr, p - errstr); + + if (!ngx_use_stderr + || level > NGX_LOG_WARN + || log->file->fd == ngx_stderr) + { + return; + } + + msg -= (err_levels[level].len + 4); + + (void) ngx_sprintf(msg, "[%V]: ", &err_levels[level]); + + ngx_write_fd(ngx_stderr, msg, p - msg); } @@ -198,9 +215,9 @@ void ngx_cdecl -ngx_log_stderr(const char *fmt, ...) +ngx_log_stderr(ngx_err_t err, const char *fmt, ...) { - u_char *p; + u_char *p, *last; va_list args; u_char errstr[NGX_MAX_ERROR_STR]; @@ -212,46 +229,75 @@ p = errstr + NGX_MAX_ERROR_STR - NGX_LINEFEED_SIZE; } - ngx_linefeed(p); + if (err) { + + last = errstr + NGX_MAX_ERROR_STR; + + if (p > last - 50) { + + /* leave a space for an error code */ + + p = last - 50; + *p++ = '.'; + *p++ = '.'; + *p++ = '.'; + } #if (NGX_WIN32) + p = ngx_snprintf(p, last - p, ((unsigned) err < 0x80000000) + ? " (%d: " : " (%Xd: ", err); +#else + p = ngx_snprintf(p, last - p, " (%d: ", err); +#endif - if (ngx_stderr_fileno == NULL) { - ngx_stderr_fileno = GetStdHandle(STD_ERROR_HANDLE); + p = ngx_strerror_r(err, p, last - p); + + if (p < last) { + *p++ = ')'; + } } -#endif + ngx_linefeed(p); - (void) ngx_write_fd(ngx_stderr_fileno, errstr, p - errstr); + ngx_write_fd(ngx_stderr, errstr, p - errstr); } ngx_log_t * ngx_log_init(void) { - ngx_log.file = &ngx_stderr; + ngx_log.file = &ngx_log_file; ngx_log.log_level = NGX_LOG_NOTICE; + /* + * we use ngx_strlen() here since BCC warns about + * condition is always false and unreachable code + */ + + if (ngx_strlen(NGX_ERROR_LOG_PATH) == 0) { + ngx_log_file.fd = ngx_stderr; + return &ngx_log; + } + + ngx_log_file.fd = ngx_open_file((u_char *) NGX_ERROR_LOG_PATH, + NGX_FILE_APPEND, + NGX_FILE_CREATE_OR_OPEN, + NGX_FILE_DEFAULT_ACCESS); + + if (ngx_log_file.fd == NGX_INVALID_FILE) { + ngx_log_stderr(ngx_errno, + "[emerg]: could not open error log file: " + ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed"); + #if (NGX_WIN32) + ngx_event_log(ngx_errno, + "could not open error log file: " + ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed"); +#endif - ngx_stderr.fd = ngx_open_file((u_char *) NGX_ERROR_LOG_PATH, - NGX_FILE_APPEND, - NGX_FILE_CREATE_OR_OPEN, - NGX_FILE_DEFAULT_ACCESS); - - if (ngx_stderr.fd == NGX_INVALID_FILE) { - ngx_event_log(ngx_errno, - "Could not open error log file: " - ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed"); return NULL; } -#else - - ngx_stderr.fd = STDERR_FILENO; - -#endif - return &ngx_log; } @@ -349,7 +395,7 @@ value = cf->args->elts; if (value[1].len == 6 && ngx_strcmp(value[1].data, "stderr") == 0) { - cf->cycle->new_log->file->fd = ngx_stderr.fd; + cf->cycle->new_log->file->fd = ngx_stderr; cf->cycle->new_log->file->name.len = 0; cf->cycle->new_log->file->name.data = NULL;