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