Android NDKはAndroidシステムのログを出力(Logcat出力)する仕組みを備えています。この仕組みを使えば、Nativeコードからログ出力が行えるのでデバッグ作業に役立ちます。
Nativeコードからログを出力する方法を紹介します。
目次
スポンサーリンク
ライブラリ(log)をリンク
Android NDKのログライブラリ(log)が必要になります。
logをfind_libraryで検索し、サンプルのNativeコード(native-lib.cpp)を含むライブラリ(native-lib)へ、シナリオでリンクします。
# Sets the minimum version of CMake required to build the native library.
cmake_minimum_required(VERSION 3.10.2)
# Declares and names the project.
project("log")
find_library( log-lib log )
add_library( native-lib SHARED native-lib.cpp )
target_link_libraries( native-lib ${log-lib} )
【ライブラリの検索パスについて】
find_libraryはデフォルトでplatforms以下を検索してライブラリを探します。
ディレクトリを覗くとlogの存在が確認できます。
sdk/ndk <--- ndkのインストール先 ├── 21.1.6352462 <--- ndkのバージョン │ ├── build │ ├── platforms │ │ ├── android-16 <--- API毎のライブラリ : : : │ │ ├── android-24 │ │ │ ├── arch-arm <--- ABI毎のライブラリ │ │ │ ├── arch-arm64 │ │ │ ├── arch-x86 │ │ │ │ └── usr │ │ │ │ └── lib : : : : : │ │ │ │ ├── libGLESv1_CM.so │ │ │ │ ├── libGLESv2.so │ │ │ │ ├── libGLESv3.so │ │ │ │ ├── libjnigraphics.so │ │ │ │ ├── liblog.so <--- ここ : : : : : │ │ │ │ └── libz.so │ │ │ └── arch-x86_64 : : : │ │ └── android-29 : :
スポンサーリンク
Nativeコードでログを出力
ヘッダの指定
ログ関連の関数を使うためにヘッダの指定が必要です。
この中に関数のプロトタイプ宣言、定数、構造体が収められています。
#include <jni.h>
#include <string>
#include <android/log.h>
extern "C" JNIEXPORT jstring JNICALL
Java_com_example_nativecode_log_MainActivity_logSample(
JNIEnv* env,
jobject /* this */) {
:
}
__android_log_write(定まったメッセージ)
定まったメッセージを出力する関数です。
android.util.Logと同様にプライオリティの指定(第1引数)が出来ます。
__android_log_write(ANDROID_LOG_ERROR, "タグ", "定まったメッセージ");
__android_log_write(ANDROID_LOG_WARN, "タグ", "定まったメッセージ");
__android_log_write(ANDROID_LOG_INFO, "タグ", "定まったメッセージ");
__android_log_write(ANDROID_LOG_DEBUG, "タグ", "定まったメッセージ");
__android_log_write(ANDROID_LOG_VERBOSE, "タグ", "定まったメッセージ");
.../com.example.nativecode.log E/タグ: 定まったメッセージ .../com.example.nativecode.log W/タグ: 定まったメッセージ .../com.example.nativecode.log I/タグ: 定まったメッセージ .../com.example.nativecode.log D/タグ: 定まったメッセージ .../com.example.nativecode.log V/タグ: 定まったメッセージ
プライオリティはヘッダの中に定義されています。
:
/**
* Android log priority values, in increasing order of priority.
*/
typedef enum android_LogPriority {
/** For internal use only. */
ANDROID_LOG_UNKNOWN = 0,
/** The default priority, for internal use only. */
ANDROID_LOG_DEFAULT, /* only for SetMinPriority() */
/** Verbose logging. Should typically be disabled for a release apk. */
ANDROID_LOG_VERBOSE,
/** Debug logging. Should typically be disabled for a release apk. */
ANDROID_LOG_DEBUG,
/** Informational logging. Should typically be disabled for a release apk. */
ANDROID_LOG_INFO,
/** Warning logging. For use with recoverable failures. */
ANDROID_LOG_WARN,
/** Error logging. For use with unrecoverable failures. */
ANDROID_LOG_ERROR,
/** Fatal logging. For use when aborting. */
ANDROID_LOG_FATAL,
/** For internal use only. */
ANDROID_LOG_SILENT, /* only for SetMinPriority(); must be last */
} android_LogPriority;
:
__android_log_print(フォーマット付き)
フォーマット付きメッセージを出力する関数です。
フォーマットはC言語のprintfと同じ指定が出来ます。
int deci = 10;
__android_log_print(ANDROID_LOG_INFO, "タグ", "フォーマット付き 整数 = %d", deci);
__android_log_print(ANDROID_LOG_INFO, "タグ", "フォーマット付き 16進 = %x", deci);
__android_log_print(ANDROID_LOG_INFO, "タグ", "フォーマット付き 8進 = %o", deci);
float pi = 0.123f;
__android_log_print(ANDROID_LOG_INFO, "タグ", "フォーマット付き 小数 = %.2f", pi);
char moji[] = "abcd";
__android_log_print(ANDROID_LOG_INFO, "タグ", "フォーマット付き 文字 = %s", moji);
.../com.example.nativecode.log I/タグ: フォーマット付き 整数 = 10 .../com.example.nativecode.log I/タグ: フォーマット付き 16進 = a .../com.example.nativecode.log I/タグ: フォーマット付き 8進 = 12 .../com.example.nativecode.log I/タグ: フォーマット付き 小数 = 0.12 .../com.example.nativecode.log I/タグ: フォーマット付き 文字 = abcd
スポンサーリンク
スポンサーリンク
