| OLD | NEW |
| 1 // Copyright 2007, Google Inc. | 1 // Copyright 2007, Google Inc. |
| 2 // All rights reserved. | 2 // All rights reserved. |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted provided that the following conditions are | 5 // modification, are permitted provided that the following conditions are |
| 6 // met: | 6 // met: |
| 7 // | 7 // |
| 8 // * Redistributions of source code must retain the above copyright | 8 // * Redistributions of source code must retain the above copyright |
| 9 // notice, this list of conditions and the following disclaimer. | 9 // notice, this list of conditions and the following disclaimer. |
| 10 // * Redistributions in binary form must reproduce the above | 10 // * Redistributions in binary form must reproduce the above |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 // protect failure_reporter if we port Google Mock to other | 94 // protect failure_reporter if we port Google Mock to other |
| 95 // compilers. | 95 // compilers. |
| 96 static FailureReporterInterface* const failure_reporter = | 96 static FailureReporterInterface* const failure_reporter = |
| 97 new GoogleTestFailureReporter(); | 97 new GoogleTestFailureReporter(); |
| 98 return failure_reporter; | 98 return failure_reporter; |
| 99 } | 99 } |
| 100 | 100 |
| 101 // Protects global resources (stdout in particular) used by Log(). | 101 // Protects global resources (stdout in particular) used by Log(). |
| 102 static Mutex g_log_mutex(Mutex::NO_CONSTRUCTOR_NEEDED_FOR_STATIC_MUTEX); | 102 static Mutex g_log_mutex(Mutex::NO_CONSTRUCTOR_NEEDED_FOR_STATIC_MUTEX); |
| 103 | 103 |
| 104 // Returns true iff a log with the given severity is visible according |
| 105 // to the --gmock_verbose flag. |
| 106 bool LogIsVisible(LogSeverity severity) { |
| 107 if (GMOCK_FLAG(verbose) == kInfoVerbosity) { |
| 108 // Always show the log if --gmock_verbose=info. |
| 109 return true; |
| 110 } else if (GMOCK_FLAG(verbose) == kErrorVerbosity) { |
| 111 // Always hide it if --gmock_verbose=error. |
| 112 return false; |
| 113 } else { |
| 114 // If --gmock_verbose is neither "info" nor "error", we treat it |
| 115 // as "warning" (its default value). |
| 116 return severity == WARNING; |
| 117 } |
| 118 } |
| 119 |
| 104 // Prints the given message to stdout iff 'severity' >= the level | 120 // Prints the given message to stdout iff 'severity' >= the level |
| 105 // specified by the --gmock_verbose flag. If stack_frames_to_skip >= | 121 // specified by the --gmock_verbose flag. If stack_frames_to_skip >= |
| 106 // 0, also prints the stack trace excluding the top | 122 // 0, also prints the stack trace excluding the top |
| 107 // stack_frames_to_skip frames. In opt mode, any positive | 123 // stack_frames_to_skip frames. In opt mode, any positive |
| 108 // stack_frames_to_skip is treated as 0, since we don't know which | 124 // stack_frames_to_skip is treated as 0, since we don't know which |
| 109 // function calls will be inlined by the compiler and need to be | 125 // function calls will be inlined by the compiler and need to be |
| 110 // conservative. | 126 // conservative. |
| 111 void Log(LogSeverity severity, const string& message, | 127 void Log(LogSeverity severity, const string& message, |
| 112 int stack_frames_to_skip) { | 128 int stack_frames_to_skip) { |
| 113 if (GMOCK_FLAG(verbose) == kErrorVerbosity) { | 129 if (!LogIsVisible(severity)) |
| 114 // The user is not interested in logs. | |
| 115 return; | 130 return; |
| 116 } else if (GMOCK_FLAG(verbose) != kInfoVerbosity) { | |
| 117 // The user is interested in warnings but not informational logs. | |
| 118 // Note that invalid values of GMOCK_FLAG(verbose) are treated as | |
| 119 // "warning", which is the default value of the flag. | |
| 120 if (severity == INFO) { | |
| 121 return; | |
| 122 } | |
| 123 } | |
| 124 | 131 |
| 125 // Ensures that logs from different threads don't interleave. | 132 // Ensures that logs from different threads don't interleave. |
| 126 MutexLock l(&g_log_mutex); | 133 MutexLock l(&g_log_mutex); |
| 127 using ::std::cout; | 134 |
| 135 // "using ::std::cout;" doesn't work with Symbian's STLport, where cout is a |
| 136 // macro. |
| 137 |
| 128 if (severity == WARNING) { | 138 if (severity == WARNING) { |
| 129 // Prints a GMOCK WARNING marker to make the warnings easily searchable. | 139 // Prints a GMOCK WARNING marker to make the warnings easily searchable. |
| 130 cout << "\nGMOCK WARNING:"; | 140 std::cout << "\nGMOCK WARNING:"; |
| 131 } | 141 } |
| 132 // Pre-pends a new-line to message if it doesn't start with one. | 142 // Pre-pends a new-line to message if it doesn't start with one. |
| 133 if (message.empty() || message[0] != '\n') { | 143 if (message.empty() || message[0] != '\n') { |
| 134 cout << "\n"; | 144 std::cout << "\n"; |
| 135 } | 145 } |
| 136 cout << message; | 146 std::cout << message; |
| 137 if (stack_frames_to_skip >= 0) { | 147 if (stack_frames_to_skip >= 0) { |
| 138 #ifdef NDEBUG | 148 #ifdef NDEBUG |
| 139 // In opt mode, we have to be conservative and skip no stack frame. | 149 // In opt mode, we have to be conservative and skip no stack frame. |
| 140 const int actual_to_skip = 0; | 150 const int actual_to_skip = 0; |
| 141 #else | 151 #else |
| 142 // In dbg mode, we can do what the caller tell us to do (plus one | 152 // In dbg mode, we can do what the caller tell us to do (plus one |
| 143 // for skipping this function's stack frame). | 153 // for skipping this function's stack frame). |
| 144 const int actual_to_skip = stack_frames_to_skip + 1; | 154 const int actual_to_skip = stack_frames_to_skip + 1; |
| 145 #endif // NDEBUG | 155 #endif // NDEBUG |
| 146 | 156 |
| 147 // Appends a new-line to message if it doesn't end with one. | 157 // Appends a new-line to message if it doesn't end with one. |
| 148 if (!message.empty() && *message.rbegin() != '\n') { | 158 if (!message.empty() && *message.rbegin() != '\n') { |
| 149 cout << "\n"; | 159 std::cout << "\n"; |
| 150 } | 160 } |
| 151 cout << "Stack trace:\n" | 161 std::cout << "Stack trace:\n" |
| 152 << ::testing::internal::GetCurrentOsStackTraceExceptTop( | 162 << ::testing::internal::GetCurrentOsStackTraceExceptTop( |
| 153 ::testing::UnitTest::GetInstance(), actual_to_skip); | 163 ::testing::UnitTest::GetInstance(), actual_to_skip); |
| 154 } | 164 } |
| 155 cout << ::std::flush; | 165 std::cout << ::std::flush; |
| 156 } | 166 } |
| 157 | 167 |
| 158 } // namespace internal | 168 } // namespace internal |
| 159 } // namespace testing | 169 } // namespace testing |
| OLD | NEW |