C語言修煉日記 ——log.h

經過下面簡單得代碼能夠控制Log得輸出位置,是否輸出code

#ifndef LOG_H_INCLUDED
#define LOG_H_INCLUDED

#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>

int fd_log_file;
char g_msgFile[1024];
pthread_mutex_t log_info_mutex;

#define LOG_FILE_Path "/tmp/airtrek.log"

#define LOG_PRINT

#ifdef LOG_PRINT

#define  print( fmt, args... ) \
	printf("pid:%u %s  "fmt, (unsigned int)pthread_self(),  __func__ , ## args); \

#elif  LOG_WRITE_FILE

#define  print( fmt, args... )\
        pthread_mutex_lock(&log_info_mutex);\
		fd_log_file = open(LOG_FILE_Path,O_CREAT|O_WRONLY|O_APPEND);\
		sprintf(g_msgFile,"pid:%u %s   "fmt,(unsigned int)pthread_self(), __func__, ## args);\
		write(fd_log_file,g_msgFile,strlen(g_msgFile));\
        close(fd_log_file);\
        pthread_mutex_unlock(&log_info_mutex);\

#else

#define  print(fmt, args... )\

#endif

#endif // LOG_H_INCLUDED
相關文章
相關標籤/搜索