Klaus Demo nginx / a8ffed5
Reopening log files code moved to a separate function. The code refactored in a way to call custom handler that can do appropriate cleanup work (if any), like flushing buffers, finishing compress streams, finalizing connections to log daemon, etc.. Valentin Bartenev 9 years ago
4 changed file(s) with 83 addition(s) and 73 deletion(s). Raw diff Collapse all Expand all
944944 file->name = *name;
945945 }
946946
947 file->buffer = NULL;
947 file->flush = NULL;
948948
949949 return file;
950950 }
953953 static void
954954 ngx_conf_flush_files(ngx_cycle_t *cycle)
955955 {
956 ssize_t n, len;
957956 ngx_uint_t i;
958957 ngx_list_part_t *part;
959958 ngx_open_file_t *file;
974973 i = 0;
975974 }
976975
977 len = file[i].pos - file[i].buffer;
978
979 if (file[i].buffer == NULL || len == 0) {
980 continue;
981 }
982
983 n = ngx_write_fd(file[i].fd, file[i].buffer, len);
984
985 if (n == -1) {
986 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
987 ngx_write_fd_n " to \"%s\" failed",
988 file[i].name.data);
989
990 } else if (n != len) {
991 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
992 ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
993 file[i].name.data, n, len);
976 if (file[i].flush) {
977 file[i].flush(&file[i], cycle->log);
994978 }
995979 }
996980 }
9090 ngx_fd_t fd;
9191 ngx_str_t name;
9292
93 u_char *buffer;
94 u_char *pos;
95 u_char *last;
96
97 #if 0
98 /* e.g. append mode, error_log */
99 ngx_uint_t flags;
100 /* e.g. reopen db file */
101 ngx_uint_t (*handler)(void *data, ngx_open_file_t *file);
93 void (*flush)(ngx_open_file_t *file, ngx_log_t *log);
10294 void *data;
103 #endif
10495 };
10596
10697
11141114 void
11151115 ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user)
11161116 {
1117 ssize_t n, len;
11181117 ngx_fd_t fd;
11191118 ngx_uint_t i;
11201119 ngx_list_part_t *part;
11381137 continue;
11391138 }
11401139
1141 len = file[i].pos - file[i].buffer;
1142
1143 if (file[i].buffer && len != 0) {
1144
1145 n = ngx_write_fd(file[i].fd, file[i].buffer, len);
1146
1147 if (n == -1) {
1148 ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
1149 ngx_write_fd_n " to \"%s\" failed",
1150 file[i].name.data);
1151
1152 } else if (n != len) {
1153 ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
1154 ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
1155 file[i].name.data, n, len);
1156 }
1157
1158 file[i].pos = file[i].buffer;
1140 if (file[i].flush) {
1141 file[i].flush(&file[i], cycle->log);
11591142 }
11601143
11611144 fd = ngx_open_file(file[i].name.data, NGX_FILE_APPEND,
3737 ngx_array_t formats; /* array of ngx_http_log_fmt_t */
3838 ngx_uint_t combined_used; /* unsigned combined_used:1 */
3939 } ngx_http_log_main_conf_t;
40
41
42 typedef struct {
43 u_char *start;
44 u_char *pos;
45 u_char *last;
46 } ngx_http_log_buf_t;
4047
4148
4249 typedef struct {
7683 u_char *buf, size_t len);
7784 static ssize_t ngx_http_log_script_write(ngx_http_request_t *r,
7885 ngx_http_log_script_t *script, u_char **name, u_char *buf, size_t len);
86
87 static void ngx_http_log_flush(ngx_open_file_t *file, ngx_log_t *log);
7988
8089 static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
8190 ngx_http_log_op_t *op);
215224 size_t len;
216225 ngx_uint_t i, l;
217226 ngx_http_log_t *log;
218 ngx_open_file_t *file;
219227 ngx_http_log_op_t *op;
228 ngx_http_log_buf_t *buffer;
220229 ngx_http_log_loc_conf_t *lcf;
221230
222231 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
257266
258267 len += NGX_LINEFEED_SIZE;
259268
260 file = log[l].file;
261
262 if (file && file->buffer) {
263
264 if (len > (size_t) (file->last - file->pos)) {
265
266 ngx_http_log_write(r, &log[l], file->buffer,
267 file->pos - file->buffer);
268
269 file->pos = file->buffer;
269 buffer = log[l].file ? log[l].file->data : NULL;
270
271 if (buffer) {
272
273 if (len > (size_t) (buffer->last - buffer->pos)) {
274
275 ngx_http_log_write(r, &log[l], buffer->start,
276 buffer->pos - buffer->start);
277
278 buffer->pos = buffer->start;
270279 }
271280
272 if (len <= (size_t) (file->last - file->pos)) {
273
274 p = file->pos;
281 if (len <= (size_t) (buffer->last - buffer->pos)) {
282
283 p = buffer->pos;
275284
276285 for (i = 0; i < log[l].format->ops->nelts; i++) {
277286 p = op[i].run(r, p, &op[i]);
279288
280289 ngx_linefeed(p);
281290
282 file->pos = p;
291 buffer->pos = p;
283292
284293 continue;
285294 }
464473 }
465474
466475
476 static void
477 ngx_http_log_flush(ngx_open_file_t *file, ngx_log_t *log)
478 {
479 size_t len;
480 ssize_t n;
481 ngx_http_log_buf_t *buffer;
482
483 buffer = file->data;
484
485 len = buffer->pos - buffer->start;
486
487 if (len == 0) {
488 return;
489 }
490
491 n = ngx_write_fd(file->fd, buffer->start, len);
492
493 if (n == -1) {
494 ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
495 ngx_write_fd_n " to \"%s\" failed",
496 file->name.data);
497
498 } else if ((size_t) n != len) {
499 ngx_log_error(NGX_LOG_ALERT, log, 0,
500 ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
501 file->name.data, n, len);
502 }
503
504 buffer->pos = buffer->start;
505 }
506
507
467508 static u_char *
468509 ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
469510 ngx_http_log_op_t *op)
847888 {
848889 ngx_http_log_loc_conf_t *llcf = conf;
849890
850 ssize_t buf;
891 ssize_t size;
851892 ngx_uint_t i, n;
852893 ngx_str_t *value, name;
853894 ngx_http_log_t *log;
895 ngx_http_log_buf_t *buffer;
854896 ngx_http_log_fmt_t *fmt;
855897 ngx_http_log_main_conf_t *lmcf;
856898 ngx_http_script_compile_t sc;
9611003 name.len = value[3].len - 7;
9621004 name.data = value[3].data + 7;
9631005
964 buf = ngx_parse_size(&name);
965
966 if (buf == NGX_ERROR) {
1006 size = ngx_parse_size(&name);
1007
1008 if (size == NGX_ERROR) {
9671009 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
9681010 "invalid buffer value \"%V\"", &name);
9691011 return NGX_CONF_ERROR;
9701012 }
9711013
972 if (log->file->buffer) {
973 if (log->file->last - log->file->pos != buf) {
1014 if (log->file->data) {
1015 buffer = log->file->data;
1016
1017 if (buffer->last - buffer->start != size) {
9741018 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
9751019 "access_log \"%V\" already defined "
9761020 "with different buffer size", &value[1]);
9801024 return NGX_CONF_OK;
9811025 }
9821026
983 log->file->buffer = ngx_palloc(cf->pool, buf);
984 if (log->file->buffer == NULL) {
1027 buffer = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_buf_t));
1028 if (buffer == NULL) {
9851029 return NGX_CONF_ERROR;
9861030 }
9871031
988 log->file->pos = log->file->buffer;
989 log->file->last = log->file->buffer + buf;
1032 buffer->start = ngx_pnalloc(cf->pool, size);
1033 if (buffer->start == NULL) {
1034 return NGX_CONF_ERROR;
1035 }
1036
1037 buffer->pos = buffer->start;
1038 buffer->last = buffer->start + size;
1039
1040 log->file->flush = ngx_http_log_flush;
1041 log->file->data = buffer;
9901042 }
9911043
9921044 return NGX_CONF_OK;