Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | TODO: log pid and tid |
| 4 | */ |
| 5 | |
| 6 | /* |
Igor Sysoev | 1d8d9ee | 2003-04-28 15:06:39 +0000 | [diff] [blame] | 7 | "[time as ctime()] [alert] 412#3 (32)Broken pipe: anything" |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 8 | |
| 9 | "[time as ctime()] [alert] (32)Broken pipe: anything" |
| 10 | "[time as ctime()] [alert] anything" |
| 11 | */ |
| 12 | |
| 13 | #include <ngx_config.h> |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 14 | #include <ngx_core.h> |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 15 | |
| 16 | |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 17 | static char *ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf); |
| 18 | |
| 19 | |
| 20 | static ngx_str_t errlog_name = ngx_string("errlog"); |
| 21 | |
| 22 | static ngx_command_t ngx_errlog_commands[] = { |
| 23 | |
| 24 | {ngx_string("error_log"), |
| 25 | NGX_MAIN_CONF|NGX_CONF_TAKE1, |
| 26 | ngx_set_error_log, |
| 27 | 0, |
| 28 | 0, |
| 29 | NULL}, |
| 30 | |
| 31 | ngx_null_command |
| 32 | }; |
| 33 | |
| 34 | |
| 35 | ngx_module_t ngx_errlog_module = { |
| 36 | NGX_MODULE, |
| 37 | &errlog_name, /* module context */ |
| 38 | ngx_errlog_commands, /* module directives */ |
| 39 | NGX_CORE_MODULE, /* module type */ |
Igor Sysoev | a7f7fa8 | 2003-07-11 04:50:59 +0000 | [diff] [blame^] | 40 | NULL, /* init module */ |
| 41 | NULL /* init child */ |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 42 | }; |
| 43 | |
| 44 | |
Igor Sysoev | f5e97c5 | 2003-06-26 15:35:36 +0000 | [diff] [blame] | 45 | static ngx_open_file_t ngx_stderr; |
| 46 | static ngx_log_t ngx_log; |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 47 | |
| 48 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 49 | static const char *err_levels[] = { |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 50 | "stderr", "emerg", "alert", "crit", "error", |
| 51 | "warn", "notice", "info", "debug" |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | #if (HAVE_VARIADIC_MACROS) |
| 55 | void ngx_log_error_core(int level, ngx_log_t *log, ngx_err_t err, |
| 56 | const char *fmt, ...) |
| 57 | #else |
| 58 | void ngx_log_error_core(int level, ngx_log_t *log, ngx_err_t err, |
| 59 | const char *fmt, va_list args) |
| 60 | #endif |
| 61 | { |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 62 | char errstr[MAX_ERROR_STR]; |
| 63 | ngx_tm_t tm; |
| 64 | size_t len; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 65 | #if (HAVE_VARIADIC_MACROS) |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 66 | va_list args; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 67 | #endif |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 68 | #if (WIN32) |
| 69 | int written; |
| 70 | #endif |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 71 | |
| 72 | ngx_localtime(&tm); |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 73 | len = ngx_snprintf(errstr, sizeof(errstr), "%4d/%02d/%02d %02d:%02d:%02d", |
Igor Sysoev | 5eef618 | 2002-12-15 21:08:04 +0000 | [diff] [blame] | 74 | tm.ngx_tm_year, tm.ngx_tm_mon, tm.ngx_tm_mday, |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 75 | tm.ngx_tm_hour, tm.ngx_tm_min, tm.ngx_tm_sec); |
| 76 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 77 | len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1, |
| 78 | " [%s] ", err_levels[level]); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 79 | |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 80 | /* pid#tid */ |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 81 | len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1, |
Igor Sysoev | 239baac | 2003-06-11 15:28:34 +0000 | [diff] [blame] | 82 | PID_FMT "#%d: ", ngx_getpid(), 0); |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 83 | |
| 84 | #if (HAVE_VARIADIC_MACROS) |
| 85 | va_start(args, fmt); |
| 86 | len += ngx_vsnprintf(errstr + len, sizeof(errstr) - len - 1, fmt, args); |
| 87 | va_end(args); |
| 88 | #else |
| 89 | len += ngx_vsnprintf(errstr + len, sizeof(errstr) - len - 1, fmt, args); |
| 90 | #endif |
| 91 | |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 92 | if (err) { |
Igor Sysoev | 1d8d9ee | 2003-04-28 15:06:39 +0000 | [diff] [blame] | 93 | |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 94 | #if (WIN32) |
Igor Sysoev | 1d8d9ee | 2003-04-28 15:06:39 +0000 | [diff] [blame] | 95 | if ((unsigned) err >= 0x80000000) { |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 96 | len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1, |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 97 | " (%X: ", err); |
Igor Sysoev | 1d8d9ee | 2003-04-28 15:06:39 +0000 | [diff] [blame] | 98 | } else { |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 99 | len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1, |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 100 | " (%d: ", err); |
Igor Sysoev | 1d8d9ee | 2003-04-28 15:06:39 +0000 | [diff] [blame] | 101 | } |
| 102 | #else |
| 103 | len += ngx_snprintf(errstr + len, sizeof(errstr) - len - 1, |
| 104 | " (%d: ", err); |
| 105 | #endif |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 106 | |
| 107 | len += ngx_strerror_r(err, errstr + len, sizeof(errstr) - len - 1); |
| 108 | if (len < sizeof(errstr) - 2) { |
| 109 | errstr[len++] = ')'; |
| 110 | } else { |
| 111 | len = sizeof(errstr) - 2; |
| 112 | } |
| 113 | } |
| 114 | |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 115 | if (level != NGX_LOG_DEBUG && log->handler) { |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 116 | len += log->handler(log->data, errstr + len, sizeof(errstr) - len - 1); |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 117 | } |
Igor Sysoev | 0ad17c0 | 2002-08-26 15:18:19 +0000 | [diff] [blame] | 118 | |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 119 | if (len > sizeof(errstr) - 2) { |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 120 | len = sizeof(errstr) - 2; |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 121 | } |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 122 | |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 123 | #if (WIN32) |
| 124 | errstr[len++] = '\r'; |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 125 | errstr[len++] = '\n'; |
Igor Sysoev | f5e97c5 | 2003-06-26 15:35:36 +0000 | [diff] [blame] | 126 | if (log->file->fd) { |
| 127 | WriteFile(log->file->fd, errstr, len, &written, NULL); |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 128 | } |
| 129 | #else |
| 130 | errstr[len++] = '\n'; |
Igor Sysoev | f5e97c5 | 2003-06-26 15:35:36 +0000 | [diff] [blame] | 131 | write(log->file->fd, errstr, len); |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 132 | #endif |
| 133 | |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 134 | |
| 135 | #if 0 |
| 136 | errstr[len] = '\0'; |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 137 | fputs(errstr, stderr); |
Igor Sysoev | 96f8377 | 2002-09-07 10:14:25 +0000 | [diff] [blame] | 138 | fflush(stderr); |
Igor Sysoev | a6717c4 | 2002-12-23 06:29:22 +0000 | [diff] [blame] | 139 | #endif |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 140 | } |
| 141 | |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 142 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 143 | #if !(HAVE_VARIADIC_MACROS) |
| 144 | |
| 145 | void ngx_log_error(int level, ngx_log_t *log, ngx_err_t err, |
| 146 | const char *fmt, ...) |
| 147 | { |
| 148 | va_list args; |
| 149 | |
| 150 | if (log->log_level >= level) { |
| 151 | va_start(args, fmt); |
| 152 | ngx_log_error_core(level, log, err, fmt, args); |
| 153 | va_end(args); |
| 154 | } |
| 155 | } |
| 156 | |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 157 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 158 | void ngx_log_debug_core(ngx_log_t *log, const char *fmt, ...) |
| 159 | { |
| 160 | va_list args; |
| 161 | |
| 162 | va_start(args, fmt); |
| 163 | ngx_log_error_core(NGX_LOG_DEBUG, log, 0, fmt, args); |
| 164 | va_end(args); |
| 165 | } |
| 166 | |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 167 | |
Igor Sysoev | 6de5c2c | 2002-08-06 16:39:45 +0000 | [diff] [blame] | 168 | void ngx_assert_core(ngx_log_t *log, const char *fmt, ...) |
| 169 | { |
| 170 | va_list args; |
| 171 | |
| 172 | va_start(args, fmt); |
| 173 | ngx_log_error_core(NGX_LOG_ALERT, log, 0, fmt, args); |
| 174 | va_end(args); |
| 175 | } |
| 176 | |
| 177 | #endif |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 178 | |
| 179 | |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 180 | #if 0 |
| 181 | |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 182 | void ngx_log_stderr(ngx_event_t *ev) |
| 183 | { |
| 184 | char errstr[MAX_ERROR_STR]; |
| 185 | ssize_t n; |
| 186 | ngx_err_t err; |
| 187 | |
| 188 | for ( ;; ) { |
| 189 | n = read((ngx_fd_t) ev->data, errstr, sizeof(errstr - 1)); |
| 190 | |
| 191 | if (n == -1) { |
Igor Sysoev | fa73aac | 2003-05-21 13:28:21 +0000 | [diff] [blame] | 192 | err = ngx_errno; |
Igor Sysoev | 1c13c66 | 2003-05-20 15:37:55 +0000 | [diff] [blame] | 193 | if (err == NGX_EAGAIN) { |
| 194 | return; |
| 195 | } |
| 196 | |
| 197 | ngx_log_error(NGX_LOG_ALERT, &ngx_log, err, "read() failed"); |
| 198 | return; |
| 199 | } |
| 200 | |
| 201 | if (n == 0) { |
| 202 | ngx_log_error(NGX_LOG_ALERT, &ngx_log, 0, "stderr clolsed"); |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | errstr[n] = '\0'; |
| 207 | ngx_log_error(NGX_LOG_STDERR, &ngx_log, 0, "%s", errstr); |
| 208 | } |
| 209 | } |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 210 | |
| 211 | #endif |
| 212 | |
| 213 | |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 214 | |
| 215 | ngx_log_t *ngx_log_init_errlog() |
| 216 | { |
| 217 | #if (WIN32) |
Igor Sysoev | be2cfc3 | 2003-06-15 18:32:13 +0000 | [diff] [blame] | 218 | |
Igor Sysoev | f5e97c5 | 2003-06-26 15:35:36 +0000 | [diff] [blame] | 219 | ngx_stderr.fd = GetStdHandle(STD_ERROR_HANDLE); |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 220 | |
Igor Sysoev | f5e97c5 | 2003-06-26 15:35:36 +0000 | [diff] [blame] | 221 | if (ngx_stderr.fd == NGX_INVALID_FILE) { |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 222 | /* TODO: where we can log error ? */ |
| 223 | return NULL; |
| 224 | |
Igor Sysoev | f5e97c5 | 2003-06-26 15:35:36 +0000 | [diff] [blame] | 225 | } else if (ngx_stderr.fd == NULL) { |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 226 | /* there are no associated standard handles */ |
| 227 | /* TODO: where we can log possible errors ? */ |
| 228 | } |
| 229 | |
| 230 | #else |
Igor Sysoev | be2cfc3 | 2003-06-15 18:32:13 +0000 | [diff] [blame] | 231 | |
Igor Sysoev | f5e97c5 | 2003-06-26 15:35:36 +0000 | [diff] [blame] | 232 | ngx_stderr.fd = STDERR_FILENO; |
Igor Sysoev | be2cfc3 | 2003-06-15 18:32:13 +0000 | [diff] [blame] | 233 | |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 234 | #endif |
| 235 | |
Igor Sysoev | f5e97c5 | 2003-06-26 15:35:36 +0000 | [diff] [blame] | 236 | ngx_log.file = &ngx_stderr; |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 237 | ngx_log.log_level = NGX_LOG_INFO; |
| 238 | /* STUB */ ngx_log.log_level = NGX_LOG_DEBUG; |
| 239 | |
| 240 | return &ngx_log; |
| 241 | } |
| 242 | |
| 243 | |
Igor Sysoev | b8c367c | 2003-07-10 16:26:57 +0000 | [diff] [blame] | 244 | ngx_log_t *ngx_log_create_errlog(ngx_cycle_t *cycle, ngx_str_t *name) |
Igor Sysoev | 96c56c9 | 2003-07-02 14:41:17 +0000 | [diff] [blame] | 245 | { |
| 246 | ngx_log_t *log; |
| 247 | |
| 248 | ngx_test_null(log, ngx_pcalloc(cycle->pool, sizeof(ngx_log_t)), NULL); |
| 249 | ngx_test_null(log->file, ngx_push_array(&cycle->open_files), NULL); |
Igor Sysoev | 340b03b | 2003-07-04 15:10:33 +0000 | [diff] [blame] | 250 | log->file->fd = NGX_INVALID_FILE; |
Igor Sysoev | b8c367c | 2003-07-10 16:26:57 +0000 | [diff] [blame] | 251 | if (name) { |
| 252 | log->file->name = *name; |
| 253 | } |
Igor Sysoev | 96c56c9 | 2003-07-02 14:41:17 +0000 | [diff] [blame] | 254 | |
| 255 | return log; |
| 256 | } |
| 257 | |
| 258 | |
Igor Sysoev | 340b03b | 2003-07-04 15:10:33 +0000 | [diff] [blame] | 259 | static char *ngx_set_error_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) |
| 260 | { |
| 261 | ngx_str_t *value; |
| 262 | |
| 263 | value = cf->args->elts; |
| 264 | |
| 265 | if (value[1].len == 6 && ngx_strcmp(value[1].data, "stderr") == 0) { |
| 266 | cf->cycle->log->file = &ngx_stderr; |
| 267 | |
| 268 | } else { |
| 269 | cf->cycle->log->file->name = value[1]; |
| 270 | } |
| 271 | |
| 272 | return NGX_CONF_OK; |
| 273 | } |
| 274 | |
| 275 | |
| 276 | #if 0 |
| 277 | |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 278 | char *ngx_log_set_errlog(ngx_conf_t *cf, ngx_command_t *cmd, ngx_log_t *log) |
| 279 | { |
| 280 | int len; |
| 281 | ngx_err_t err; |
| 282 | ngx_str_t *value; |
| 283 | |
| 284 | value = cf->args->elts; |
| 285 | |
Igor Sysoev | f5e97c5 | 2003-06-26 15:35:36 +0000 | [diff] [blame] | 286 | log->file->fd = ngx_open_file(value[1].data, |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 287 | NGX_FILE_RDWR, |
| 288 | NGX_FILE_CREATE_OR_OPEN|NGX_FILE_APPEND); |
| 289 | |
Igor Sysoev | f5e97c5 | 2003-06-26 15:35:36 +0000 | [diff] [blame] | 290 | if (log->file->fd == NGX_INVALID_FILE) { |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 291 | err = ngx_errno; |
| 292 | len = ngx_snprintf(ngx_conf_errstr, sizeof(ngx_conf_errstr) - 1, |
| 293 | ngx_open_file_n " \"%s\" failed (%d: ", |
| 294 | value[1].data, err); |
| 295 | len += ngx_strerror_r(err, ngx_conf_errstr + len, |
| 296 | sizeof(ngx_conf_errstr) - len - 1); |
| 297 | ngx_conf_errstr[len++] = ')'; |
| 298 | ngx_conf_errstr[len++] = '\0'; |
| 299 | return ngx_conf_errstr; |
| 300 | } |
| 301 | |
| 302 | #if (WIN32) |
Igor Sysoev | f5e97c5 | 2003-06-26 15:35:36 +0000 | [diff] [blame] | 303 | if (ngx_file_append_mode(log->file->fd) == NGX_ERROR) { |
Igor Sysoev | 1c10462 | 2003-06-03 15:42:58 +0000 | [diff] [blame] | 304 | err = ngx_errno; |
| 305 | len = ngx_snprintf(ngx_conf_errstr, sizeof(ngx_conf_errstr) - 1, |
| 306 | ngx_file_append_mode_n " \"%s\" failed (%d: ", |
| 307 | value[1].data, err); |
| 308 | len += ngx_strerror_r(err, ngx_conf_errstr + len, |
| 309 | sizeof(ngx_conf_errstr) - len - 1); |
| 310 | ngx_conf_errstr[len++] = ')'; |
| 311 | ngx_conf_errstr[len++] = '\0'; |
| 312 | return ngx_conf_errstr; |
| 313 | } |
| 314 | #endif |
| 315 | |
| 316 | return NGX_CONF_OK; |
| 317 | } |
Igor Sysoev | 340b03b | 2003-07-04 15:10:33 +0000 | [diff] [blame] | 318 | |
| 319 | #endif |