log管理器内存分配池化,修复部分log写入部分与内存池部分存在的恶性bug
This commit is contained in:
@ -9,7 +9,12 @@
|
||||
#include <fcntl.h>
|
||||
#include <string.h>
|
||||
|
||||
int write_into_block(char *writein,char *org,int *length,int maxlength,char *logname)
|
||||
|
||||
logs* getbody(void **log_p)
|
||||
{
|
||||
return (logs*)*log_p;
|
||||
}
|
||||
int write_into_block(char *writein,char *org,size_t* length,size_t maxlength,char *logname)
|
||||
{
|
||||
if(writein == NULL||org == NULL||length == NULL||logname == NULL)
|
||||
return -1;
|
||||
@ -18,69 +23,63 @@ int write_into_block(char *writein,char *org,int *length,int maxlength,char *log
|
||||
strcpy(&writein[*length],org);
|
||||
*length +=strlen(org);//栈内存充足
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
int n = *length + strlen(org) - maxlength;
|
||||
int n = *length + strlen(org) - maxlength+1;
|
||||
strncpy(&writein[*length],org,strlen(org)-n);//栈内存不足
|
||||
writein[maxlength-1] = '\0';
|
||||
int fd = open(logname,O_CREAT | O_WRONLY | O_APPEND, 0644);
|
||||
if(fd != -1)
|
||||
{
|
||||
write(fd,writein,maxlength);
|
||||
int eno = write(fd,writein,strlen(writein));
|
||||
if(eno <strlen(writein))
|
||||
perror("log");
|
||||
close(fd);
|
||||
}
|
||||
else if(fd == -1)
|
||||
perror("log:");//仅警告
|
||||
strncpy(writein,&org[0],n);//剩余部分拷贝
|
||||
*length = n;
|
||||
strcpy(writein,&org[*length]);//剩余部分拷贝
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
int in_log(logs *log,log_manager *self)
|
||||
int in_log(log_manager *self,const char *logbody,const char *info)
|
||||
{
|
||||
if(log == NULL||self == NULL)
|
||||
if(self == NULL)
|
||||
return -1;
|
||||
void **log_p = self->mempool->GetBlock(self->mempool,LOGMOD);
|
||||
if(log_p == NULL)
|
||||
{
|
||||
perror("Mem_runout");
|
||||
return -1;
|
||||
}
|
||||
logs *log = getbody(log_p);
|
||||
snprintf(log->info,INFO_LENGTH,"%s",info);
|
||||
snprintf(log->log,MAX_LOG_LENGTH,"%s",logbody);
|
||||
log->log[MAX_LOG_LENGTH-1] = '\0';
|
||||
log->next = NULL;
|
||||
sem_wait(&self->log_sem);//加锁
|
||||
if(self->log == NULL){
|
||||
self->log = log;
|
||||
self->rear = log;
|
||||
self->count++;
|
||||
self->log = log_p;
|
||||
self->rear = getbody(log_p);
|
||||
atomic_fetch_add(&self->count,1);
|
||||
sem_post(&self->log_sem);
|
||||
return self->count;
|
||||
}
|
||||
if(self->count == 1)
|
||||
self->log->next = log;
|
||||
if(self->count == 1){
|
||||
logs *p = getbody(self->log);
|
||||
p->next = log_p;
|
||||
}
|
||||
self->count++;
|
||||
log->next = NULL;
|
||||
self->rear->next = log;
|
||||
self->rear->next = log_p;
|
||||
self->rear = log;
|
||||
sem_post(&self->log_sem);
|
||||
return self->count;
|
||||
}
|
||||
|
||||
logs *out_log(log_manager *self)
|
||||
{
|
||||
if(self == NULL)
|
||||
return NULL;
|
||||
sem_wait(&self->log_sem);
|
||||
logs *buf = self->log;
|
||||
if(buf==NULL)
|
||||
{
|
||||
sem_post(&self->log_sem);
|
||||
return NULL;
|
||||
}
|
||||
if(self->log->next ==NULL)
|
||||
self->log = self->rear = NULL;
|
||||
else
|
||||
self->log = self->log->next;
|
||||
self->count--;
|
||||
sem_post(&self->log_sem);
|
||||
buf->next =NULL;
|
||||
return buf;
|
||||
}
|
||||
|
||||
int sleep_with_signal(log_manager *self)
|
||||
{
|
||||
struct timespec ts;
|
||||
@ -90,7 +89,7 @@ int sleep_with_signal(log_manager *self)
|
||||
if (clock_gettime(CLOCK_MONOTONIC, &ts) != 0)
|
||||
return -1; /* 罕见失败 */
|
||||
|
||||
ts.tv_sec += 1000;
|
||||
ts.tv_sec += LOG_SLEEP_LENGTH;
|
||||
/* 纳秒部分无需处理,1000 s 整不会溢出 */
|
||||
|
||||
pthread_mutex_lock(&self->mtx); /* 进入临界区 */
|
||||
@ -119,92 +118,55 @@ int cleanup(log_manager *self)
|
||||
if(self->log ==NULL)
|
||||
return 1;
|
||||
logs *tobeclean,*loc;
|
||||
void **tobeclean_p;
|
||||
sem_wait(&self->log_sem);//获取信号量
|
||||
loc = self->log;
|
||||
void **loc_p = self->log;
|
||||
|
||||
self->log = NULL;
|
||||
self->count = 0;//摘取log链
|
||||
atomic_store(&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 = 0,buf_lengthbk = 0;
|
||||
loc = getbody(loc_p);
|
||||
|
||||
char logbuf[MAX_LOG_LENGTH];
|
||||
|
||||
size_t buf_length = 0;
|
||||
int fd;
|
||||
while(loc->next !=NULL)
|
||||
{
|
||||
tobeclean = loc;
|
||||
loc = loc->next;
|
||||
if(logbuf != failback){
|
||||
logbufbk = logbuf;
|
||||
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;//降级策略,堆空间不足时,使用预分配栈空间
|
||||
buf_length = 0;
|
||||
}
|
||||
if(logbuf != failback){
|
||||
buf_lengthbk = buf_length;
|
||||
buf_length = strlen(logbuf)+strlen(tobeclean->log);
|
||||
strcpy(logbuf+buf_lengthbk,tobeclean->log);
|
||||
}
|
||||
else
|
||||
{
|
||||
write_into_block(logbuf,tobeclean->log,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
}
|
||||
free(tobeclean);
|
||||
}
|
||||
if(logbuf != failback){
|
||||
logbufbk = logbuf;
|
||||
logbuf = (char*)realloc(logbuf,strlen(logbuf)+strlen(loc->log)+2);
|
||||
if(logbuf == NULL)
|
||||
{
|
||||
free(logbufbk);
|
||||
logbuf = failback;
|
||||
strcpy(logbuf,loc->log);
|
||||
buf_length = strlen(loc->log);
|
||||
|
||||
}
|
||||
else{
|
||||
buf_lengthbk = buf_length;
|
||||
buf_length = strlen(logbuf)+strlen(loc->log);
|
||||
strcpy(logbuf+buf_lengthbk,loc->log);
|
||||
}
|
||||
}
|
||||
else{
|
||||
write_into_block(logbuf,loc->log,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
}
|
||||
tobeclean_p = loc_p;
|
||||
tobeclean = getbody(tobeclean_p);
|
||||
loc_p = loc->next;
|
||||
loc = getbody(loc_p);
|
||||
|
||||
int eno = write_into_block(logbuf,tobeclean->info,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
if(eno == -1)
|
||||
perror("log");
|
||||
eno = write_into_block(logbuf,":",&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
eno = write_into_block(logbuf,tobeclean->log,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
if(eno == -1)
|
||||
perror("log");//非业务逻辑只警告
|
||||
|
||||
logbuf[buf_length] = '\0';
|
||||
self->mempool->FreeBlock(self->mempool,tobeclean_p);
|
||||
}
|
||||
write_into_block(logbuf,loc->info,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
write_into_block(logbuf,":",&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
write_into_block(logbuf,loc->log,&buf_length,MAX_LOG_LENGTH,"log.txt");
|
||||
|
||||
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;
|
||||
perror("log:");
|
||||
}
|
||||
int error_buf = write(fd,logbuf,strlen(logbuf));
|
||||
if(error_buf==-1){
|
||||
close(fd);
|
||||
if(logbuf != failback)
|
||||
free(logbuf);
|
||||
return -1;
|
||||
}
|
||||
else if(error_buf<buf_length)
|
||||
else if(error_buf<strlen(logbuf)){
|
||||
perror("file");
|
||||
write(fd,"unknown error case log write cut down\n",38);
|
||||
if(logbuf != failback)
|
||||
free(logbuf);
|
||||
}
|
||||
close(fd);
|
||||
return 0;
|
||||
}
|
||||
@ -219,13 +181,9 @@ void log_manager_stop(log_manager *self)
|
||||
self->stop = 1;
|
||||
/* 置退出标志 */
|
||||
printf("SYS:stopping loger\n");
|
||||
logs *log = malloc(sizeof(logs));
|
||||
strcpy(log->log,"SYS:stopping loger\n");
|
||||
self->in_log(log,self);
|
||||
log = malloc(sizeof(logs));
|
||||
strcpy(log->log,"SYS:done\n");
|
||||
self->in_log(self,"stopping loger\n","SYS:");
|
||||
printf("SYS:done\n");
|
||||
self->in_log(log,self);
|
||||
self->in_log(self,"done","SYS:");
|
||||
pthread_mutex_unlock(&self->mtx);
|
||||
pthread_cond_broadcast(&self->cond); /* 唤醒所有等待线程 */
|
||||
}
|
||||
@ -250,7 +208,7 @@ void *clear_log(void *self_p)
|
||||
}
|
||||
}
|
||||
|
||||
int init_loger(log_manager *self)
|
||||
int init_loger(log_manager *self,mem_ctl *mempool)
|
||||
{
|
||||
if(self == NULL)
|
||||
{
|
||||
@ -280,11 +238,11 @@ int init_loger(log_manager *self)
|
||||
return -1;
|
||||
}
|
||||
self->in_log = in_log;
|
||||
self->out_log = out_log;
|
||||
self->clear_log = clear_log;
|
||||
self->log = NULL;
|
||||
self->stop = 0;
|
||||
self->cleanup = cleanup;
|
||||
self->count = 0;
|
||||
atomic_init(&self->count,0);
|
||||
self->mempool = mempool;
|
||||
return 0;
|
||||
}
|
||||
@ -2,6 +2,7 @@
|
||||
#define LOG
|
||||
|
||||
#include "config.h"
|
||||
#include "memctl/memctl.h"
|
||||
#include <semaphore.h>
|
||||
#include <pthread.h>
|
||||
|
||||
@ -9,27 +10,27 @@
|
||||
typedef struct logs
|
||||
{
|
||||
char log[MAX_LOG_LENGTH];
|
||||
struct logs *next;
|
||||
void **next;
|
||||
char info[INFO_LENGTH];
|
||||
}logs;
|
||||
|
||||
typedef struct log_manager
|
||||
{
|
||||
pthread_t pid;
|
||||
int (*in_log)(logs *,struct log_manager*);
|
||||
logs* (*out_log)(struct log_manager*);
|
||||
mem_ctl *mempool;
|
||||
int (*in_log)(struct log_manager*,const char *,const char *);
|
||||
void *(*clear_log)(void*);
|
||||
int (*cleanup)(struct log_manager*);
|
||||
sem_t log_sem;
|
||||
logs *log;
|
||||
void **log;
|
||||
logs *rear;
|
||||
int count;
|
||||
atomic_int count;
|
||||
pthread_mutex_t mtx;
|
||||
pthread_cond_t cond;
|
||||
int stop;
|
||||
}log_manager;
|
||||
|
||||
void log_manager_stop(log_manager *self);
|
||||
int init_loger(log_manager *self);
|
||||
int init_loger(log_manager *self,mem_ctl *mempool);
|
||||
|
||||
#endif
|
||||
@ -11,6 +11,7 @@
|
||||
#include "tem/ctl.h"
|
||||
#include "tools/toml/toml.h"
|
||||
|
||||
|
||||
int quit_server(netm *self)
|
||||
{
|
||||
if(self ==NULL)
|
||||
@ -44,15 +45,18 @@ int quit_server(netm *self)
|
||||
return 0;
|
||||
}
|
||||
|
||||
int quit_mempool(mem_ctl *mem_ctler)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
void quit_all(int status,void *self_p)
|
||||
{
|
||||
alres *resouce =(alres*)self_p;
|
||||
//转换参数
|
||||
|
||||
resouce->network->shutdown_pool(resouce->network);
|
||||
logs *netlog = (logs*)malloc(sizeof(logs));
|
||||
netlog->next = NULL;
|
||||
memcpy(netlog->log,"shuting down networkserver",27);
|
||||
|
||||
|
||||
if(resouce->network->statue == SERVER_ON)
|
||||
{
|
||||
@ -62,7 +66,7 @@ void quit_all(int status,void *self_p)
|
||||
{
|
||||
resouce->network->shutdown_pool(resouce->network);
|
||||
}
|
||||
resouce->loger->in_log(netlog,resouce->loger);
|
||||
resouce->loger->in_log(resouce->loger,"shutting down network pool","SYS:");
|
||||
free(resouce->network);
|
||||
//释放网络资源
|
||||
if(resouce->tem->command !=NULL){
|
||||
@ -97,4 +101,5 @@ void quit_all(int status,void *self_p)
|
||||
|
||||
free(resouce->loger);
|
||||
//清理日志
|
||||
free(resouce);
|
||||
}
|
||||
|
||||
@ -4,11 +4,14 @@
|
||||
#include "network/network.h"
|
||||
#include "tem/ctl.h"
|
||||
#include "tools/log/log.h"
|
||||
#include "memctl/memctl.h"
|
||||
|
||||
typedef struct all_resources
|
||||
{
|
||||
Ctl *tem;
|
||||
netm *network;
|
||||
log_manager *loger;
|
||||
mem_ctl *memctler;
|
||||
|
||||
}alres;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user