shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | // Copyright 2007, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright |
| 9 | // notice, this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above |
| 11 | // copyright notice, this list of conditions and the following disclaimer |
| 12 | // in the documentation and/or other materials provided with the |
| 13 | // distribution. |
| 14 | // * Neither the name of Google Inc. nor the names of its |
| 15 | // contributors may be used to endorse or promote products derived from |
| 16 | // this software without specific prior written permission. |
| 17 | // |
| 18 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | // |
| 30 | // Author: wan@google.com (Zhanyong Wan) |
| 31 | |
| 32 | // Google Mock - a framework for writing C++ mock classes. |
| 33 | // |
| 34 | // This file defines some utilities useful for implementing Google |
| 35 | // Mock. They are subject to change without notice, so please DO NOT |
| 36 | // USE THEM IN USER CODE. |
| 37 | |
| 38 | #include <gmock/internal/gmock-internal-utils.h> |
| 39 | |
| 40 | #include <ostream> // NOLINT |
| 41 | #include <string> |
| 42 | #include <gmock/gmock.h> |
| 43 | #include <gmock/internal/gmock-port.h> |
| 44 | #include <gtest/gtest.h> |
| 45 | |
| 46 | namespace testing { |
| 47 | namespace internal { |
| 48 | |
| 49 | // This class reports Google Mock failures as Google Test failures. A |
| 50 | // user can define another class in a similar fashion if he intends to |
| 51 | // use Google Mock with a testing framework other than Google Test. |
| 52 | class GoogleTestFailureReporter : public FailureReporterInterface { |
| 53 | public: |
| 54 | virtual void ReportFailure(FailureType type, const char* file, int line, |
| 55 | const string& message) { |
| 56 | AssertHelper(type == FATAL ? TPRT_FATAL_FAILURE : TPRT_NONFATAL_FAILURE, |
| 57 | file, line, message.c_str()) = Message(); |
| 58 | if (type == FATAL) { |
| 59 | abort(); |
| 60 | } |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | // Returns the global failure reporter. Will create a |
| 65 | // GoogleTestFailureReporter and return it the first time called. |
| 66 | FailureReporterInterface* GetFailureReporter() { |
| 67 | // Points to the global failure reporter used by Google Mock. gcc |
| 68 | // guarantees that the following use of failure_reporter is |
| 69 | // thread-safe. We may need to add additional synchronization to |
| 70 | // protect failure_reporter if we port Google Mock to other |
| 71 | // compilers. |
| 72 | static FailureReporterInterface* const failure_reporter = |
| 73 | new GoogleTestFailureReporter(); |
| 74 | return failure_reporter; |
| 75 | } |
| 76 | |
| 77 | // Protects global resources (stdout in particular) used by Log(). |
| 78 | static Mutex g_log_mutex(Mutex::NO_CONSTRUCTOR_NEEDED_FOR_STATIC_MUTEX); |
| 79 | |
| 80 | // Prints the given message to stdout iff 'severity' >= the level |
| 81 | // specified by the --gmock_verbose flag. If stack_frames_to_skip >= |
| 82 | // 0, also prints the stack trace excluding the top |
| 83 | // stack_frames_to_skip frames. In opt mode, any positive |
| 84 | // stack_frames_to_skip is treated as 0, since we don't know which |
| 85 | // function calls will be inlined by the compiler and need to be |
| 86 | // conservative. |
| 87 | void Log(LogSeverity severity, const string& message, |
| 88 | int stack_frames_to_skip) { |
| 89 | if (GMOCK_FLAG(verbose) == kErrorVerbosity) { |
| 90 | // The user is not interested in logs. |
| 91 | return; |
| 92 | } else if (GMOCK_FLAG(verbose) != kInfoVerbosity) { |
| 93 | // The user is interested in warnings but not informational logs. |
| 94 | // Note that invalid values of GMOCK_FLAG(verbose) are treated as |
| 95 | // "warning", which is the default value of the flag. |
| 96 | if (severity == INFO) { |
| 97 | return; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | // Ensures that logs from different threads don't interleave. |
| 102 | MutexLock l(&g_log_mutex); |
| 103 | using ::std::cout; |
| 104 | if (severity == WARNING) { |
| 105 | // Prints a GMOCK WARNING marker to make the warnings easily searchable. |
| 106 | cout << "\nGMOCK WARNING:"; |
| 107 | } |
| 108 | // Pre-pends a new-line to message if it doesn't start with one. |
| 109 | if (message.empty() || message[0] != '\n') { |
| 110 | cout << "\n"; |
| 111 | } |
| 112 | cout << message; |
| 113 | if (stack_frames_to_skip >= 0) { |
| 114 | #ifdef NDEBUG |
| 115 | // In opt mode, we have to be conservative and skip no stack frame. |
| 116 | const int actual_to_skip = 0; |
| 117 | #else |
| 118 | // In dbg mode, we can do what the caller tell us to do (plus one |
| 119 | // for skipping this function's stack frame). |
| 120 | const int actual_to_skip = stack_frames_to_skip + 1; |
| 121 | #endif // NDEBUG |
| 122 | |
| 123 | // Appends a new-line to message if it doesn't end with one. |
| 124 | if (!message.empty() && *message.rbegin() != '\n') { |
| 125 | cout << "\n"; |
| 126 | } |
| 127 | cout << "Stack trace:\n" |
| 128 | << ::testing::internal::GetCurrentOsStackTraceExceptTop( |
| 129 | ::testing::UnitTest::GetInstance(), actual_to_skip); |
| 130 | } |
| 131 | cout << ::std::flush; |
| 132 | } |
| 133 | |
| 134 | } // namespace internal |
| 135 | } // namespace testing |