program story

Android NDK 코드에 로그인하는 간단한 방법이 있습니까?

inputbox 2020. 11. 17. 08:05
반응형

Android NDK 코드에 로그인하는 간단한 방법이 있습니까?


Eclipse를 사용하여 Android NDK 애플리케이션에서 C 코드를 쉽게 디버깅하는 방법을 찾고 있습니다. gdb 또는 이와 유사한 것을 사용하여 앱을 디버깅하는 방법을 읽었지만 원하는 것은 어떻게 든 Eclipse에 메시지를 푸시하는 방법입니다.

C에서 인쇄 기능을 사용하고 DDMS 로그 또는 이와 유사한 것으로 보는 것만 큼 간단한 솔루션을 찾고 있습니다. 누구든지 이것을 한 경험이 있습니까?


Android 로깅 기능을 사용할 수 있습니다.

#include <android/log.h>

#define APPNAME "MyApp"

__android_log_print(ANDROID_LOG_VERBOSE, APPNAME, "The value of 1 + 1 is %d", 1+1);

Android.mk 파일에서 로깅 라이브러리에 대해서도 링크해야합니다.

  LOCAL_LDLIBS := -llog

가장 쉬운 방법은 아마도 printf () 문을 시스템 로그로 리디렉션하는 것입니다 ( 공식 ADB 참조 매뉴얼 의 "Viewing stdout and stderr"섹션을 기반으로합니다) .

명령 줄에 다음 3 개의 명령을 입력합니다.

adb shell stop
adb shell setprop log.redirect-stdio true
adb shell start

그런 다음 Eclipse 디버거의 "LogCat"창을 보거나 명령 줄에 다음을 입력하여 "printf ()"문의 출력을 볼 수 있습니다.

adb logcat

에뮬레이터 또는 장치에서 전송하기 전에 데이터가 버퍼링되므로 반드시 stdout 버퍼를 플러시해야합니다. 예 :

printf("Hello, I am %d years old!\n", 30);
fflush(stdout);

그러면 "I / stdout :"으로 시작하는 로그 메시지가 표시됩니다.


지금까지 아무도 다른 로그 수준에 대한 정보를 게시하지 않았습니다. 대답은 로깅 "그림"을 가득 채우려는 시도 입니다.

#include <android/log.h>

#define TAG "MY_TAG"

#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,    TAG, __VA_ARGS__)
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN,     TAG, __VA_ARGS__)
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO,     TAG, __VA_ARGS__)
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG,    TAG, __VA_ARGS__)

용법:

char err[] = "wrong";
LOGE("Something went %s", err);

링크 안드로이드 아래와 같이 로그 라이브러리입니다.

Android.mk :

LOCAL_LDLIBS := -llog

CMakeLists.txt :

find_library( log-lib log )
target_link_libraries( ${log-lib} )

추가 자료 : 로깅


디버거를 사용하는 대체 솔루션이 여기에 설명되어 있습니다.

Eclipse에서 JNI로 래핑 된 C 코드를 효과적으로 디버깅하려면 어떻게해야합니까? (Android Dev)


ADT 20에는 Eclipse에서 NDK 프로젝트 빌드 및 디버깅을 지원하는 NDK 플러그인이 포함되어 있습니다. 이 문서 는 NDK 플러그인을 설치하고 사용하는 방법을 설명합니다. 지침은 매우 간단하며 몇 단계로만 구성됩니다.

이것은 내가 찾은 가장 간단한 솔루션이며 나를 위해 일했습니다.

참고 : ADT 번들을 사용하는 경우 새 소프트웨어를 설치하여 C 개발 도구를 설치하기 만하면 (스크린 샷 참조) 즉시 "NDK 플러그인 사용"부분으로 이동할 수 있습니다.

c 개발 도구 설치

Edit: It seems there is an issue with CDT in eclipse juno http://code.google.com/p/android/issues/detail?id=33788 causing eclipse's debugger to be unable to find breakpoints. Workaround I used is to start app in debug mode (not debug as native app but 'regular' debug) and then in command line I went to my project root and typed ndk-gdb (this creates gdb.setup file in obj/local/armeabi folder). After that breakpoints worked as usual.

In comments related to the issue on the link above they suggest some other workarounds but I didn't try them since they seemed to require more effort than this.


You can also a little util

#include <android/log.h>

#define LOGI(...) ((void)__android_log_print(ANDROID_LOG_INFO, "native-lib", __VA_ARGS__))

Usage:

std::string hello = "Hello from C++";
int a = 1;
LOGI("int %d, string: %s", a, hello.c_str());

참고URL : https://stackoverflow.com/questions/4629308/any-simple-way-to-log-in-android-ndk-code

반응형