hta@webrtc.org | 86a6aac | 2012-06-18 13:22:08 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #ifndef WEBRTC_SYSTEM_WRAPPERS_SOURCE_UNITTEST_UTILITIES_H_ |
| 12 | #define WEBRTC_SYSTEM_WRAPPERS_SOURCE_UNITTEST_UTILITIES_H_ |
| 13 | |
| 14 | // This file contains utilities that make it simpler to write unittests |
| 15 | // that are appropriate for the system_wrappers classes. |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <string.h> |
| 19 | |
| 20 | #include "system_wrappers/interface/trace.h" |
| 21 | |
| 22 | namespace webrtc { |
| 23 | |
| 24 | class TestTraceCallback : public TraceCallback { |
| 25 | public: |
andrew@webrtc.org | 23ec30b | 2012-11-15 05:33:25 +0000 | [diff] [blame] | 26 | virtual void Print(TraceLevel level, const char* msg, int length) { |
| 27 | if (msg) { |
hta@webrtc.org | 86a6aac | 2012-06-18 13:22:08 +0000 | [diff] [blame] | 28 | char* cmd_print = new char[length+1]; |
andrew@webrtc.org | 23ec30b | 2012-11-15 05:33:25 +0000 | [diff] [blame] | 29 | memcpy(cmd_print, msg, length); |
hta@webrtc.org | 86a6aac | 2012-06-18 13:22:08 +0000 | [diff] [blame] | 30 | cmd_print[length] = '\0'; |
| 31 | printf("%s\n", cmd_print); |
| 32 | fflush(stdout); |
| 33 | delete[] cmd_print; |
| 34 | } |
| 35 | } |
| 36 | }; |
| 37 | |
| 38 | // A class that turns on tracing to stdout at the beginning of the test, |
| 39 | // and turns it off once the test is finished. |
| 40 | // Intended usage: |
| 41 | // class SomeTest : public ::testing::Test { |
| 42 | // protected: |
| 43 | // SomeTest() |
| 44 | // : trace_(false) {} // Change to true to turn on tracing. |
| 45 | // private: |
| 46 | // ScopedTracing trace_; |
| 47 | // } |
| 48 | class ScopedTracing { |
| 49 | public: |
| 50 | explicit ScopedTracing(bool logOn) { |
| 51 | logging_ = logOn; |
| 52 | StartTrace(); |
| 53 | } |
| 54 | |
| 55 | ~ScopedTracing() { |
| 56 | StopTrace(); |
| 57 | } |
| 58 | |
| 59 | private: |
| 60 | void StartTrace() { |
| 61 | if (logging_) { |
| 62 | Trace::CreateTrace(); |
| 63 | Trace::SetLevelFilter(webrtc::kTraceAll); |
| 64 | Trace::SetTraceCallback(&trace_); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | void StopTrace() { |
| 69 | if (logging_) { |
andrew@webrtc.org | c1ffd33 | 2013-03-22 17:13:23 +0000 | [diff] [blame^] | 70 | Trace::SetTraceCallback(NULL); |
hta@webrtc.org | 86a6aac | 2012-06-18 13:22:08 +0000 | [diff] [blame] | 71 | Trace::ReturnTrace(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | private: |
| 76 | bool logging_; |
| 77 | TestTraceCallback trace_; |
| 78 | }; |
| 79 | |
| 80 | } // namespace webrtc |
| 81 | |
| 82 | #endif // WEBRTC_SYSTEM_WRAPPERS_SOURCE_UNITTEST_UTILITIES_H_ |