mirror of
https://github.com/armink/EasyLogger.git
synced 2025-02-08 00:54:09 +08:00
Support log-rotate.
rotate each xxx.log.n-1 to xxx.log.n(n < local_cfg.max_rotate), and xxx.log to xxx.log.0 when xxx.log achieves local_cfg.max_size and local_cfg.max_rotate > 0.
This commit is contained in:
parent
afabf352f3
commit
1507a7b611
@ -35,4 +35,7 @@
|
|||||||
/* EasyLogger file log plugin's using file max size */
|
/* EasyLogger file log plugin's using file max size */
|
||||||
#define ELOG_FILE_MAX_SIZE (10 * 1024 * 1024)
|
#define ELOG_FILE_MAX_SIZE (10 * 1024 * 1024)
|
||||||
|
|
||||||
|
/* EasyLogger file log plugin's using max rotate file count */
|
||||||
|
#define ELOG_FILE_MAX_ROTATE 10
|
||||||
|
|
||||||
#endif /* _ELOG_FILE_CFG_H_ */
|
#endif /* _ELOG_FILE_CFG_H_ */
|
||||||
|
@ -31,6 +31,8 @@
|
|||||||
#include <sys/stat.h>
|
#include <sys/stat.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <string.h>
|
||||||
|
|
||||||
#include <file/elog_file.h>
|
#include <file/elog_file.h>
|
||||||
#include <file/elog_file_cfg.h>
|
#include <file/elog_file_cfg.h>
|
||||||
@ -53,6 +55,7 @@ ElogErrCode elog_file_init(void)
|
|||||||
|
|
||||||
cfg.name = ELOG_FILE_NAME;
|
cfg.name = ELOG_FILE_NAME;
|
||||||
cfg.max_size = ELOG_FILE_MAX_SIZE;
|
cfg.max_size = ELOG_FILE_MAX_SIZE;
|
||||||
|
cfg.max_rotate = ELOG_FILE_MAX_ROTATE;
|
||||||
|
|
||||||
elog_file_config(&cfg);
|
elog_file_config(&cfg);
|
||||||
|
|
||||||
@ -70,8 +73,13 @@ void elog_file_write(const char *log, size_t size)
|
|||||||
statbuf.st_size = 0;
|
statbuf.st_size = 0;
|
||||||
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 (local_cfg.max_rotate > 0) {
|
||||||
|
elog_file_rotate();
|
||||||
|
} else {
|
||||||
return;
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
elog_file_port_lock();
|
elog_file_port_lock();
|
||||||
|
|
||||||
@ -103,6 +111,42 @@ void elog_file_config(ElogFileCfg *cfg)
|
|||||||
|
|
||||||
local_cfg.name = cfg->name;
|
local_cfg.name = cfg->name;
|
||||||
local_cfg.max_size = cfg->max_size;
|
local_cfg.max_size = cfg->max_size;
|
||||||
|
local_cfg.max_rotate = cfg->max_rotate;
|
||||||
|
|
||||||
|
fp = fopen(local_cfg.name, "a+");
|
||||||
|
if (fp)
|
||||||
|
fd = fileno(fp);
|
||||||
|
else
|
||||||
|
fd = -1;
|
||||||
|
|
||||||
|
elog_file_port_unlock();
|
||||||
|
}
|
||||||
|
|
||||||
|
void elog_file_rotate(void)
|
||||||
|
{
|
||||||
|
if (local_cfg.max_rotate <= 0) return;
|
||||||
|
|
||||||
|
elog_file_port_lock();
|
||||||
|
|
||||||
|
if (fp) {
|
||||||
|
fclose(fp);
|
||||||
|
fp = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* mv xxx.log.n-1 => xxx.log.n, and xxx.log => xxx.log.0 */
|
||||||
|
size_t base = strlen(local_cfg.name);
|
||||||
|
char* oldpath = (char*)malloc(base + 10); // use c99 variable length array or alloca(3) instead?
|
||||||
|
char* newpath = (char*)malloc(base + 10);
|
||||||
|
memcpy(oldpath, local_cfg.name, base);
|
||||||
|
memcpy(newpath, local_cfg.name, base);
|
||||||
|
int n;
|
||||||
|
for (n = local_cfg.max_rotate - 1; n >= 0; --n) {
|
||||||
|
sprintf(oldpath + base, n ? ".%d" : "", n-1);
|
||||||
|
sprintf(newpath + base, ".%d", n);
|
||||||
|
rename(oldpath, newpath);
|
||||||
|
}
|
||||||
|
free(oldpath);
|
||||||
|
free(newpath);
|
||||||
|
|
||||||
fp = fopen(local_cfg.name, "a+");
|
fp = fopen(local_cfg.name, "a+");
|
||||||
if (fp)
|
if (fp)
|
||||||
|
@ -49,12 +49,14 @@ extern "C" {
|
|||||||
typedef struct {
|
typedef struct {
|
||||||
char *name; /* file name */
|
char *name; /* file name */
|
||||||
size_t max_size; /* file max size */
|
size_t max_size; /* file max size */
|
||||||
|
int max_rotate; /* max rotate file count */
|
||||||
} ElogFileCfg;
|
} ElogFileCfg;
|
||||||
|
|
||||||
/* elog_file.c */
|
/* elog_file.c */
|
||||||
ElogErrCode elog_file_init(void);
|
ElogErrCode elog_file_init(void);
|
||||||
void elog_file_write(const char *log, size_t size);
|
void elog_file_write(const char *log, size_t size);
|
||||||
void elog_file_config(ElogFileCfg *cfg);
|
void elog_file_config(ElogFileCfg *cfg);
|
||||||
|
void elog_file_rotate(void);
|
||||||
void elog_file_deinit(void);
|
void elog_file_deinit(void);
|
||||||
|
|
||||||
/* elog_file_port.c */
|
/* elog_file_port.c */
|
||||||
|
@ -35,4 +35,7 @@
|
|||||||
/* EasyLogger file log plugin's using file max size */
|
/* EasyLogger file log plugin's using file max size */
|
||||||
#define ELOG_FILE_MAX_SIZE /* @note you must define it for a value */
|
#define ELOG_FILE_MAX_SIZE /* @note you must define it for a value */
|
||||||
|
|
||||||
|
/* EasyLogger file log plugin's using max rotate file count */
|
||||||
|
#define ELOG_FILE_MAX_ROTATE /* @note you must define it for a value */
|
||||||
|
|
||||||
#endif /* _ELOG_FILE_CFG_H_ */
|
#endif /* _ELOG_FILE_CFG_H_ */
|
||||||
|
Loading…
x
Reference in New Issue
Block a user