准备好android toolchain

配置linux环境变量

export TOP=/home/user/android_toolchain/toolchain
SYSROOT=$TOP/sysroot
export PATH=$TOP/bin:$PATH
target_host=aarch64-linux-android
export AR=$target_host-ar
export AS=$target_host-as
export CC=$target_host-clang
export CXX=$target_host-clang++
export LDFLAGS="-pie"
export PKG_CONFIG_SYSROOT_DIR=${TOP}/prebuild/target
export PKG_CONFIG_PATH=${PKG_CONFIG_SYSROOT_DIR}/lib/pkgconfig
export LDFLAGS="${LDFLAGS} -Wl,-rpath-link=${TOP}/prebuild/target/lib"

工程项目中链接log库

aux_source_directory(. all_source_files)
set(CMAKE_CXX_FLAGS_1 "-std=c++11 -pedantic")
set(CMAKE_CXX_FLAGS_2 "-Wno-variadic-macros -Wall -Wunused -Wunreachable-code")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_1} ${CMAKE_CXX_FLAGS_2}")
include_directories(
    ../include/
)
add_library(my_function ${all_source_files})
target_link_libraries(my_function log)

在项目中自定义trace.h头文件供使用,如下所示

#ifndef _TRACE_H
#define _TRACE_H

#include <iostream>
#include <sstream>
#include <android/log.h>

const std::string LOG_TAG = "PersonalLog";
namespace MyProject {

#define _LOGGER_INFO(LOG_TAG, printContent, ...) \
__android_log_print(ANDROID_LOG_INFO, LOG_TAG.c_str(), "[%s:%d]" printContent, __FUNCTION__, __LINE__, ##__VA_ARGS__) 
#define LOGI(...) _LOGGER_INFO(LOG_TAG, __VA_ARGS__)

#define _LOGGER_DEBUG(LOG_TAG, printContent, ...) \
__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG.c_str(), "[%s:%d]" printContent, __FUNCTION__, __LINE__, ##__VA_ARGS__) 
#define LOGD(...) _LOGGER_DEBUG(LOG_TAG, __VA_ARGS__)

#define _LOGGER_WARN(LOG_TAG, printContent, ...) \
__android_log_print(ANDROID_LOG_WARN, LOG_TAG.c_str(), "[%s:%d]" printContent, __FUNCTION__, __LINE__, ##__VA_ARGS__) 
#define LOGW(...) _LOGGER_WARN(LOG_TAG, __VA_ARGS__)

#define _LOGGER_ERROR(LOG_TAG, printContent, ...) \
__android_log_print(ANDROID_LOG_ERROR, LOG_TAG.c_str(), "[%s:%d]" printContent, __FUNCTION__, __LINE__, ##__VA_ARGS__) 
#define LOGE(...) _LOGGER_ERROR(LOG_TAG, __VA_ARGS__)
}

#endif

Logo

更多推荐