1 /* 2 * Copyright (C) 2020 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 17 #ifndef CHRE_PLATFORM_LINUX_PLATFORM_LOG_H_ 18 #define CHRE_PLATFORM_LINUX_PLATFORM_LOG_H_ 19 20 #include <condition_variable> 21 #include <mutex> 22 #include <queue> 23 #include <thread> 24 25 #include "chre/util/singleton.h" 26 #include "chre_api/chre/re.h" 27 28 namespace chre { 29 30 /** 31 * Storage for the Linux implementation of the PlatformLog class. 32 */ 33 class PlatformLog { 34 public: 35 PlatformLog(); 36 37 ~PlatformLog(); 38 39 /** 40 * Logs message with printf-style arguments. No trailing newline is required 41 * for this method. 42 */ log(chreLogLevel logLevel,const char * formatStr,...)43 void log(chreLogLevel logLevel, const char *formatStr, ...) { 44 va_list args; 45 va_start(args, formatStr); 46 logVa(logLevel, formatStr, args); 47 va_end(args); 48 } 49 50 /** 51 * Logs message with printf-style arguments. No trailing newline is required 52 * for this method. Uses va_list parameter instead of ... 53 */ 54 void logVa(chreLogLevel logLevel, const char *formatStr, va_list args); 55 56 private: 57 /** 58 * A looper method that idles on a condition variable on logs becoming 59 * available. When logs are available, they are output via std::cout. 60 */ 61 void logLooper(); 62 63 //! The thread that waits on incoming log messages and sends them out to 64 //! std::cout. 65 std::thread mLoggerThread; 66 67 //! A mutex to guard the shared queue and exit condition of this class. 68 std::mutex mMutex; 69 70 //! The condition variable to signal that the log looper has messages 71 //! available to output. 72 std::condition_variable mConditionVariable; 73 74 //! A queue of incoming log messages. 75 std::queue<char *> mLogQueue; 76 77 //! A flag to indicate that the logger should shut down. 78 bool mStopLogger = false; 79 }; 80 81 typedef Singleton<PlatformLog> PlatformLogSingleton; 82 83 } // namespace chre 84 85 #endif // CHRE_PLATFORM_LINUX_PLATFORM_LOG_H_ 86