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
3// BSD-style license that can be found in the LICENSE file.
4
5#include "platform/assert.h"
6
7#include "include/dart_api.h"
8#include "platform/globals.h"
9#include "platform/syslog.h"
10
11namespace dart {
12
13bool Expect::failed_ = false;
14
15void DynamicAssertionHelper::Print(const char* format, va_list arguments) {
16 // Take only the last 1KB of the file name if it is longer.
17 const intptr_t file_len = strlen(file_);
18 const intptr_t file_offset = (file_len > (1 * KB)) ? file_len - (1 * KB) : 0;
19 const char* file = file_ + file_offset;
20
21 // Print the file and line number into the buffer.
22 char buffer[4 * KB];
23 MSAN_UNPOISON(buffer, sizeof(buffer));
24 intptr_t file_and_line_length =
25 snprintf(buffer, sizeof(buffer), "%s: %d: error: ", file, line_);
26
27 // Print the error message into the buffer.
28 vsnprintf(buffer + file_and_line_length,
29 sizeof(buffer) - file_and_line_length, format, arguments);
30
31 // Print the buffer on stderr and/or syslog.
32 Syslog::PrintErr("%s\n", buffer);
33}
34
35void Assert::Fail(const char* format, ...) {
36 va_list arguments;
37 va_start(arguments, format);
38 Print(format, arguments);
39 va_end(arguments);
40
41 // Abort right away.
42 Dart_DumpNativeStackTrace(NULL);
43 Dart_PrepareToAbort();
44 abort();
45}
46
47void Expect::Fail(const char* format, ...) {
48 va_list arguments;
49 va_start(arguments, format);
50 Print(format, arguments);
51 va_end(arguments);
52
53 // Wait until the program is exiting before producing a non-zero exit
54 // code through abort.
55 failed_ = true;
56}
57
58} // namespace dart
59