打印到 stderr 的原始代码:
extern "C" {
/* error: output error message */
void Error(const int error, char *message, ...)
{
va_list arg;
fflush(stdout);
fflush(stderr);
if (error > 0)
fprintf(stderr, "\nError: ");
else
fprintf(stderr, "\nWarning: ");
va_start(arg, message);
vfprintf(stderr, message, arg);
va_end(arg);
fflush(stderr);
if (error > 0)
exit(error);
}
void main(){
Error(0,"Problem %s in file", "sometext");
}
}//extern "C"
我修改了我的代码看起来像那样。它应该打印到 logcat。
extern "C" {
#include <android/log.h>
#include <jni.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <math.h>
/* error: output error message */
void Error(const int error, char *message, ...)
{
va_list arg;
va_start(arg, message);
if (error > 0)
__android_log_print(ANDROID_LOG_ERROR, "HTS_API", message, arg);
else
__android_log_print(ANDROID_LOG_WARN, "HTS_API", message, arg);
va_end(arg);
if (error > 0)
exit(error);
}
void main(){
Error(0,"Problem %s in file", "sometext");
}
}//extern "C"
问题是我的代码输出:'Problem |�;A.|�;A.在文件中'
直接调用记录器函数,我会得到预期的输出。
__android_log_print(ANDROID_LOG_WARN, "HTS_API","文件中的问题 %s", "sometext");
预期的输出是:'Problem sometext in file'
我做错了什么?
最佳答案
__android_log_print
不接受 va_list
作为参数。它接受可变参数列表。
看起来像是在他们添加的最新 NDK 中
int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap);
这就是你想要的。以前,您必须通过将 Error
重新实现为具有可变参数列表的宏,或使用 vsprintf
在缓冲区中格式化错误消息,然后再解决此问题说
__android_log_print(ANDROID_LOG_WARN, "HTS_API", "%s", buf);
关于android - 如何将变量参数传递给 __android_log_print?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11188905/