LoginSignup
0
0

More than 1 year has passed since last update.

Androidアプリ(NDK)開発で時々logcat出力されない

Last updated at Posted at 2022-05-14

問題

AndroidのNDKを使った開発で、C++コードでベタにlogcat出力すると、全く出力されなかったり、少しだけ出力されたりする。

対策

一旦適当な長さでキャッシュしてから書き出すように変更して、usleepでちょっと待つようにしたら少し遅くなるけどlogcat出力されるようになった。リリースビルドが必要。いつもこれでうまくいくのかは不明。

C++(NDK)のlogcat実装例

#include <android/log.h>
#include <unistd.h>

#define dprintf(...) { \
    __android_log_print(ANDROID_LOG_DEBUG, "MyNDK:", __VA_ARGS__);\
    usleep(5000);\
}
....
void printTbl(std::vector<int> &tbl){
     float numPrintUnit = 20.0f; // キャッシュするサイズ(usleepとのバランス)
     int numTbl = tbl.size();
     int splits = ceil(numTbl/numPrintUnit);
     for(int j=0; j<splits; j++){
         std::ostringstream log;
         int st=j*numPrintUnit, ed=((st+numPrintUnit)<numTbl)?(st+numPrintUnit):numTbl;
         for (int i = st; i < ed; i++) {
            log << "tbl[" << i << "]=" << tbl.at(i) << "\n";
         }
         dprintf(log.str().c_str());
     }
}

関連してよく使うコマンド

  • logcatのバッファーをクリア
    adb logcat -c
  • logcatのバッファーサイズを確認
    adb logcat -G

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0