1 /* 2 * Copyright 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 #pragma once 17 18 #include <android/log.h> 19 20 #include <sstream> 21 22 extern std::ostream aout; 23 extern std::ostream aerr; 24 25 /*! 26 * Use this class to create an output stream that writes to logcat. By default, a global one is 27 * defined as @a aout 28 */ 29 class AndroidOut : public std::stringbuf { 30 public: 31 /*! 32 * Creates a new output stream for logcat 33 * @param kLogTag the log tag to output 34 */ AndroidOut(const char * logTag,android_LogPriority priority)35 inline AndroidOut(const char* logTag, android_LogPriority priority) 36 : mLogTag(logTag), mPriority(priority) {} 37 38 protected: sync()39 virtual int sync() override { 40 __android_log_print(mPriority, mLogTag, "%s", str().c_str()); 41 42 str(""); 43 return 0; 44 } 45 46 private: 47 const char* mLogTag; 48 android_LogPriority mPriority; 49 }; 50