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

@ -65,26 +65,36 @@ __exit:
} }
/* /*
* rotate the log file xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 * Reopen file
*
* it will return true when rotate successfully
*/ */
static bool elog_file_rotate(void) static bool elog_file_reopen(void)
{ {
#define SUFFIX_LEN 10 FILE *tmp_fp;
/* 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;
FILE *fp_bak = fp;
oldpath = (char *) malloc(base + SUFFIX_LEN); tmp_fp = fopen(local_cfg.name, "a+");
newpath = (char *) malloc(base + SUFFIX_LEN); if (tmp_fp) {
if (fp)
fclose(fp);
fp = tmp_fp;
fd = fileno(fp);
return true;
}
if (oldpath == NULL || newpath == NULL) {
return false; return false;
} }
/*
* rotate the log file xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0
*/
static void elog_file_rotate(void)
{
#define SUFFIX_LEN 10
/* mv xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */
int n;
char oldpath[256], newpath[256];
size_t base = strlen(local_cfg.name);
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,22 +103,23 @@ 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) {
if (fp_bak) {
fclose(fp_bak);
} }
fd = fileno(fp);
/*
* Check if it needed retate
*/
static bool elog_file_retate_check(void)
{
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