用
一、定义宏变量BUF_SIZE
#define BUF_SIZE 1024
二、定义log_st结构体
typedef struct _log_st log_st;
struct _log_st
{
char path[128];
int fd;
int size;
int level;
int num;
};
三、定义log_init函数
参数说明:path——您要存储的文件路径;size——单个文件的最大大小,如果超过该大小则新建新的文件用来存储;level——日志输出方式,建议在上层限制其值的范围为0到3,0表示日志既不输出到屏幕也不创建文件和保存到文件,1表示日志保存到文件但不输出到屏幕,2表示日志既输出到屏幕也保存到文件,3表示日志只输出到文件而不创建文件和存入文件;num——日志文件命名方式,非0表示以(int)time(NULL)作为文件名来保存文件,文件数量随着日志量的递增而递增;0表示以“.new”和“.bak”为文件名来保存文件,文件数量不超过两个,随着日志量的递增,旧的日志文件将被新的覆盖,更直观的说就是说.new”和“.bak”文件只保存最近的日志。
log_st *log_init(char *path, int size, int level, int num)
{
char new_path[128] = {0};
if (NULL == path || 0 == level) return NULL;
log_st *log = (log_st *)malloc(sizeof(log_st));
memset(log, 0, sizeof(log_st));
if (level != 3)
{
//the num use to control file naming
log->num = num;
if(num)
snprintf(new_path, 128, "%s%d", path, (int)time(NULL));
else
snprintf(new_path, 128, "%s.new", path);
if(-1 == (log->fd = open(new_path, O_RDWR|O_APPEND|O_CREAT|O_SYNC, S_IRUSR|S_IWUSR|S_IROTH)))
{
free(log);
log = NULL;
return NULL;
}
}
strncpy(log->path, path, 128);
log->size = (size > 0 ? size:0);
log->level = (level > 0 ? level:0);
return log;
}
四、定义log_debug函数
void log_debug(log_st *log, const char *msg, ...)
{
va_list ap;
time_t now;
char *pos;
char _n = '\n';
char message[BUF_SIZE] = {0};
int nMessageLen = 0;
int sz;
if(NULL == log || 0 == log->level) return;
now = time(NULL);
pos = ctime(&now);
sz = strlen(pos);
pos[sz-1]=']';
snprintf(message, BUF_SIZE, "[%s ", pos);
for (pos = message; *pos; pos++);
sz = pos - message;
va_start(ap, msg);
nMessageLen = vsnprintf(pos, BUF_SIZE - sz, msg, ap);
va_end(ap);
if (nMessageLen <= 0) return;
if (3 == log->level)
{
printf("%s\n", message);
return;
}
if (2 == log->level)
printf("%s\n", message);
write(log->fd, message, strlen(message));
write(log->fd, &_n, 1);
fsync(log->fd);
}
五、定义log_checksize函数
void log_checksize(log_st *log)
{
struct stat stat_buf;
char new_path[128] = {0};
char bak_path[128] = {0};
if(NULL == log || 3 == log->level || '\0' == log->path[0]) return;
memset(&stat_buf, 0, sizeof(struct stat));
fstat(log->fd, &stat_buf);
if(stat_buf.st_size > log->size)
{
close(log->fd);
if(log->num)
snprintf(new_path, 128, "%s%d", log->path, (int)time(NULL));
else
{
snprintf(bak_path, 128, "%s.bak", log->path);
snprintf(new_path, 128, "%s.new", log->path);
remove(bak_path); //delete the file *.bak first
rename(new_path, bak_path); //change the name of the file *.new to *.bak
}
//create a new file
log->fd = open(new_path, O_RDWR|O_APPEND|O_CREAT|O_SYNC, S_IRUSR|S_IWUSR|S_IROTH);
}
}
除非注明,文章均为CppLive 编程在线原创,转载请注明出处,谢谢。