gtest_ringbuffer_dumper.h 2.4 KB

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