Commit f86294c6 authored by Hardy Simpson's avatar Hardy Simpson

bugfix: when multiple processes write to one static file with rotation, not...

bugfix: when multiple processes write to one static file with rotation, not the newest file but archive files will be appended.
parent e67e5cf1
......@@ -539,8 +539,7 @@ static int zlog_rotater_unlock(zlog_rotater_t *a_rotater)
int zlog_rotater_rotate(zlog_rotater_t *a_rotater,
char *base_path, size_t msg_len,
char *archive_path, long archive_max_size, int archive_max_count,
int *reopen_fd, int reopen_flags, unsigned int reopen_perms)
char *archive_path, long archive_max_size, int archive_max_count)
{
int rc = 0;
struct zlog_stat info;
......@@ -552,9 +551,6 @@ int zlog_rotater_rotate(zlog_rotater_t *a_rotater,
return 0;
}
/* just one thread in one process in the global system run code here,
* so it is safe to reopen the fd of file */
if (stat(base_path, &info)) {
rc = -1;
zc_error("stat [%s] fail, errno[%d]", base_path, errno);
......@@ -578,19 +574,6 @@ int zlog_rotater_rotate(zlog_rotater_t *a_rotater,
//zc_debug("zlog_rotater_file_ls_mv success");
if (reopen_fd == NULL) goto exit;
if (close(*reopen_fd)) {
rc = -1;
zc_error("close fail, errno[%d]", errno);
} /* still try open again */
if ((*reopen_fd = open(base_path, reopen_flags, reopen_perms)) < 0) {
rc = -1;
zc_error("open fail, errno[%d]", errno);
goto exit;
}
exit:
/* unlock file */
if (zlog_rotater_unlock(a_rotater)) {
......
......@@ -49,8 +49,7 @@ void zlog_rotater_del(zlog_rotater_t *a_rotater);
*/
int zlog_rotater_rotate(zlog_rotater_t *a_rotater,
char *base_path, size_t msg_len,
char *archive_path, long archive_max_size, int archive_max_count,
int *reopen_fd, int reopen_flags, unsigned int reopen_perms);
char *archive_path, long archive_max_size, int archive_max_count);
void zlog_rotater_profile(zlog_rotater_t *a_rotater, int flag);
......
......@@ -134,15 +134,34 @@ static int zlog_rule_output_static_file_rotate(zlog_rule_t * a_rule, zlog_thread
{
size_t len;
struct zlog_stat info;
int fd;
if (zlog_format_gen_msg(a_rule->format, a_thread)) {
zc_error("zlog_format_gen_msg fail");
return -1;
}
fd = open(a_rule->file_path,
a_rule->file_open_flags | O_WRONLY | O_APPEND | O_CREAT, a_rule->file_perms);
if (fd < 0) {
zc_error("open file[%s] fail, errno[%d]", a_rule->file_path, errno);
return -1;
}
len = zlog_buf_len(a_thread->msg_buf);
if (write(a_rule->static_fd, zlog_buf_str(a_thread->msg_buf), len) < 0) {
if (write(fd, zlog_buf_str(a_thread->msg_buf), len) < 0) {
zc_error("write fail, errno[%d]", errno);
close(fd);
return -1;
}
if (a_rule->fsync_period && ++a_rule->fsync_count >= a_rule->fsync_period) {
a_rule->fsync_count = 0;
if (fsync(fd)) zc_error("fsync[%d] fail, errno[%d]", fd, errno);
}
if (close(fd) < 0) {
zc_error("close fail, maybe cause by write, errno[%d]", errno);
return -1;
}
......@@ -170,9 +189,7 @@ static int zlog_rule_output_static_file_rotate(zlog_rule_t * a_rule, zlog_thread
if (zlog_rotater_rotate(zlog_env_conf->rotater,
a_rule->file_path, len,
zlog_rule_gen_archive_path(a_rule, a_thread),
a_rule->archive_max_size, a_rule->archive_max_count,
&(a_rule->static_fd),
a_rule->file_open_flags | O_WRONLY | O_APPEND | O_CREAT, a_rule->file_perms)
a_rule->archive_max_size, a_rule->archive_max_count)
) {
zc_error("zlog_rotater_rotate fail");
return -1;
......@@ -293,8 +310,7 @@ static int zlog_rule_output_dynamic_file_rotate(zlog_rule_t * a_rule, zlog_threa
if (zlog_rotater_rotate(zlog_env_conf->rotater,
path, len,
zlog_rule_gen_archive_path(a_rule, a_thread),
a_rule->archive_max_size, a_rule->archive_max_count,
NULL, 0, 0)
a_rule->archive_max_size, a_rule->archive_max_count)
) {
zc_error("zlog_rotater_rotate fail");
return -1;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment