gtest_ringbuffer_dumper.h (2786B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 // Original author: bcampen@mozilla.com 8 9 #ifndef gtest_ringbuffer_dumper_h__ 10 #define gtest_ringbuffer_dumper_h__ 11 12 #include "mozilla/SyncRunnable.h" 13 14 #define GTEST_HAS_RTTI 0 15 #include "gtest/gtest.h" 16 #include "mtransport_test_utils.h" 17 #include "rlogconnector.h" 18 #include "runnable_utils.h" 19 20 using mozilla::RLogConnector; 21 using mozilla::WrapRunnable; 22 23 namespace test { 24 class RingbufferDumper : public ::testing::EmptyTestEventListener { 25 public: 26 explicit RingbufferDumper(MtransportTestUtils* test_utils) 27 : test_utils_(test_utils) {} 28 29 void ClearRingBuffer_s() { 30 RLogConnector::CreateInstance(); 31 // Set limit to zero to clear the ringbuffer 32 RLogConnector::GetInstance()->SetLogLimit(0); 33 RLogConnector::GetInstance()->SetLogLimit(UINT32_MAX); 34 } 35 36 void DestroyRingBuffer_s() { RLogConnector::DestroyInstance(); } 37 38 void DumpRingBuffer_s() { 39 std::deque<std::string> logs; 40 // Get an unlimited number of log lines, with no filter 41 RLogConnector::GetInstance()->GetAny(0, &logs); 42 for (auto l = logs.begin(); l != logs.end(); ++l) { 43 std::cout << *l << std::endl; 44 } 45 ClearRingBuffer_s(); 46 } 47 48 virtual void OnTestStart(const ::testing::TestInfo& testInfo) override { 49 running_ = true; 50 mozilla::SyncRunnable::DispatchToThread( 51 test_utils_->sts_target(), 52 WrapRunnable(this, &RingbufferDumper::ClearRingBuffer_s)); 53 } 54 55 virtual void OnTestEnd(const ::testing::TestInfo& testInfo) override { 56 running_ = false; 57 mozilla::SyncRunnable::DispatchToThread( 58 test_utils_->sts_target(), 59 WrapRunnable(this, &RingbufferDumper::DestroyRingBuffer_s)); 60 } 61 62 // Called after a failed assertion or a SUCCEED() invocation. 63 virtual void OnTestPartResult( 64 const ::testing::TestPartResult& testResult) override { 65 if (testResult.failed()) { 66 if (!running_) { 67 // Why does gtest sometimes call this without ever calling OnTestStart? 68 running_ = true; 69 mozilla::SyncRunnable::DispatchToThread( 70 test_utils_->sts_target(), 71 WrapRunnable(this, &RingbufferDumper::ClearRingBuffer_s)); 72 } 73 // Dump (and empty) the RLogConnector 74 mozilla::SyncRunnable::DispatchToThread( 75 test_utils_->sts_target(), 76 WrapRunnable(this, &RingbufferDumper::DumpRingBuffer_s)); 77 } 78 } 79 80 private: 81 MtransportTestUtils* test_utils_; 82 bool running_ = false; 83 }; 84 85 } // namespace test 86 87 #endif // gtest_ringbuffer_dumper_h__