Is “std::cout” usable in Android-ndk

前端 未结 4 1493
-上瘾入骨i
-上瘾入骨i 2020-12-02 18:31

In Android-ndk, we could use \"__android_log_write\", \"__android_log_print\", ... etc to output messages to the \"LogCat\" window. How about if I use \"std::cout\" to outpu

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-02 19:01

    Another option:

    #include 
    
    class MyStream
    {
    private:
       std::stringstream m_ss;
       int m_logLevel;
    public:
    
       MyStream(int Xi_logLevel)
       {
          m_logLevel = Xi_logLevel;
       };
       ~MyStream()
       {
          __android_log_print(m_logLevel,LOG_TAG,"%s", m_ss.str().c_str());
       }
    
       template MyStream& operator<<(T const& Xi_val)
       {
          m_ss << Xi_val;
          return *this;
       }
    };
    
    #define MY_LOG(LOG_LEVEL) MyStream(ANDROID_LOG_##LOG_LEVEL) << __FUNCTION__ << ":" << __LINE__ << " : "
    

    PROS:

    (1) The messages are printed immediately.

    CONS:

    (1) You must to change your code (std::cout -> MY_LOG(X)).

    (2) Each a single print produces an object and destroys it.

    (*** This answer base on this answer)

提交回复
热议问题