Merge pull request #55 from tianlongqin/master

change retate function
pull/60/head
朱天龙 (Armink) 6 years ago committed by GitHub
commit b596fbe205
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -64,26 +64,36 @@ __exit:
return result; return result;
} }
/*
* Reopen file
*/
static bool elog_file_reopen(void)
{
FILE *tmp_fp;
tmp_fp = fopen(local_cfg.name, "a+");
if (tmp_fp) {
if (fp)
fclose(fp);
fp = tmp_fp;
fd = fileno(fp);
return true;
}
return false;
}
/* /*
* rotate the log file xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 * rotate the log file xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0
*
* it will return true when rotate successfully
*/ */
static bool elog_file_rotate(void) static void elog_file_rotate(void)
{ {
#define SUFFIX_LEN 10 #define SUFFIX_LEN 10
/* mv xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */ /* mv xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */
size_t base = strlen(local_cfg.name);
char *oldpath = NULL, *newpath = NULL;
int n; int n;
FILE *fp_bak = fp; char oldpath[256], newpath[256];
size_t base = strlen(local_cfg.name);
oldpath = (char *) malloc(base + SUFFIX_LEN);
newpath = (char *) malloc(base + SUFFIX_LEN);
if (oldpath == NULL || newpath == NULL) {
return false;
}
memcpy(oldpath, local_cfg.name, base); memcpy(oldpath, local_cfg.name, base);
memcpy(newpath, local_cfg.name, base); memcpy(newpath, local_cfg.name, base);
@ -93,21 +103,22 @@ static bool elog_file_rotate(void)
snprintf(newpath + base, SUFFIX_LEN, ".%d", n); snprintf(newpath + base, SUFFIX_LEN, ".%d", n);
rename(oldpath, newpath); rename(oldpath, newpath);
} }
free(oldpath); }
free(newpath);
fp = fopen(local_cfg.name, "a+"); /*
if (fp) { * Check if it needed retate
if (fp_bak) { */
fclose(fp_bak); static bool elog_file_retate_check(void)
} {
fd = fileno(fp); struct stat statbuf;
statbuf.st_size = 0;
if (stat(local_cfg.name, &statbuf) < 0)
return false;
if (statbuf.st_size > local_cfg.max_size)
return true; return true;
} else {
fp = fp_bak;
fd = -1;
return false; return false;
}
} }
void elog_file_write(const char *log, size_t size) void elog_file_write(const char *log, size_t size)
@ -123,12 +134,19 @@ void elog_file_write(const char *log, size_t size)
fstat(fd, &statbuf); fstat(fd, &statbuf);
if (unlikely(statbuf.st_size > local_cfg.max_size)) { if (unlikely(statbuf.st_size > local_cfg.max_size)) {
#if ELOG_FILE_MAX_ROTATE > 0
if (elog_file_retate_check()) {
/* rotate the log file */ /* rotate the log file */
if (local_cfg.max_rotate <= 0 || !elog_file_rotate()) { elog_file_rotate();
/* not enabled rotate or rotate failed */ }
if (!elog_file_reopen()) {
elog_file_port_unlock(); elog_file_port_unlock();
return; return;
} }
#else
return ;
#endif
} }
fwrite(log, size, 1, fp); fwrite(log, size, 1, fp);

Loading…
Cancel
Save