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..
This commit is contained in:
Valentin Bartenev
2012-12-23 15:36:52 +00:00
parent df71cd1125
commit a8ffed5751
4 changed files with 77 additions and 67 deletions

View File

@@ -945,7 +945,7 @@ ngx_conf_open_file(ngx_cycle_t *cycle, ngx_str_t *name)
file->name = *name;
}
file->buffer = NULL;
file->flush = NULL;
return file;
}
@@ -954,7 +954,6 @@ ngx_conf_open_file(ngx_cycle_t *cycle, ngx_str_t *name)
static void
ngx_conf_flush_files(ngx_cycle_t *cycle)
{
ssize_t n, len;
ngx_uint_t i;
ngx_list_part_t *part;
ngx_open_file_t *file;
@@ -975,23 +974,8 @@ ngx_conf_flush_files(ngx_cycle_t *cycle)
i = 0;
}
len = file[i].pos - file[i].buffer;
if (file[i].buffer == NULL || len == 0) {
continue;
}
n = ngx_write_fd(file[i].fd, file[i].buffer, len);
if (n == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
ngx_write_fd_n " to \"%s\" failed",
file[i].name.data);
} else if (n != len) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
file[i].name.data, n, len);
if (file[i].flush) {
file[i].flush(&file[i], cycle->log);
}
}
}

View File

@@ -91,17 +91,8 @@ struct ngx_open_file_s {
ngx_fd_t fd;
ngx_str_t name;
u_char *buffer;
u_char *pos;
u_char *last;
#if 0
/* e.g. append mode, error_log */
ngx_uint_t flags;
/* e.g. reopen db file */
ngx_uint_t (*handler)(void *data, ngx_open_file_t *file);
void (*flush)(ngx_open_file_t *file, ngx_log_t *log);
void *data;
#endif
};

View File

@@ -1115,7 +1115,6 @@ ngx_test_lockfile(u_char *file, ngx_log_t *log)
void
ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user)
{
ssize_t n, len;
ngx_fd_t fd;
ngx_uint_t i;
ngx_list_part_t *part;
@@ -1139,24 +1138,8 @@ ngx_reopen_files(ngx_cycle_t *cycle, ngx_uid_t user)
continue;
}
len = file[i].pos - file[i].buffer;
if (file[i].buffer && len != 0) {
n = ngx_write_fd(file[i].fd, file[i].buffer, len);
if (n == -1) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, ngx_errno,
ngx_write_fd_n " to \"%s\" failed",
file[i].name.data);
} else if (n != len) {
ngx_log_error(NGX_LOG_ALERT, cycle->log, 0,
ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
file[i].name.data, n, len);
}
file[i].pos = file[i].buffer;
if (file[i].flush) {
file[i].flush(&file[i], cycle->log);
}
fd = ngx_open_file(file[i].name.data, NGX_FILE_APPEND,

View File

@@ -40,6 +40,13 @@ typedef struct {
} ngx_http_log_main_conf_t;
typedef struct {
u_char *start;
u_char *pos;
u_char *last;
} ngx_http_log_buf_t;
typedef struct {
ngx_array_t *lengths;
ngx_array_t *values;
@@ -78,6 +85,8 @@ static void ngx_http_log_write(ngx_http_request_t *r, ngx_http_log_t *log,
static ssize_t ngx_http_log_script_write(ngx_http_request_t *r,
ngx_http_log_script_t *script, u_char **name, u_char *buf, size_t len);
static void ngx_http_log_flush(ngx_open_file_t *file, ngx_log_t *log);
static u_char *ngx_http_log_pipe(ngx_http_request_t *r, u_char *buf,
ngx_http_log_op_t *op);
static u_char *ngx_http_log_time(ngx_http_request_t *r, u_char *buf,
@@ -216,8 +225,8 @@ ngx_http_log_handler(ngx_http_request_t *r)
size_t len;
ngx_uint_t i, l;
ngx_http_log_t *log;
ngx_open_file_t *file;
ngx_http_log_op_t *op;
ngx_http_log_buf_t *buffer;
ngx_http_log_loc_conf_t *lcf;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
@@ -258,21 +267,21 @@ ngx_http_log_handler(ngx_http_request_t *r)
len += NGX_LINEFEED_SIZE;
file = log[l].file;
buffer = log[l].file ? log[l].file->data : NULL;
if (file && file->buffer) {
if (buffer) {
if (len > (size_t) (file->last - file->pos)) {
if (len > (size_t) (buffer->last - buffer->pos)) {
ngx_http_log_write(r, &log[l], file->buffer,
file->pos - file->buffer);
ngx_http_log_write(r, &log[l], buffer->start,
buffer->pos - buffer->start);
file->pos = file->buffer;
buffer->pos = buffer->start;
}
if (len <= (size_t) (file->last - file->pos)) {
if (len <= (size_t) (buffer->last - buffer->pos)) {
p = file->pos;
p = buffer->pos;
for (i = 0; i < log[l].format->ops->nelts; i++) {
p = op[i].run(r, p, &op[i]);
@@ -280,7 +289,7 @@ ngx_http_log_handler(ngx_http_request_t *r)
ngx_linefeed(p);
file->pos = p;
buffer->pos = p;
continue;
}
@@ -465,6 +474,38 @@ ngx_http_log_script_write(ngx_http_request_t *r, ngx_http_log_script_t *script,
}
static void
ngx_http_log_flush(ngx_open_file_t *file, ngx_log_t *log)
{
size_t len;
ssize_t n;
ngx_http_log_buf_t *buffer;
buffer = file->data;
len = buffer->pos - buffer->start;
if (len == 0) {
return;
}
n = ngx_write_fd(file->fd, buffer->start, len);
if (n == -1) {
ngx_log_error(NGX_LOG_ALERT, log, ngx_errno,
ngx_write_fd_n " to \"%s\" failed",
file->name.data);
} else if ((size_t) n != len) {
ngx_log_error(NGX_LOG_ALERT, log, 0,
ngx_write_fd_n " to \"%s\" was incomplete: %z of %uz",
file->name.data, n, len);
}
buffer->pos = buffer->start;
}
static u_char *
ngx_http_log_copy_short(ngx_http_request_t *r, u_char *buf,
ngx_http_log_op_t *op)
@@ -848,10 +889,11 @@ ngx_http_log_set_log(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
ngx_http_log_loc_conf_t *llcf = conf;
ssize_t buf;
ssize_t size;
ngx_uint_t i, n;
ngx_str_t *value, name;
ngx_http_log_t *log;
ngx_http_log_buf_t *buffer;
ngx_http_log_fmt_t *fmt;
ngx_http_log_main_conf_t *lmcf;
ngx_http_script_compile_t sc;
@@ -962,16 +1004,18 @@ buffer:
name.len = value[3].len - 7;
name.data = value[3].data + 7;
buf = ngx_parse_size(&name);
size = ngx_parse_size(&name);
if (buf == NGX_ERROR) {
if (size == NGX_ERROR) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"invalid buffer value \"%V\"", &name);
return NGX_CONF_ERROR;
}
if (log->file->buffer) {
if (log->file->last - log->file->pos != buf) {
if (log->file->data) {
buffer = log->file->data;
if (buffer->last - buffer->start != size) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"access_log \"%V\" already defined "
"with different buffer size", &value[1]);
@@ -981,13 +1025,21 @@ buffer:
return NGX_CONF_OK;
}
log->file->buffer = ngx_palloc(cf->pool, buf);
if (log->file->buffer == NULL) {
buffer = ngx_pcalloc(cf->pool, sizeof(ngx_http_log_buf_t));
if (buffer == NULL) {
return NGX_CONF_ERROR;
}
log->file->pos = log->file->buffer;
log->file->last = log->file->buffer + buf;
buffer->start = ngx_pnalloc(cf->pool, size);
if (buffer->start == NULL) {
return NGX_CONF_ERROR;
}
buffer->pos = buffer->start;
buffer->last = buffer->start + size;
log->file->flush = ngx_http_log_flush;
log->file->data = buffer;
}
return NGX_CONF_OK;