OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 #include "platform/assert.h" | 5 #include "platform/assert.h" |
6 | 6 |
7 #include <sstream> | |
8 #include <string> | |
9 | |
10 #include "platform/globals.h" | 7 #include "platform/globals.h" |
11 #include "vm/os.h" | 8 #include "vm/os.h" |
12 | 9 |
13 namespace dart { | 10 namespace dart { |
14 | 11 |
15 // Exit with a failure code when we miss an EXPECT check. | 12 // Exit with a failure code when we miss an EXPECT check. |
16 static void failed_exit(void) { | 13 static void failed_exit(void) { |
17 exit(255); | 14 exit(255); |
18 } | 15 } |
19 | 16 |
20 void DynamicAssertionHelper::Fail(const char* format, ...) { | 17 void DynamicAssertionHelper::Fail(const char* format, ...) { |
21 std::ostringstream stream; | 18 // Take only the last 1KB of the file name if it is longer. |
22 stream << file_ << ":" << line_ << ": error: "; | 19 const intptr_t file_len = strlen(file_); |
| 20 const intptr_t file_offset = (file_len > (1 * KB)) ? file_len - (1 * KB) : 0; |
| 21 const char* file = file_ + file_offset; |
23 | 22 |
| 23 // Print the file and line number into the buffer. |
| 24 char buffer[4 * KB]; |
| 25 intptr_t file_and_line_length = |
| 26 snprintf(buffer, sizeof(buffer), "%s: %d: error: ", file, line_); |
| 27 |
| 28 // Print the error message into the buffer. |
24 va_list arguments; | 29 va_list arguments; |
25 va_start(arguments, format); | 30 va_start(arguments, format); |
26 char buffer[4 * KB]; | 31 vsnprintf(buffer + file_and_line_length, |
27 vsnprintf(buffer, sizeof(buffer), format, arguments); | 32 sizeof(buffer) - file_and_line_length, |
| 33 format, |
| 34 arguments); |
28 va_end(arguments); | 35 va_end(arguments); |
29 stream << buffer << std::endl; | |
30 | 36 |
31 // Get the message from the string stream and dump it on stderr. | 37 // Print the buffer on stderr. |
32 std::string message = stream.str(); | 38 fprintf(stderr, "%s\n", buffer); |
33 fprintf(stderr, "%s", message.c_str()); | |
34 fflush(stderr); | 39 fflush(stderr); |
35 | 40 |
36 // In case of failed assertions, abort right away. Otherwise, wait | 41 // In case of failed assertions, abort right away. Otherwise, wait |
37 // until the program is exiting before producing a non-zero exit | 42 // until the program is exiting before producing a non-zero exit |
38 // code through abort. | 43 // code through abort. |
39 // TODO(5411324): replace std::abort with OS::Abort so that we can handle | 44 // TODO(5411324): replace std::abort with OS::Abort so that we can handle |
40 // restoring of signal handlers before aborting. | 45 // restoring of signal handlers before aborting. |
41 if (kind_ == ASSERT) { | 46 if (kind_ == ASSERT) { |
42 OS::Abort(); | 47 OS::Abort(); |
43 } | 48 } |
44 static bool failed = false; | 49 static bool failed = false; |
45 if (!failed) { | 50 if (!failed) { |
46 atexit(&failed_exit); | 51 atexit(&failed_exit); |
47 } | 52 } |
48 failed = true; | 53 failed = true; |
49 } | 54 } |
50 | 55 |
51 } // namespace dart | 56 } // namespace dart |
OLD | NEW |