linux使用轻量级日志库EasyLogger-生成动态链接库

首先下载EasyLogger源码到本地

git clone https://github.com/armink/EasyLogger.git

将demo/linux/easylogger 下移植好的文件替换工程根目录easylogger下的相同文件,对应docs/zh/文件夹下有详细的配置说明,在此不赘述,配置完成后编写makefile:

CROSS_COMPILE = arm-linux-gnueabihf-
AS      = $(CROSS_COMPILE)as  
LD      = $(CROSS_COMPILE)ld  
CC      = $(CROSS_COMPILE)gcc  
CPP     = $(CROSS_COMPILE)g++  
AR      = $(CROSS_COMPILE)ar  
NM      = $(CROSS_COMPILE)nm
 

SOURCE_ROOT = $(shell pwd)
 
CFLAGS		:= -Wall -O2 -fno-builtin
CPPFLAGS	:= 
 
INCLUDE_DIR := -I $(SOURCE_ROOT)/ \
	-I$(SOURCE_ROOT)/inc\
	-I$(SOURCE_ROOT)/plugins/file
 
APP_NAME=libeasylogger.so
 

all: $(APP_NAME)
APP_OBJECTC += src/elog.c
APP_OBJECTC += src/elog_async.c
APP_OBJECTC += src/elog_buf.c
APP_OBJECTC += src/elog_utils.c
APP_OBJECTC += port/elog_port.c
APP_OBJECTC += plugins/file/elog_file.c
APP_OBJECTC += plugins/file/elog_file_port.c


STATIC_OBJ_O  = $(patsubst %.c, %.o, $(APP_OBJECTC))
 
STATIC_OBJ_C  = $(foreach file, $(STATIC_OBJ_O), $(file) )
 
$(STATIC_OBJ_C) : %.o:%.c 
	$(CC)  $(INCLUDE_DIR) $(CPPFLAGS) -c  -fPIC $(APP_OBJECTC)

$(APP_NAME): $(STATIC_OBJ_C)	
	$(CC) -shared -o $(APP_NAME) ./*.o
	
clean:
	@rm -f *.o *.so
.PHONY: clean	

将makefile放到工程根目录,执行make,即可在当前目录生成.so文件,将.so文件放到嵌入式系统/usr/lib或者/usr/local/lib目录下,即可在程序中使用easylogger了

初始化:

#include "elog.h"
#include "elog_cfg.h"
#include "elog_file.h"
#include "elog_file_cfg.h"

void easyloggerInit(void)
{
    setbuf(stdout, NULL);
    /* initialize EasyLogger */
    elog_init();
    /* set EasyLogger log format */
    elog_set_fmt(ELOG_LVL_ASSERT, ELOG_FMT_ALL);
    elog_set_fmt(ELOG_LVL_ERROR, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
    elog_set_fmt(ELOG_LVL_WARN, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
    elog_set_fmt(ELOG_LVL_INFO, ELOG_FMT_LVL | ELOG_FMT_TAG | ELOG_FMT_TIME);
    elog_set_fmt(ELOG_LVL_DEBUG, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
    elog_set_fmt(ELOG_LVL_VERBOSE, ELOG_FMT_ALL & ~ELOG_FMT_FUNC);
#ifdef ELOG_COLOR_ENABLE
    elog_set_text_color_enabled(true);
#endif
/* start EasyLogger */
    elog_start();
}

log_d("easyLogger debug");

log_i("easyLogger info");

log_w("easyLogger worn");

log_e("easyLogger error");

你可能感兴趣的:(c/c++,linux)