fix get thread id in mac OS

connection_manager
yuqing 2018-05-10 11:24:02 +08:00
parent 6b69e90016
commit fe2d4ff780
1 changed files with 24 additions and 9 deletions

View File

@ -1,6 +1,11 @@
#ifdef OS_LINUX
#include <sys/syscall.h> #include <sys/syscall.h>
#endif
#include <stdarg.h> #include <stdarg.h>
#include <stdio.h> #include <stdio.h>
#include <pthread.h>
#include "sf_util.h" #include "sf_util.h"
int64_t getticks() int64_t getticks()
@ -15,32 +20,43 @@ void log_plus(const int priority, const char* file,
{ {
char buf[2048]; char buf[2048];
int hlen; int hlen;
va_list ap;
#ifdef DEBUG_FLAG
long tid;
#endif
if (g_log_context.log_level < priority) { if (g_log_context.log_level < priority) {
return; return;
} }
#ifdef DEBUG_FLAG #ifdef DEBUG_FLAG
hlen = snprintf(buf, sizeof(buf), "%s:%d %ld ", file, line, (long)syscall(SYS_gettid));
#ifdef OS_LINUX
tid = (long)syscall(SYS_gettid);
#else
tid = (long)pthread_self();
#endif
hlen = snprintf(buf, sizeof(buf), "%s:%d %ld ", file, line, tid);
#else #else
hlen = snprintf(buf, sizeof(buf), "%s:%d ", file, line); hlen = snprintf(buf, sizeof(buf), "%s:%d ", file, line);
#endif #endif
va_list ap;
va_start(ap, fmt); va_start(ap, fmt);
hlen += vsnprintf(buf+hlen, sizeof(buf)-hlen, fmt, ap); hlen += vsnprintf(buf+hlen, sizeof(buf)-hlen, fmt, ap);
va_end(ap); va_end(ap);
log_it_ex1(&g_log_context, priority, buf, hlen); log_it_ex1(&g_log_context, priority, buf, hlen);
} }
int sf_printbuffer(char* buffer,int32_t len) int sf_printbuffer(char* buffer,int32_t len)
{ {
int i; int i;
if(buffer==NULL) if(buffer == NULL) {
{
fprintf(stderr, "common-utils parameter is fail"); fprintf(stderr, "common-utils parameter is fail");
return(-1); return(-1);
} }
for(i=0;i<len;i++)
{ for(i=0; i<len; i++) {
if(i%16 == 0) if(i % 16 == 0) {
{
fprintf(stderr,"\n"); fprintf(stderr,"\n");
} }
fprintf(stderr,"[%02x]", (unsigned char)buffer[i]); fprintf(stderr,"[%02x]", (unsigned char)buffer[i]);
@ -49,4 +65,3 @@ int sf_printbuffer(char* buffer,int32_t len)
return(0); return(0);
} }