优化日志
This commit is contained in:
@ -95,16 +95,15 @@ int cleanup(log_manager *self)
|
||||
loc = self->log;
|
||||
self->log = NULL;
|
||||
self->count = 0;//摘取log链
|
||||
|
||||
sem_post(&self->log_sem);
|
||||
//释放信号量
|
||||
|
||||
char *logbuf;
|
||||
void *logbufbk;
|
||||
char failback[MAX_LOG_LENGTH];
|
||||
logbuf = (char*)malloc(1);
|
||||
logbuf[0] = '\0';
|
||||
size_t buf_length,buf_lengthbk = 0;
|
||||
int fd;
|
||||
while(loc->next !=NULL)
|
||||
{
|
||||
tobeclean = loc;
|
||||
@ -114,6 +113,12 @@ int cleanup(log_manager *self)
|
||||
logbuf = (char*)realloc(logbuf,strlen(logbuf)+strlen(tobeclean->log)+1);//为日志分配新的内存
|
||||
}
|
||||
if(logbuf == NULL){
|
||||
fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0644);
|
||||
if(fd != -1)
|
||||
{
|
||||
write(fd,logbufbk,buf_length);
|
||||
close(fd);
|
||||
}
|
||||
free(logbufbk);
|
||||
logbuf = failback;//降级策略,堆空间不足时,使用预分配栈空间,保留最后一条日志
|
||||
}
|
||||
@ -128,14 +133,6 @@ int cleanup(log_manager *self)
|
||||
}
|
||||
free(tobeclean);
|
||||
}
|
||||
int fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0644);
|
||||
if(fd == -1){
|
||||
perror("file:");
|
||||
free(loc);
|
||||
if(logbuf != failback)
|
||||
free(logbuf);
|
||||
return -1;
|
||||
}
|
||||
if(logbuf != failback){
|
||||
logbufbk = logbuf;
|
||||
logbuf = (char*)realloc(logbuf,strlen(logbuf)+strlen(loc->log)+2);
|
||||
@ -163,6 +160,14 @@ int cleanup(log_manager *self)
|
||||
logbuf[buf_length] = '\0';
|
||||
}
|
||||
free(loc);
|
||||
|
||||
fd = open("log.txt",O_CREAT | O_WRONLY | O_APPEND, 0644);
|
||||
if(fd == -1){
|
||||
perror("file:");
|
||||
if(logbuf != failback)
|
||||
free(logbuf);
|
||||
return -1;
|
||||
}
|
||||
int error_buf = write(fd,logbuf,strlen(logbuf));
|
||||
if(error_buf==-1){
|
||||
close(fd);
|
||||
@ -174,7 +179,7 @@ int cleanup(log_manager *self)
|
||||
write(fd,"unknown error case log write cut down\n",38);
|
||||
if(logbuf != failback)
|
||||
free(logbuf);
|
||||
close(fd);
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user