- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
打印到 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/
打印到 stderr 的原始代码: extern "C" { /* error: output error message */ void Error(const int error, char *m
我使用这个库:https://github.com/mysolution/hyphenator在 JNI 中,我创建了这个函数: int main2() { //load russian hyphe
我正在尝试将此日志记录语句移植到工作中,因此它将在 linux 和 android 上运行我的#define'ing: __android_log_print(ANDROID_LOG_ERROR, L
我的 make 文件有什么问题? Android.mk LOCAL_PATH := $(call my-dir) include $(CLEAR_VARS) LOCAL_MODULE := fo
我有一个类似的功能 __android_log_print(ANDROID_LOG_INFO, "HelloNDK!"); 在我的 C 代码上 我不会在我的 LogCat 上找到那个输出。我需要设置什
有一些像这样的问题,但提供的解决方案都没有解决问题。构建包含两个模块的应用程序时,Build 会输出标题中的错误。其中一个模块具有 native 代码文件。 我检查了很多线程,所有线程都建议包含 -l
此问题存在,但无法解决我的问题。我得到了对 __android_log_print 的 undefined reference ,但我有包含 header #include 我的 Android.m
这是.cpp文件,代码是 JNIEXPORT jint JNICALL Java_com_example_compute_MainActivity_AddNumbers(JNIEnv *env, jo
当在 native 代码中使用 __android_log_print() 时,它会打印并在文本末尾添加一个 /n。 我正在尝试获得与 printf 相同的行为,当未使用 /n 时,它会在同一行上打印
我有兴趣通过 NDK 应用演示 printf 漏洞。明确地说,我知道要登录控制台,我们可以使用 __android_log_print(ANDROID_LOG_DEBUG, "LOG_TAG", "P
我有以下代码,我想用它来将 trainingLables Mat 的内容打印到 android 控制台: #include JNIEXPORT jintArray JNICALL Java_com
我有一个 Android NDK 项目,其 NDK 部分从命令行编译正常;我以前在 Eclipse 下将编译好的东西作为纯 Java 项目运行没有问题。但是我将它转换为 C/C++ 项目,现在甚至无法
我是一名优秀的程序员,十分优秀!