1 /* 2 * Copyright 2022 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 #include <cstddef> 18 #include <future> 19 #include <string> 20 21 namespace bluetooth { 22 namespace testing { 23 24 class LogCapture { 25 public: 26 LogCapture(); 27 ~LogCapture(); 28 29 // Rewind file pointer to start of log 30 // Returns a |this| pointer for chaining. See |Find| 31 LogCapture* Rewind(); 32 // Searches from filepointer to end of file for |to_find| string 33 // Returns true if found, false otherwise 34 bool Find(std::string to_find); 35 // Reads and returns the entirety of the backing store into a string 36 std::string Read(); 37 // Flushes contents of log capture back to |stderr| 38 void Flush(); 39 // Synchronize buffer contents to file descriptor 40 void Sync(); 41 // Returns the backing store size in bytes 42 size_t Size() const; 43 // Truncates and resets the file pointer discarding all logs up to this point 44 void Reset(); 45 // Wait until the provided string shows up in the logs 46 void WaitUntilLogContains(std::promise<void>* promise, std::string text); 47 48 private: 49 std::pair<int, int> create_backing_store() const; 50 bool set_non_blocking(int fd) const; 51 void clean_up(); 52 53 int dup_fd_{-1}; 54 int fd_{-1}; 55 int original_stderr_fd_{-1}; 56 }; 57 58 } // namespace testing 59 } // namespace bluetooth 60