comparison 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
comparison
equal deleted inserted replaced
2742:e138f820b5dc 2743:d8316f307b6a
46 NGX_MODULE_V1_PADDING 46 NGX_MODULE_V1_PADDING
47 }; 47 };
48 48
49 49
50 static ngx_log_t ngx_log; 50 static ngx_log_t ngx_log;
51 static ngx_open_file_t ngx_stderr; 51 static ngx_open_file_t ngx_log_file;
52 ngx_uint_t ngx_use_stderr = 1;
52 53
53 54
54 static ngx_str_t err_levels[] = { 55 static ngx_str_t err_levels[] = {
55 ngx_string("stderr"), 56 ngx_string("stderr"),
56 ngx_string("emerg"), 57 ngx_string("emerg"),
84 #endif 85 #endif
85 { 86 {
86 #if (NGX_HAVE_VARIADIC_MACROS) 87 #if (NGX_HAVE_VARIADIC_MACROS)
87 va_list args; 88 va_list args;
88 #endif 89 #endif
89 u_char errstr[NGX_MAX_ERROR_STR], *p, *last; 90 u_char *p, *last, *msg;
91 u_char errstr[NGX_MAX_ERROR_STR];
90 92
91 if (log->file->fd == NGX_INVALID_FILE) { 93 if (log->file->fd == NGX_INVALID_FILE) {
92 return; 94 return;
93 } 95 }
94 96
106 ngx_log_pid, ngx_log_tid); 108 ngx_log_pid, ngx_log_tid);
107 109
108 if (log->connection) { 110 if (log->connection) {
109 p = ngx_snprintf(p, last - p, "*%uA ", log->connection); 111 p = ngx_snprintf(p, last - p, "*%uA ", log->connection);
110 } 112 }
113
114 msg = p;
111 115
112 #if (NGX_HAVE_VARIADIC_MACROS) 116 #if (NGX_HAVE_VARIADIC_MACROS)
113 117
114 va_start(args, fmt); 118 va_start(args, fmt);
115 p = ngx_vsnprintf(p, last - p, fmt, args); 119 p = ngx_vsnprintf(p, last - p, fmt, args);
156 } 160 }
157 161
158 ngx_linefeed(p); 162 ngx_linefeed(p);
159 163
160 (void) ngx_write_fd(log->file->fd, errstr, p - errstr); 164 (void) ngx_write_fd(log->file->fd, errstr, p - errstr);
165
166 if (!ngx_use_stderr
167 || level > NGX_LOG_WARN
168 || log->file->fd == ngx_stderr)
169 {
170 return;
171 }
172
173 msg -= (err_levels[level].len + 4);
174
175 (void) ngx_sprintf(msg, "[%V]: ", &err_levels[level]);
176
177 ngx_write_fd(ngx_stderr, msg, p - msg);
161 } 178 }
162 179
163 180
164 #if !(NGX_HAVE_VARIADIC_MACROS) 181 #if !(NGX_HAVE_VARIADIC_MACROS)
165 182
196 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err, text, param); 213 ngx_log_error(NGX_LOG_ALERT, ngx_cycle->log, err, text, param);
197 } 214 }
198 215
199 216
200 void ngx_cdecl 217 void ngx_cdecl
201 ngx_log_stderr(const char *fmt, ...) 218 ngx_log_stderr(ngx_err_t err, const char *fmt, ...)
202 { 219 {
203 u_char *p; 220 u_char *p, *last;
204 va_list args; 221 va_list args;
205 u_char errstr[NGX_MAX_ERROR_STR]; 222 u_char errstr[NGX_MAX_ERROR_STR];
206 223
207 va_start(args, fmt); 224 va_start(args, fmt);
208 p = ngx_vsnprintf(errstr, NGX_MAX_ERROR_STR, fmt, args); 225 p = ngx_vsnprintf(errstr, NGX_MAX_ERROR_STR, fmt, args);
210 227
211 if (p > errstr + NGX_MAX_ERROR_STR - NGX_LINEFEED_SIZE) { 228 if (p > errstr + NGX_MAX_ERROR_STR - NGX_LINEFEED_SIZE) {
212 p = errstr + NGX_MAX_ERROR_STR - NGX_LINEFEED_SIZE; 229 p = errstr + NGX_MAX_ERROR_STR - NGX_LINEFEED_SIZE;
213 } 230 }
214 231
232 if (err) {
233
234 last = errstr + NGX_MAX_ERROR_STR;
235
236 if (p > last - 50) {
237
238 /* leave a space for an error code */
239
240 p = last - 50;
241 *p++ = '.';
242 *p++ = '.';
243 *p++ = '.';
244 }
245
246 #if (NGX_WIN32)
247 p = ngx_snprintf(p, last - p, ((unsigned) err < 0x80000000)
248 ? " (%d: " : " (%Xd: ", err);
249 #else
250 p = ngx_snprintf(p, last - p, " (%d: ", err);
251 #endif
252
253 p = ngx_strerror_r(err, p, last - p);
254
255 if (p < last) {
256 *p++ = ')';
257 }
258 }
259
215 ngx_linefeed(p); 260 ngx_linefeed(p);
216 261
217 #if (NGX_WIN32) 262 ngx_write_fd(ngx_stderr, errstr, p - errstr);
218
219 if (ngx_stderr_fileno == NULL) {
220 ngx_stderr_fileno = GetStdHandle(STD_ERROR_HANDLE);
221 }
222
223 #endif
224
225 (void) ngx_write_fd(ngx_stderr_fileno, errstr, p - errstr);
226 } 263 }
227 264
228 265
229 ngx_log_t * 266 ngx_log_t *
230 ngx_log_init(void) 267 ngx_log_init(void)
231 { 268 {
232 ngx_log.file = &ngx_stderr; 269 ngx_log.file = &ngx_log_file;
233 ngx_log.log_level = NGX_LOG_NOTICE; 270 ngx_log.log_level = NGX_LOG_NOTICE;
234 271
272 /*
273 * we use ngx_strlen() here since BCC warns about
274 * condition is always false and unreachable code
275 */
276
277 if (ngx_strlen(NGX_ERROR_LOG_PATH) == 0) {
278 ngx_log_file.fd = ngx_stderr;
279 return &ngx_log;
280 }
281
282 ngx_log_file.fd = ngx_open_file((u_char *) NGX_ERROR_LOG_PATH,
283 NGX_FILE_APPEND,
284 NGX_FILE_CREATE_OR_OPEN,
285 NGX_FILE_DEFAULT_ACCESS);
286
287 if (ngx_log_file.fd == NGX_INVALID_FILE) {
288 ngx_log_stderr(ngx_errno,
289 "[emerg]: could not open error log file: "
290 ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed");
291
235 #if (NGX_WIN32) 292 #if (NGX_WIN32)
236
237 ngx_stderr.fd = ngx_open_file((u_char *) NGX_ERROR_LOG_PATH,
238 NGX_FILE_APPEND,
239 NGX_FILE_CREATE_OR_OPEN,
240 NGX_FILE_DEFAULT_ACCESS);
241
242 if (ngx_stderr.fd == NGX_INVALID_FILE) {
243 ngx_event_log(ngx_errno, 293 ngx_event_log(ngx_errno,
244 "Could not open error log file: " 294 "could not open error log file: "
245 ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed"); 295 ngx_open_file_n " \"" NGX_ERROR_LOG_PATH "\" failed");
296 #endif
297
246 return NULL; 298 return NULL;
247 } 299 }
248
249 #else
250
251 ngx_stderr.fd = STDERR_FILENO;
252
253 #endif
254 300
255 return &ngx_log; 301 return &ngx_log;
256 } 302 }
257 303
258 304
347 ngx_str_t *value; 393 ngx_str_t *value;
348 394
349 value = cf->args->elts; 395 value = cf->args->elts;
350 396
351 if (value[1].len == 6 && ngx_strcmp(value[1].data, "stderr") == 0) { 397 if (value[1].len == 6 && ngx_strcmp(value[1].data, "stderr") == 0) {
352 cf->cycle->new_log->file->fd = ngx_stderr.fd; 398 cf->cycle->new_log->file->fd = ngx_stderr;
353 cf->cycle->new_log->file->name.len = 0; 399 cf->cycle->new_log->file->name.len = 0;
354 cf->cycle->new_log->file->name.data = NULL; 400 cf->cycle->new_log->file->name.data = NULL;
355 401
356 } else { 402 } else {
357 cf->cycle->new_log->file->name = value[1]; 403 cf->cycle->new_log->file->name = value[1];