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 
17 #include "mtransport_test_utils.h"
18 #include "runnable_utils.h"
19 #include "rlogconnector.h"
20 
21 using mozilla::RLogConnector;
22 using mozilla::WrapRunnable;
23 
24 namespace test {
25 class RingbufferDumper : public ::testing::EmptyTestEventListener {
26  public:
RingbufferDumper(MtransportTestUtils * test_utils)27   explicit RingbufferDumper(MtransportTestUtils* test_utils)
28       : test_utils_(test_utils) {}
29 
ClearRingBuffer_s()30   void ClearRingBuffer_s() {
31     RLogConnector::CreateInstance();
32     // Set limit to zero to clear the ringbuffer
33     RLogConnector::GetInstance()->SetLogLimit(0);
34     RLogConnector::GetInstance()->SetLogLimit(UINT32_MAX);
35   }
36 
DestroyRingBuffer_s()37   void DestroyRingBuffer_s() { RLogConnector::DestroyInstance(); }
38 
DumpRingBuffer_s()39   void DumpRingBuffer_s() {
40     std::deque<std::string> logs;
41     // Get an unlimited number of log lines, with no filter
42     RLogConnector::GetInstance()->GetAny(0, &logs);
43     for (auto l = logs.begin(); l != logs.end(); ++l) {
44       std::cout << *l << std::endl;
45     }
46     ClearRingBuffer_s();
47   }
48 
OnTestStart(const::testing::TestInfo & testInfo)49   virtual void OnTestStart(const ::testing::TestInfo& testInfo) override {
50     mozilla::SyncRunnable::DispatchToThread(
51         test_utils_->sts_target(),
52         WrapRunnable(this, &RingbufferDumper::ClearRingBuffer_s));
53   }
54 
OnTestEnd(const::testing::TestInfo & testInfo)55   virtual void OnTestEnd(const ::testing::TestInfo& testInfo) override {
56     mozilla::SyncRunnable::DispatchToThread(
57         test_utils_->sts_target(),
58         WrapRunnable(this, &RingbufferDumper::DestroyRingBuffer_s));
59   }
60 
61   // Called after a failed assertion or a SUCCEED() invocation.
OnTestPartResult(const::testing::TestPartResult & testResult)62   virtual void OnTestPartResult(
63       const ::testing::TestPartResult& testResult) override {
64     if (testResult.failed()) {
65       // Dump (and empty) the RLogConnector
66       mozilla::SyncRunnable::DispatchToThread(
67           test_utils_->sts_target(),
68           WrapRunnable(this, &RingbufferDumper::DumpRingBuffer_s));
69     }
70   }
71 
72  private:
73   MtransportTestUtils* test_utils_;
74 };
75 
76 }  // namespace test
77 
78 #endif  // gtest_ringbuffer_dumper_h__
79