2016-06-19 1 views
1

데비안 사용자 공간에서 응용 프로그램을 실행 중이며 로깅을 위해 rsyslog를 사용하고 있습니다. I는 다음의 명령을 메인 쓰레드에 로그를 열이 후rsyslog가 동일한 로그를 두 번 인쇄합니다.

openlog(NULL, LOG_CONS | LOG_NDELAY, LOG_LOCAL0); 

메인 스레드 (분리) 2 개 스레드 작성

pthread_create(&tx_tid, NULL,tx_main, NULL); 
if(0 != th_ret_val) 
{ 
    LOG(LEVEL_ERR,"failed to create tx thread, ret_val = %d",th_ret_val); 
} 
pthread_detach(tx_tid); 

pthread_create(&rx_tid, NULL,rx_main, NULL); 
if(0 != th_ret_val) 
{ 
    LOG(LEVEL_ERR,"failed to create rx thread, ret_val = %d",th_ret_val); 
} 
pthread_detach(rx_tid); 

과는 pthread_exit를 행한다.

로그에 몇 가지 로그가 두 번 인쇄됩니다 (로그에 증가하는 토큰 번호를 추가하고 동일한 토큰 번호가 추가되어 동일한 로그임을 알 수 있음) 또한 일부 LOG는 다음과 같습니다. 실종!

왜 이런 일이 발생하는지 설명 해주는 사람이 있습니까?

참고

#define LOG(prio, ...) my_log(__FILE__, __LINE__, __func__, prio, __VA_ARGS__) 

my_log 다음과 같이 구현된다 : LOG 매크로 인

#define LOG_MAX_LEN 200 
[LOG_MAX_LEN] full_fmt

정적 CHAR;

void my_log (const char * file, int line, const char * func, int prio, const char * fmt, ...) { va_list args;

snprintf(full_fmt, LOG_MAX_LEN, "LOG:tid-%d %-30s:%003d, %-20s - %s", pthread_self(), file, line, func, fmt); 
va_start(args, full_fmt); 
vsyslog(prio, full_fmt, args); 
//vprintf(full_fmt, args); 
va_end(args); 

은}

답변

0

내가 문제의 원인을 발견했다. 뮤텍스로 my_log를 보호하지 못했고 syslog로 보낸 메시지는 전체 문자열 full_fmt입니다. 뮤텍스를 추가하면이 문제가 해결됩니다.

관련 문제