Do not start log thread before logging is actually required to prevent daemon problem

This commit is contained in:
z3APA3A 2020-10-30 23:20:44 +03:00
parent 9e51fe7308
commit b3a843aacb

View File

@ -26,7 +26,7 @@ typedef enum {
FREEPARAM FREEPARAM
} EVENTTYPE; } EVENTTYPE;
static int loginit = 0;
struct clientparam logparam; struct clientparam logparam;
struct srvparam logsrv; struct srvparam logsrv;
@ -51,6 +51,8 @@ static void delayregisterlog (struct LOGGER * log);
static void delaydolog(struct logevent *evt); static void delaydolog(struct logevent *evt);
static void delayfreeparam(struct clientparam *param); static void delayfreeparam(struct clientparam *param);
static void initlog2(void);
void logpush(struct logevent *evt); void logpush(struct logevent *evt);
#ifdef WITHMAIN #ifdef WITHMAIN
@ -163,6 +165,10 @@ struct LOGGER * registerlog(const char * logstring, int logtype){
if(!logstring || !strcmp(logstring, "NUL") || !strcmp(logstring, "/dev/null")) return NULL; if(!logstring || !strcmp(logstring, "NUL") || !strcmp(logstring, "/dev/null")) return NULL;
pthread_mutex_lock(&log_mutex); pthread_mutex_lock(&log_mutex);
if(!loginit){
initlog2();
loginit = 1;
}
for(log = loggers; log; log=log->next){ for(log = loggers; log; log=log->next){
if(!strcmp(logstring, log->selector)){ if(!strcmp(logstring, log->selector)){
if(logtype >= 0) log->rotate = logtype; if(logtype >= 0) log->rotate = logtype;
@ -297,14 +303,10 @@ void logpush(struct logevent *evt){
#endif #endif
} }
void initlog(void){ static void initlog2(void){
pthread_t thread; pthread_t thread;
srvinit(&logsrv, &logparam);
pthread_mutex_init(&log_mutex, NULL);
errld.fp = stdout;
#ifdef _WIN32 #ifdef _WIN32
{
HANDLE h; HANDLE h;
if(!(log_sem = CreateSemaphore(NULL, 0, MAX_SEM_COUNT, NULL))) exit(11); if(!(log_sem = CreateSemaphore(NULL, 0, MAX_SEM_COUNT, NULL))) exit(11);
#ifndef _WINCE #ifndef _WINCE
@ -318,9 +320,7 @@ void initlog(void){
else { else {
exit(10); exit(10);
} }
}
#else #else
{
pthread_attr_t pa; pthread_attr_t pa;
pthread_attr_init(&pa); pthread_attr_init(&pa);
@ -329,10 +329,15 @@ void initlog(void){
if(sem_init(&log_sem, 0, 0)) exit(11); if(sem_init(&log_sem, 0, 0)) exit(11);
if(pthread_create(&thread, &pa, logthreadfunc, NULL)) exit(10); if(pthread_create(&thread, &pa, logthreadfunc, NULL)) exit(10);
}
#endif #endif
} }
void initlog(void){
srvinit(&logsrv, &logparam);
pthread_mutex_init(&log_mutex, NULL);
errld.fp = stdout;
}
static void delaydolog(struct logevent *evt){ static void delaydolog(struct logevent *evt){
if(!evt->log->logfunc || !evt->log->logfunc->log) return; if(!evt->log->logfunc || !evt->log->logfunc->log) return;