自定义函数android,Android C 自定义一个print函数

首先我们看下Android NDK中关于log的定义

针对宏定义

__android_log_print

int __android_log_print(

int prio,

const char *tag,

const char *fmt,

...

)

Writes a formatted string to the log, with priority prio and tag tag.

针对va_list

__android_log_vprint

int __android_log_vprint(

int prio,

const char *tag,

const char *fmt,

va_list ap

)

Equivalent to __android_log_print, but taking a va_list.

(If __android_log_print is like printf, this is like vprintf.)

实际的使用方案

针对宏定义

log.h

/* arm linux androideabi gcc */

#if defined(ANDROID) || defined(__ANDROID__)

#include

#define TAG "swack_lib"

#define LOGD(...) \

{ \

__android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__); \

}

#define LOGE(...) \

{ \

__android_log_print( \

ANDROID_LOG_ERROR, TAG, "LOGE (%s:%i) ", __func__, __LINE__); \

__android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__); \

/*exit(1);*/ \

}

#endif

针对va_list

log.h

#ifndef LOG_H_

#define LOG_H_

#include

#define LOGD(...) \

{ \

test_logd(__VA_ARGS__); \

}

#define LOGE(...) \

{ \

test_loge(__func__, __LINE__, __VA_ARGS__); \

}

void test_logd(const char* format, ...);

void test_loge(const char* func,

const uint32_t line,

const char* format,

...);

#endif // LOG_H_

log.c

#include "mixo_pal.h"

#include

#define TAG "swack_lib"

void test_logd(const char* format, ...)

{

va_list arg;

va_start(arg, format);

__android_log_vprint(ANDROID_LOG_DEBUG, TAG, format, arg);

va_end(arg);

}

void test_loge(const char* func,

const uint32_t line,

const char* format,

...)

{

va_list arg;

va_start(arg, format);

__android_log_print(ANDROID_LOG_ERROR, TAG, "LOGE (%s:%i) ", func, line);

__android_log_vprint(ANDROID_LOG_ERROR, TAG, format, arg);

va_end(arg);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值